Skip to content

Commit

Permalink
remove openFile
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jul 3, 2023
1 parent a58525b commit cfc768f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 135 deletions.
20 changes: 5 additions & 15 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,8 @@ pub enum Event<'a, T: 'static> {
/// gets emitted. You generally want to treat this as an "do on quit" event.
LoopDestroyed,

/// Emitted when the app is open by external resources, like opening a Url.
Opened { event: OpenEvent },
}

/// What the app is opening.
#[derive(Debug, PartialEq, Clone)]
pub enum OpenEvent {
/// App is opening the given list of URLs.
Url(Vec<url::Url>),
File(Vec<PathBuf>),
/// Emitted when the app is open by external resources, like opening a file or deeplink.
Opened { urls: Vec<url::Url> },
}

impl<T: Clone> Clone for Event<'static, T> {
Expand Down Expand Up @@ -218,9 +210,7 @@ impl<T: Clone> Clone for Event<'static, T> {
position: *position,
},
GlobalShortcutEvent(accelerator_id) => GlobalShortcutEvent(*accelerator_id),
Opened { event } => Opened {
event: event.clone(),
},
Opened { urls } => Opened { urls: urls.clone() },
}
}
}
Expand Down Expand Up @@ -260,7 +250,7 @@ impl<'a, T> Event<'a, T> {
position,
}),
GlobalShortcutEvent(accelerator_id) => Ok(GlobalShortcutEvent(accelerator_id)),
Opened { event } => Ok(Opened { event }),
Opened { urls } => Ok(Opened { urls }),
}
}

Expand Down Expand Up @@ -302,7 +292,7 @@ impl<'a, T> Event<'a, T> {
position,
}),
GlobalShortcutEvent(accelerator_id) => Some(GlobalShortcutEvent(accelerator_id)),
Opened { event } => Some(Opened { event }),
Opened { urls } => Some(Opened { urls }),
}
}
}
Expand Down
23 changes: 0 additions & 23 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,6 @@ impl From<ActivationPolicy> for NSApplicationActivationPolicy {
}
}

/// Kind of resource the application opens.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OpenResourceKind {
/// The application expects the user to open an URL with it.
Url,
/// The application expects the user to open files with it.
File,
}

impl Default for OpenResourceKind {
fn default() -> Self {
Self::File
}
}

pub trait CustomMenuItemExtMacOS {
fn set_native_image(&mut self, native_image: NativeImage);
}
Expand Down Expand Up @@ -526,9 +511,6 @@ pub trait EventLoopExtMacOS {
/// [`run`](crate::event_loop::EventLoop::run) or
/// [`run_return`](crate::platform::run_return::EventLoopExtRunReturn::run_return)
fn set_activate_ignoring_other_apps(&mut self, ignore: bool);

/// Sets which kind of resource the application opens.
fn set_application_open_resource_kind(&mut self, kind: OpenResourceKind);
}

impl<T> EventLoopExtMacOS for EventLoop<T> {
Expand All @@ -552,11 +534,6 @@ impl<T> EventLoopExtMacOS for EventLoop<T> {
get_aux_state_mut(&**self.event_loop.delegate).activate_ignoring_other_apps = ignore;
}
}

#[inline]
fn set_application_open_resource_kind(&mut self, kind: OpenResourceKind) {
self.event_loop.open_resource_kind = kind;
}
}

/// Additional methods on `MonitorHandle` that are specific to MacOS.
Expand Down
6 changes: 2 additions & 4 deletions src/platform_impl/ios/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use objc::{

use crate::{
dpi::PhysicalPosition,
event::{DeviceId as RootDeviceId, Event, Force, OpenEvent, Touch, TouchPhase, WindowEvent},
event::{DeviceId as RootDeviceId, Event, Force, Touch, TouchPhase, WindowEvent},
platform::ios::MonitorHandleExtIOS,
platform_impl::platform::{
app_state::{self, OSCapabilities},
Expand Down Expand Up @@ -580,9 +580,7 @@ pub fn create_delegate_class() {

let url = url::Url::parse(std::str::from_utf8(bytes).unwrap()).unwrap();

app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened {
event: OpenEvent::Url(vec![url]),
}));
app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened { urls: vec![url] }));

YES
}
Expand Down
73 changes: 7 additions & 66 deletions src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use crate::{
platform::macos::{ActivationPolicy, OpenResourceKind},
platform_impl::platform::app_state::AppState,
};
use crate::{platform::macos::ActivationPolicy, platform_impl::platform::app_state::AppState};

