Skip to content

Commit 6e842e7

Browse files
committed
Port dioxus to objc2
1 parent 88120a7 commit 6e842e7

File tree

5 files changed

+58
-90
lines changed

5 files changed

+58
-90
lines changed

Cargo.lock

Lines changed: 4 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ infer = "0.19.0"
367367
dunce = "1.0.5"
368368
percent-encoding = "2.3.1"
369369
muda = "0.17.0"
370+
objc2 = { version = "0.6.3", features = ["exception"] }
371+
objc2-foundation = { version = "0.3.2", default-features = false }
372+
objc2-ui-kit = { version = "0.3.2", default-features = false }
373+
objc2-app-kit = { version = "0.3.2", default-features = false }
370374
tray-icon = "0.21.0"
371375
open = "5.3.2"
372376
webbrowser = "1.0"

packages/desktop/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ muda = { workspace = true }
6868
tray-icon = { workspace = true }
6969

7070
[target.'cfg(target_os = "ios")'.dependencies]
71-
objc = "0.2.7"
72-
objc_id = "0.1.1"
71+
objc2 = { workspace = true }
72+
objc2-foundation = { workspace = true, features = ["NSThread"] }
73+
objc2-ui-kit = { workspace = true, features = ["UIView", "UIViewController"] }
7374

7475
# use rustls on android
7576
[target.'cfg(target_os = "android")'.dependencies]
@@ -84,9 +85,8 @@ ndk-context = { version = "0.1.1" }
8485
tungstenite = { workspace = true, features = ["native-tls"] }
8586

8687
[target.'cfg(target_os = "macos")'.dependencies]
87-
cocoa = "0.26.1"
88-
core-foundation = "0.10.1"
89-
objc = "0.2.7"
88+
objc2 = { workspace = true }
89+
objc2-app-kit = { workspace = true, features = ["NSView"] }
9090

9191
[build-dependencies]
9292
lazy-js-bundle = { workspace = true }

packages/desktop/src/desktop_context.rs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ use tao::{
2323
};
2424
use wry::{RequestAsyncResponder, WebView};
2525

26+
#[cfg(target_os = "ios")]
27+
use objc2::rc::Retained;
28+
#[cfg(target_os = "ios")]
29+
use objc2_ui_kit::UIView;
2630
#[cfg(target_os = "ios")]
2731
use tao::platform::ios::WindowExtIOS;
2832

@@ -71,7 +75,7 @@ pub struct DesktopService {
7175
pub(crate) close_behaviour: Rc<Cell<WindowCloseBehaviour>>,
7276

7377
#[cfg(target_os = "ios")]
74-
pub(crate) views: Rc<std::cell::RefCell<Vec<*mut objc::runtime::Object>>>,
78+
pub(crate) views: Rc<std::cell::RefCell<Vec<Retained<UIView>>>>,
7579
}
7680

7781
/// A smart pointer to the current window.
@@ -288,51 +292,54 @@ impl DesktopService {
288292
self.asset_handlers.remove_handler(name).map(|_| ())
289293
}
290294

