Skip to content

Commit

Permalink
Create an API for assigning the menubar contents
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbrunsfeld committed Apr 8, 2021
1 parent 0a12774 commit 334de06
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 60 deletions.
18 changes: 18 additions & 0 deletions gpui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ pub trait UpdateView {
F: FnOnce(&mut T, &mut ViewContext<T>) -> S;
}

pub struct Menu<'a> {
pub name: &'a str,
pub items: &'a [MenuItem<'a>],
}

pub enum MenuItem<'a> {
Action {
name: &'a str,
keystroke: Option<&'a str>,
action: &'a str,
},
Separator,
}

#[derive(Clone)]
pub struct App(Rc<RefCell<MutableAppContext>>);

Expand Down Expand Up @@ -365,6 +379,10 @@ impl MutableAppContext {
&self.ctx
}

pub fn platform(&self) -> Arc<dyn platform::App> {
self.platform.clone()
}

pub fn foreground_executor(&self) -> &Rc<executor::Foreground> {
&self.foreground
}
Expand Down
8 changes: 8 additions & 0 deletions gpui/src/platform/mac/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{BoolExt as _, Dispatcher, FontSystem, Window};
use crate::{executor, platform};
use anyhow::Result;
use cocoa::{appkit::NSApplication, base::nil};
use objc::{msg_send, sel, sel_impl};
use std::{rc::Rc, sync::Arc};

pub struct App {
Expand Down Expand Up @@ -41,4 +42,11 @@ impl platform::App for App {
fn fonts(&self) -> Arc<dyn platform::FontSystem> {
self.fonts.clone()
}

fn quit(&self) {
unsafe {
let app = NSApplication::sharedApplication(nil);
let _: () = msg_send![app, terminate: nil];
}
}
}
159 changes: 109 additions & 50 deletions gpui/src/platform/mac/runner.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::platform::Event;
use crate::{keymap::Keystroke, platform::Event, Menu, MenuItem};
use cocoa::{
appkit::{
NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular, NSMenu,
NSMenuItem, NSWindow,
NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular,
NSEventModifierFlags, NSMenu, NSMenuItem, NSWindow,
},
base::{id, nil, selector},
foundation::{NSArray, NSAutoreleasePool, NSString},
foundation::{NSArray, NSAutoreleasePool, NSInteger, NSString},
};
use ctor::ctor;
use objc::{
Expand Down Expand Up @@ -53,6 +53,10 @@ unsafe fn build_classes() {
sel!(applicationDidResignActive:),
did_resign_active as extern "C" fn(&mut Object, Sel, id),
);
decl.add_method(
sel!(handleGPUIMenuItem:),
handle_menu_item as extern "C" fn(&mut Object, Sel, id),
);
decl.add_method(
sel!(application:openFiles:),
open_files as extern "C" fn(&mut Object, Sel, id, id),
Expand All @@ -68,12 +72,89 @@ pub struct Runner {
resign_active_callback: Option<Box<dyn FnMut()>>,
event_callback: Option<Box<dyn FnMut(Event) -> bool>>,
open_files_callback: Option<Box<dyn FnMut(Vec<PathBuf>)>>,
menu_command_callback: Option<Box<dyn FnMut(&str)>>,
menu_item_actions: Vec<String>,
}

impl Runner {
pub fn new() -> Self {
Default::default()
}

unsafe fn create_menu_bar(&mut self, menus: &[Menu]) -> id {
let menu_bar = NSMenu::new(nil).autorelease();
self.menu_item_actions.clear();

for menu_config in menus {
let menu_bar_item = NSMenuItem::new(nil).autorelease();
let menu = NSMenu::new(nil).autorelease();

menu.setTitle_(ns_string(menu_config.name));

for item_config in menu_config.items {
let item;

match item_config {
MenuItem::Separator => {
item = NSMenuItem::separatorItem(nil);
}
MenuItem::Action {
name,
keystroke,
action,
} => {
if let Some(keystroke) = keystroke {
let keystroke = Keystroke::parse(keystroke).unwrap_or_else(|err| {
panic!(
"Invalid keystroke for menu item {}:{} - {:?}",
menu_config.name, name, err
)
});

let mut mask = NSEventModifierFlags::empty();
for (modifier, flag) in &[
(keystroke.cmd, NSEventModifierFlags::NSCommandKeyMask),
(keystroke.ctrl, NSEventModifierFlags::NSControlKeyMask),
(keystroke.alt, NSEventModifierFlags::NSAlternateKeyMask),
] {
if *modifier {
mask |= *flag;
}
}

item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string(name),
selector("handleGPUIMenuItem:"),
ns_string(&keystroke.key),
)
.autorelease();
item.setKeyEquivalentModifierMask_(mask);
} else {
item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string(name),
selector("handleGPUIMenuItem:"),
ns_string(""),
)
.autorelease();
}

let tag = self.menu_item_actions.len() as NSInteger;
let _: () = msg_send![item, setTag: tag];
self.menu_item_actions.push(action.to_string());
}
}

menu.addItem_(item);
}

menu_bar_item.setSubmenu_(menu);
menu_bar.addItem_(menu_bar_item);
}

menu_bar
}
}

impl crate::platform::Runner for Runner {
Expand All @@ -82,6 +163,11 @@ impl crate::platform::Runner for Runner {
self
}

fn on_menu_command<F: 'static + FnMut(&str)>(mut self, callback: F) -> Self {
self.menu_command_callback = Some(Box::new(callback));
self
}

fn on_become_active<F: 'static + FnMut()>(mut self, callback: F) -> Self {
log::info!("become active");
self.become_active_callback = Some(Box::new(callback));
Expand All @@ -103,6 +189,14 @@ impl crate::platform::Runner for Runner {
self
}

fn set_menus(mut self, menus: &[Menu]) -> Self {
unsafe {
let app: id = msg_send![APP_CLASS, sharedApplication];
app.setMainMenu_(self.create_menu_bar(menus));
}
self
}

fn run(self) {
unsafe {
let self_ptr = Box::into_raw(Box::new(self));
Expand All @@ -114,7 +208,6 @@ impl crate::platform::Runner for Runner {
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
(*app).set_ivar(RUNNER_IVAR, self_ptr as *mut c_void);
(*app_delegate).set_ivar(RUNNER_IVAR, self_ptr as *mut c_void);
app.setMainMenu_(create_menu_bar());
app.setDelegate_(app_delegate);
app.run();
pool.drain();
Expand Down Expand Up @@ -190,51 +283,17 @@ extern "C" fn open_files(this: &mut Object, _: Sel, _: id, paths: id) {
}
}

unsafe fn create_menu_bar() -> id {
let menu_bar = NSMenu::new(nil).autorelease();

// App menu
let app_menu_item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string("Application"),
Sel::from_ptr(ptr::null()),
ns_string(""),
)
.autorelease();
let quit_item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string("Quit"),
selector("terminate:"),
ns_string("q\0"),
)
.autorelease();
let app_menu = NSMenu::new(nil).autorelease();
app_menu.addItem_(quit_item);
app_menu_item.setSubmenu_(app_menu);
menu_bar.addItem_(app_menu_item);

// File menu
let file_menu_item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string("File"),
Sel::from_ptr(ptr::null()),
ns_string(""),
)
.autorelease();
let open_item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string("Open"),
selector("openDocument:"),
ns_string("o\0"),
)
.autorelease();
let file_menu = NSMenu::new(nil).autorelease();
file_menu.setTitle_(ns_string("File"));
file_menu.addItem_(open_item);
file_menu_item.setSubmenu_(file_menu);
menu_bar.addItem_(file_menu_item);