use cocoa::base::id;
use cocoa::foundation::NSString;
use objc::{
declare::ClassDecl,
runtime::{Class, Object, Sel, BOOL, YES},
runtime::{Class, Object, Sel},
};
use std::{
cell::{RefCell, RefMut},
Expand Down Expand Up @@ -39,37 +36,8 @@ pub struct AppDelegateClass(pub *const Class);
unsafe impl Send for AppDelegateClass {}
unsafe impl Sync for AppDelegateClass {}

pub unsafe fn app_delegate_class(
classname: &str,
open_resource_kind: OpenResourceKind,
) -> AppDelegateClass {
let superclass = &*APP_DELEGATE_CLASS.0;
let mut decl = ClassDecl::new(classname, superclass).unwrap();

match open_resource_kind {
OpenResourceKind::Url => {
decl.add_method(
sel!(application:openURLs:),
application_open_urls as extern "C" fn(&Object, Sel, id, id),
);
}
OpenResourceKind::File => {
decl.add_method(
sel!(application:openFile:),
application_open_file as extern "C" fn(&Object, Sel, id, id) -> cocoa::base::BOOL,
);
decl.add_method(
sel!(application:openFiles:),
application_open_files as extern "C" fn(&Object, Sel, id, id),
);
}
}

AppDelegateClass(decl.register())
}

lazy_static! {
static ref APP_DELEGATE_CLASS: AppDelegateClass = unsafe {
pub static ref APP_DELEGATE_CLASS: AppDelegateClass = unsafe {
let superclass = class!(NSResponder);
let mut decl = ClassDecl::new("TaoAppDelegateParent", superclass).unwrap();

Expand All @@ -84,6 +52,10 @@ lazy_static! {
sel!(applicationWillTerminate:),
application_will_terminate as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(application:openURLs:),
application_open_urls as extern "C" fn(&Object, Sel, id, id),
);
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);

AppDelegateClass(decl.register())
Expand Down Expand Up @@ -151,34 +123,3 @@ extern "C" fn application_open_urls(_: &Object, _: Sel, _: id, urls: id) -> () {
AppState::open_urls(urls);
trace!("Completed `application:openURLs:`");
}

extern "C" fn application_open_file(_: &Object, _: Sel, _: id, file: id) -> BOOL {
use std::{ffi::OsStr, os::unix::prelude::OsStrExt};

trace!("Trigger `application:openFile:`");

let filename = OsStr::from_bytes(unsafe { CStr::from_ptr(file.UTF8String()) }.to_bytes()).into();

trace!("Get `application:openFile:` URLs: {:?}", filename);
AppState::open_file(filename);
trace!("Completed `application:openFile:`");

YES
}

extern "C" fn application_open_files(_: &Object, _: Sel, _: id, files: id) -> () {
use std::{ffi::OsStr, os::unix::prelude::OsStrExt};

trace!("Trigger `application:openFiles:`");

let filenames = unsafe {
(0..files.count())
.map(|i| {
OsStr::from_bytes(CStr::from_ptr(files.objectAtIndex(i).UTF8String()).to_bytes()).into()
})
.collect::<Vec<_>>()
};
trace!("Get `application:openFiles:` URLs: {:?}", filenames);
AppState::open_files(filenames);
trace!("Completed `application:openFiles:`");
}
20 changes: 1 addition & 19 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,25 +303,7 @@ impl AppState {
}

pub fn open_urls(urls: Vec<url::Url>) {
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened {
event: crate::event::OpenEvent::Url(urls),
}));
}

pub fn open_file(filename: std::path::PathBuf) {
if let Some(ref mut callback) = *HANDLER.callback.lock().unwrap() {
callback.handle_nonuser_event(
Event::Opened {
event: crate::event::OpenEvent::File(vec![filename]),
},
&mut HANDLER.control_flow.lock().unwrap(),
)
}
}
pub fn open_files(files: Vec<std::path::PathBuf>) {
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened {
event: crate::event::OpenEvent::File(files),
}));
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened { urls }));
}

pub fn wakeup(panic_info: Weak<PanicInfo>) {
Expand Down
10 changes: 2 additions & 8 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ use crate::{
event::Event,
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootWindowTarget},
monitor::MonitorHandle as RootMonitorHandle,
platform::macos::OpenResourceKind,
platform_impl::platform::{
app::APP_CLASS,
app_delegate::app_delegate_class,
app_delegate::APP_DELEGATE_CLASS,
app_state::AppState,
monitor::{self, MonitorHandle},
observer::*,
Expand Down Expand Up @@ -116,7 +115,6 @@ impl<T: 'static> EventLoopWindowTarget<T> {

pub struct EventLoop<T: 'static> {
pub(crate) delegate: IdRef,
pub(crate) open_resource_kind: OpenResourceKind,

window_target: Rc<RootWindowTarget<T>>,
panic_info: Rc<PanicInfo>,
Expand All @@ -136,7 +134,6 @@ impl<T> EventLoop<T> {
setup_control_flow_observers(Rc::downgrade(&panic_info));
EventLoop {
delegate: IdRef::new(nil),
open_resource_kind: Default::default(),
window_target: Rc::new(RootWindowTarget {
p: Default::default(),
_marker: PhantomData,
Expand Down Expand Up @@ -176,10 +173,7 @@ impl<T> EventLoop<T> {
// be marked as main.
let app: id = msg_send![APP_CLASS.0, sharedApplication];

let delegate = IdRef::new(msg_send![
app_delegate_class("TaoAppDelegate", self.open_resource_kind).0,
new
]);
let delegate = IdRef::new(msg_send![APP_DELEGATE_CLASS.0, new]);
let pool = NSAutoreleasePool::new(nil);
let _: () = msg_send![app, setDelegate:*delegate];
let _: () = msg_send![pool, drain];
Expand Down

0 comments on commit cfc768f

Please sign in to comment.