| @@ -1,1102 +1,1130 @@ |
| 1 | // use anyhow::{anyhow, Result}; | 1 | use super::BoolExt; |
| 2 | // use block::ConcreteBlock; | 2 | use crate::{ |
| 3 | // use cocoa::{ | 3 | AnyWindowHandle, ClipboardItem, CursorStyle, Event, ForegroundExecutor, MacDispatcher, |
| 4 | // appkit::{ | 4 | MacScreen, MacTextSystem, MacWindow, PathPromptOptions, Platform, PlatformScreen, |
| 5 | // NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular, | 5 | PlatformTextSystem, PlatformWindow, Result, SemanticVersion, WindowOptions, |
| 6 | // NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSPasteboard, | 6 | }; |
| 7 | // NSSavePanel, NSWindow, | 7 | use anyhow::anyhow; |
| 8 | // }, | 8 | use block::ConcreteBlock; |
| 9 | // base::{id, nil, selector, BOOL, YES}, | 9 | use cocoa::{ |
| 10 | // foundation::{NSArray, NSAutoreleasePool, NSData, NSInteger, NSString, NSUInteger, NSURL}, | 10 | appkit::{ |
| 11 | // }; | 11 | NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular, |
| 12 | // use core_foundation::{ | 12 | NSModalResponse, NSOpenPanel, NSPasteboard, NSPasteboardTypeString, NSSavePanel, NSWindow, |
| 13 | // base::{CFTypeRef, OSStatus}, | 13 | }, |
| 14 | // dictionary::CFDictionaryRef, | 14 | base::{id, nil, BOOL, YES}, |
| 15 | // string::CFStringRef, | 15 | foundation::{ |
| 16 | // }; | 16 | NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSProcessInfo, NSString, |
| 17 | // use ctor::ctor; | 17 | NSUInteger, NSURL, |
| 18 | // use objc::{ | 18 | }, |
| 19 | // class, | 19 | }; |
| 20 | // declare::ClassDecl, | 20 | use core_foundation::{ |
| 21 | // msg_send, | 21 | base::{CFType, CFTypeRef, OSStatus, TCFType as _}, |
| 22 | // runtime::{Class, Object, Sel}, | 22 | boolean::CFBoolean, |
| 23 | // sel, sel_impl, | 23 | data::CFData, |
| 24 | // }; | 24 | dictionary::{CFDictionary, CFDictionaryRef, CFMutableDictionary}, |
| 25 | | 25 | string::{CFString, CFStringRef}, |
| 26 | // use postage::oneshot; | 26 | }; |
| 27 | // use ptr::null_mut; | 27 | use ctor::ctor; |
| 28 | // use std::{ | 28 | use futures::channel::oneshot; |
| 29 | // cell::{Cell, RefCell}, | 29 | use objc::{ |
| 30 | // convert::TryInto, | 30 | class, |
| 31 | // ffi::{c_void, CStr, OsStr}, | 31 | declare::ClassDecl, |
| 32 | // os::{raw::c_char, unix::ffi::OsStrExt}, | 32 | msg_send, |
| 33 | // path::{Path, PathBuf}, | 33 | runtime::{Class, Object, Sel}, |
| 34 | // process::Command, | 34 | sel, sel_impl, |
| 35 | // ptr, | 35 | }; |
| 36 | // rc::Rc, | 36 | use ptr::null_mut; |
| 37 | // slice, str, | 37 | use std::{ |
| 38 | // sync::Arc, | 38 | cell::{Cell, RefCell}, |
| 39 | // }; | 39 | convert::TryInto, |
| 40 | | 40 | ffi::{c_void, CStr, OsStr}, |
| 41 | // use crate::Event; | 41 | os::{raw::c_char, unix::ffi::OsStrExt}, |
| 42 | | 42 | path::{Path, PathBuf}, |
| 43 | // #[allow(non_upper_case_globals)] | 43 | process::Command, |
| 44 | // const NSUTF8StringEncoding: NSUInteger = 4; | 44 | ptr, |
| 45 | | 45 | rc::Rc, |
| 46 | // const MAC_PLATFORM_IVAR: &str = "platform"; | 46 | slice, str, |
| 47 | // static mut APP_CLASS: *const Class = ptr::null(); | 47 | sync::Arc, |
| 48 | // static mut APP_DELEGATE_CLASS: *const Class = ptr::null(); | 48 | }; |
| 49 | | 49 | use time::UtcOffset; |
| 50 | // #[ctor] | 50 | |
| 51 | // unsafe fn build_classes() { | 51 | #[allow(non_upper_case_globals)] |
| 52 | // APP_CLASS = { | 52 | const NSUTF8StringEncoding: NSUInteger = 4; |
| 53 | // let mut decl = ClassDecl::new("GPUIApplication", class!(NSApplication)).unwrap(); | 53 | |
| 54 | // decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR); | 54 | #[allow(non_upper_case_globals)] |
| 55 | // decl.add_method( | 55 | pub const NSViewLayerContentsRedrawDuringViewResize: NSInteger = 2; |
| 56 | // sel!(sendEvent:), | 56 | |
| 57 | // send_event as extern "C" fn(&mut Object, Sel, id), | 57 | const MAC_PLATFORM_IVAR: &str = "platform"; |
| 58 | // ); | 58 | static mut APP_CLASS: *const Class = ptr::null(); |
| 59 | // decl.register() | 59 | static mut APP_DELEGATE_CLASS: *const Class = ptr::null(); |
| 60 | // }; | 60 | |
| 61 | | 61 | #[ctor] |
| 62 | // APP_DELEGATE_CLASS = { | 62 | unsafe fn build_classes() { |
| 63 | // let mut decl = ClassDecl::new("GPUIApplicationDelegate", class!(NSResponder)).unwrap(); | 63 | APP_CLASS = { |
| 64 | // decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR); | 64 | let mut decl = ClassDecl::new("GPUIApplication", class!(NSApplication)).unwrap(); |
| 65 | // decl.add_method( | 65 | decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR); |
| 66 | // sel!(applicationDidFinishLaunching:), | 66 | decl.add_method( |
| 67 | // did_finish_launching as extern "C" fn(&mut Object, Sel, id), | 67 | sel!(sendEvent:), |
| 68 | // ); | 68 | send_event as extern "C" fn(&mut Object, Sel, id), |
| 69 | // decl.add_method( | 69 | ); |
| 70 | // sel!(applicationShouldHandleReopen:hasVisibleWindows:), | 70 | decl.register() |
| 71 | // should_handle_reopen as extern "C" fn(&mut Object, Sel, id, bool), | 71 | }; |
| 72 | // ); | 72 | |
| 73 | // decl.add_method( | 73 | APP_DELEGATE_CLASS = { |
| 74 | // sel!(applicationDidBecomeActive:), | 74 | let mut decl = ClassDecl::new("GPUIApplicationDelegate", class!(NSResponder)).unwrap(); |
| 75 | // did_become_active as extern "C" fn(&mut Object, Sel, id), | 75 | decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR); |
| 76 | // ); | 76 | decl.add_method( |
| 77 | // decl.add_method( | 77 | sel!(applicationDidFinishLaunching:), |
| 78 | // sel!(applicationDidResignActive:), | 78 | did_finish_launching as extern "C" fn(&mut Object, Sel, id), |
| 79 | // did_resign_active as extern "C" fn(&mut Object, Sel, id), | 79 | ); |
| 80 | // ); | 80 | decl.add_method( |
| 81 | // decl.add_method( | 81 | sel!(applicationShouldHandleReopen:hasVisibleWindows:), |
| 82 | // sel!(applicationWillTerminate:), | 82 | should_handle_reopen as extern "C" fn(&mut Object, Sel, id, bool), |
| 83 | // will_terminate as extern "C" fn(&mut Object, Sel, id), | 83 | ); |
| 84 | // ); | 84 | decl.add_method( |
| 85 | // decl.add_method( | 85 | sel!(applicationDidBecomeActive:), |
| 86 | // sel!(handleGPUIMenuItem:), | 86 | did_become_active as extern "C" fn(&mut Object, Sel, id), |
| 87 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 87 | ); |
| 88 | // ); | 88 | decl.add_method( |
| 89 | // // Add menu item handlers so that OS save panels have the correct key commands | 89 | sel!(applicationDidResignActive:), |
| 90 | // decl.add_method( | 90 | did_resign_active as extern "C" fn(&mut Object, Sel, id), |
| 91 | // sel!(cut:), | 91 | ); |
| 92 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 92 | decl.add_method( |
| 93 | // ); | 93 | sel!(applicationWillTerminate:), |
| 94 | // decl.add_method( | 94 | will_terminate as extern "C" fn(&mut Object, Sel, id), |
| 95 | // sel!(copy:), | 95 | ); |
| 96 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 96 | decl.add_method( |
| 97 | // ); | 97 | sel!(handleGPUIMenuItem:), |
| 98 | // decl.add_method( | 98 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 99 | // sel!(paste:), | 99 | ); |
| 100 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 100 | // Add menu item handlers so that OS save panels have the correct key commands |
| 101 | // ); | 101 | decl.add_method( |
| 102 | // decl.add_method( | 102 | sel!(cut:), |
| 103 | // sel!(selectAll:), | 103 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 104 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 104 | ); |
| 105 | // ); | 105 | decl.add_method( |
| 106 | // decl.add_method( | 106 | sel!(copy:), |
| 107 | // sel!(undo:), | 107 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 108 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 108 | ); |
| 109 | // ); | 109 | decl.add_method( |
| 110 | // decl.add_method( | 110 | sel!(paste:), |
| 111 | // sel!(redo:), | 111 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 112 | // handle_menu_item as extern "C" fn(&mut Object, Sel, id), | 112 | ); |
| 113 | // ); | 113 | decl.add_method( |
| 114 | // decl.add_method( | 114 | sel!(selectAll:), |
| 115 | // sel!(validateMenuItem:), | 115 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 116 | // validate_menu_item as extern "C" fn(&mut Object, Sel, id) -> bool, | 116 | ); |
| 117 | // ); | 117 | decl.add_method( |
| 118 | // decl.add_method( | 118 | sel!(undo:), |
| 119 | // sel!(menuWillOpen:), | 119 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 120 | // menu_will_open as extern "C" fn(&mut Object, Sel, id), | 120 | ); |
| 121 | // ); | 121 | decl.add_method( |
| 122 | // decl.add_method( | 122 | sel!(redo:), |
| 123 | // sel!(application:openURLs:), | 123 | handle_menu_item as extern "C" fn(&mut Object, Sel, id), |
| 124 | // open_urls as extern "C" fn(&mut Object, Sel, id, id), | 124 | ); |
| 125 | // ); | 125 | decl.add_method( |
| 126 | // decl.register() | 126 | sel!(validateMenuItem:), |
| 127 | // } | 127 | validate_menu_item as extern "C" fn(&mut Object, Sel, id) -> bool, |
| 128 | // } | 128 | ); |
| 129 | | 129 | decl.add_method( |
| 130 | // pub struct MacForegroundPlatformState { | 130 | sel!(menuWillOpen:), |
| 131 | // dispatcher: Arc<MacDispatcher>, | 131 | menu_will_open as extern "C" fn(&mut Object, Sel, id), |
| 132 | // fonts: Arc<MacFontSystem>, | 132 | ); |
| 133 | // pasteboard: id, | 133 | decl.add_method( |
| 134 | // text_hash_pasteboard_type: id, | 134 | sel!(application:openURLs:), |
| 135 | // metadata_pasteboard_type: id, | 135 | open_urls as extern "C" fn(&mut Object, Sel, id, id), |
| 136 | // become_active: Option<Box<dyn FnMut()>>, | 136 | ); |
| 137 | // resign_active: Option<Box<dyn FnMut()>>, | 137 | decl.register() |
| 138 | // reopen: Option<Box<dyn FnMut()>>, | 138 | } |
| 139 | // quit: Option<Box<dyn FnMut()>>, | 139 | } |
| 140 | // event: Option<Box<dyn FnMut(Event) -> bool>>, | 140 | |
| 141 | // // menu_command: Option<Box<dyn FnMut(&dyn Action)>>, | 141 | pub struct MacPlatform(RefCell<MacPlatformState>); |
| 142 | // validate_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>, | 142 | |
| 143 | // will_open_menu: Option<Box<dyn FnMut()>>, | 143 | pub struct MacPlatformState { |
| 144 | // open_urls: Option<Box<dyn FnMut(Vec<String>)>>, | 144 | executor: Rc<ForegroundExecutor>, |
| 145 | // finish_launching: Option<Box<dyn FnOnce()>>, | 145 | text_system: Arc<MacTextSystem>, |
| 146 | // // menu_actions: Vec<Box<dyn Action>>, | 146 | pasteboard: id, |
| 147 | // // foreground: Rc<executor::Foreground>, | 147 | text_hash_pasteboard_type: id, |
| 148 | // } | 148 | metadata_pasteboard_type: id, |
| 149 | | 149 | become_active: Option<Box<dyn FnMut()>>, |
| 150 | // impl MacForegroundPlatform { | 150 | resign_active: Option<Box<dyn FnMut()>>, |
| 151 | // pub fn new(foreground: Rc<executor::Foreground>) -> Self { | 151 | reopen: Option<Box<dyn FnMut()>>, |
| 152 | // Self(RefCell::new(MacForegroundPlatformState { | 152 | quit: Option<Box<dyn FnMut()>>, |
| 153 | // become_active: None, | 153 | event: Option<Box<dyn FnMut(Event) -> bool>>, |
| 154 | // resign_active: None, | 154 | // menu_command: Option<Box<dyn FnMut(&dyn Action)>>, |
| 155 | // reopen: None, | 155 | // validate_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>, |
| 156 | // quit: None, | 156 | will_open_menu: Option<Box<dyn FnMut()>>, |
| 157 | // event: None, | 157 | open_urls: Option<Box<dyn FnMut(Vec<String>)>>, |
| 158 | // // menu_command: None, | 158 | finish_launching: Option<Box<dyn FnOnce()>>, |
| 159 | // validate_menu_command: None, | 159 | // menu_actions: Vec<Box<dyn Action>>, |
| 160 | // will_open_menu: None, | 160 | } |
| 161 | // open_urls: None, | 161 | |
| 162 | // finish_launching: None, | 162 | impl MacPlatform { |
| 163 | // menu_actions: Default::default(), | 163 | pub fn new() -> Self { |
| 164 | // foreground, | 164 | Self(RefCell::new(MacPlatformState { |
| 165 | // })) | 165 | executor: Rc::new(ForegroundExecutor::new(Arc::new(MacDispatcher)).unwrap()), |
| 166 | // } | 166 | text_system: Arc::new(MacTextSystem::new()), |
| 167 | | 167 | pasteboard: unsafe { NSPasteboard::generalPasteboard(nil) }, |
| 168 | // unsafe fn create_menu_bar( | 168 | text_hash_pasteboard_type: unsafe { ns_string("zed-text-hash") }, |
| 169 | // &self, | 169 | metadata_pasteboard_type: unsafe { ns_string("zed-metadata") }, |
| 170 | // menus: Vec<Menu>, | 170 | become_active: None, |
| 171 | // delegate: id, | 171 | resign_active: None, |
| 172 | // actions: &mut Vec<Box<dyn Action>>, | 172 | reopen: None, |
| 173 | // keystroke_matcher: &KeymapMatcher, | 173 | quit: None, |
| 174 | // ) -> id { | 174 | event: None, |
| 175 | // let application_menu = NSMenu::new(nil).autorelease(); | 175 | will_open_menu: None, |
| 176 | // application_menu.setDelegate_(delegate); | 176 | open_urls: None, |
| 177 | | 177 | finish_launching: None, |
| 178 | // for menu_config in menus { | 178 | // menu_command: None, |
| 179 | // let menu = NSMenu::new(nil).autorelease(); | 179 | // validate_menu_command: None, |
| 180 | // menu.setTitle_(ns_string(menu_config.name)); | 180 | // menu_actions: Default::default(), |
| 181 | // menu.setDelegate_(delegate); | 181 | })) |
| 182 | | 182 | } |
| 183 | // for item_config in menu_config.items { | 183 | |
| 184 | // menu.addItem_(self.create_menu_item( | 184 | unsafe fn read_from_pasteboard(&self, kind: id) -> Option<&[u8]> { |
| 185 | // item_config, | 185 | let pasteboard = self.0.borrow().pasteboard; |
| 186 | // delegate, | 186 | let data = pasteboard.dataForType(kind); |
| 187 | // actions, | 187 | if data == nil { |
| 188 | // keystroke_matcher, | 188 | None |
| 189 | // )); | 189 | } else { |
| 190 | // } | 190 | Some(slice::from_raw_parts( |
| 191 | | 191 | data.bytes() as *mut u8, |
| 192 | // let menu_item = NSMenuItem::new(nil).autorelease(); | 192 | data.length() as usize, |
| 193 | // menu_item.setSubmenu_(menu); | 193 | )) |
| 194 | // application_menu.addItem_(menu_item); | 194 | } |
| 195 | | 195 | } |
| 196 | // if menu_config.name == "Window" { | 196 | |
| 197 | // let app: id = msg_send![APP_CLASS, sharedApplication]; | 197 | // unsafe fn create_menu_bar( |
| 198 | // app.setWindowsMenu_(menu); | 198 | // &self, |
| 199 | // } | 199 | // menus: Vec<Menu>, |
| 200 | // } | 200 | // delegate: id, |
| 201 | | 201 | // actions: &mut Vec<Box<dyn Action>>, |
| 202 | // application_menu | 202 | // keystroke_matcher: &KeymapMatcher, |
| 203 | // } | 203 | // ) -> id { |
| 204 | | 204 | // let application_menu = NSMenu::new(nil).autorelease(); |
| 205 | // // unsafe fn create_menu_item( | 205 | // application_menu.setDelegate_(delegate); |
| 206 | // // &self, | 206 | |
| 207 | // // item: MenuItem, | 207 | // for menu_config in menus { |
| 208 | // // delegate: id, | 208 | // let menu = NSMenu::new(nil).autorelease(); |
| 209 | // // actions: &mut Vec<Box<dyn Action>>, | 209 | // menu.setTitle_(ns_string(menu_config.name)); |
| 210 | // // keystroke_matcher: &KeymapMatcher, | 210 | // menu.setDelegate_(delegate); |
| 211 | // // ) -> id { | 211 | |
| 212 | // // match item { | 212 | // for item_config in menu_config.items { |
| 213 | // // MenuItem::Separator => NSMenuItem::separatorItem(nil), | 213 | // menu.addItem_(self.create_menu_item( |
| 214 | // // MenuItem::Action { | 214 | // item_config, |
| 215 | // // name, | 215 | // delegate, |
| 216 | // // action, | 216 | // actions, |
| 217 | // // os_action, | 217 | // keystroke_matcher, |
| 218 | // // } => { | 218 | // )); |
| 219 | // // // TODO | 219 | // } |
| 220 | // // let keystrokes = keystroke_matcher | 220 | |
| 221 | // // .bindings_for_action(action.id()) | 221 | // let menu_item = NSMenuItem::new(nil).autorelease(); |
| 222 | // // .find(|binding| binding.action().eq(action.as_ref())) | 222 | // menu_item.setSubmenu_(menu); |
| 223 | // // .map(|binding| binding.keystrokes()); | 223 | // application_menu.addItem_(menu_item); |
| 224 | // // let selector = match os_action { | 224 | |
| 225 | // // Some(crate::OsAction::Cut) => selector("cut:"), | 225 | // if menu_config.name == "Window" { |
| 226 | // // Some(crate::OsAction::Copy) => selector("copy:"), | 226 | // let app: id = msg_send![APP_CLASS, sharedApplication]; |
| 227 | // // Some(crate::OsAction::Paste) => selector("paste:"), | 227 | // app.setWindowsMenu_(menu); |
| 228 | // // Some(crate::OsAction::SelectAll) => selector("selectAll:"), | 228 | // } |
| 229 | // // Some(crate::OsAction::Undo) => selector("undo:"), | 229 | // } |
| 230 | // // Some(crate::OsAction::Redo) => selector("redo:"), | 230 | |
| 231 | // // None => selector("handleGPUIMenuItem:"), | 231 | // application_menu |
| 232 | // // }; | 232 | // } |
| 233 | | 233 | |
| 234 | // // let item; | 234 | // unsafe fn create_menu_item( |
| 235 | // // if let Some(keystrokes) = keystrokes { | 235 | // &self, |
| 236 | // // if keystrokes.len() == 1 { | 236 | // item: MenuItem, |
| 237 | // // let keystroke = &keystrokes[0]; | 237 | // delegate: id, |
| 238 | // // let mut mask = NSEventModifierFlags::empty(); | 238 | // actions: &mut Vec<Box<dyn Action>>, |
| 239 | // // for (modifier, flag) in &[ | 239 | // keystroke_matcher: &KeymapMatcher, |
| 240 | // // (keystroke.cmd, NSEventModifierFlags::NSCommandKeyMask), | 240 | // ) -> id { |
| 241 | // // (keystroke.ctrl, NSEventModifierFlags::NSControlKeyMask), | 241 | // match item { |
| 242 | // // (keystroke.alt, NSEventModifierFlags::NSAlternateKeyMask), | 242 | // MenuItem::Separator => NSMenuItem::separatorItem(nil), |
| 243 | // // (keystroke.shift, NSEventModifierFlags::NSShiftKeyMask), | 243 | // MenuItem::Action { |
| 244 | // // ] { | 244 | // name, |
| 245 | // // if *modifier { | 245 | // action, |
| 246 | // // mask |= *flag; | 246 | // os_action, |
| 247 | // // } | 247 | // } => { |
| 248 | // // } | 248 | // // TODO |
| 249 | | 249 | // let keystrokes = keystroke_matcher |
| 250 | // // item = NSMenuItem::alloc(nil) | 250 | // .bindings_for_action(action.id()) |
| 251 | // // .initWithTitle_action_keyEquivalent_( | 251 | // .find(|binding| binding.action().eq(action.as_ref())) |
| 252 | // // ns_string(name), | 252 | // .map(|binding| binding.keystrokes()); |
| 253 | // // selector, | 253 | // let selector = match os_action { |
| 254 | // // ns_string(key_to_native(&keystroke.key).as_ref()), | 254 | // Some(crate::OsAction::Cut) => selector("cut:"), |
| 255 | // // ) | 255 | // Some(crate::OsAction::Copy) => selector("copy:"), |
| 256 | // // .autorelease(); | 256 | // Some(crate::OsAction::Paste) => selector("paste:"), |
| 257 | // // item.setKeyEquivalentModifierMask_(mask); | 257 | // Some(crate::OsAction::SelectAll) => selector("selectAll:"), |
| 258 | // // } | 258 | // Some(crate::OsAction::Undo) => selector("undo:"), |
| 259 | // // // For multi-keystroke bindings, render the keystroke as part of the title. | 259 | // Some(crate::OsAction::Redo) => selector("redo:"), |
| 260 | // // else { | 260 | // None => selector("handleGPUIMenuItem:"), |
| 261 | // // use std::fmt::Write; | 261 | // }; |
| 262 | | 262 | |
| 263 | // // let mut name = format!("{name} ["); | 263 | // let item; |
| 264 | // // for (i, keystroke) in keystrokes.iter().enumerate() { | 264 | // if let Some(keystrokes) = keystrokes { |
| 265 | // // if i > 0 { | 265 | // if keystrokes.len() == 1 { |
| 266 | // // name.push(' '); | 266 | // let keystroke = &keystrokes[0]; |
| 267 | // // } | 267 | // let mut mask = NSEventModifierFlags::empty(); |
| 268 | // // write!(&mut name, "{}", keystroke).unwrap(); | 268 | // for (modifier, flag) in &[ |
| 269 | // // } | 269 | // (keystroke.cmd, NSEventModifierFlags::NSCommandKeyMask), |
| 270 | // // name.push(']'); | 270 | // (keystroke.ctrl, NSEventModifierFlags::NSControlKeyMask), |
| 271 | | 271 | // (keystroke.alt, NSEventModifierFlags::NSAlternateKeyMask), |
| 272 | // // item = NSMenuItem::alloc(nil) | 272 | // (keystroke.shift, NSEventModifierFlags::NSShiftKeyMask), |
| 273 | // // .initWithTitle_action_keyEquivalent_( | 273 | // ] { |
| 274 | // // ns_string(&name), | 274 | // if *modifier { |
| 275 | // // selector, | 275 | // mask |= *flag; |
| 276 | // // ns_string(""), | 276 | // } |
| 277 | // // ) | 277 | // } |
| 278 | // // .autorelease(); | 278 | |
| 279 | // // } | 279 | // item = NSMenuItem::alloc(nil) |
| 280 | // // } else { | 280 | // .initWithTitle_action_keyEquivalent_( |
| 281 | // // item = NSMenuItem::alloc(nil) | 281 | // ns_string(name), |
| 282 | // // .initWithTitle_action_keyEquivalent_( | 282 | // selector, |
| 283 | // // ns_string(name), | 283 | // ns_string(key_to_native(&keystroke.key).as_ref()), |
| 284 | // // selector, | 284 | // ) |
| 285 | // // ns_string(""), | 285 | // .autorelease(); |
| 286 | // // ) | 286 | // item.setKeyEquivalentModifierMask_(mask); |
| 287 | // // .autorelease(); | 287 | // } |
| 288 | // // } | 288 | // // For multi-keystroke bindings, render the keystroke as part of the title. |
| 289 | | 289 | // else { |
| 290 | // // let tag = actions.len() as NSInteger; | 290 | // use std::fmt::Write; |
| 291 | // // let _: () = msg_send![item, setTag: tag]; | 291 | |
| 292 | // // actions.push(action); | 292 | // let mut name = format!("{name} ["); |
| 293 | // // item | 293 | // for (i, keystroke) in keystrokes.iter().enumerate() { |
| 294 | // // } | 294 | // if i > 0 { |
| 295 | // // MenuItem::Submenu(Menu { name, items }) => { | 295 | // name.push(' '); |
| 296 | // // let item = NSMenuItem::new(nil).autorelease(); | 296 | // } |
| 297 | // // let submenu = NSMenu::new(nil).autorelease(); | 297 | // write!(&mut name, "{}", keystroke).unwrap(); |
| 298 | // // submenu.setDelegate_(delegate); | 298 | // } |
| 299 | // // for item in items { | 299 | // name.push(']'); |
| 300 | // // submenu.addItem_(self.create_menu_item( | 300 | |
| 301 | // // item, | 301 | // item = NSMenuItem::alloc(nil) |
| 302 | // // delegate, | 302 | // .initWithTitle_action_keyEquivalent_( |
| 303 | // // actions, | 303 | // ns_string(&name), |
| 304 | // // keystroke_matcher, | 304 | // selector, |
| 305 | // // )); | 305 | // ns_string(""), |
| 306 | // // } | 306 | // ) |
| 307 | // // item.setSubmenu_(submenu); | 307 | // .autorelease(); |
| 308 | // // item.setTitle_(ns_string(name)); | 308 | // } |
| 309 | // // item | 309 | // } else { |
| 310 | // // } | 310 | // item = NSMenuItem::alloc(nil) |
| 311 | // // } | 311 | // .initWithTitle_action_keyEquivalent_( |
| 312 | // // } | 312 | // ns_string(name), |
| 313 | | 313 | // selector, |
| 314 | // unsafe fn read_from_pasteboard(&self, kind: id) -> Option<&[u8]> { | 314 | // ns_string(""), |
| 315 | // let data = self.pasteboard.dataForType(kind); | 315 | // ) |
| 316 | // if data == nil { | 316 | // .autorelease(); |
| 317 | // None | 317 | // } |
| 318 | // } else { | 318 | |
| 319 | // Some(slice::from_raw_parts( | 319 | // let tag = actions.len() as NSInteger; |
| 320 | // data.bytes() as *mut u8, | 320 | // let _: () = msg_send![item, setTag: tag]; |
| 321 | // data.length() as usize, | 321 | // actions.push(action); |
| 322 | // )) | 322 | // item |
| 323 | // } | 323 | // } |
| 324 | // } | 324 | // MenuItem::Submenu(Menu { name, items }) => { |
| 325 | // } | 325 | // let item = NSMenuItem::new(nil).autorelease(); |
| 326 | | 326 | // let submenu = NSMenu::new(nil).autorelease(); |
| 327 | // // impl platform::ForegroundPlatform for MacForegroundPlatform { | 327 | // submenu.setDelegate_(delegate); |
| 328 | // // fn on_become_active(&self, callback: Box<dyn FnMut()>) { | 328 | // for item in items { |
| 329 | // // self.0.borrow_mut().become_active = Some(callback); | 329 | // submenu.addItem_(self.create_menu_item( |
| 330 | // // } | 330 | // item, |
| 331 | | 331 | // delegate, |
| 332 | // // fn on_resign_active(&self, callback: Box<dyn FnMut()>) { | 332 | // actions, |
| 333 | // // self.0.borrow_mut().resign_active = Some(callback); | 333 | // keystroke_matcher, |
| 334 | // // } | 334 | // )); |
| 335 | | 335 | // } |
| 336 | // // fn on_quit(&self, callback: Box<dyn FnMut()>) { | 336 | // item.setSubmenu_(submenu); |
| 337 | // // self.0.borrow_mut().quit = Some(callback); | 337 | // item.setTitle_(ns_string(name)); |
| 338 | // // } | 338 | // item |
| 339 | | 339 | // } |
| 340 | // // fn on_reopen(&self, callback: Box<dyn FnMut()>) { | 340 | // } |
| 341 | // // self.0.borrow_mut().reopen = Some(callback); | 341 | // } |
| 342 | // // } | 342 | } |
| 343 | | 343 | |
| 344 | // // fn on_event(&self, callback: Box<dyn FnMut(platform::Event) -> bool>) { | 344 | impl Platform for MacPlatform { |
| 345 | // // self.0.borrow_mut().event = Some(callback); | 345 | fn executor(&self) -> Rc<ForegroundExecutor> { |
| 346 | // // } | 346 | self.0.borrow().executor.clone() |
| 347 | | 347 | } |
| 348 | // // fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) { | 348 | |
| 349 | // // self.0.borrow_mut().open_urls = Some(callback); | 349 | fn text_system(&self) -> Arc<dyn PlatformTextSystem> { |
| 350 | // // } | 350 | self.0.borrow().text_system.clone() |
| 351 | | 351 | } |
| 352 | // // fn run(&self, on_finish_launching: Box<dyn FnOnce()>) { | 352 | |
| 353 | // // self.0.borrow_mut().finish_launching = Some(on_finish_launching); | 353 | fn run(&self, on_finish_launching: Box<dyn FnOnce()>) { |
| 354 | | 354 | self.0.borrow_mut().finish_launching = Some(on_finish_launching); |
| 355 | // // unsafe { | 355 | |
| 356 | // // let app: id = msg_send![APP_CLASS, sharedApplication]; | 356 | unsafe { |
| 357 | // // let app_delegate: id = msg_send![APP_DELEGATE_CLASS, new]; | 357 | let app: id = msg_send![APP_CLASS, sharedApplication]; |
| 358 | // // app.setDelegate_(app_delegate); | 358 | let app_delegate: id = msg_send![APP_DELEGATE_CLASS, new]; |
| 359 | | 359 | app.setDelegate_(app_delegate); |
| 360 | // // let self_ptr = self as *const Self as *const c_void; | 360 | |
| 361 | // // (*app).set_ivar(MAC_PLATFORM_IVAR, self_ptr); | 361 | let self_ptr = self as *const Self as *const c_void; |
| 362 | // // (*app_delegate).set_ivar(MAC_PLATFORM_IVAR, self_ptr); | 362 | (*app).set_ivar(MAC_PLATFORM_IVAR, self_ptr); |
| 363 | | 363 | (*app_delegate).set_ivar(MAC_PLATFORM_IVAR, self_ptr); |
| 364 | // // let pool = NSAutoreleasePool::new(nil); | 364 | |
| 365 | // // app.run(); | 365 | let pool = NSAutoreleasePool::new(nil); |
| 366 | // // pool.drain(); | 366 | app.run(); |
| 367 | | 367 | pool.drain(); |
| 368 | // // (*app).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>()); | 368 | |
| 369 | // // (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>()); | 369 | (*app).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>()); |
| 370 | // // } | 370 | (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>()); |
| 371 | // // } | 371 | } |
| 372 | | 372 | } |
| 373 | // // fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>) { | 373 | |
| 374 | // // self.0.borrow_mut().menu_command = Some(callback); | 374 | fn quit(&self) { |
| 375 | // // } | 375 | // Quitting the app causes us to close windows, which invokes `Window::on_close` callbacks |
| 376 | | 376 | // synchronously before this method terminates. If we call `Platform::quit` while holding a |
| 377 | // // fn on_will_open_menu(&self, callback: Box<dyn FnMut()>) { | 377 | // borrow of the app state (which most of the time we will do), we will end up |
| 378 | // // self.0.borrow_mut().will_open_menu = Some(callback); | 378 | // double-borrowing the app state in the `on_close` callbacks for our open windows. To solve |
| 379 | // // } | 379 | // this, we make quitting the application asynchronous so that we aren't holding borrows to |
| 380 | | 380 | // the app state on the stack when we actually terminate the app. |
| 381 | // // fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) { | 381 | |
| 382 | // // self.0.borrow_mut().validate_menu_command = Some(callback); | 382 | use super::dispatcher::{dispatch_async_f, dispatch_get_main_queue}; |
| 383 | // // } | 383 | |
| 384 | | 384 | unsafe { |
| 385 | // // fn set_menus(&self, menus: Vec<Menu>, keystroke_matcher: &KeymapMatcher) { | 385 | dispatch_async_f(dispatch_get_main_queue(), ptr::null_mut(), Some(quit)); |
| 386 | // // unsafe { | 386 | } |
| 387 | // // let app: id = msg_send![APP_CLASS, sharedApplication]; | 387 | |
| 388 | // // let mut state = self.0.borrow_mut(); | 388 | unsafe extern "C" fn quit(_: *mut c_void) { |
| 389 | // // let actions = &mut state.menu_actions; | 389 | let app = NSApplication::sharedApplication(nil); |
| 390 | // // app.setMainMenu_(self.create_menu_bar( | 390 | let _: () = msg_send![app, terminate: nil]; |
| 391 | // // menus, | 391 | } |
| 392 | // // app.delegate(), | 392 | } |
| 393 | // // actions, | 393 | |
| 394 | // // keystroke_matcher, | 394 | fn restart(&self) { |
| 395 | // // )); | 395 | use std::os::unix::process::CommandExt as _; |
| 396 | // // } | 396 | |
| 397 | // // } | 397 | let app_pid = std::process::id().to_string(); |
| 398 | | 398 | let app_path = self |
| 399 | // // fn prompt_for_paths( | 399 | .app_path() |
| 400 | // // &self, | 400 | .ok() |
| 401 | // // options: platform::PathPromptOptions, | 401 | // When the app is not bundled, `app_path` returns the |
| 402 | // // ) -> oneshot::Receiver<Option<Vec<PathBuf>>> { | 402 | // directory containing the executable. Disregard this |
| 403 | // // unsafe { | 403 | // and get the path to the executable itself. |
| 404 | // // let panel = NSOpenPanel::openPanel(nil); | 404 | .and_then(|path| (path.extension()?.to_str()? == "app").then_some(path)) |
| 405 | // // panel.setCanChooseDirectories_(options.directories.to_objc()); | 405 | .unwrap_or_else(|| std::env::current_exe().unwrap()); |
| 406 | // // panel.setCanChooseFiles_(options.files.to_objc()); | 406 | |
| 407 | // // panel.setAllowsMultipleSelection_(options.multiple.to_objc()); | 407 | // Wait until this process has exited and then re-open this path. |
| 408 | // // panel.setResolvesAliases_(false.to_objc()); | 408 | let script = r#" |
| 409 | // // let (done_tx, done_rx) = oneshot::channel(); | 409 | while kill -0 $0 2> /dev/null; do |
| 410 | // // let done_tx = Cell::new(Some(done_tx)); | 410 | sleep 0.1 |
| 411 | // // let block = ConcreteBlock::new(move |response: NSModalResponse| { | 411 | done |
| 412 | // // let result = if response == NSModalResponse::NSModalResponseOk { | 412 | open "$1" |
| 413 | // // let mut result = Vec::new(); | 413 | "#; |
| 414 | // // let urls = panel.URLs(); | 414 | |
| 415 | // // for i in 0..urls.count() { | 415 | let restart_process = Command::new("/bin/bash") |
| 416 | // // let url = urls.objectAtIndex(i); | 416 | .arg("-c") |
| 417 | // // if url.isFileURL() == YES { | 417 | .arg(script) |
| 418 | // // if let Ok(path) = ns_url_to_path(url) { | 418 | .arg(app_pid) |
| 419 | // // result.push(path) | 419 | .arg(app_path) |
| 420 | // // } | 420 | .process_group(0) |
| 421 | // // } | 421 | .spawn(); |
| 422 | // // } | 422 | |
| 423 | // // Some(result) | 423 | match restart_process { |
| 424 | // // } else { | 424 | Ok(_) => self.quit(), |
| 425 | // // None | 425 | Err(e) => log::error!("failed to spawn restart script: {:?}", e), |
| 426 | // // }; | 426 | } |
| 427 | | 427 | } |
| 428 | // // if let Some(mut done_tx) = done_tx.take() { | 428 | |
| 429 | // // let _ = postage::sink::Sink::try_send(&mut done_tx, result); | 429 | fn activate(&self, ignoring_other_apps: bool) { |
| 430 | // // } | 430 | unsafe { |
| 431 | // // }); | 431 | let app = NSApplication::sharedApplication(nil); |
| 432 | // // let block = block.copy(); | 432 | app.activateIgnoringOtherApps_(ignoring_other_apps.to_objc()); |
| 433 | // // let _: () = msg_send![panel, beginWithCompletionHandler: block]; | 433 | } |
| 434 | // // done_rx | 434 | } |
| 435 | // // } | 435 | |
| 436 | // // } | 436 | fn hide(&self) { |
| 437 | | 437 | unsafe { |
| 438 | // // fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> { | 438 | let app = NSApplication::sharedApplication(nil); |
| 439 | // // unsafe { | 439 | let _: () = msg_send![app, hide: nil]; |
| 440 | // // let panel = NSSavePanel::savePanel(nil); | 440 | } |
| 441 | // // let path = ns_string(directory.to_string_lossy().as_ref()); | 441 | } |
| 442 | // // let url = NSURL::fileURLWithPath_isDirectory_(nil, path, true.to_objc()); | 442 | |
| 443 | // // panel.setDirectoryURL(url); | 443 | fn hide_other_apps(&self) { |
| 444 | | 444 | unsafe { |
| 445 | // // let (done_tx, done_rx) = oneshot::channel(); | 445 | let app = NSApplication::sharedApplication(nil); |
| 446 | // // let done_tx = Cell::new(Some(done_tx)); | 446 | let _: () = msg_send![app, hideOtherApplications: nil]; |
| 447 | // // let block = ConcreteBlock::new(move |response: NSModalResponse| { | 447 | } |
| 448 | // // let mut result = None; | 448 | } |
| 449 | // // if response == NSModalResponse::NSModalResponseOk { | 449 | |
| 450 | // // let url = panel.URL(); | 450 | fn unhide_other_apps(&self) { |
| 451 | // // if url.isFileURL() == YES { | 451 | unsafe { |
| 452 | // // result = ns_url_to_path(panel.URL()).ok() | 452 | let app = NSApplication::sharedApplication(nil); |
| 453 | // // } | 453 | let _: () = msg_send![app, unhideAllApplications: nil]; |
| 454 | // // } | 454 | } |
| 455 | | 455 | } |
| 456 | // // if let Some(mut done_tx) = done_tx.take() { | 456 | |
| 457 | // // let _ = postage::sink::Sink::try_send(&mut done_tx, result); | 457 | fn screens(&self) -> Vec<Rc<dyn PlatformScreen>> { |
| 458 | // // } | 458 | MacScreen::all() |
| 459 | // // }); | 459 | .into_iter() |
| 460 | // // let block = block.copy(); | 460 | .map(|screen| Rc::new(screen) as Rc<_>) |
| 461 | // // let _: () = msg_send![panel, beginWithCompletionHandler: block]; | 461 | .collect() |
| 462 | // // done_rx | 462 | } |
| 463 | // // } | 463 | |
| 464 | // // } | 464 | fn screen_by_id(&self, id: uuid::Uuid) -> Option<Rc<dyn PlatformScreen>> { |
| 465 | | 465 | MacScreen::find_by_id(id).map(|screen| Rc::new(screen) as Rc<_>) |
| 466 | // // fn reveal_path(&self, path: &Path) { | 466 | } |
| 467 | // // unsafe { | 467 | |
| 468 | // // let path = path.to_path_buf(); | 468 | fn main_window(&self) -> Option<AnyWindowHandle> { |
| 469 | // // self.0 | 469 | MacWindow::main_window() |
| 470 | // // .borrow() | 470 | } |
| 471 | // // .foreground | 471 | |
| 472 | // // .spawn(async move { | 472 | // fn add_status_item(&self, _handle: AnyWindowHandle) -> Box<dyn platform::Window> { |
| 473 | // // let full_path = ns_string(path.to_str().unwrap_or("")); | 473 | // Box::new(StatusItem::add(self.fonts())) |
| 474 | // // let root_full_path = ns_string(""); | 474 | // } |
| 475 | // // let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace]; | 475 | |
| 476 | // // let _: BOOL = msg_send![ | 476 | fn open_window( |
| 477 | // // workspace, | 477 | &self, |
| 478 | // // selectFile: full_path | 478 | handle: AnyWindowHandle, |
| 479 | // // inFileViewerRootedAtPath: root_full_path | 479 | options: WindowOptions, |
| 480 | // // ]; | 480 | ) -> Box<dyn PlatformWindow> { |
| 481 | // // }) | 481 | Box::new(MacWindow::open( |
| 482 | // // .detach(); | 482 | handle, |
| 483 | // // } | 483 | options, |
| 484 | // // } | 484 | self.executor(), |
| 485 | // // } | 485 | self.text_system(), |
| 486 | | 486 | )) |
| 487 | // // impl Platform for MacPlatform { | 487 | } |
| 488 | // // fn dispatcher(&self) -> Arc<dyn platform::Dispatcher> { | 488 | |
| 489 | // // self.dispatcher.clone() | 489 | fn open_url(&self, url: &str) { |
| 490 | // // } | 490 | unsafe { |
| 491 | | 491 | let url = NSURL::alloc(nil) |
| 492 | // // fn fonts(&self) -> Arc<dyn platform::FontSystem> { | 492 | .initWithString_(ns_string(url)) |
| 493 | // // self.fonts.clone() | 493 | .autorelease(); |
| 494 | // // } | 494 | let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace]; |
| 495 | | 495 | msg_send![workspace, openURL: url] |
| 496 | // // fn activate(&self, ignoring_other_apps: bool) { | 496 | } |
| 497 | // // unsafe { | 497 | } |
| 498 | // // let app = NSApplication::sharedApplication(nil); | 498 | |
| 499 | // // app.activateIgnoringOtherApps_(ignoring_other_apps.to_objc()); | 499 | fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) { |
| 500 | // // } | 500 | self.0.borrow_mut().open_urls = Some(callback); |
| 501 | // // } | 501 | } |
| 502 | | 502 | |
| 503 | // // fn hide(&self) { | 503 | fn prompt_for_paths( |
| 504 | // // unsafe { | 504 | &self, |
| 505 | // // let app = NSApplication::sharedApplication(nil); | 505 | options: PathPromptOptions, |
| 506 | // // let _: () = msg_send![app, hide: nil]; | 506 | ) -> oneshot::Receiver<Option<Vec<PathBuf>>> { |
| 507 | // // } | 507 | unsafe { |
| 508 | // // } | 508 | let panel = NSOpenPanel::openPanel(nil); |
| 509 | | 509 | panel.setCanChooseDirectories_(options.directories.to_objc()); |
| 510 | // // fn hide_other_apps(&self) { | 510 | panel.setCanChooseFiles_(options.files.to_objc()); |
| 511 | // // unsafe { | 511 | panel.setAllowsMultipleSelection_(options.multiple.to_objc()); |
| 512 | // // let app = NSApplication::sharedApplication(nil); | 512 | panel.setResolvesAliases_(false.to_objc()); |
| 513 | // // let _: () = msg_send![app, hideOtherApplications: nil]; | 513 | let (done_tx, done_rx) = oneshot::channel(); |
| 514 | // // } | 514 | let done_tx = Cell::new(Some(done_tx)); |
| 515 | // // } | 515 | let block = ConcreteBlock::new(move |response: NSModalResponse| { |
| 516 | | 516 | let result = if response == NSModalResponse::NSModalResponseOk { |
| 517 | // // fn unhide_other_apps(&self) { | 517 | let mut result = Vec::new(); |
| 518 | // // unsafe { | 518 | let urls = panel.URLs(); |
| 519 | // // let app = NSApplication::sharedApplication(nil); | 519 | for i in 0..urls.count() { |
| 520 | // // let _: () = msg_send![app, unhideAllApplications: nil]; | 520 | let url = urls.objectAtIndex(i); |
| 521 | // // } | 521 | if url.isFileURL() == YES { |
| 522 | // // } | 522 | if let Ok(path) = ns_url_to_path(url) { |
| 523 | | 523 | result.push(path) |
| 524 | // // fn quit(&self) { | 524 | } |
| 525 | // // // Quitting the app causes us to close windows, which invokes `Window::on_close` callbacks | 525 | } |
| 526 | // // // synchronously before this method terminates. If we call `Platform::quit` while holding a | 526 | } |
| 527 | // // // borrow of the app state (which most of the time we will do), we will end up | 527 | Some(result) |
| 528 | // // // double-borrowing the app state in the `on_close` callbacks for our open windows. To solve | 528 | } else { |
| 529 | // // // this, we make quitting the application asynchronous so that we aren't holding borrows to | 529 | None |
| 530 | // // // the app state on the stack when we actually terminate the app. | 530 | }; |
| 531 | | 531 | |
| 532 | // // use super::dispatcher::{dispatch_async_f, dispatch_get_main_queue}; | 532 | if let Some(mut done_tx) = done_tx.take() { |
| 533 | | 533 | let _ = done_tx.send(result); |
| 534 | // // unsafe { | 534 | } |
| 535 | // // dispatch_async_f(dispatch_get_main_queue(), ptr::null_mut(), Some(quit)); | 535 | }); |
| 536 | // // } | 536 | let block = block.copy(); |
| 537 | | 537 | let _: () = msg_send![panel, beginWithCompletionHandler: block]; |
| 538 | // // unsafe extern "C" fn quit(_: *mut c_void) { | 538 | done_rx |
| 539 | // // let app = NSApplication::sharedApplication(nil); | 539 | } |
| 540 | // // let _: () = msg_send![app, terminate: nil]; | 540 | } |
| 541 | // // } | 541 | |
| 542 | // // } | 542 | fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> { |
| 543 | | 543 | unsafe { |
| 544 | // // fn screen_by_id(&self, id: uuid::Uuid) -> Option<Rc<dyn platform::Screen>> { | 544 | let panel = NSSavePanel::savePanel(nil); |
| 545 | // // Screen::find_by_id(id).map(|screen| Rc::new(screen) as Rc<_>) | 545 | let path = ns_string(directory.to_string_lossy().as_ref()); |
| 546 | // // } | 546 | let url = NSURL::fileURLWithPath_isDirectory_(nil, path, true.to_objc()); |
| 547 | | 547 | panel.setDirectoryURL(url); |
| 548 | // // fn screens(&self) -> Vec<Rc<dyn platform::Screen>> { | 548 | |
| 549 | // // Screen::all() | 549 | let (done_tx, done_rx) = oneshot::channel(); |
| 550 | // // .into_iter() | 550 | let done_tx = Cell::new(Some(done_tx)); |
| 551 | // // .map(|screen| Rc::new(screen) as Rc<_>) | 551 | let block = ConcreteBlock::new(move |response: NSModalResponse| { |
| 552 | // // .collect() | 552 | let mut result = None; |
| 553 | // // } | 553 | if response == NSModalResponse::NSModalResponseOk { |
| 554 | | 554 | let url = panel.URL(); |
| 555 | // // fn open_window( | 555 | if url.isFileURL() == YES { |
| 556 | // // &self, | 556 | result = ns_url_to_path(panel.URL()).ok() |
| 557 | // // handle: AnyWindowHandle, | 557 | } |
| 558 | // // options: platform::WindowOptions, | 558 | } |
| 559 | // // executor: Rc<executor::Foreground>, | 559 | |
| 560 | // // ) -> Box<dyn platform::Window> { | 560 | if let Some(mut done_tx) = done_tx.take() { |
| 561 | // // Box::new(MacWindow::open(handle, options, executor, self.fonts())) | 561 | let _ = done_tx.send(result); |
| 562 | // // } | 562 | } |
| 563 | | 563 | }); |
| 564 | // // fn main_window(&self) -> Option<AnyWindowHandle> { | 564 | let block = block.copy(); |
| 565 | // // MacWindow::main_window() | 565 | let _: () = msg_send![panel, beginWithCompletionHandler: block]; |
| 566 | // // } | 566 | done_rx |
| 567 | | 567 | } |
| 568 | // // fn add_status_item(&self, _handle: AnyWindowHandle) -> Box<dyn platform::Window> { | 568 | } |
| 569 | // // Box::new(StatusItem::add(self.fonts())) | 569 | |
| 570 | // // } | 570 | fn reveal_path(&self, path: &Path) { |
| 571 | | 571 | unsafe { |
| 572 | // // fn write_to_clipboard(&self, item: ClipboardItem) { | 572 | let path = path.to_path_buf(); |
| 573 | // // unsafe { | 573 | self.0 |
| 574 | // // self.pasteboard.clearContents(); | 574 | .borrow() |
| 575 | | 575 | .executor |
| 576 | // // let text_bytes = NSData::dataWithBytes_length_( | 576 | .spawn(async move { |
| 577 | // // nil, | 577 | let full_path = ns_string(path.to_str().unwrap_or("")); |
| 578 | // // item.text.as_ptr() as *const c_void, | 578 | let root_full_path = ns_string(""); |
| 579 | // // item.text.len() as u64, | 579 | let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace]; |
| 580 | // // ); | 580 | let _: BOOL = msg_send![ |
| 581 | // // self.pasteboard | 581 | workspace, |
| 582 | // // .setData_forType(text_bytes, NSPasteboardTypeString); | 582 | selectFile: full_path |
| 583 | | 583 | inFileViewerRootedAtPath: root_full_path |
| 584 | // // if let Some(metadata) = item.metadata.as_ref() { | 584 | ]; |
| 585 | // // let hash_bytes = ClipboardItem::text_hash(&item.text).to_be_bytes(); | 585 | }) |
| 586 | // // let hash_bytes = NSData::dataWithBytes_length_( | 586 | .detach(); |
| 587 | // // nil, | 587 | } |
| 588 | // // hash_bytes.as_ptr() as *const c_void, | 588 | } |
| 589 | // // hash_bytes.len() as u64, | 589 | |
| 590 | // // ); | 590 | fn on_become_active(&self, callback: Box<dyn FnMut()>) { |
| 591 | // // self.pasteboard | 591 | self.0.borrow_mut().become_active = Some(callback); |
| 592 | // // .setData_forType(hash_bytes, self.text_hash_pasteboard_type); | 592 | } |
| 593 | | 593 | |
| 594 | // // let metadata_bytes = NSData::dataWithBytes_length_( | 594 | fn on_resign_active(&self, callback: Box<dyn FnMut()>) { |
| 595 | // // nil, | 595 | self.0.borrow_mut().resign_active = Some(callback); |
| 596 | // // metadata.as_ptr() as *const c_void, | 596 | } |
| 597 | // // metadata.len() as u64, | 597 | |
| 598 | // // ); | 598 | fn on_quit(&self, callback: Box<dyn FnMut()>) { |
| 599 | // // self.pasteboard | 599 | self.0.borrow_mut().quit = Some(callback); |
| 600 | // // .setData_forType(metadata_bytes, self.metadata_pasteboard_type); | 600 | } |
| 601 | // // } | 601 | |
| 602 | // // } | 602 | fn on_reopen(&self, callback: Box<dyn FnMut()>) { |
| 603 | // // } | 603 | self.0.borrow_mut().reopen = Some(callback); |
| 604 | | 604 | } |
| 605 | // // fn read_from_clipboard(&self) -> Option<ClipboardItem> { | 605 | |
| 606 | // // unsafe { | 606 | fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>) { |
| 607 | // // if let Some(text_bytes) = self.read_from_pasteboard(NSPasteboardTypeString) { | 607 | self.0.borrow_mut().event = Some(callback); |
| 608 | // // let text = String::from_utf8_lossy(text_bytes).to_string(); | 608 | } |
| 609 | // // let hash_bytes = self | 609 | |
| 610 | // // .read_from_pasteboard(self.text_hash_pasteboard_type) | 610 | fn os_name(&self) -> &'static str { |
| 611 | // // .and_then(|bytes| bytes.try_into().ok()) | 611 | "macOS" |
| 612 | // // .map(u64::from_be_bytes); | 612 | } |
| 613 | // // let metadata_bytes = self | 613 | |
| 614 | // // .read_from_pasteboard(self.metadata_pasteboard_type) | 614 | fn os_version(&self) -> Result<SemanticVersion> { |
| 615 | // // .and_then(|bytes| String::from_utf8(bytes.to_vec()).ok()); | 615 | unsafe { |
| 616 | | 616 | let process_info = NSProcessInfo::processInfo(nil); |
| 617 | // // if let Some((hash, metadata)) = hash_bytes.zip(metadata_bytes) { | 617 | let version = process_info.operatingSystemVersion(); |
| 618 | // // if hash == ClipboardItem::text_hash(&text) { | 618 | Ok(SemanticVersion { |
| 619 | // // Some(ClipboardItem { | 619 | major: version.majorVersion as usize, |
| 620 | // // text, | 620 | minor: version.minorVersion as usize, |
| 621 | // // metadata: Some(metadata), | 621 | patch: version.patchVersion as usize, |
| 622 | // // }) | 622 | }) |
| 623 | // // } else { | 623 | } |
| 624 | // // Some(ClipboardItem { | 624 | } |
| 625 | // // text, | 625 | |
| 626 | // // metadata: None, | 626 | fn app_version(&self) -> Result<SemanticVersion> { |
| 627 | // // }) | 627 | unsafe { |
| 628 | // // } | 628 | let bundle: id = NSBundle::mainBundle(); |
| 629 | // // } else { | 629 | if bundle.is_null() { |
| 630 | // // Some(ClipboardItem { | 630 | Err(anyhow!("app is not running inside a bundle")) |
| 631 | // // text, | 631 | } else { |
| 632 | // // metadata: None, | 632 | let version: id = msg_send![bundle, objectForInfoDictionaryKey: ns_string("CFBundleShortVersionString")]; |
| 633 | // // }) | 633 | let len = msg_send![version, lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; |
| 634 | // // } | 634 | let bytes = version.UTF8String() as *const u8; |
| 635 | // // } else { | 635 | let version = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap(); |
| 636 | // // None | 636 | version.parse() |
| 637 | // // } | 637 | } |
| 638 | // // } | 638 | } |
| 639 | // // } | 639 | } |
| 640 | | 640 | |
| 641 | // // fn open_url(&self, url: &str) { | 641 | fn app_path(&self) -> Result<PathBuf> { |
| 642 | // // unsafe { | 642 | unsafe { |
| 643 | // // let url = NSURL::alloc(nil) | 643 | let bundle: id = NSBundle::mainBundle(); |
| 644 | // // .initWithString_(ns_string(url)) | 644 | if bundle.is_null() { |
| 645 | // // .autorelease(); | 645 | Err(anyhow!("app is not running inside a bundle")) |
| 646 | // // let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace]; | 646 | } else { |
| 647 | // // msg_send![workspace, openURL: url] | 647 | Ok(path_from_objc(msg_send![bundle, bundlePath])) |
| 648 | // // } | 648 | } |
| 649 | // // } | 649 | } |
| 650 | | 650 | } |
| 651 | // // fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { | 651 | |
| 652 | // // let url = CFString::from(url); | 652 | fn local_timezone(&self) -> UtcOffset { |
| 653 | // // let username = CFString::from(username); | 653 | unsafe { |
| 654 | // // let password = CFData::from_buffer(password); | 654 | let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone]; |
| 655 | | 655 | let seconds_from_gmt: NSInteger = msg_send![local_timezone, secondsFromGMT]; |
| 656 | // // unsafe { | 656 | UtcOffset::from_whole_seconds(seconds_from_gmt.try_into().unwrap()).unwrap() |
| 657 | // // use security::*; | 657 | } |
| 658 | | 658 | } |
| 659 | // // // First, check if there are already credentials for the given server. If so, then | 659 | |
| 660 | // // // update the username and password. | 660 | fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> { |
| 661 | // // let mut verb = "updating"; | 661 | unsafe { |
| 662 | // // let mut query_attrs = CFMutableDictionary::with_capacity(2); | 662 | let bundle: id = NSBundle::mainBundle(); |
| 663 | // // query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); | 663 | if bundle.is_null() { |
| 664 | // // query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); | 664 | Err(anyhow!("app is not running inside a bundle")) |
| 665 | | 665 | } else { |
| 666 | // // let mut attrs = CFMutableDictionary::with_capacity(4); | 666 | let name = ns_string(name); |
| 667 | // // attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); | 667 | let url: id = msg_send![bundle, URLForAuxiliaryExecutable: name]; |
| 668 | // // attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); | 668 | if url.is_null() { |
| 669 | // // attrs.set(kSecAttrAccount as *const _, username.as_CFTypeRef()); | 669 | Err(anyhow!("resource not found")) |
| 670 | // // attrs.set(kSecValueData as *const _, password.as_CFTypeRef()); | 670 | } else { |
| 671 | | 671 | ns_url_to_path(url) |
| 672 | // // let mut status = SecItemUpdate( | 672 | } |
| 673 | // // query_attrs.as_concrete_TypeRef(), | 673 | } |
| 674 | // // attrs.as_concrete_TypeRef(), | 674 | } |
| 675 | // // ); | 675 | } |
| 676 | | 676 | |
| 677 | // // // If there were no existing credentials for the given server, then create them. | 677 | fn set_cursor_style(&self, style: CursorStyle) { |
| 678 | // // if status == errSecItemNotFound { | 678 | unsafe { |
| 679 | // // verb = "creating"; | 679 | let new_cursor: id = match style { |
| 680 | // // status = SecItemAdd(attrs.as_concrete_TypeRef(), ptr::null_mut()); | 680 | CursorStyle::Arrow => msg_send![class!(NSCursor), arrowCursor], |
| 681 | // // } | 681 | CursorStyle::ResizeLeftRight => { |
| 682 | | 682 | msg_send![class!(NSCursor), resizeLeftRightCursor] |
| 683 | // // if status != errSecSuccess { | 683 | } |
| 684 | // // return Err(anyhow!("{} password failed: {}", verb, status)); | 684 | CursorStyle::ResizeUpDown => msg_send![class!(NSCursor), resizeUpDownCursor], |
| 685 | // // } | 685 | CursorStyle::PointingHand => msg_send![class!(NSCursor), pointingHandCursor], |
| 686 | // // } | 686 | CursorStyle::IBeam => msg_send![class!(NSCursor), IBeamCursor], |
| 687 | // // Ok(()) | 687 | }; |
| 688 | // // } | 688 | |
| 689 | | 689 | let old_cursor: id = msg_send![class!(NSCursor), currentCursor]; |
| 690 | // // fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>> { | 690 | if new_cursor != old_cursor { |
| 691 | // // let url = CFString::from(url); | 691 | let _: () = msg_send![new_cursor, set]; |
| 692 | // // let cf_true = CFBoolean::true_value().as_CFTypeRef(); | 692 | } |
| 693 | | 693 | } |
| 694 | // // unsafe { | 694 | } |
| 695 | // // use security::*; | 695 | |
| 696 | | 696 | fn should_auto_hide_scrollbars(&self) -> bool { |
| 697 | // // // Find any credentials for the given server URL. | 697 | #[allow(non_upper_case_globals)] |
| 698 | // // let mut attrs = CFMutableDictionary::with_capacity(5); | 698 | const NSScrollerStyleOverlay: NSInteger = 1; |
| 699 | // // attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); | 699 | |
| 700 | // // attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); | 700 | unsafe { |
| 701 | // // attrs.set(kSecReturnAttributes as *const _, cf_true); | 701 | let style: NSInteger = msg_send![class!(NSScroller), preferredScrollerStyle]; |
| 702 | // // attrs.set(kSecReturnData as *const _, cf_true); | 702 | style == NSScrollerStyleOverlay |
| 703 | | 703 | } |
| 704 | // // let mut result = CFTypeRef::from(ptr::null()); | 704 | } |
| 705 | // // let status = SecItemCopyMatching(attrs.as_concrete_TypeRef(), &mut result); | 705 | |
| 706 | // // match status { | 706 | fn write_to_clipboard(&self, item: ClipboardItem) { |
| 707 | // // security::errSecSuccess => {} | 707 | let state = self.0.borrow(); |
| 708 | // // security::errSecItemNotFound | security::errSecUserCanceled => return Ok(None), | 708 | unsafe { |
| 709 | // // _ => return Err(anyhow!("reading password failed: {}", status)), | 709 | state.pasteboard.clearContents(); |
| 710 | // // } | 710 | |
| 711 | | 711 | let text_bytes = NSData::dataWithBytes_length_( |
| 712 | // // let result = CFType::wrap_under_create_rule(result) | 712 | nil, |
| 713 | // // .downcast::<CFDictionary>() | 713 | item.text.as_ptr() as *const c_void, |
| 714 | // // .ok_or_else(|| anyhow!("keychain item was not a dictionary"))?; | 714 | item.text.len() as u64, |
| 715 | // // let username = result | 715 | ); |
| 716 | // // .find(kSecAttrAccount as *const _) | 716 | state |
| 717 | // // .ok_or_else(|| anyhow!("account was missing from keychain item"))?; | 717 | .pasteboard |
| 718 | // // let username = CFType::wrap_under_get_rule(*username) | 718 | .setData_forType(text_bytes, NSPasteboardTypeString); |
| 719 | // // .downcast::<CFString>() | 719 | |
| 720 | // // .ok_or_else(|| anyhow!("account was not a string"))?; | 720 | if let Some(metadata) = item.metadata.as_ref() { |
| 721 | // // let password = result | 721 | let hash_bytes = ClipboardItem::text_hash(&item.text).to_be_bytes(); |
| 722 | // // .find(kSecValueData as *const _) | 722 | let hash_bytes = NSData::dataWithBytes_length_( |
| 723 | // // .ok_or_else(|| anyhow!("password was missing from keychain item"))?; | 723 | nil, |
| 724 | // // let password = CFType::wrap_under_get_rule(*password) | 724 | hash_bytes.as_ptr() as *const c_void, |
| 725 | // // .downcast::<CFData>() | 725 | hash_bytes.len() as u64, |
| 726 | // // .ok_or_else(|| anyhow!("password was not a string"))?; | 726 | ); |
| 727 | | 727 | state |
| 728 | // // Ok(Some((username.to_string(), password.bytes().to_vec()))) | 728 | .pasteboard |
| 729 | // // } | 729 | .setData_forType(hash_bytes, state.text_hash_pasteboard_type); |
| 730 | // // } | 730 | |
| 731 | | 731 | let metadata_bytes = NSData::dataWithBytes_length_( |
| 732 | // // fn delete_credentials(&self, url: &str) -> Result<()> { | 732 | nil, |
| 733 | // // let url = CFString::from(url); | 733 | metadata.as_ptr() as *const c_void, |
| 734 | | 734 | metadata.len() as u64, |
| 735 | // // unsafe { | 735 | ); |
| 736 | // // use security::*; | 736 | state |
| 737 | | 737 | .pasteboard |
| 738 | // // let mut query_attrs = CFMutableDictionary::with_capacity(2); | 738 | .setData_forType(metadata_bytes, state.metadata_pasteboard_type); |
| 739 | // // query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); | 739 | } |
| 740 | // // query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); | 740 | } |
| 741 | | 741 | } |
| 742 | // // let status = SecItemDelete(query_attrs.as_concrete_TypeRef()); | 742 | |
| 743 | | 743 | fn read_from_clipboard(&self) -> Option<ClipboardItem> { |
| 744 | // // if status != errSecSuccess { | 744 | let state = self.0.borrow(); |
| 745 | // // return Err(anyhow!("delete password failed: {}", status)); | 745 | unsafe { |
| 746 | // // } | 746 | if let Some(text_bytes) = self.read_from_pasteboard(NSPasteboardTypeString) { |
| 747 | // // } | 747 | let text = String::from_utf8_lossy(text_bytes).to_string(); |
| 748 | // // Ok(()) | 748 | let hash_bytes = self |
| 749 | // // } | 749 | .read_from_pasteboard(state.text_hash_pasteboard_type) |
| 750 | | 750 | .and_then(|bytes| bytes.try_into().ok()) |
| 751 | // // fn set_cursor_style(&self, style: CursorStyle) { | 751 | .map(u64::from_be_bytes); |
| 752 | // // unsafe { | 752 | let metadata_bytes = self |
| 753 | // // let new_cursor: id = match style { | 753 | .read_from_pasteboard(state.metadata_pasteboard_type) |
| 754 | // // CursorStyle::Arrow => msg_send![class!(NSCursor), arrowCursor], | 754 | .and_then(|bytes| String::from_utf8(bytes.to_vec()).ok()); |
| 755 | // // CursorStyle::ResizeLeftRight => { | 755 | |
| 756 | // // msg_send![class!(NSCursor), resizeLeftRightCursor] | 756 | if let Some((hash, metadata)) = hash_bytes.zip(metadata_bytes) { |
| 757 | // // } | 757 | if hash == ClipboardItem::text_hash(&text) { |
| 758 | // // CursorStyle::ResizeUpDown => msg_send![class!(NSCursor), resizeUpDownCursor], | 758 | Some(ClipboardItem { |
| 759 | // // CursorStyle::PointingHand => msg_send![class!(NSCursor), pointingHandCursor], | 759 | text, |
| 760 | // // CursorStyle::IBeam => msg_send![class!(NSCursor), IBeamCursor], | 760 | metadata: Some(metadata), |
| 761 | // // }; | 761 | }) |
| 762 | | 762 | } else { |
| 763 | // // let old_cursor: id = msg_send![class!(NSCursor), currentCursor]; | 763 | Some(ClipboardItem { |
| 764 | // // if new_cursor != old_cursor { | 764 | text, |
| 765 | // // let _: () = msg_send![new_cursor, set]; | 765 | metadata: None, |
| 766 | // // } | 766 | }) |
| 767 | // // } | 767 | } |
| 768 | // // } | 768 | } else { |
| 769 | | 769 | Some(ClipboardItem { |
| 770 | // // fn should_auto_hide_scrollbars(&self) -> bool { | 770 | text, |
| 771 | // // #[allow(non_upper_case_globals)] | 771 | metadata: None, |
| 772 | // // const NSScrollerStyleOverlay: NSInteger = 1; | 772 | }) |
| 773 | | 773 | } |
| 774 | // // unsafe { | 774 | } else { |
| 775 | // // let style: NSInteger = msg_send![class!(NSScroller), preferredScrollerStyle]; | 775 | None |
| 776 | // // style == NSScrollerStyleOverlay | 776 | } |
| 777 | // // } | 777 | } |
| 778 | // // } | 778 | } |
| 779 | | 779 | |
| 780 | // // fn local_timezone(&self) -> UtcOffset { | 780 | fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { |
| 781 | // // unsafe { | 781 | let url = CFString::from(url); |
| 782 | // // let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone]; | 782 | let username = CFString::from(username); |
| 783 | // // let seconds_from_gmt: NSInteger = msg_send![local_timezone, secondsFromGMT]; | 783 | let password = CFData::from_buffer(password); |
| 784 | // // UtcOffset::from_whole_seconds(seconds_from_gmt.try_into().unwrap()).unwrap() | 784 | |
| 785 | // // } | 785 | unsafe { |
| 786 | // // } | 786 | use security::*; |
| 787 | | 787 | |
| 788 | // // fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> { | 788 | // First, check if there are already credentials for the given server. If so, then |
| 789 | // // unsafe { | 789 | // update the username and password. |
| 790 | // // let bundle: id = NSBundle::mainBundle(); | 790 | let mut verb = "updating"; |
| 791 | // // if bundle.is_null() { | 791 | let mut query_attrs = CFMutableDictionary::with_capacity(2); |
| 792 | // // Err(anyhow!("app is not running inside a bundle")) | 792 | query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); |
| 793 | // // } else { | 793 | query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); |
| 794 | // // let name = ns_string(name); | 794 | |
| 795 | // // let url: id = msg_send![bundle, URLForAuxiliaryExecutable: name]; | 795 | let mut attrs = CFMutableDictionary::with_capacity(4); |
| 796 | // // if url.is_null() { | 796 | attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); |
| 797 | // // Err(anyhow!("resource not found")) | 797 | attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); |
| 798 | // // } else { | 798 | attrs.set(kSecAttrAccount as *const _, username.as_CFTypeRef()); |
| 799 | // // ns_url_to_path(url) | 799 | attrs.set(kSecValueData as *const _, password.as_CFTypeRef()); |
| 800 | // // } | 800 | |
| 801 | // // } | 801 | let mut status = SecItemUpdate( |
| 802 | // // } | 802 | query_attrs.as_concrete_TypeRef(), |
| 803 | // // } | 803 | attrs.as_concrete_TypeRef(), |
| 804 | | 804 | ); |
| 805 | // // fn app_path(&self) -> Result<PathBuf> { | 805 | |
| 806 | // // unsafe { | 806 | // If there were no existing credentials for the given server, then create them. |
| 807 | // // let bundle: id = NSBundle::mainBundle(); | 807 | if status == errSecItemNotFound { |
| 808 | // // if bundle.is_null() { | 808 | verb = "creating"; |
| 809 | // // Err(anyhow!("app is not running inside a bundle")) | 809 | status = SecItemAdd(attrs.as_concrete_TypeRef(), ptr::null_mut()); |
| 810 | // // } else { | 810 | } |
| 811 | // // Ok(path_from_objc(msg_send![bundle, bundlePath])) | 811 | |
| 812 | // // } | 812 | if status != errSecSuccess { |
| 813 | // // } | 813 | return Err(anyhow!("{} password failed: {}", verb, status)); |
| 814 | // // } | 814 | } |
| 815 | | 815 | } |
| 816 | // // fn app_version(&self) -> Result<platform::AppVersion> { | 816 | Ok(()) |
| 817 | // // unsafe { | 817 | } |
| 818 | // // let bundle: id = NSBundle::mainBundle(); | 818 | |
| 819 | // // if bundle.is_null() { | 819 | // fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>) { |
| 820 | // // Err(anyhow!("app is not running inside a bundle")) | 820 | // self.0.borrow_mut().menu_command = Some(callback); |
| 821 | // // } else { | 821 | // } |
| 822 | // // let version: id = msg_send![bundle, objectForInfoDictionaryKey: ns_string("CFBundleShortVersionString")]; | 822 | |
| 823 | // // let len = msg_send![version, lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; | 823 | // fn on_will_open_menu(&self, callback: Box<dyn FnMut()>) { |
| 824 | // // let bytes = version.UTF8String() as *const u8; | 824 | // self.0.borrow_mut().will_open_menu = Some(callback); |
| 825 | // // let version = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap(); | 825 | // } |
| 826 | // // version.parse() | 826 | |
| 827 | // // } | 827 | // fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) { |
| 828 | // // } | 828 | // self.0.borrow_mut().validate_menu_command = Some(callback); |
| 829 | // // } | 829 | // } |
| 830 | | 830 | |
| 831 | // // fn os_name(&self) -> &'static str { | 831 | // fn set_menus(&self, menus: Vec<Menu>, keystroke_matcher: &KeymapMatcher) { |
| 832 | // // "macOS" | 832 | // unsafe { |
| 833 | // // } | 833 | // let app: id = msg_send![APP_CLASS, sharedApplication]; |
| 834 | | 834 | // let mut state = self.0.borrow_mut(); |
| 835 | // // fn os_version(&self) -> Result<crate::platform::AppVersion> { | 835 | // let actions = &mut state.menu_actions; |
| 836 | // // unsafe { | 836 | // app.setMainMenu_(self.create_menu_bar( |
| 837 | // // let process_info = NSProcessInfo::processInfo(nil); | 837 | // menus, |
| 838 | // // let version = process_info.operatingSystemVersion(); | 838 | // app.delegate(), |
| 839 | // // Ok(AppVersion { | 839 | // actions, |
| 840 | // // major: version.majorVersion as usize, | 840 | // keystroke_matcher, |
| 841 | // // minor: version.minorVersion as usize, | 841 | // )); |
| 842 | // // patch: version.patchVersion as usize, | 842 | // } |
| 843 | // // }) | 843 | // } |
| 844 | // // } | 844 | |
| 845 | // // } | 845 | fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>> { |
| 846 | | 846 | let url = CFString::from(url); |
| 847 | // // fn restart(&self) { | 847 | let cf_true = CFBoolean::true_value().as_CFTypeRef(); |
| 848 | // // use std::os::unix::process::CommandExt as _; | 848 | |
| 849 | | 849 | unsafe { |
| 850 | // // let app_pid = std::process::id().to_string(); | 850 | use security::*; |
| 851 | // // let app_path = self | 851 | |
| 852 | // // .app_path() | 852 | // Find any credentials for the given server URL. |
| 853 | // // .ok() | 853 | let mut attrs = CFMutableDictionary::with_capacity(5); |
| 854 | // // // When the app is not bundled, `app_path` returns the | 854 | attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); |
| 855 | // // // directory containing the executable. Disregard this | 855 | attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); |
| 856 | // // // and get the path to the executable itself. | 856 | attrs.set(kSecReturnAttributes as *const _, cf_true); |
| 857 | // // .and_then(|path| (path.extension()?.to_str()? == "app").then_some(path)) | 857 | attrs.set(kSecReturnData as *const _, cf_true); |
| 858 | // // .unwrap_or_else(|| std::env::current_exe().unwrap()); | 858 | |
| 859 | | 859 | let mut result = CFTypeRef::from(ptr::null()); |
| 860 | // // // Wait until this process has exited and then re-open this path. | 860 | let status = SecItemCopyMatching(attrs.as_concrete_TypeRef(), &mut result); |
| 861 | // // let script = r#" | 861 | match status { |
| 862 | // // while kill -0 $0 2> /dev/null; do | 862 | security::errSecSuccess => {} |
| 863 | // // sleep 0.1 | 863 | security::errSecItemNotFound | security::errSecUserCanceled => return Ok(None), |
| 864 | // // done | 864 | _ => return Err(anyhow!("reading password failed: {}", status)), |
| 865 | // // open "$1" | 865 | } |
| 866 | // // "#; | 866 | |
| 867 | | 867 | let result = CFType::wrap_under_create_rule(result) |
| 868 | // // let restart_process = Command::new("/bin/bash") | 868 | .downcast::<CFDictionary>() |
| 869 | // // .arg("-c") | 869 | .ok_or_else(|| anyhow!("keychain item was not a dictionary"))?; |
| 870 | // // .arg(script) | 870 | let username = result |
| 871 | // // .arg(app_pid) | 871 | .find(kSecAttrAccount as *const _) |
| 872 | // // .arg(app_path) | 872 | .ok_or_else(|| anyhow!("account was missing from keychain item"))?; |
| 873 | // // .process_group(0) | 873 | let username = CFType::wrap_under_get_rule(*username) |
| 874 | // // .spawn(); | 874 | .downcast::<CFString>() |
| 875 | | 875 | .ok_or_else(|| anyhow!("account was not a string"))?; |
| 876 | // // match restart_process { | 876 | let password = result |
| 877 | // // Ok(_) => self.quit(), | 877 | .find(kSecValueData as *const _) |
| 878 | // // Err(e) => log::error!("failed to spawn restart script: {:?}", e), | 878 | .ok_or_else(|| anyhow!("password was missing from keychain item"))?; |
| 879 | // // } | 879 | let password = CFType::wrap_under_get_rule(*password) |
| 880 | // // } | 880 | .downcast::<CFData>() |
| 881 | // // } | 881 | .ok_or_else(|| anyhow!("password was not a string"))?; |
| 882 | | 882 | |
| 883 | // unsafe fn path_from_objc(path: id) -> PathBuf { | 883 | Ok(Some((username.to_string(), password.bytes().to_vec()))) |
| 884 | // let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; | 884 | } |
| 885 | // let bytes = path.UTF8String() as *const u8; | 885 | } |
| 886 | // let path = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap(); | 886 | |
| 887 | // PathBuf::from(path) | 887 | fn delete_credentials(&self, url: &str) -> Result<()> { |
| 888 | // } | 888 | let url = CFString::from(url); |
| 889 | | 889 | |
| 890 | // unsafe fn get_foreground_platform(object: &mut Object) -> &MacForegroundPlatform { | 890 | unsafe { |
| 891 | // let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR); | 891 | use security::*; |
| 892 | // assert!(!platform_ptr.is_null()); | 892 | |
| 893 | // &*(platform_ptr as *const MacForegroundPlatform) | 893 | let mut query_attrs = CFMutableDictionary::with_capacity(2); |
| 894 | // } | 894 | query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); |
| 895 | | 895 | query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); |
| 896 | // extern "C" fn send_event(this: &mut Object, _sel: Sel, native_event: id) { | 896 | |
| 897 | // unsafe { | 897 | let status = SecItemDelete(query_attrs.as_concrete_TypeRef()); |
| 898 | // if let Some(event) = Event::from_native(native_event, None) { | 898 | |
| 899 | // let platform = get_foreground_platform(this); | 899 | if status != errSecSuccess { |
| 900 | // if let Some(callback) = platform.0.borrow_mut().event.as_mut() { | 900 | return Err(anyhow!("delete password failed: {}", status)); |
| 901 | // if callback(event) { | 901 | } |
| 902 | // return; | 902 | } |
| 903 | // } | 903 | Ok(()) |
| 904 | // } | 904 | } |
| 905 | // } | 905 | } |
| 906 | // msg_send![super(this, class!(NSApplication)), sendEvent: native_event] | 906 | |
| 907 | // } | 907 | unsafe fn path_from_objc(path: id) -> PathBuf { |
| 908 | // } | 908 | let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; |
| 909 | | 909 | let bytes = path.UTF8String() as *const u8; |
| 910 | // extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) { | 910 | let path = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap(); |
| 911 | // unsafe { | 911 | PathBuf::from(path) |
| 912 | // let app: id = msg_send![APP_CLASS, sharedApplication]; | 912 | } |
| 913 | // app.setActivationPolicy_(NSApplicationActivationPolicyRegular); | 913 | |
| 914 | | 914 | unsafe fn get_foreground_platform(object: &mut Object) -> &MacPlatform { |
| 915 | // let platform = get_foreground_platform(this); | 915 | let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR); |
| 916 | // let callback = platform.0.borrow_mut().finish_launching.take(); | 916 | assert!(!platform_ptr.is_null()); |
| 917 | // if let Some(callback) = callback { | 917 | &*(platform_ptr as *const MacPlatform) |
| 918 | // callback(); | 918 | } |
| 919 | // } | 919 | |
| 920 | // } | 920 | extern "C" fn send_event(this: &mut Object, _sel: Sel, native_event: id) { |
| 921 | // } | 921 | unsafe { |
| 922 | | 922 | if let Some(event) = Event::from_native(native_event, None) { |
| 923 | // extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_windows: bool) { | 923 | let platform = get_foreground_platform(this); |
| 924 | // if !has_open_windows { | 924 | if let Some(callback) = platform.0.borrow_mut().event.as_mut() { |
| 925 | // let platform = unsafe { get_foreground_platform(this) }; | 925 | if callback(event) { |
| 926 | // if let Some(callback) = platform.0.borrow_mut().reopen.as_mut() { | 926 | return; |
| 927 | // callback(); | 927 | } |
| 928 | // } | 928 | } |
| 929 | // } | 929 | } |
| 930 | // } | 930 | msg_send![super(this, class!(NSApplication)), sendEvent: native_event] |
| 931 | | 931 | } |
| 932 | // extern "C" fn did_become_active(this: &mut Object, _: Sel, _: id) { | 932 | } |
| 933 | // let platform = unsafe { get_foreground_platform(this) }; | 933 | |
| 934 | // if let Some(callback) = platform.0.borrow_mut().become_active.as_mut() { | 934 | extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) { |
| 935 | // callback(); | 935 | unsafe { |
| 936 | // } | 936 | let app: id = msg_send![APP_CLASS, sharedApplication]; |
| 937 | // } | 937 | app.setActivationPolicy_(NSApplicationActivationPolicyRegular); |
| 938 | | 938 | |
| 939 | // extern "C" fn did_resign_active(this: &mut Object, _: Sel, _: id) { | 939 | let platform = get_foreground_platform(this); |
| 940 | // let platform = unsafe { get_foreground_platform(this) }; | 940 | let callback = platform.0.borrow_mut().finish_launching.take(); |
| 941 | // if let Some(callback) = platform.0.borrow_mut().resign_active.as_mut() { | 941 | if let Some(callback) = callback { |
| 942 | // callback(); | 942 | callback(); |
| 943 | // } | 943 | } |
| 944 | // } | 944 | } |
| 945 | | 945 | } |
| 946 | // extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) { | 946 | |
| 947 | // let platform = unsafe { get_foreground_platform(this) }; | 947 | extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_windows: bool) { |
| 948 | // if let Some(callback) = platform.0.borrow_mut().quit.as_mut() { | 948 | if !has_open_windows { |
| 949 | // callback(); | 949 | let platform = unsafe { get_foreground_platform(this) }; |
| 950 | // } | 950 | if let Some(callback) = platform.0.borrow_mut().reopen.as_mut() { |
| 951 | // } | 951 | callback(); |
| 952 | | 952 | } |
| 953 | // extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) { | 953 | } |
| 954 | // let urls = unsafe { | 954 | } |
| 955 | // (0..urls.count()) | 955 | |
| 956 | // .into_iter() | 956 | extern "C" fn did_become_active(this: &mut Object, _: Sel, _: id) { |
| 957 | // .filter_map(|i| { | 957 | let platform = unsafe { get_foreground_platform(this) }; |
| 958 | // let url = urls.objectAtIndex(i); | 958 | if let Some(callback) = platform.0.borrow_mut().become_active.as_mut() { |
| 959 | // match CStr::from_ptr(url.absoluteString().UTF8String() as *mut c_char).to_str() { | 959 | callback(); |
| 960 | // Ok(string) => Some(string.to_string()), | 960 | } |
| 961 | // Err(err) => { | 961 | } |
| 962 | // log::error!("error converting path to string: {}", err); | 962 | |
| 963 | // None | 963 | extern "C" fn did_resign_active(this: &mut Object, _: Sel, _: id) { |
| 964 | // } | 964 | let platform = unsafe { get_foreground_platform(this) }; |
| 965 | // } | 965 | if let Some(callback) = platform.0.borrow_mut().resign_active.as_mut() { |
| 966 | // }) | 966 | callback(); |
| 967 | // .collect::<Vec<_>>() | 967 | } |
| 968 | // }; | 968 | } |
| 969 | // let platform = unsafe { get_foreground_platform(this) }; | 969 | |
| 970 | // if let Some(callback) = platform.0.borrow_mut().open_urls.as_mut() { | 970 | extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) { |
| 971 | // callback(urls); | 971 | let platform = unsafe { get_foreground_platform(this) }; |
| 972 | // } | 972 | if let Some(callback) = platform.0.borrow_mut().quit.as_mut() { |
| 973 | // } | 973 | callback(); |
| 974 | | 974 | } |
| 975 | // extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) { | 975 | } |
| 976 | // unsafe { | 976 | |
| 977 | // let platform = get_foreground_platform(this); | 977 | extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) { |
| 978 | // let mut platform = platform.0.borrow_mut(); | 978 | let urls = unsafe { |
| 979 | // if let Some(mut callback) = platform.menu_command.take() { | 979 | (0..urls.count()) |
| 980 | // let tag: NSInteger = msg_send![item, tag]; | 980 | .into_iter() |
| 981 | // let index = tag as usize; | 981 | .filter_map(|i| { |
| 982 | // if let Some(action) = platform.menu_actions.get(index) { | 982 | let url = urls.objectAtIndex(i); |
| 983 | // callback(action.as_ref()); | 983 | match CStr::from_ptr(url.absoluteString().UTF8String() as *mut c_char).to_str() { |
| 984 | // } | 984 | Ok(string) => Some(string.to_string()), |
| 985 | // platform.menu_command = Some(callback); | 985 | Err(err) => { |
| 986 | // } | 986 | log::error!("error converting path to string: {}", err); |
| 987 | // } | 987 | None |
| 988 | // } | 988 | } |
| 989 | | 989 | } |
| 990 | // extern "C" fn validate_menu_item(this: &mut Object, _: Sel, item: id) -> bool { | 990 | }) |
| 991 | // unsafe { | 991 | .collect::<Vec<_>>() |
| 992 | // let mut result = false; | 992 | }; |
| 993 | // let platform = get_foreground_platform(this); | 993 | let platform = unsafe { get_foreground_platform(this) }; |
| 994 | // let mut platform = platform.0.borrow_mut(); | 994 | if let Some(callback) = platform.0.borrow_mut().open_urls.as_mut() { |
| 995 | // if let Some(mut callback) = platform.validate_menu_command.take() { | 995 | callback(urls); |
| 996 | // let tag: NSInteger = msg_send![item, tag]; | 996 | } |
| 997 | // let index = tag as usize; | 997 | } |
| 998 | // if let Some(action) = platform.menu_actions.get(index) { | 998 | |
| 999 | // result = callback(action.as_ref()); | 999 | extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) { |
| 1000 | // } | 1000 | todo!() |
| 1001 | // platform.validate_menu_command = Some(callback); | 1001 | // unsafe { |
| 1002 | // } | 1002 | // let platform = get_foreground_platform(this); |
| 1003 | // result | 1003 | // let mut platform = platform.0.borrow_mut(); |
| 1004 | // } | 1004 | // if let Some(mut callback) = platform.menu_command.take() { |
| 1005 | // } | 1005 | // let tag: NSInteger = msg_send![item, tag]; |
| 1006 | | 1006 | // let index = tag as usize; |
| 1007 | // extern "C" fn menu_will_open(this: &mut Object, _: Sel, _: id) { | 1007 | // if let Some(action) = platform.menu_actions.get(index) { |
| 1008 | // unsafe { | 1008 | // callback(action.as_ref()); |
| 1009 | // let platform = get_foreground_platform(this); | 1009 | // } |
| 1010 | // let mut platform = platform.0.borrow_mut(); | 1010 | // platform.menu_command = Some(callback); |
| 1011 | // if let Some(mut callback) = platform.will_open_menu.take() { | 1011 | // } |
| 1012 | // callback(); | 1012 | // } |
| 1013 | // platform.will_open_menu = Some(callback); | 1013 | } |
| 1014 | // } | 1014 | |
| 1015 | // } | 1015 | extern "C" fn validate_menu_item(this: &mut Object, _: Sel, item: id) -> bool { |
| 1016 | // } | 1016 | todo!() |
| 1017 | | 1017 | // unsafe { |
| 1018 | // unsafe fn ns_string(string: &str) -> id { | 1018 | // let mut result = false; |
| 1019 | // NSString::alloc(nil).init_str(string).autorelease() | 1019 | // let platform = get_foreground_platform(this); |
| 1020 | // } | 1020 | // let mut platform = platform.0.borrow_mut(); |
| 1021 | | 1021 | // if let Some(mut callback) = platform.validate_menu_command.take() { |
| 1022 | // unsafe fn ns_url_to_path(url: id) -> Result<PathBuf> { | 1022 | // let tag: NSInteger = msg_send![item, tag]; |
| 1023 | // let path: *mut c_char = msg_send![url, fileSystemRepresentation]; | 1023 | // let index = tag as usize; |
| 1024 | // if path.is_null() { | 1024 | // if let Some(action) = platform.menu_actions.get(index) { |
| 1025 | // Err(anyhow!( | 1025 | // result = callback(action.as_ref()); |
| 1026 | // "url is not a file path: {}", | 1026 | // } |
| 1027 | // CStr::from_ptr(url.absoluteString().UTF8String()).to_string_lossy() | 1027 | // platform.validate_menu_command = Some(callback); |
| 1028 | // )) | 1028 | // } |
| 1029 | // } else { | 1029 | // result |
| 1030 | // Ok(PathBuf::from(OsStr::from_bytes( | 1030 | // } |
| 1031 | // CStr::from_ptr(path).to_bytes(), | 1031 | } |
| 1032 | // ))) | 1032 | |
| 1033 | // } | 1033 | extern "C" fn menu_will_open(this: &mut Object, _: Sel, _: id) { |
| 1034 | // } | 1034 | unsafe { |
| 1035 | | 1035 | let platform = get_foreground_platform(this); |
| 1036 | // mod security { | 1036 | let mut platform = platform.0.borrow_mut(); |
| 1037 | // #![allow(non_upper_case_globals)] | 1037 | if let Some(mut callback) = platform.will_open_menu.take() { |
| 1038 | // use super::*; | 1038 | callback(); |
| 1039 | | 1039 | platform.will_open_menu = Some(callback); |
| 1040 | // #[link(name = "Security", kind = "framework")] | 1040 | } |
| 1041 | // extern "C" { | 1041 | } |
| 1042 | // pub static kSecClass: CFStringRef; | 1042 | } |
| 1043 | // pub static kSecClassInternetPassword: CFStringRef; | 1043 | |
| 1044 | // pub static kSecAttrServer: CFStringRef; | 1044 | unsafe fn ns_string(string: &str) -> id { |
| 1045 | // pub static kSecAttrAccount: CFStringRef; | 1045 | NSString::alloc(nil).init_str(string).autorelease() |
| 1046 | // pub static kSecValueData: CFStringRef; | 1046 | } |
| 1047 | // pub static kSecReturnAttributes: CFStringRef; | 1047 | |
| 1048 | // pub static kSecReturnData: CFStringRef; | 1048 | unsafe fn ns_url_to_path(url: id) -> Result<PathBuf> { |
| 1049 | | 1049 | let path: *mut c_char = msg_send![url, fileSystemRepresentation]; |
| 1050 | // pub fn SecItemAdd(attributes: CFDictionaryRef, result: *mut CFTypeRef) -> OSStatus; | 1050 | if path.is_null() { |
| 1051 | // pub fn SecItemUpdate(query: CFDictionaryRef, attributes: CFDictionaryRef) -> OSStatus; | 1051 | Err(anyhow!( |
| 1052 | // pub fn SecItemDelete(query: CFDictionaryRef) -> OSStatus; | 1052 | "url is not a file path: {}", |
| 1053 | // pub fn SecItemCopyMatching(query: CFDictionaryRef, result: *mut CFTypeRef) -> OSStatus; | 1053 | CStr::from_ptr(url.absoluteString().UTF8String()).to_string_lossy() |
| 1054 | // } | 1054 | )) |
| 1055 | | 1055 | } else { |
| 1056 | // pub const errSecSuccess: OSStatus = 0; | 1056 | Ok(PathBuf::from(OsStr::from_bytes( |
| 1057 | // pub const errSecUserCanceled: OSStatus = -128; | 1057 | CStr::from_ptr(path).to_bytes(), |
| 1058 | // pub const errSecItemNotFound: OSStatus = -25300; | 1058 | ))) |
| 1059 | // } | 1059 | } |
| 1060 | | 1060 | } |
| 1061 | // #[cfg(test)] | 1061 | |
| 1062 | // mod tests { | 1062 | mod security { |
| 1063 | // use crate::platform::Platform; | 1063 | #![allow(non_upper_case_globals)] |
| 1064 | | 1064 | use super::*; |
| 1065 | // use super::*; | 1065 | |
| 1066 | | 1066 | #[link(name = "Security", kind = "framework")] |
| 1067 | // #[test] | 1067 | extern "C" { |
| 1068 | // fn test_clipboard() { | 1068 | pub static kSecClass: CFStringRef; |
| 1069 | // let platform = build_platform(); | 1069 | pub static kSecClassInternetPassword: CFStringRef; |
| 1070 | // assert_eq!(platform.read_from_clipboard(), None); | 1070 | pub static kSecAttrServer: CFStringRef; |
| 1071 | | 1071 | pub static kSecAttrAccount: CFStringRef; |
| 1072 | // let item = ClipboardItem::new("1".to_string()); | 1072 | pub static kSecValueData: CFStringRef; |
| 1073 | // platform.write_to_clipboard(item.clone()); | 1073 | pub static kSecReturnAttributes: CFStringRef; |
| 1074 | // assert_eq!(platform.read_from_clipboard(), Some(item)); | 1074 | pub static kSecReturnData: CFStringRef; |
| 1075 | | 1075 | |
| 1076 | // let item = ClipboardItem::new("2".to_string()).with_metadata(vec![3, 4]); | 1076 | pub fn SecItemAdd(attributes: CFDictionaryRef, result: *mut CFTypeRef) -> OSStatus; |
| 1077 | // platform.write_to_clipboard(item.clone()); | 1077 | pub fn SecItemUpdate(query: CFDictionaryRef, attributes: CFDictionaryRef) -> OSStatus; |
| 1078 | // assert_eq!(platform.read_from_clipboard(), Some(item)); | 1078 | pub fn SecItemDelete(query: CFDictionaryRef) -> OSStatus; |
| 1079 | | 1079 | pub fn SecItemCopyMatching(query: CFDictionaryRef, result: *mut CFTypeRef) -> OSStatus; |
| 1080 | // let text_from_other_app = "text from other app"; | 1080 | } |
| 1081 | // unsafe { | 1081 | |
| 1082 | // let bytes = NSData::dataWithBytes_length_( | 1082 | pub const errSecSuccess: OSStatus = 0; |
| 1083 | // nil, | 1083 | pub const errSecUserCanceled: OSStatus = -128; |
| 1084 | // text_from_other_app.as_ptr() as *const c_void, | 1084 | pub const errSecItemNotFound: OSStatus = -25300; |
| 1085 | // text_from_other_app.len() as u64, | 1085 | } |
| 1086 | // ); | 1086 | |
| 1087 | // platform | 1087 | #[cfg(test)] |
| 1088 | // .pasteboard | 1088 | mod tests { |
| 1089 | // .setData_forType(bytes, NSPasteboardTypeString); | 1089 | use crate::ClipboardItem; |
| 1090 | // } | 1090 | |
| 1091 | // assert_eq!( | 1091 | use super::*; |
| 1092 | // platform.read_from_clipboard(), | 1092 | |
| 1093 | // Some(ClipboardItem::new(text_from_other_app.to_string())) | 1093 | #[test] |
| 1094 | // ); | 1094 | fn test_clipboard() { |
| 1095 | // } | 1095 | let platform = build_platform(); |
| 1096 | | 1096 | assert_eq!(platform.read_from_clipboard(), None); |
| 1097 | // fn build_platform() -> MacPlatform { | 1097 | |
| 1098 | // let mut platform = MacPlatform::new(); | 1098 | let item = ClipboardItem::new("1".to_string()); |
| 1099 | // platform.pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) }; | 1099 | platform.write_to_clipboard(item.clone()); |
| 1100 | // platform | 1100 | assert_eq!(platform.read_from_clipboard(), Some(item)); |
| 1101 | // } | 1101 | |
| 1102 | // } | 1102 | let item = ClipboardItem::new("2".to_string()).with_metadata(vec![3, 4]); |
| | 1103 | platform.write_to_clipboard(item.clone()); |
| | 1104 | assert_eq!(platform.read_from_clipboard(), Some(item)); |
| | 1105 | |
| | 1106 | let text_from_other_app = "text from other app"; |
| | 1107 | unsafe { |
| | 1108 | let bytes = NSData::dataWithBytes_length_( |
| | 1109 | nil, |
| | 1110 | text_from_other_app.as_ptr() as *const c_void, |
| | 1111 | text_from_other_app.len() as u64, |
| | 1112 | ); |
| | 1113 | platform |
| | 1114 | .0 |
| | 1115 | .borrow_mut() |
| | 1116 | .pasteboard |
| | 1117 | .setData_forType(bytes, NSPasteboardTypeString); |
| | 1118 | } |
| | 1119 | assert_eq!( |
| | 1120 | platform.read_from_clipboard(), |
| | 1121 | Some(ClipboardItem::new(text_from_other_app.to_string())) |
| | 1122 | ); |
| | 1123 | } |
| | 1124 | |
| | 1125 | fn build_platform() -> MacPlatform { |
| | 1126 | let mut platform = MacPlatform::new(); |
| | 1127 | platform.0.borrow_mut().pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) }; |
| | 1128 | platform |
| | 1129 | } |
| | 1130 | } |