menu_bar
extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
unsafe {
let runner = get_runner(this);
if let Some(callback) = runner.menu_command_callback.as_mut() {
let tag: NSInteger = msg_send![item, tag];
let index = tag as usize;
if let Some(action) = runner.menu_item_actions.get(index) {
callback(&action);
}
}
}
}

unsafe fn ns_string(string: &str) -> id {
Expand Down
7 changes: 5 additions & 2 deletions gpui/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ use crate::{
vector::Vector2F,
},
text_layout::Line,
Scene,
Menu, Scene,
};
use anyhow::Result;
use async_task::Runnable;
pub use event::Event;
use std::{ops::Range, path::PathBuf, rc::Rc, sync::Arc};

pub trait Runner {
fn on_finish_launching<F: 'static + FnOnce()>(self, callback: F) -> Self where;
fn on_finish_launching<F: 'static + FnOnce()>(self, callback: F) -> Self;
fn on_menu_command<F: 'static + FnMut(&str)>(self, callback: F) -> Self;
fn on_become_active<F: 'static + FnMut()>(self, callback: F) -> Self;
fn on_resign_active<F: 'static + FnMut()>(self, callback: F) -> Self;
fn on_event<F: 'static + FnMut(Event) -> bool>(self, callback: F) -> Self;
fn on_open_files<F: 'static + FnMut(Vec<PathBuf>)>(self, callback: F) -> Self;
fn set_menus(self, menus: &[Menu]) -> Self;
fn run(self);
}

Expand All @@ -40,6 +42,7 @@ pub trait App {
executor: Rc<executor::Foreground>,
) -> Result<Box<dyn Window>>;
fn fonts(&self) -> Arc<dyn FontSystem>;
fn quit(&self);
}

pub trait Dispatcher: Send + Sync {
Expand Down
2 changes: 2 additions & 0 deletions gpui/src/platform/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl super::App for App {
fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
self.fonts.clone()
}

fn quit(&self) {}
}

impl Window {
Expand Down
1 change: 1 addition & 0 deletions zed/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod assets;
pub mod editor;
pub mod file_finder;
pub mod menus;
mod operation_queue;
pub mod settings;
mod sum_tree;
Expand Down
24 changes: 16 additions & 8 deletions zed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::LevelFilter;
use simplelog::SimpleLogger;
use std::{fs, path::PathBuf};
use zed::{
assets, editor, file_finder, settings,
assets, editor, file_finder, menus, settings,
workspace::{self, OpenParams},
};

Expand All @@ -14,10 +14,18 @@ fn main() {
let app = gpui::App::new(assets::Assets).unwrap();
let (_, settings_rx) = settings::channel(&app.font_cache()).unwrap();

{
let mut app = app.clone();
platform::runner()
.on_finish_launching(move || {
platform::runner()
.set_menus(menus::MENUS)
.on_menu_command({
let app = app.clone();
move |command| {
log::info!("menu command: {}", command);
app.dispatch_global_action(command, ())
}
})
.on_finish_launching({
let mut app = app.clone();
move || {
workspace::init(&mut app);
editor::init(&mut app);
file_finder::init(&mut app);
Expand All @@ -36,9 +44,9 @@ fn main() {
},
);
}
})
.run();
}
}
})
.run();
}

fn init_logger() {
Expand Down

0 comments on commit 334de06

Please sign in to comment.