Add an API for setting a window's title

This controls how the window appears in the Window menu.
This commit is contained in:
Max Brunsfeld
2022-05-27 10:45:55 -07:00
parent f4d13ef596
commit 04bd57b2c7
5 changed files with 44 additions and 3 deletions
+5
View File
@@ -202,6 +202,11 @@ impl MacForegroundPlatform {
menu_bar_item.setSubmenu_(menu);
menu_bar.addItem_(menu_bar_item);
if menu_name == "Window" {
let app: id = msg_send![APP_CLASS, sharedApplication];
app.setWindowsMenu_(menu);
}
}
menu_bar
+8 -1
View File
@@ -386,8 +386,15 @@ impl platform::Window for Window {
}
fn activate(&self) {
unsafe { msg_send![self.0.borrow().native_window, makeKeyAndOrderFront: nil] }
}
fn set_title(&mut self, title: &str) {
unsafe {
let _: () = msg_send![self.0.borrow().native_window, makeKeyAndOrderFront: nil];
let app = NSApplication::sharedApplication(nil);
let window = self.0.borrow().native_window;
let title = ns_string(title);
msg_send![app, changeWindowsItem:window title:title filename:false]
}
}
}
+10
View File
@@ -37,6 +37,7 @@ pub struct Window {
event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
resize_handlers: Vec<Box<dyn FnMut()>>,
close_handlers: Vec<Box<dyn FnOnce()>>,
pub(crate) title: Option<String>,
pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
}
@@ -189,9 +190,14 @@ impl Window {
close_handlers: Vec::new(),
scale_factor: 1.0,
current_scene: None,
title: None,
pending_prompts: Default::default(),
}
}
pub fn title(&self) -> Option<String> {
self.title.clone()
}
}
impl super::Dispatcher for Dispatcher {
@@ -248,6 +254,10 @@ impl super::Window for Window {
}
fn activate(&self) {}
fn set_title(&mut self, title: &str) {
self.title = Some(title.to_string())
}
}
pub fn platform() -> Platform {