-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(macos): move custom class to individual files
- Loading branch information
Showing
14 changed files
with
1,080 additions
and
936 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.