Skip to content

Commit

Permalink
chore: target os import
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Jul 10, 2024
1 parent 2fb6be9 commit 4eec6fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
15 changes: 7 additions & 8 deletions examples/reparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use objc2::rc::Retained;
use objc2_app_kit::NSWindow;
use tao::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
Expand All @@ -13,7 +11,10 @@ use tao::{
use wry::WebViewBuilder;

#[cfg(target_os = "macos")]
use {tao::platform::macos::WindowExtMacOS, wry::WebViewExtMacOS};
use {
objc2::rc::Retained, objc2_app_kit::NSWindow, tao::platform::macos::WindowExtMacOS,
wry::WebViewExtMacOS,
};
#[cfg(target_os = "windows")]
use {tao::platform::windows::WindowExtWindows, wry::WebViewExtWindows};

Expand Down Expand Up @@ -92,11 +93,9 @@ fn main() -> wry::Result<()> {
webview_container = new_parent.id();

#[cfg(target_os = "macos")]
unsafe {
let parent_window: Retained<NSWindow> =
Retained::from_raw(new_parent.ns_window() as *mut NSWindow).unwrap();
webview.reparent(parent_window).unwrap();
}
webview
.reparent(new_parent.ns_window() as *mut NSWindow)
.unwrap();
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,18 @@ use android::*;

#[cfg(gtk)]
pub(crate) mod webkitgtk;
use objc2::rc::Retained;
use objc2_app_kit::NSWindow;
use objc2_web_kit::WKUserContentController;
/// Re-exported [raw-window-handle](https://docs.rs/raw-window-handle/latest/raw_window_handle/) crate.
pub use raw_window_handle;
use raw_window_handle::HasWindowHandle;
#[cfg(gtk)]
use webkitgtk::*;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc2::rc::Retained;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc2_app_kit::NSWindow;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc2_web_kit::WKUserContentController;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) mod wkwebview;
#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand Down Expand Up @@ -1612,7 +1615,7 @@ pub trait WebViewExtMacOS {
/// Returns NSWindow associated with the WKWebView webview
fn ns_window(&self) -> Retained<NSWindow>;
/// Attaches this webview to the given NSWindow and removes it from the current one.
fn reparent(&self, window: Retained<NSWindow>) -> Result<()>;
fn reparent(&self, window: *mut NSWindow) -> Result<()>;
// Prints with extra options
fn print_with_options(&self, options: &PrintOptions) -> Result<()>;
}
Expand All @@ -1635,7 +1638,7 @@ impl WebViewExtMacOS for WebView {
self.webview.webview.window().unwrap().clone()
}

fn reparent(&self, window: Retained<NSWindow>) -> Result<()> {
fn reparent(&self, window: *mut NSWindow) -> Result<()> {
self.webview.reparent(window)
}

Expand Down
14 changes: 8 additions & 6 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use objc2::{
};
use objc2_app_kit::{
NSApp, NSApplication, NSAutoresizingMaskOptions, NSDragOperation, NSDraggingInfo, NSEvent,
NSModalResponse, NSModalResponseOK, NSPrintInfo, NSTitlebarSeparatorStyle, NSView,
NSModalResponse, NSModalResponseOK, NSOpenPanel, NSPrintInfo, NSTitlebarSeparatorStyle, NSView,
NSWindow,
};
use objc2_foundation::{
ns_string, CGPoint, CGRect, CGSize, MainThreadMarker, NSArray, NSBundle, NSData, NSDate,
Expand Down Expand Up @@ -196,7 +197,8 @@ impl InnerWebView {
) {
unsafe {
#[cfg(feature = "tracing")]
tracing::info_span!(parent: None, "wry::custom_protocol::handle", uri = tracing::field::Empty).entered();
let span = tracing::info_span!(parent: None, "wry::custom_protocol::handle", uri = tracing::field::Empty)
.entered();

let task_key = (*task).hash(); // hash by task object address
let task_uuid = (*webview).add_custom_task_key(task_key);
Expand All @@ -216,7 +218,7 @@ impl InnerWebView {
let uri = url.absoluteString().unwrap().to_string();

#[cfg(feature = "tracing")]
span.record("uri", uri);
span.record("uri", uri.clone());

// Get request method (GET, POST, PUT etc...)
let method = request.HTTPMethod().unwrap().to_string();
Expand Down Expand Up @@ -714,7 +716,7 @@ impl InnerWebView {
) {
unsafe {
if let Some(mtm) = MainThreadMarker::new() {
let open_panel = objc2_app_kit::NSOpenPanel::openPanel(mtm);
let open_panel = NSOpenPanel::openPanel(mtm);
open_panel.setCanChooseFiles(true);
let allow_multi = open_panel_params.allowsMultipleSelection();
open_panel.setAllowsMultipleSelection(allow_multi);
Expand Down Expand Up @@ -1130,9 +1132,9 @@ r#"Object.defineProperty(window, 'ipc', {
}

#[cfg(target_os = "macos")]
pub(crate) fn reparent(&self, window: Retained<objc2_app_kit::NSWindow>) -> crate::Result<()> {
pub(crate) fn reparent(&self, window: *mut NSWindow) -> crate::Result<()> {
unsafe {
let content_view = window.contentView().unwrap();
let content_view = (*window).contentView().unwrap();
content_view.addSubview(&self.webview);
}

Expand Down

0 comments on commit 4eec6fb

Please sign in to comment.