| @@ -1,9 +1,9 @@ |
| 1 | 1 | use super::BoolExt; |
| 2 | 2 | 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, |
| 7 | 7 | }; |
| 8 | 8 | use anyhow::anyhow; |
| 9 | 9 | use block::ConcreteBlock; |
| @@ -155,12 +155,12 @@ pub struct MacPlatformState { · 145 unmodified lines |
| 155 | 155 | reopen: Option<Box<dyn FnMut()>>, |
| 156 | 156 | quit: Option<Box<dyn FnMut()>>, |
| 157 | 157 | 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>>, |
| 160 | 160 | will_open_menu: Option<Box<dyn FnMut()>>, |
| 161 | + menu_actions: Vec<Box<dyn Action>>, |
| 161 | 162 | open_urls: Option<Box<dyn FnMut(Vec<String>)>>, |
| 162 | 163 | finish_launching: Option<Box<dyn FnOnce()>>, |
| 163 | | − // menu_actions: Vec<Box<dyn Action>>, |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | impl MacPlatform { |
| @@ -179,12 +179,12 @@ impl MacPlatform { · 12 unmodified lines |
| 179 | 179 | reopen: None, |
| 180 | 180 | quit: None, |
| 181 | 181 | event: None, |
| 182 | + menu_command: None, |
| 183 | + validate_menu_command: None, |
| 182 | 184 | will_open_menu: None, |
| 185 | + menu_actions: Default::default(), |
| 183 | 186 | open_urls: None, |
| 184 | 187 | finish_launching: None, |
| 185 | | − // menu_command: None, |
| 186 | | − // validate_menu_command: None, |
| 187 | | − // menu_actions: Default::default(), |
| 188 | 188 | })) |
| 189 | 189 | } |
| 190 | 190 | |
| @@ -681,17 +681,17 @@ impl Platform for MacPlatform { · 490 unmodified lines |
| 681 | 681 | } |
| 682 | 682 | } |
| 683 | 683 | |
| 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 | + } |
| 687 | 687 | |
| 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 | + } |
| 691 | 691 | |
| 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 | + } |
| 695 | 695 | |
| 696 | 696 | // fn set_menus(&self, menus: Vec<Menu>, keystroke_matcher: &KeymapMatcher) { |
| 697 | 697 | // unsafe { |
| @@ -956,7 +956,7 @@ unsafe fn path_from_objc(path: id) -> PathBuf { · 258 unmodified lines |
| 956 | 956 | PathBuf::from(path) |
| 957 | 957 | } |
| 958 | 958 | |
| 959 | | −unsafe fn get_foreground_platform(object: &mut Object) -> &MacPlatform { |
| 959 | +unsafe fn get_mac_platform(object: &mut Object) -> &MacPlatform { |
| 960 | 960 | let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR); |
| 961 | 961 | assert!(!platform_ptr.is_null()); |
| 962 | 962 | &*(platform_ptr as *const MacPlatform) |
| @@ -965,7 +965,7 @@ unsafe fn get_foreground_platform(object: &mut Object) -> &MacPlatform { · 2 unmodified lines |
| 965 | 965 | extern "C" fn send_event(this: &mut Object, _sel: Sel, native_event: id) { |
| 966 | 966 | unsafe { |
| 967 | 967 | if let Some(event) = InputEvent::from_native(native_event, None) { |
| 968 | | − let platform = get_foreground_platform(this); |
| 968 | + let platform = get_mac_platform(this); |
| 969 | 969 | if let Some(callback) = platform.0.lock().event.as_mut() { |
| 970 | 970 | if !callback(event) { |
| 971 | 971 | return; |
| @@ -981,7 +981,7 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) { · 9 unmodified lines |
| 981 | 981 | let app: id = msg_send![APP_CLASS, sharedApplication]; |
| 982 | 982 | app.setActivationPolicy_(NSApplicationActivationPolicyRegular); |
| 983 | 983 | |
| 984 | | − let platform = get_foreground_platform(this); |
| 984 | + let platform = get_mac_platform(this); |
| 985 | 985 | let callback = platform.0.lock().finish_launching.take(); |
| 986 | 986 | if let Some(callback) = callback { |
| 987 | 987 | callback(); |
| @@ -991,7 +991,7 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) { · 3 unmodified lines |
| 991 | 991 | |
| 992 | 992 | extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_windows: bool) { |
| 993 | 993 | if !has_open_windows { |
| 994 | | − let platform = unsafe { get_foreground_platform(this) }; |
| 994 | + let platform = unsafe { get_mac_platform(this) }; |
| 995 | 995 | if let Some(callback) = platform.0.lock().reopen.as_mut() { |
| 996 | 996 | callback(); |
| 997 | 997 | } |
| @@ -999,21 +999,21 @@ extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_wi · 1 unmodified line |
| 999 | 999 | } |
| 1000 | 1000 | |
| 1001 | 1001 | 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) }; |
| 1003 | 1003 | if let Some(callback) = platform.0.lock().become_active.as_mut() { |
| 1004 | 1004 | callback(); |
| 1005 | 1005 | } |
| 1006 | 1006 | } |
| 1007 | 1007 | |
| 1008 | 1008 | 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) }; |
| 1010 | 1010 | if let Some(callback) = platform.0.lock().resign_active.as_mut() { |
| 1011 | 1011 | callback(); |
| 1012 | 1012 | } |
| 1013 | 1013 | } |
| 1014 | 1014 | |
| 1015 | 1015 | 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) }; |
| 1017 | 1017 | if let Some(callback) = platform.0.lock().quit.as_mut() { |
| 1018 | 1018 | callback(); |
| 1019 | 1019 | } |
| @@ -1035,49 +1035,47 @@ extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) { · 15 unmodified lines |
| 1035 | 1035 | }) |
| 1036 | 1036 | .collect::<Vec<_>>() |
| 1037 | 1037 | }; |
| 1038 | | − let platform = unsafe { get_foreground_platform(this) }; |
| 1038 | + let platform = unsafe { get_mac_platform(this) }; |
| 1039 | 1039 | if let Some(callback) = platform.0.lock().open_urls.as_mut() { |
| 1040 | 1040 | callback(urls); |
| 1041 | 1041 | } |
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 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 | + } |
| 1058 | 1057 | } |
| 1059 | 1058 | |
| 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 | + } |
| 1076 | 1074 | } |
| 1077 | 1075 | |
| 1078 | 1076 | extern "C" fn menu_will_open(this: &mut Object, _: Sel, _: id) { |
| 1079 | 1077 | unsafe { |
| 1080 | | − let platform = get_foreground_platform(this); |
| 1078 | + let platform = get_mac_platform(this); |
| 1081 | 1079 | let mut platform = platform.0.lock(); |
| 1082 | 1080 | if let Some(mut callback) = platform.will_open_menu.take() { |
| 1083 | 1081 | callback(); |