Start work on creating and saving new files

This commit is contained in:
Max Brunsfeld
2021-05-04 19:04:11 -07:00
parent 2ce9f271b5
commit 5fd084ec09
10 changed files with 227 additions and 75 deletions
+31 -1
View File
@@ -21,7 +21,7 @@ use std::{
fmt::{self, Debug},
hash::{Hash, Hasher},
marker::PhantomData,
path::PathBuf,
path::{Path, PathBuf},
rc::{self, Rc},
sync::{Arc, Weak},
time::Duration,
@@ -586,6 +586,22 @@ impl MutableAppContext {
);
}
pub fn prompt_for_new_path<F>(&self, directory: &Path, done_fn: F)
where
F: 'static + FnOnce(Option<PathBuf>, &mut MutableAppContext),
{
let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
let foreground = self.foreground.clone();
self.platform().prompt_for_new_path(
directory,
Box::new(move |path| {
foreground
.spawn(async move { (done_fn)(path, &mut *app.borrow_mut()) })
.detach();
}),
);
}
pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) {
self.pending_effects
.push_back(Effect::ViewNotification { window_id, view_id });
@@ -1765,6 +1781,20 @@ impl<'a, T: View> ViewContext<'a, T> {
&self.app.ctx.background
}
pub fn prompt_for_paths<F>(&self, options: PathPromptOptions, done_fn: F)
where
F: 'static + FnOnce(Option<Vec<PathBuf>>, &mut MutableAppContext),
{
self.app.prompt_for_paths(options, done_fn)
}
pub fn prompt_for_new_path<F>(&self, directory: &Path, done_fn: F)
where
F: 'static + FnOnce(Option<PathBuf>, &mut MutableAppContext),
{
self.app.prompt_for_new_path(directory, done_fn)
}
pub fn debug_elements(&self) -> crate::json::Value {
self.app.debug_elements(self.window_id).unwrap()
}
+39 -2
View File
@@ -5,7 +5,7 @@ use cocoa::{
appkit::{
NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular,
NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSPasteboard,
NSPasteboardTypeString, NSWindow,
NSPasteboardTypeString, NSSavePanel, NSWindow,
},
base::{id, nil, selector},
foundation::{NSArray, NSAutoreleasePool, NSData, NSInteger, NSString, NSURL},
@@ -25,7 +25,7 @@ use std::{
convert::TryInto,
ffi::{c_void, CStr},
os::raw::c_char,
path::PathBuf,
path::{Path, PathBuf},
ptr,
rc::Rc,
slice, str,
@@ -305,6 +305,43 @@ impl platform::Platform for MacPlatform {
}
}
fn prompt_for_new_path(
&self,
directory: &Path,
done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
) {
unsafe {
let panel = NSSavePanel::savePanel(nil);
let path = ns_string(directory.to_string_lossy().as_ref());
let url = NSURL::fileURLWithPath_isDirectory_(nil, path, true.to_objc());
panel.setDirectoryURL(url);
let done_fn = Cell::new(Some(done_fn));
let block = ConcreteBlock::new(move |response: NSModalResponse| {
let result = if response == NSModalResponse::NSModalResponseOk {
let url = panel.URL();
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://") {
Some(PathBuf::from(path))
} else {
None
}
} else {
None
};
if let Some(done_fn) = done_fn.take() {
(done_fn)(result);
}
});
let block = block.copy();
let _: () = msg_send![panel, beginWithCompletionHandler: block];
}
}
fn fonts(&self) -> Arc<dyn platform::FontSystem> {
self.fonts.clone()
}
+12 -1
View File
@@ -19,7 +19,13 @@ use crate::{
};
use async_task::Runnable;
pub use event::Event;
use std::{any::Any, ops::Range, path::PathBuf, rc::Rc, sync::Arc};
use std::{
any::Any,
ops::Range,
path::{Path, PathBuf},
rc::Rc,
sync::Arc,
};
pub trait Platform {
fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>);
@@ -45,6 +51,11 @@ pub trait Platform {
options: PathPromptOptions,
done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
);
fn prompt_for_new_path(
&self,
directory: &Path,
done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
);
fn quit(&self);
fn write_to_clipboard(&self, item: ClipboardItem);
fn read_from_clipboard(&self) -> Option<ClipboardItem>;
+3 -1
View File
@@ -1,6 +1,6 @@
use crate::ClipboardItem;
use pathfinder_geometry::vector::Vector2F;
use std::{any::Any, cell::RefCell, rc::Rc, sync::Arc};
use std::{any::Any, cell::RefCell, path::Path, rc::Rc, sync::Arc};
struct Platform {
dispatcher: Arc<dyn super::Dispatcher>,
@@ -77,6 +77,8 @@ impl super::Platform for Platform {
) {
}
fn prompt_for_new_path(&self, _: &Path, _: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {}
fn write_to_clipboard(&self, item: ClipboardItem) {
*self.current_clipboard_item.borrow_mut() = Some(item);
}