Skip to content

Commit

Permalink
refactor(macos): move custom class to individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Jul 11, 2024
1 parent 1abff79 commit 4f76fd1
Show file tree
Hide file tree
Showing 14 changed files with 1,077 additions and 931 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ objc2-app-kit = { version = "0.2.0", features = [
"NSResponder",
"NSOpenPanel",
"NSSavePanel",
"NSWindow",
"NSMenu",
] }
objc2-ui-kit = { version = "0.2.2", features = [
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub(crate) mod wkwebview;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use wkwebview::*;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use wkwebview::{PrintMargin, PrintOptions};
pub use wkwebview::{PrintMargin, PrintOptions, WryWebView};

#[cfg(target_os = "windows")]
pub(crate) mod webview2;
Expand Down Expand Up @@ -1631,10 +1631,6 @@ impl WebViewExtMacOS for WebView {
}

fn ns_window(&self) -> Retained<NSWindow> {
// unsafe {
// let ns_window: cocoa::base::id = msg_send![self.webview.webview, window];
// ns_window
// }
self.webview.webview.window().unwrap().clone()
}

Expand Down
98 changes: 98 additions & 0 deletions src/wkwebview/class/document_title_changed_observer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2020-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{ffi::c_void, ptr::null_mut};

use objc2::{
declare_class, msg_send, msg_send_id,
mutability::InteriorMutable,
rc::Retained,
runtime::{AnyObject, NSObject},
ClassType, DeclaredClass,
};
use objc2_foundation::{
NSDictionary, NSKeyValueChangeKey, NSKeyValueObservingOptions,
NSObjectNSKeyValueObserverRegistration, NSObjectProtocol, NSString,
};

use crate::WryWebView;
pub struct DocumentTitleChangedObserverIvars {
pub object: Retained<WryWebView>,
pub handler: Box<dyn Fn(String)>,
}

declare_class!(
pub struct DocumentTitleChangedObserver;

unsafe impl ClassType for DocumentTitleChangedObserver {
type Super = NSObject;
type Mutability = InteriorMutable;
const NAME: &'static str = "DocumentTitleChangedObserver";
}

impl DeclaredClass for DocumentTitleChangedObserver {
type Ivars = DocumentTitleChangedObserverIvars;
}

unsafe impl DocumentTitleChangedObserver {
#[method(observeValueForKeyPath:ofObject:change:context:)]
fn observe_value_for_key_path(
&self,
key_path: Option<&NSString>,
of_object: Option<&AnyObject>,
_change: Option<&NSDictionary<NSKeyValueChangeKey, AnyObject>>,
_context: *mut c_void,
) {
if let (Some(key_path), Some(object)) = (key_path, of_object) {
if key_path.to_string() == "title" {
unsafe {
let handler = &self.ivars().handler;
// if !handler.is_null() {
let title: *const NSString = msg_send![object, title];
handler((*title).to_string());
// }
}
}
}
}
}

unsafe impl NSObjectProtocol for DocumentTitleChangedObserver {}
);

impl DocumentTitleChangedObserver {
pub fn new(webview: Retained<WryWebView>, handler: Box<dyn Fn(String)>) -> Retained<Self> {
let observer = Self::alloc().set_ivars(DocumentTitleChangedObserverIvars {
object: webview,
handler,
});

let observer: Retained<Self> = unsafe { msg_send_id![super(observer), init] };

unsafe {
observer
.ivars()
.object
.addObserver_forKeyPath_options_context(
&observer,
&NSString::from_str("title"),
NSKeyValueObservingOptions::NSKeyValueObservingOptionNew,
null_mut(),
);
}

observer
}
}

impl Drop for DocumentTitleChangedObserver {
fn drop(&mut self) {
unsafe {
self
.ivars()
.object
.removeObserver_forKeyPath(&self, &NSString::from_str("title"));
}
}
}
12 changes: 12 additions & 0 deletions src/wkwebview/class/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2020-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

pub mod wry_web_view;
pub mod wry_web_view_delegate;
pub mod document_title_changed_observer;
pub mod wry_navigation_delegate;
pub mod wry_download_delegate;
pub mod wry_web_view_ui_delegate;
pub mod wry_web_view_parent;
pub mod url_scheme_handler;
Loading

0 comments on commit 4f76fd1

Please sign in to comment.