Fix Recent Documents List (continues #8952) (#9919)

@SomeoneToIgnore This code should 100% work for future Zed users, but
for current Zed users, Zed's internal list of recents may not be synced
w/ macOS' Recent Documents at first. If needed this can be fixed by
calling `cx.refresh_recent_documents` on startup, but that feels a bit
unnecessary.

Release Notes:

- Fixes behavior of Recent Documents list on macOS
This commit is contained in:
Daniel Zhu 2024-03-29 14:17:25 -07:00 committed by GitHub
parent 35e1229fbb
commit 30193647f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 17 additions and 89 deletions

View file

@ -1129,17 +1129,14 @@ impl AppContext {
self.platform.set_menus(menus, &self.keymap.borrow());
}
/// Adds given path to list of recent paths for the application.
/// Adds given path to the bottom of the list of recent paths for the application.
/// The list is usually shown on the application icon's context menu in the dock,
/// and allows to open the recent files via that context menu.
pub fn add_recent_documents(&mut self, paths: &[PathBuf]) {
self.platform.add_recent_documents(paths);
/// If the path is already in the list, it will be moved to the bottom of the list.
pub fn add_recent_document(&mut self, path: &Path) {
self.platform.add_recent_document(path);
}
/// Clears the list of recent paths from the application.
pub fn clear_recent_documents(&mut self) {
self.platform.clear_recent_documents();
}
/// Dispatch an action to the currently active window or global action handler
/// See [action::Action] for more information on how actions work
pub fn dispatch_action(&mut self, action: &dyn Action) {

View file

@ -118,8 +118,7 @@ pub(crate) trait Platform: 'static {
fn on_event(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>);
fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
fn add_recent_documents(&self, _paths: &[PathBuf]) {}
fn clear_recent_documents(&self) {}
fn add_recent_document(&self, _path: &Path) {}
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);

View file

@ -764,12 +764,8 @@ impl Platform for MacPlatform {
}
}
fn add_recent_documents(&self, paths: &[PathBuf]) {
for path in paths {
let Some(path_str) = path.to_str() else {
log::error!("Not adding to recent documents a non-unicode path: {path:?}");
continue;
};
fn add_recent_document(&self, path: &Path) {
if let Some(path_str) = path.to_str() {
unsafe {
let document_controller: id =
msg_send![class!(NSDocumentController), sharedDocumentController];
@ -779,14 +775,6 @@ impl Platform for MacPlatform {
}
}
fn clear_recent_documents(&self) {
unsafe {
let document_controller: id =
msg_send![class!(NSDocumentController), sharedDocumentController];
let _: () = msg_send![document_controller, clearRecentDocuments:nil];
}
}
fn local_timezone(&self) -> UtcOffset {
unsafe {
let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone];

View file

@ -9,7 +9,7 @@ use futures::channel::oneshot;
use parking_lot::Mutex;
use std::{
cell::RefCell,
path::PathBuf,
path::{Path, PathBuf},
rc::{Rc, Weak},
sync::Arc,
};
@ -239,9 +239,7 @@ impl Platform for TestPlatform {
fn set_menus(&self, _menus: Vec<crate::Menu>, _keymap: &Keymap) {}
fn add_recent_documents(&self, _paths: &[PathBuf]) {}
fn clear_recent_documents(&self) {}
fn add_recent_document(&self, _paths: &Path) {}
fn on_app_menu_action(&self, _callback: Box<dyn FnMut(&dyn crate::Action)>) {}