Implement File > Open menu item
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ pub trait App {
|
||||
options: WindowOptions,
|
||||
executor: Rc<executor::Foreground>,
|
||||
) -> Result<Box<dyn Window>>;
|
||||
fn prompt_for_paths(&self, options: PathPromptOptions) -> Option<Vec<PathBuf>>;
|
||||
fn fonts(&self) -> Arc<dyn FontSystem>;
|
||||
fn quit(&self);
|
||||
}
|
||||
@@ -66,6 +67,12 @@ pub struct WindowOptions<'a> {
|
||||
pub title: Option<&'a str>,
|
||||
}
|
||||
|
||||
pub struct PathPromptOptions {
|
||||
pub files: bool,
|
||||
pub directories: bool,
|
||||
pub multiple: bool,
|
||||
}
|
||||
|
||||
pub trait FontSystem: Send + Sync {
|
||||
fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
|
||||
fn select_font(
|
||||
|
||||
@@ -48,6 +48,10 @@ impl super::App for App {
|
||||
}
|
||||
|
||||
fn quit(&self) {}
|
||||
|
||||
fn prompt_for_paths(&self, _: super::PathPromptOptions) -> Option<Vec<std::path::PathBuf>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Window {
|
||||
|
||||
Reference in New Issue
Block a user