History

Reintroduce menu-related platform callbacks

Nathan Sobo committed 2 years ago

Commit dffe0ea1 parent 30725d63 files · +73 −59Browse files

Unified · Split

3 files
  1. crates/gpui2/src/platform.rs +5 −1
  2. crates/gpui2/src/platform/mac/platform.rs +56 −58
  3. crates/gpui2/src/platform/test/platform.rs +12 −0
crates/gpui2/src/platform.rs+5−1
@@ -5,7 +5,7 @@ mod mac; · 4 unmodified lines
55 mod test;
66
77 use crate::{
8− point, size, AnyWindowHandle, BackgroundExecutor, Bounds, DevicePixels, Font, FontId,
8+ point, size, Action, AnyWindowHandle, BackgroundExecutor, Bounds, DevicePixels, Font, FontId,
99 FontMetrics, FontRun, ForegroundExecutor, GlobalPixels, GlyphId, InputEvent, LineLayout,
1010 Pixels, Point, RenderGlyphParams, RenderImageParams, RenderSvgParams, Result, Scene,
1111 SharedString, Size, TaskLabel,
@@ -90,6 +90,10 @@ pub trait Platform: 'static { · 78 unmodified lines
9090 fn on_reopen(&self, callback: Box<dyn FnMut()>);
9191 fn on_event(&self, callback: Box<dyn FnMut(InputEvent) -> bool>);
9292
93+ fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>);
94+ fn on_will_open_menu(&self, callback: Box<dyn FnMut()>);
95+ fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
96+
9397 fn os_name(&self) -> &'static str;
9498 fn os_version(&self) -> Result<SemanticVersion>;
9599 fn app_version(&self) -> Result<SemanticVersion>;
crates/gpui2/src/platform/mac/platform.rs+56−58
@@ -1,9 +1,9 @@
11 use super::BoolExt;
22 use crate::{
3− AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId, ForegroundExecutor,
4− InputEvent, MacDispatcher, MacDisplay, MacDisplayLinker, MacTextSystem, MacWindow,
5− PathPromptOptions, Platform, PlatformDisplay, PlatformTextSystem, PlatformWindow, Result,
6− SemanticVersion, VideoTimestamp, WindowOptions,
3+ Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId,
4+ ForegroundExecutor, InputEvent, MacDispatcher, MacDisplay, MacDisplayLinker, MacTextSystem,
5+ MacWindow, PathPromptOptions, Platform, PlatformDisplay, PlatformTextSystem, PlatformWindow,
6+ Result, SemanticVersion, VideoTimestamp, WindowOptions,
77 };
88 use anyhow::anyhow;
99 use block::ConcreteBlock;
@@ -155,12 +155,12 @@ pub struct MacPlatformState { · 145 unmodified lines
155155 reopen: Option<Box<dyn FnMut()>>,
156156 quit: Option<Box<dyn FnMut()>>,
157157 event: Option<Box<dyn FnMut(InputEvent) -> bool>>,
158− // menu_command: Option<Box<dyn FnMut(&dyn Action)>>,
159− // validate_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
158+ menu_command: Option<Box<dyn FnMut(&dyn Action)>>,
159+ validate_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
160160 will_open_menu: Option<Box<dyn FnMut()>>,
161+ menu_actions: Vec<Box<dyn Action>>,
161162 open_urls: Option<Box<dyn FnMut(Vec<String>)>>,
162163 finish_launching: Option<Box<dyn FnOnce()>>,
163− // menu_actions: Vec<Box<dyn Action>>,
164164 }
165165
166166 impl MacPlatform {
@@ -179,12 +179,12 @@ impl MacPlatform { · 12 unmodified lines
179179 reopen: None,
180180 quit: None,
181181 event: None,
182+ menu_command: None,
183+ validate_menu_command: None,
182184 will_open_menu: None,
185+ menu_actions: Default::default(),
183186 open_urls: None,
184187 finish_launching: None,
185− // menu_command: None,
186− // validate_menu_command: None,
187− // menu_actions: Default::default(),
188188 }))
189189 }
190190
@@ -681,17 +681,17 @@ impl Platform for MacPlatform { · 490 unmodified lines
681681 }
682682 }
683683
684− // fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>) {
685− // self.0.lock().menu_command = Some(callback);
686− // }
684+ fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>) {
685+ self.0.lock().menu_command = Some(callback);
686+ }
687687
688− // fn on_will_open_menu(&self, callback: Box<dyn FnMut()>) {
689− // self.0.lock().will_open_menu = Some(callback);
690− // }
688+ fn on_will_open_menu(&self, callback: Box<dyn FnMut()>) {
689+ self.0.lock().will_open_menu = Some(callback);
690+ }
691691
692− // fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) {
693− // self.0.lock().validate_menu_command = Some(callback);
694− // }
692+ fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) {
693+ self.0.lock().validate_menu_command = Some(callback);
694+ }
695695
696696 // fn set_menus(&self, menus: Vec<Menu>, keystroke_matcher: &KeymapMatcher) {
697697 // unsafe {
@@ -956,7 +956,7 @@ unsafe fn path_from_objc(path: id) -> PathBuf { · 258 unmodified lines
956956 PathBuf::from(path)
957957 }
958958
959−unsafe fn get_foreground_platform(object: &mut Object) -> &MacPlatform {
959+unsafe fn get_mac_platform(object: &mut Object) -> &MacPlatform {
960960 let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR);
961961 assert!(!platform_ptr.is_null());
962962 &*(platform_ptr as *const MacPlatform)
@@ -965,7 +965,7 @@ unsafe fn get_foreground_platform(object: &mut Object) -> &MacPlatform { · 2 unmodified lines
965965 extern "C" fn send_event(this: &mut Object, _sel: Sel, native_event: id) {
966966 unsafe {
967967 if let Some(event) = InputEvent::from_native(native_event, None) {
968− let platform = get_foreground_platform(this);
968+ let platform = get_mac_platform(this);
969969 if let Some(callback) = platform.0.lock().event.as_mut() {
970970 if !callback(event) {
971971 return;
@@ -981,7 +981,7 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) { · 9 unmodified lines
981981 let app: id = msg_send![APP_CLASS, sharedApplication];
982982 app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
983983
984− let platform = get_foreground_platform(this);
984+ let platform = get_mac_platform(this);
985985 let callback = platform.0.lock().finish_launching.take();
986986 if let Some(callback) = callback {
987987 callback();
@@ -991,7 +991,7 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) { · 3 unmodified lines
991991
992992 extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_windows: bool) {
993993 if !has_open_windows {
994− let platform = unsafe { get_foreground_platform(this) };
994+ let platform = unsafe { get_mac_platform(this) };
995995 if let Some(callback) = platform.0.lock().reopen.as_mut() {
996996 callback();
997997 }
@@ -999,21 +999,21 @@ extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_wi · 1 unmodified line
999999 }
10001000
10011001 extern "C" fn did_become_active(this: &mut Object, _: Sel, _: id) {
1002− let platform = unsafe { get_foreground_platform(this) };
1002+ let platform = unsafe { get_mac_platform(this) };
10031003 if let Some(callback) = platform.0.lock().become_active.as_mut() {
10041004 callback();
10051005 }
10061006 }
10071007
10081008 extern "C" fn did_resign_active(this: &mut Object, _: Sel, _: id) {
1009− let platform = unsafe { get_foreground_platform(this) };
1009+ let platform = unsafe { get_mac_platform(this) };
10101010 if let Some(callback) = platform.0.lock().resign_active.as_mut() {
10111011 callback();
10121012 }
10131013 }
10141014
10151015 extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) {
1016− let platform = unsafe { get_foreground_platform(this) };
1016+ let platform = unsafe { get_mac_platform(this) };
10171017 if let Some(callback) = platform.0.lock().quit.as_mut() {
10181018 callback();
10191019 }
@@ -1035,49 +1035,47 @@ extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) { · 15 unmodified lines
10351035 })
10361036 .collect::<Vec<_>>()
10371037 };
1038− let platform = unsafe { get_foreground_platform(this) };
1038+ let platform = unsafe { get_mac_platform(this) };
10391039 if let Some(callback) = platform.0.lock().open_urls.as_mut() {
10401040 callback(urls);
10411041 }
10421042 }
10431043
1044−extern "C" fn handle_menu_item(__this: &mut Object, _: Sel, __item: id) {
1045− todo!()
1046− // unsafe {
1047− // let platform = get_foreground_platform(this);
1048− // let mut platform = platform.0.lock();
1049− // if let Some(mut callback) = platform.menu_command.take() {
1050− // let tag: NSInteger = msg_send![item, tag];
1051− // let index = tag as usize;
1052− // if let Some(action) = platform.menu_actions.get(index) {
1053− // callback(action.as_ref());
1054− // }
1055− // platform.menu_command = Some(callback);
1056− // }
1057− // }
1044+extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
1045+ unsafe {
1046+ let platform = get_mac_platform(this);
1047+ let mut platform = platform.0.lock();
1048+ if let Some(mut callback) = platform.menu_command.take() {
1049+ let tag: NSInteger = msg_send![item, tag];
1050+ let index = tag as usize;
1051+ if let Some(action) = platform.menu_actions.get(index) {
1052+ callback(action.as_ref());
1053+ }
1054+ platform.menu_command = Some(callback);
1055+ }
1056+ }
10581057 }
10591058
1060−extern "C" fn validate_menu_item(__this: &mut Object, _: Sel, __item: id) -> bool {
1061− todo!()
1062− // unsafe {
1063− // let mut result = false;
1064− // let platform = get_foreground_platform(this);
1065− // let mut platform = platform.0.lock();
1066− // if let Some(mut callback) = platform.validate_menu_command.take() {
1067− // let tag: NSInteger = msg_send![item, tag];
1068− // let index = tag as usize;
1069− // if let Some(action) = platform.menu_actions.get(index) {
1070− // result = callback(action.as_ref());
1071− // }
1072− // platform.validate_menu_command = Some(callback);
1073− // }
1074− // result
1075− // }
1059+extern "C" fn validate_menu_item(this: &mut Object, _: Sel, item: id) -> bool {
1060+ unsafe {
1061+ let mut result = false;
1062+ let platform = get_mac_platform(this);
1063+ let mut platform = platform.0.lock();
1064+ if let Some(mut callback) = platform.validate_menu_command.take() {
1065+ let tag: NSInteger = msg_send![item, tag];
1066+ let index = tag as usize;
1067+ if let Some(action) = platform.menu_actions.get(index) {
1068+ result = callback(action.as_ref());
1069+ }
1070+ platform.validate_menu_command = Some(callback);
1071+ }
1072+ result
1073+ }
10761074 }
10771075
10781076 extern "C" fn menu_will_open(this: &mut Object, _: Sel, _: id) {
10791077 unsafe {
1080− let platform = get_foreground_platform(this);
1078+ let platform = get_mac_platform(this);
10811079 let mut platform = platform.0.lock();
10821080 if let Some(mut callback) = platform.will_open_menu.take() {
10831081 callback();
crates/gpui2/src/platform/test/platform.rs+12−0
@@ -205,6 +205,18 @@ impl Platform for TestPlatform { · 204 unmodified lines
205205 unimplemented!()
206206 }
207207
208+ fn on_menu_command(&self, _callback: Box<dyn FnMut(&dyn crate::Action)>) {
209+ unimplemented!()
210+ }
211+
212+ fn on_will_open_menu(&self, _callback: Box<dyn FnMut()>) {
213+ unimplemented!()
214+ }
215+
216+ fn on_validate_menu_command(&self, _callback: Box<dyn FnMut(&dyn crate::Action) -> bool>) {
217+ unimplemented!()
218+ }
219+
208220 fn os_name(&self) -> &'static str {
209221 "test"
210222 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t