295+
#[cfg(target_os = "ios")]
296+
/// Get a retained reference to the current UIView
297+
pub fn ui_view(&self) -> objc2::rc::Retained<objc2_ui_kit::UIView> {
298+
use objc2::rc::Retained;
299+
use objc2_ui_kit::UIView;
300+
let ui_view = self.window.ui_view().cast::<UIView>();
301+
unsafe { Retained::retain(ui_view) }.unwrap()
302+
}
303+
304+
#[cfg(target_os = "ios")]
305+
/// Get a retained reference to the current UIViewController
306+
pub fn ui_view_controller(&self) -> objc2::rc::Retained<objc2_ui_kit::UIViewController> {
307+
use objc2::rc::Retained;
308+
use objc2_ui_kit::UIViewController;
309+
let ui_view_controller = self.window.ui_view_controller().cast::<UIViewController>();
310+
unsafe { Retained::retain(ui_view_controller) }.unwrap()
311+
}
312+
291313
/// Push an objc view to the window
292314
#[cfg(target_os = "ios")]
293-
pub fn push_view(&self, view: objc_id::ShareId<objc::runtime::Object>) {
294-
let window = &self.window;
295-
296-
unsafe {
297-
use objc::runtime::Object;
298-
use objc::*;
299-
assert!(is_main_thread());
300-
let ui_view = window.ui_view() as *mut Object;
301-
let ui_view_frame: *mut Object = msg_send![ui_view, frame];
302-
let _: () = msg_send![view, setFrame: ui_view_frame];
303-
let _: () = msg_send![view, setAutoresizingMask: 31];
304-
305-
let ui_view_controller = window.ui_view_controller() as *mut Object;
306-
let _: () = msg_send![ui_view_controller, setView: view];
307-
self.views.borrow_mut().push(ui_view);
308-
}
315+
pub fn push_view(&self, new_view: Retained<UIView>) {
316+
use objc2_ui_kit::UIViewAutoresizing;
317+
318+
assert!(is_main_thread());
319+
let current_ui_view = self.ui_view();
320+
let current_ui_view_frame = current_ui_view.frame();
321+
322+
new_view.setFrame(current_ui_view_frame);
323+
new_view.setAutoresizingMask(UIViewAutoresizing::from_bits(31).unwrap());
324+
325+
let ui_view_controller = self.ui_view_controller();
326+
ui_view_controller.setView(Some(&new_view));
327+
self.views.borrow_mut().push(new_view);
309328
}
310329

311330
/// Pop an objc view from the window
312331
#[cfg(target_os = "ios")]
313332
pub fn pop_view(&self) {
314-
let window = &self.window;
315-
316-
unsafe {
317-
use objc::runtime::Object;
318-
use objc::*;
319-
assert!(is_main_thread());
320-
if let Some(view) = self.views.borrow_mut().pop() {
321-
let ui_view_controller = window.ui_view_controller() as *mut Object;
322-
let _: () = msg_send![ui_view_controller, setView: view];
323-
}
333+
assert!(is_main_thread());
334+
if let Some(view) = self.views.borrow_mut().pop() {
335+
self.ui_view_controller().setView(Some(&view));
324336
}
325337
}
326338
}
327339

328340
#[cfg(target_os = "ios")]
329341
fn is_main_thread() -> bool {
330-
use objc::runtime::{Class, BOOL, NO};
331-
use objc::*;
332-
333-
let cls = Class::get("NSThread").unwrap();
334-
let result: BOOL = unsafe { msg_send![cls, isMainThread] };
335-
result != NO
342+
objc2_foundation::NSThread::isMainThread_class()
336343
}
337344

338345
/// A [`DesktopContext`] that is pending creation.

packages/desktop/src/webview.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,13 @@ impl WebviewInstance {
234234

235235
// https://developer.apple.com/documentation/appkit/nswindowcollectionbehavior/nswindowcollectionbehaviormanaged
236236
#[cfg(target_os = "macos")]
237-
#[allow(deprecated)]
238237
{
239-
use cocoa::appkit::NSWindowCollectionBehavior;
240-
use cocoa::base::id;
241-
use objc::{msg_send, sel, sel_impl};
238+
use objc2::rc::Retained;
239+
use objc2_app_kit::{NSWindow, NSWindowCollectionBehavior};
242240
use tao::platform::macos::WindowExtMacOS;
243-
244-
unsafe {
245-
let window: id = window.ns_window() as id;
246-
#[allow(unexpected_cfgs)]
247-
let _: () = msg_send![window, setCollectionBehavior: NSWindowCollectionBehavior::NSWindowCollectionBehaviorManaged];
248-
}
241+
let ns_window: Retained<NSWindow> =
242+
unsafe { Retained::retain(window.ns_window().cast()) }.unwrap();
243+
ns_window.setCollectionBehavior(NSWindowCollectionBehavior::Managed)
249244
}
250245

251246
let mut web_context = WebContext::new(cfg.data_dir.clone());

0 commit comments

Comments
 (0)