use ferrokit_core::kept::{UiCell, UiLocal};
use std::cell::{Cell, RefCell};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use objc2::rc::{Retained, Weak};
use objc2::runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{MainThreadMarker, MainThreadOnly, define_class, msg_send};
use objc2_app_kit::{NSApplication, NSApplicationDelegate, NSApplicationTerminateReply, NSWindow};
use objc2_foundation::{NSString, NSURL, NSUserDefaults};
use crate::l10n::{t, t_with};
use crate::chrome::{MenuItem, Modifier, ToolbarItem, dynamic_submenu, menu_item, menu_separator};
use crate::dialogs::{Alert, OpenOptions, alert, open_panel, save_panel};
use crate::signal::Owner;
use crate::signal::{Effect, Signal, batch, untrack};
use crate::view::{AnyView, View};
use crate::{Window, mtm};
type Reader<T> = Rc<dyn Fn(&Path) -> std::io::Result<T>>;
type Writer<T> = Rc<dyn Fn(&T, &Path) -> std::io::Result<Option<T>>>;
type DocView<T> = Rc<dyn Fn(&Document<T>) -> AnyView>;
type DocToolbar<T> = Rc<dyn Fn(&Document<T>) -> Vec<ToolbarItem>>;
pub struct DocumentType<T: 'static> {
extensions: Vec<String>,
empty: Rc<dyn Fn() -> T>,
read: Option<Reader<T>>,
write: Option<Writer<T>>,
view: DocView<T>,
toolbar: Option<DocToolbar<T>>,
size: (f64, f64),
style: Option<Rc<dyn Fn(Window) -> Window>>,
related: Vec<String>,
atomic: bool,
}
impl<T: Clone + PartialEq + 'static> DocumentType<T> {
pub fn new<V: View>(extensions: &[&str], empty: impl Fn() -> T + 'static, view: impl Fn(&Document<T>) -> V + 'static) -> Self {
DocumentType {
extensions: extensions.iter().map(|e| e.to_string()).collect(),
empty: Rc::new(empty),
read: None,
write: None,
view: Rc::new(move |d| AnyView::new(view(d))),
toolbar: None,
size: (640.0, 480.0),
style: None,
related: Vec::new(),
atomic: false,
}
}
pub fn read(mut self, f: impl Fn(&Path) -> std::io::Result<T> + 'static) -> Self {
self.read = Some(Rc::new(f));
self
}
pub fn write(mut self, f: impl Fn(&T, &Path) -> std::io::Result<()> + 'static) -> Self {
self.write = Some(Rc::new(move |c, p| f(c, p).map(|()| None)));
self.atomic = true;
self
}
pub fn write_returning(mut self, f: impl Fn(&T, &Path) -> std::io::Result<T> + 'static) -> Self {
self.write = Some(Rc::new(move |c, p| f(c, p).map(Some)));
self.atomic = false;
self
}
pub fn related(mut self, suffixes: &[&str]) -> Self {
self.related = suffixes.iter().map(|s| s.to_string()).collect();
self
}
pub fn toolbar(mut self, items: impl Fn(&Document<T>) -> Vec<ToolbarItem> + 'static) -> Self {
self.toolbar = Some(Rc::new(items));
self
}
pub fn size(mut self, width: f64, height: f64) -> Self {
self.size = (width, height);
self
}
pub fn window(mut self, style: impl Fn(Window) -> Window + 'static) -> Self {
self.style = Some(Rc::new(style));
self
}
}
pub struct Document<T: 'static>(Rc<DocInner<T>>);
impl<T> Clone for Document<T> {
fn clone(&self) -> Self {
Document(self.0.clone())
}
}
struct DocInner<T: 'static> {
content: Signal<T>,
path: Signal<Option<PathBuf>>,
edited: Signal<bool>,
saved: RefCell<T>,
untitled: u32,
kind: Rc<DocumentType<T>>,
window: RefCell<Weak<NSWindow>>,
closing: Cell<bool>,
effects: RefCell<Vec<Effect>>,
related: RefCell<Option<crate::RelatedFiles>>,
_owner: Owner,
}
impl<T: Clone + PartialEq + 'static> Document<T> {
pub fn content(&self) -> &Signal<T> {
&self.0.content
}
pub fn path(&self) -> Option<PathBuf> {
self.0.path.get()
}
pub fn is_edited(&self) -> bool {
self.0.edited.get()
}
pub fn title(&self) -> String {
match self.0.path.get() {
Some(p) => p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(),
None if self.0.untitled <= 1 => t("Untitled"),
None => t_with("Untitled {n}", &[("n", &self.0.untitled)]),
}
}
pub fn window(&self) -> Option<Retained<NSWindow>> {
self.0.window.borrow().load()
}
}
trait AnyDoc {
fn window(&self) -> Option<Retained<NSWindow>>;
fn path(&self) -> Option<PathBuf>;
fn edited(&self) -> bool;
fn writable(&self) -> bool;
fn title(&self) -> String;
fn extension(&self) -> String;
fn save_to(&self, path: &Path) -> std::io::Result<()>;
fn revert(&self) -> std::io::Result<()>;
fn closing(&self) -> &Cell<bool>;
fn keep_related(&self, related: Option<crate::RelatedFiles>);
}
impl<T: Clone + PartialEq + 'static> AnyDoc for Document<T> {
fn window(&self) -> Option<Retained<NSWindow>> {
Document::window(self)
}
fn path(&self) -> Option<PathBuf> {
self.0.path.get_untracked()
}
fn edited(&self) -> bool {
self.0.edited.get_untracked()
}
fn writable(&self) -> bool {
self.0.kind.write.is_some()
}
fn title(&self) -> String {
untrack(|| Document::title(self))
}
fn extension(&self) -> String {
self.0.kind.extensions.first().cloned().unwrap_or_default()
}
fn save_to(&self, path: &Path) -> std::io::Result<()> {
let write = self.0.kind.write.as_ref().ok_or_else(|| std::io::Error::other("this kind of document can't be saved"))?;
let content = self.0.content.get_untracked();
let related = self.0.kind.claim(path);
let kept = if self.0.kind.atomic { replacing(path, |to| write(&content, to))? } else { write(&content, path)? };
if related.is_some() {
*self.0.related.borrow_mut() = related;
}
let content = kept.unwrap_or(content);
*self.0.saved.borrow_mut() = content.clone();
batch(|| {
self.0.content.set_if_changed(content);
self.0.path.set(Some(path.to_path_buf()));
self.0.edited.set_if_changed(false);
});
note_recent(path);
Ok(())
}
fn revert(&self) -> std::io::Result<()> {
let (Some(path), Some(read)) = (self.path(), self.0.kind.read.as_ref()) else {
return Err(std::io::Error::other("the document has no file to revert to"));
};
let content = read(&path)?;
*self.0.saved.borrow_mut() = content.clone();
batch(|| {
self.0.content.set(content);
self.0.edited.set_if_changed(false);
});
Ok(())
}
fn closing(&self) -> &Cell<bool> {
&self.0.closing
}
fn keep_related(&self, related: Option<crate::RelatedFiles>) {
*self.0.related.borrow_mut() = related;
}
}
impl<T> DocumentType<T> {
fn claim(&self, path: &Path) -> Option<crate::RelatedFiles> {
let suffixes: Vec<&str> = self.related.iter().map(String::as_str).collect();
(!suffixes.is_empty()).then(|| crate::related_files(path, &suffixes))
}
}
trait Opener {
fn new_document(&self) -> Retained<NSWindow>;
fn open(&self, path: &Path) -> std::io::Result<Retained<NSWindow>>;
fn extensions(&self) -> Vec<String>;
fn can_open(&self) -> bool;
}
impl<T: Clone + PartialEq + 'static> Opener for Rc<DocumentType<T>> {
fn new_document(&self) -> Retained<NSWindow> {
let n = UNTITLED.with(|u| u.replace(u.get() + 1) + 1);
show(self.clone(), (self.empty)(), None, n)
}
fn open(&self, path: &Path) -> std::io::Result<Retained<NSWindow>> {
let read = self.read.as_ref().ok_or_else(|| std::io::Error::other("this app can't open files"))?;
let related = self.claim(path);
let content = read(path)?;
note_recent(path);
let window = show(self.clone(), content, Some(path.to_path_buf()), 0);
if let Some(doc) = document_of(&window) {
doc.keep_related(related);
}
Ok(window)
}
fn extensions(&self) -> Vec<String> {
self.extensions.clone()
}
fn can_open(&self) -> bool {
self.read.is_some()
}
}
thread_local! {
static DOCS: UiCell<Vec<Rc<dyn AnyDoc>>> = const { UiCell::new(Vec::new()) };
static OPENER: UiCell<Option<Rc<dyn Opener>>> = const { UiCell::new(None) };
static UNTITLED: Cell<u32> = const { Cell::new(0) };
static DELEGATE: UiCell<Option<Retained<AppDelegate>>> = const { UiCell::new(None) };
static AUTO_UNTITLED: UiCell<Weak<NSWindow>> = UiCell::new(Weak::default());
}
fn show<T: Clone + PartialEq + 'static>(kind: Rc<DocumentType<T>>, content: T, path: Option<PathBuf>, untitled: u32) -> Retained<NSWindow> {
let owner = Owner::new();
let (content_signal, edited_signal, path) = owner.with(|| (Signal::new(content.clone()), Signal::new(false), Signal::new(path)));
let doc = Document(Rc::new(DocInner {
saved: RefCell::new(content),
content: content_signal,
edited: edited_signal,
path,
untitled,
kind: kind.clone(),
window: RefCell::new(Weak::default()),
closing: Cell::new(false),
effects: RefCell::default(),
related: RefCell::new(None),
_owner: owner.clone(),
}));
let saved = Rc::downgrade(&doc.0);
let edited = Effect::new(move || {
let Some(d) = saved.upgrade() else { return };
let differs = content_signal.with(|c| *c != *d.saved.borrow());
edited_signal.set_if_changed(differs);
});
let view = owner.with(|| (kind.view)(&doc));
let extensions: Vec<&str> = kind.extensions.iter().map(String::as_str).collect();
let mut spec = Window::new(&untrack(|| doc.title())).size(kind.size.0, kind.size.1).view(view).on_drop_files(&extensions, |paths| {
for path in paths {
if let Err(e) = open_document(&path) {
eprintln!("ferrokit: couldn't open {}: {e}", path.display());
}
}
});
if let Some(toolbar) = &kind.toolbar {
spec = spec.toolbar(owner.with(|| toolbar(&doc)));
}
if let Some(style) = &kind.style {
spec = style(spec);
}
let window = crate::open(spec);
if let Some(p) = doc.path() {
window.setFrameUsingName(&frame_name(&p));
}
close_welcome();
*doc.0.window.borrow_mut() = Weak::from_retained(&window);
let (d, w) = (Rc::downgrade(&doc.0), Weak::from_retained(&window));
let title = Effect::new(move || {
let Some(d) = d.upgrade().map(Document) else { return };
let (name, path, edited) = (d.title(), d.path(), d.is_edited());
if let Some(w) = w.load() {
w.setTitle(&NSString::from_str(&name));
w.setDocumentEdited(edited);
if let Some(name) = path.as_deref().map(frame_name).filter(|n| w.frameAutosaveName().to_string() != n.to_string()) {
w.setFrameAutosaveName(&name);
}
let url = path.map(|p| NSURL::fileURLWithPath(&NSString::from_str(&p.to_string_lossy())));
w.setRepresentedURL(url.as_deref());
}
});
doc.0.effects.borrow_mut().extend([edited, title]);
DOCS.with_borrow_mut(|docs| docs.push(Rc::new(doc)));
window
}
fn document_of(window: &NSWindow) -> Option<Rc<dyn AnyDoc>> {
DOCS.with_borrow(|docs| docs.iter().find(|d| d.window().is_some_and(|w| std::ptr::eq(&*w, window))).cloned())
}
fn replacing<R>(path: &Path, write: impl FnOnce(&Path) -> std::io::Result<R>) -> std::io::Result<R> {
use objc2_foundation::{NSFileManager, NSFileManagerItemReplacementOptions, NSSearchPathDirectory, NSSearchPathDomainMask};
let Some(name) = path.file_name().filter(|_| path.exists()) else { return write(path) };
let files = NSFileManager::defaultManager();
let url = NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()));
let folder = files.URLForDirectory_inDomain_appropriateForURL_create_error(NSSearchPathDirectory::ItemReplacementDirectory, NSSearchPathDomainMask::UserDomainMask, Some(&url), true);
let Some(folder_path) = folder.as_ref().ok().and_then(|f| f.path()).map(|p| PathBuf::from(p.to_string())) else { return write(path) };
let new = folder_path.join(name);
let result = write(&new).and_then(|kept| {
let new_url = NSURL::fileURLWithPath(&NSString::from_str(&new.to_string_lossy()));
files
.replaceItemAtURL_withItemAtURL_backupItemName_options_resultingItemURL_error(&url, &new_url, None, NSFileManagerItemReplacementOptions::empty(), None)
.map(|()| kept)
.map_err(|e| std::io::Error::other(e.localizedDescription().to_string()))
});
if let Ok(folder) = &folder {
let _ = files.removeItemAtURL_error(folder);
}
result
}
fn current() -> Option<Rc<dyn AnyDoc>> {
let app = NSApplication::sharedApplication(mtm());
let key = app.keyWindow();
let of_key = key.as_ref().and_then(|w| document_of(w).or_else(|| w.sheetParent().and_then(|p| document_of(&p))));
if of_key.is_some() {
return of_key;
}
let main = app.mainWindow();
if let Some(d) = main.as_ref().and_then(|w| document_of(w)) {
return Some(d);
}
if key.is_some() || main.is_some() {
return None;
}
DOCS.with_borrow(|docs| docs.iter().rev().find(|d| d.window().is_some_and(|w| w.isVisible())).cloned())
}
pub(crate) fn forget(window: &NSWindow) {
DOCS.with_borrow_mut(|docs| docs.retain(|d| d.window().is_some_and(|w| !std::ptr::eq(&*w, window))));
}
fn frame_name(path: &Path) -> Retained<NSString> {
NSString::from_str(&format!("Ferrokit document {}", path.display()))
}
const OPEN_KEY: &str = "FerrokitOpenDocuments";
fn keeps_windows() -> bool {
NSUserDefaults::standardUserDefaults().boolForKey(&NSString::from_str("NSQuitAlwaysKeepsWindows"))
}
pub(crate) fn remember_open() {
let paths: Vec<Retained<NSString>> = DOCS.with_borrow(|docs| docs.iter().filter_map(|d| d.path()).map(|p| NSString::from_str(&p.to_string_lossy())).collect());
let list = objc2_foundation::NSArray::from_retained_slice(&paths);
unsafe { NSUserDefaults::standardUserDefaults().setObject_forKey(Some(&list), &NSString::from_str(OPEN_KEY)) };
}
pub(crate) fn restore_open() -> usize {
if !keeps_windows() {
return 0;
}
let Some(list) = NSUserDefaults::standardUserDefaults().stringArrayForKey(&NSString::from_str(OPEN_KEY)) else { return 0 };
list.iter().map(|p| PathBuf::from(p.to_string())).filter(|p| {
reach(p);
p.exists()
}).filter(|p| open_document(p).is_ok()).count()
}
pub(crate) fn new_launch_document() {
let window = new_document();
AUTO_UNTITLED.with_borrow_mut(|a| *a = window.as_ref().map(Weak::from_retained).unwrap_or_default());
}
fn close_auto_untitled() {
let Some(window) = AUTO_UNTITLED.with_borrow_mut(std::mem::take).load() else { return };
let opened_more = DOCS.with_borrow(|docs| docs.len() > 1);
if opened_more && !document_edited(&window) && document_of(&window).is_some_and(|d| d.path().is_none()) {
window.close();
}
}
type Welcome = (Option<Rc<dyn Fn() -> Window>>, Weak<NSWindow>);
thread_local! {
static WELCOME: UiCell<Welcome> = UiCell::new((None, Weak::default()));
}
pub(crate) fn set_welcome(make: Rc<dyn Fn() -> Window>) {
WELCOME.with_borrow_mut(|w| w.0 = Some(make));
}
pub fn show_welcome() -> bool {
let (make, open) = WELCOME.with_borrow(|w| (w.0.clone(), w.1.load().filter(|w| w.isVisible())));
if let Some(w) = open {
w.makeKeyAndOrderFront(None);
return true;
}
let Some(make) = make else { return false };
let w = crate::open_with(move || make());
w.center();
w.makeKeyAndOrderFront(None);
WELCOME.with_borrow_mut(|s| s.1 = Weak::from_retained(&w));
true
}
pub fn welcome_window() -> Option<Retained<NSWindow>> {
WELCOME.with_borrow(|w| w.1.load()).filter(|w| w.isVisible())
}
fn close_welcome() {
if let Some(w) = WELCOME.with_borrow_mut(|w| std::mem::take(&mut w.1)).load() {
w.close();
}
}
pub fn new_document() -> Option<Retained<NSWindow>> {
let opener = OPENER.with_borrow(Clone::clone)?;
Some(opener.new_document())
}
pub fn open_document(path: &Path) -> std::io::Result<Retained<NSWindow>> {
let open = DOCS.with_borrow(|docs| docs.iter().find(|d| d.path().as_deref() == Some(path)).and_then(|d| d.window()));
if let Some(w) = open {
w.makeKeyAndOrderFront(None);
note_recent(path);
return Ok(w);
}
let opener = OPENER.with_borrow(Clone::clone).ok_or_else(|| std::io::Error::other("the app has no document type"))?;
reach(path);
let window = opener.open(path)?;
close_auto_untitled();
Ok(window)
}
pub fn save_document_as(window: &NSWindow, path: &Path) -> std::io::Result<()> {
document_of(window).ok_or_else(|| std::io::Error::other("not a document window"))?.save_to(path)
}
pub fn save_document(window: &NSWindow) -> std::io::Result<()> {
let doc = document_of(window).ok_or_else(|| std::io::Error::other("not a document window"))?;
let path = doc.path().ok_or_else(|| std::io::Error::other("the document has no file yet"))?;
doc.save_to(&path)
}
pub fn revert_document(window: &NSWindow) -> std::io::Result<()> {
document_of(window).ok_or_else(|| std::io::Error::other("not a document window"))?.revert()
}
pub fn document_edited(window: &NSWindow) -> bool {
document_of(window).is_some_and(|d| d.edited())
}
const RECENTS_KEY: &str = "ferrokit recent documents";
const RECENTS_MAX: usize = 10;
pub fn recent_documents() -> Vec<PathBuf> {
let list = NSUserDefaults::standardUserDefaults().stringForKey(&NSString::from_str(RECENTS_KEY));
list.map(|l| l.to_string()).unwrap_or_default().lines().map(PathBuf::from).filter(|p| {
reach(p);
p.exists()
}).collect()
}
fn set_recents(list: &[PathBuf]) {
let joined = list.iter().map(|p| p.to_string_lossy().into_owned()).collect::<Vec<_>>().join("\n");
unsafe { NSUserDefaults::standardUserDefaults().setObject_forKey(Some(&NSString::from_str(&joined)), &NSString::from_str(RECENTS_KEY)) };
}
fn note_recent(path: &Path) {
let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
remember_bookmark(&path);
let mut list: Vec<PathBuf> = recent_documents().into_iter().filter(|p| *p != path).collect();
list.insert(0, path);
list.truncate(RECENTS_MAX);
set_recents(&list);
}
fn sandboxed() -> bool {
std::env::var_os("APP_SANDBOX_CONTAINER_ID").is_some()
}
fn bookmark_key(path: &Path) -> Retained<NSString> {
NSString::from_str(&format!("Ferrokit bookmark {}", path.display()))
}
thread_local! {
static REACHED: UiCell<Vec<(PathBuf, Retained<NSURL>)>> = const { UiCell::new(Vec::new()) };
}
fn remember_bookmark(path: &Path) {
if !sandboxed() {
return;
}
let url = NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()));
if let Ok(data) = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error(objc2_foundation::NSURLBookmarkCreationOptions::WithSecurityScope, None, None) {
unsafe { NSUserDefaults::standardUserDefaults().setObject_forKey(Some(&data), &bookmark_key(path)) };
}
}
pub(crate) fn reach(path: &Path) -> bool {
if !sandboxed() || REACHED.with_borrow(|r| r.iter().any(|(p, _)| p == path)) {
return true;
}
let Some(data) = NSUserDefaults::standardUserDefaults().dataForKey(&bookmark_key(path)) else { return false };
let mut stale = objc2::runtime::Bool::NO;
let url = unsafe { NSURL::URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error(&data, objc2_foundation::NSURLBookmarkResolutionOptions::WithSecurityScope, None, &mut stale) };
let Ok(url) = url else { return false };
if !unsafe { url.startAccessingSecurityScopedResource() } {
return false;
}
if stale.as_bool() {
remember_bookmark(path);
}
REACHED.with_borrow_mut(|r| r.push((path.to_path_buf(), url)));
true
}
pub fn remember_access(path: &Path) {
remember_bookmark(path);
}
pub fn regain_access(path: &Path) -> bool {
reach(path)
}
fn clear_recents() {
set_recents(&[]);
}
pub(crate) fn file_items() -> (Vec<MenuItem>, Vec<MenuItem>) {
let can_open = || OPENER.with_borrow(|o| o.as_ref().is_some_and(|o| o.can_open()));
let mut above = vec![menu_item(t("New"), || {
new_document();
})
.key("n")];
if can_open() {
above.push(menu_item(t("Open…"), choose_documents).key("o"));
above.push(dynamic_submenu(&t("Open Recent"), || {
let mut items: Vec<MenuItem> = recent_documents()
.into_iter()
.map(|p| {
let name = p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
menu_item(&name, move || open_reporting(&p))
})
.collect();
let any = !items.is_empty();
if any {
items.push(menu_separator());
}
items.push(menu_item(t("Clear Menu"), clear_recents).disabled(move || !(any)));
items
}));
}
let writable = || current().is_some_and(|d| d.writable());
let after = vec![
menu_item(t("Save"), || {
if let Some(d) = current() {
save_flow(d, |_| {});
}
})
.key("s")
.disabled(move || !writable()),
menu_item(t("Save As…"), || {
if let Some(d) = current() {
save_as_flow(d, |_| {});
}
})
.key_with("s", &[Modifier::Shift])
.disabled(move || !writable()),
menu_item(t("Revert to Saved"), confirm_revert).disabled(|| !current().is_some_and(|d| d.edited() && d.path().is_some())),
];
(above, after)
}
pub fn choose_documents() {
let Some(opener) = OPENER.with_borrow(Clone::clone) else { return };
let app = NSApplication::sharedApplication(mtm());
let options = OpenOptions { extensions: opener.extensions(), multiple: true, directories: false };
let open_each = |paths: Option<Vec<PathBuf>>| paths.into_iter().flatten().for_each(|p| open_reporting(&p));
match app.keyWindow().or_else(|| app.mainWindow()) {
Some(w) => open_panel(&w, options, open_each),
None => crate::dialogs::open_panel_alone(options, open_each),
}
}
fn open_reporting(path: &Path) {
if let Err(e) = open_document(path) {
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
report(None, &t_with("The document “{name}” couldn't be opened.", &[("name", &name)]), &e.to_string());
}
}
fn report(window: Option<&NSWindow>, title: &str, message: &str) {
let app = NSApplication::sharedApplication(mtm());
let window = window.map(objc2::Message::retain).or_else(|| app.keyWindow()).or_else(|| app.mainWindow());
match window {
Some(w) => alert(&w, Alert::new(title, message).warning(), |_| {}),
None => eprintln!("{title} {message}"),
}
}
fn save_flow(doc: Rc<dyn AnyDoc>, done: impl FnOnce(bool) + 'static) {
match doc.path() {
Some(path) => {
let ok = doc.save_to(&path).map_err(|e| report(doc.window().as_deref(), &t_with("The document “{name}” couldn't be saved.", &[("name", &doc.title())]), &e.to_string()));
done(ok.is_ok());
}
None => save_as_flow(doc, done),
}
}
fn save_as_flow(doc: Rc<dyn AnyDoc>, done: impl FnOnce(bool) + 'static) {
let Some(window) = doc.window() else { return done(false) };
let name = match doc.path() {
Some(p) => p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(),
None => format!("{}.{}", doc.title(), doc.extension()),
};
let dir = doc.path().and_then(|p| p.parent().map(Path::to_path_buf));
save_panel(&window, &name, dir.as_deref(), move |path| match path {
Some(path) => {
let ok = doc.save_to(&path).map_err(|e| report(doc.window().as_deref(), &t_with("The document “{name}” couldn't be saved.", &[("name", &doc.title())]), &e.to_string()));
done(ok.is_ok());
}
None => done(false),
});
}
fn confirm_revert() {
let Some(doc) = current() else { return };
let Some(window) = doc.window() else { return };
let question = Alert::new(&t_with("Revert to the saved version of “{name}”?", &[("name", &doc.title())]), &t("Your current changes will be lost."))
.button(&t("Revert"))
.button(&t("Cancel"));
alert(&window, question, move |i| {
if i == 0
&& let Err(e) = doc.revert()
{
report(doc.window().as_deref(), "The document couldn't be reverted.", &e.to_string());
}
});
}
pub(crate) fn window_should_close(window: &NSWindow) -> bool {
let Some(doc) = document_of(window) else { return true };
if !doc.edited() || doc.closing().get() {
return true;
}
close_flow(doc, |_| {});
false
}
fn close_flow(doc: Rc<dyn AnyDoc>, done: impl FnOnce(bool) + 'static) {
let Some(window) = doc.window() else { return done(true) };
window.makeKeyAndOrderFront(None);
let question = Alert::new(&t_with("Do you want to save the changes made to the document “{name}”?", &[("name", &doc.title())]), &t("Your changes will be lost if you don't save them."))
.button(&t("Save"))
.button(&t("Cancel"))
.button(&t("Don't Save"));
let close = move |doc: &Rc<dyn AnyDoc>| {
doc.closing().set(true);
if let Some(w) = doc.window() {
w.close();
}
};
alert(&window, question, move |i| match i {
0 => {
let d = doc.clone();
save_flow(doc, move |saved| {
if saved {
close(&d);
}
done(saved);
});
}
2 => {
close(&doc);
done(true);
}
_ => done(false),
});
}
fn review_and_quit() {
let edited: Vec<Rc<dyn AnyDoc>> = DOCS.with_borrow(|docs| docs.iter().filter(|d| d.edited()).cloned().collect());
fn next(mut rest: Vec<Rc<dyn AnyDoc>>) {
let app = NSApplication::sharedApplication(mtm());
let Some(doc) = rest.pop() else { return app.replyToApplicationShouldTerminate(true) };
close_flow(doc, move |closed| {
if closed {
next(rest);
} else {
NSApplication::sharedApplication(mtm()).replyToApplicationShouldTerminate(false);
}
});
}
next(edited);
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitAppDelegate"]
struct AppDelegate;
unsafe impl NSObjectProtocol for AppDelegate {}
impl AppDelegate {
#[unsafe(method(acceptsPreviewPanelControl:))]
fn accepts_preview(&self, _panel: &AnyObject) -> bool {
crate::failure::guard("the app delegate (accepts_preview)", false, || {
crate::quicklook::source().is_some()
})
}
#[unsafe(method(beginPreviewPanelControl:))]
fn begin_preview(&self, panel: &AnyObject) {
crate::failure::guard("the app delegate (begin_preview)", (), || {
if let Some(source) = crate::quicklook::source() {
let _: () = unsafe { msg_send![panel, setDataSource: &*source] };
}
})
}
#[unsafe(method(endPreviewPanelControl:))]
fn end_preview(&self, _panel: &AnyObject) {
crate::failure::guard("the app delegate (end_preview)", (), || {
})
}
}
unsafe impl NSApplicationDelegate for AppDelegate {
#[unsafe(method(application:openURLs:))]
fn open_urls(&self, _app: &NSApplication, urls: &objc2_foundation::NSArray<NSURL>) {
crate::failure::guard("the app delegate (open_urls)", (), || {
let (files, links): (Vec<_>, Vec<_>) = urls.iter().partition(|u| u.isFileURL());
let paths: Vec<PathBuf> = files.iter().filter_map(|u| u.path()).map(|p| PathBuf::from(p.to_string())).collect();
let links: Vec<String> = links.iter().filter_map(|u| u.absoluteString()).map(|s| s.to_string()).collect();
crate::later(move || {
for p in &paths {
open_reporting(p);
}
if !links.is_empty() {
match OPEN_URL.with_borrow(Clone::clone) {
Some(f) => links.iter().for_each(|l| f(l)),
None => eprintln!("ferrokit: {} opened, but the app has no App::on_open_url", links.join(", ")),
}
}
});
})
}
#[unsafe(method_id(applicationDockMenu:))]
fn dock_menu(&self, _app: &NSApplication) -> Option<Retained<objc2_app_kit::NSMenu>> {
crate::failure::guard("the app delegate (dock_menu)", None, || {
let items = DOCK_MENU.with_borrow(Clone::clone);
items.map(|items| {
let (menu, targets) = crate::chrome::build_menu(MainThreadMarker::from(self), items());
DOCK_TARGETS.with_borrow_mut(|t| *t = targets);
menu
})
})
}
#[unsafe(method(applicationShouldTerminate:))]
fn should_terminate(&self, _app: &NSApplication) -> NSApplicationTerminateReply {
crate::failure::guard("the app delegate (should_terminate)", NSApplicationTerminateReply::TerminateCancel, || {
remember_open();
if !DOCS.with_borrow(|docs| docs.iter().any(|d| d.edited())) {
return NSApplicationTerminateReply::TerminateNow;
}
crate::later(review_and_quit);
NSApplicationTerminateReply::TerminateLater
})
}
}
);
pub(crate) type DockItems = Rc<dyn Fn() -> Vec<crate::MenuItem>>;
pub(crate) type OpenUrl = Rc<dyn Fn(&str)>;
thread_local! {
pub(crate) static OPEN_URL: UiCell<Option<OpenUrl>> = const { UiCell::new(None) };
pub(crate) static DOCK_MENU: UiCell<Option<DockItems>> = const { UiCell::new(None) };
static DOCK_TARGETS: UiCell<Vec<Retained<NSObject>>> = const { UiCell::new(Vec::new()) };
}
pub(crate) fn install<T: Clone + PartialEq + 'static>(kind: DocumentType<T>) {
let opener: Rc<dyn Opener> = Rc::new(Rc::new(kind));
OPENER.with_borrow_mut(|o| *o = Some(opener));
install_delegate();
}
pub(crate) fn install_delegate() {
if DELEGATE.with_borrow(Option::is_some) {
return;
}
let mtm = MainThreadMarker::new().expect("main thread");
let delegate = AppDelegate::alloc(mtm).set_ivars(());
let delegate: Retained<AppDelegate> = unsafe { msg_send![super(delegate), init] };
NSApplication::sharedApplication(mtm).setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
DELEGATE.with_borrow_mut(|d| *d = Some(delegate));
}