Implement File > Open menu item

This commit is contained in:
Max Brunsfeld
2021-04-08 22:25:54 -07:00
parent f656b387b3
commit 7ebcbdc0cb
7 changed files with 88 additions and 19 deletions
+37 -2
View File
@@ -1,9 +1,13 @@
use super::{BoolExt as _, Dispatcher, FontSystem, Window};
use crate::{executor, platform};
use anyhow::Result;
use cocoa::{appkit::NSApplication, base::nil};
use cocoa::{
appkit::{NSApplication, NSOpenPanel, NSModalResponse},
base::nil,
foundation::{NSArray, NSString, NSURL},
};
use objc::{msg_send, sel, sel_impl};
use std::{rc::Rc, sync::Arc};
use std::{path::PathBuf, rc::Rc, sync::Arc};
pub struct App {
dispatcher: Arc<Dispatcher>,
@@ -39,6 +43,37 @@ impl platform::App for App {
Ok(Box::new(Window::open(options, executor, self.fonts())?))
}
fn prompt_for_paths(
&self,
options: platform::PathPromptOptions,
) -> Option<Vec<std::path::PathBuf>> {
unsafe {
let panel = NSOpenPanel::openPanel(nil);
panel.setCanChooseDirectories_(options.directories.to_objc());
panel.setCanChooseFiles_(options.files.to_objc());
panel.setAllowsMultipleSelection_(options.multiple.to_objc());
panel.setResolvesAliases_(false.to_objc());
let response = panel.runModal();
if response == NSModalResponse::NSModalResponseOk {
let mut result = Vec::new();
let urls = panel.URLs();
for i in 0..urls.count() {
let url = urls.objectAtIndex(i);
let string = url.absoluteString();
let string = std::ffi::CStr::from_ptr(string.UTF8String())
.to_string_lossy()
.to_string();
if let Some(path) = string.strip_prefix("file://") {
result.push(PathBuf::from(path));
}
}
Some(result)
} else {
None
}
}
}
fn fonts(&self) -> Arc<dyn platform::FontSystem> {
self.fonts.clone()
}