-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add CefAppProtocol bindings and partially port SimpleApplication from original cefsimple #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tasuren
wants to merge
6
commits into
tauri-apps:dev
Choose a base branch
from
tasuren:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65f4580
feat: add CefAppProtocol bindings
tasuren 7b9b3cd
feat: port SimpleApplication from original cefsimple
tasuren 3791949
fix: resolve cargo build warning about default-features on macOS
tasuren 5d2f459
feat: use SimpleApplication in cefsimple
tasuren f24d523
Merge branch 'dev' of https://github.com/tauri-apps/cef-rs into cefsi…
tasuren d6827d2
Merge branch 'dev' of https://github.com/tauri-apps/cef-rs into dev
tasuren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,121 @@ | ||
use std::cell::Cell; | ||
|
||
use objc2::{ | ||
define_class, extern_protocol, msg_send, rc::Retained, runtime::*, ClassType, DefinedClass, | ||
MainThreadMarker, | ||
}; | ||
use objc2_app_kit::{NSApp, NSApplication}; | ||
|
||
extern_protocol!( | ||
/// The binding of `CrAppProtocol`. | ||
#[allow(clippy::missing_safety_doc)] | ||
pub unsafe trait CrAppProtocol { | ||
#[unsafe(method(isHandlingSendEvent))] | ||
unsafe fn is_handling_send_event(&self) -> Bool; | ||
} | ||
); | ||
|
||
extern_protocol!( | ||
/// The binding of `CrAppControlProtocol`. | ||
#[allow(clippy::missing_safety_doc)] | ||
pub unsafe trait CrAppControlProtocol: CrAppProtocol { | ||
#[unsafe(method(setHandlingSendEvent:))] | ||
unsafe fn set_handling_send_event(&self, handling_send_event: Bool); | ||
} | ||
); | ||
|
||
extern_protocol!( | ||
/// The binding of `CefAppProtocol`. | ||
#[allow(clippy::missing_safety_doc)] | ||
pub unsafe trait CefAppProtocol: CrAppControlProtocol {} | ||
); | ||
|
||
/// Instance variables of `SimpleApplication`. | ||
pub struct SimpleApplicationIvars { | ||
handling_send_event: Cell<Bool>, | ||
} | ||
|
||
define_class!( | ||
/// A default `NSApplication` subclass that implements the required CEF protocols. | ||
/// | ||
/// This class provides the necessary `CefAppProtocol` conformance to | ||
/// ensure that events are handled correctly by the Chromium framework on macOS. | ||
/// | ||
/// # Usage | ||
/// | ||
/// For most new applications built with cef-rs, this is the class you should use. | ||
/// It must be activated by calling the `init` method at the very beginning | ||
/// of your `main` function. | ||
/// | ||
/// ```no_run | ||
/// // In your main function: | ||
/// #[cfg(target_os = "macos")] | ||
/// cef::application_mac::SimpleApplication::init(); | ||
/// ``` | ||
/// | ||
/// # Custom Implementations | ||
/// | ||
/// You should not use this implementation if you are integrating cef-rs | ||
/// into an existing macOS application that already has its own custom | ||
/// `NSApplication` subclass. | ||
/// | ||
/// In that scenario, you should instead implement the [`CrAppProtocol`], | ||
/// [`CrAppControlProtocol`], and [`CefAppProtocol`] traits on your existing | ||
/// application class with objc2 crate. | ||
#[unsafe(super(NSApplication))] | ||
#[ivars = SimpleApplicationIvars] | ||
pub struct SimpleApplication; | ||
|
||
unsafe impl CrAppControlProtocol for SimpleApplication { | ||
#[unsafe(method(setHandlingSendEvent:))] | ||
unsafe fn set_handling_send_event(&self, handling_send_event: Bool) { | ||
self.ivars().handling_send_event.set(handling_send_event); | ||
} | ||
} | ||
|
||
unsafe impl CrAppProtocol for SimpleApplication { | ||
#[unsafe(method(isHandlingSendEvent))] | ||
unsafe fn is_handling_send_event(&self) -> Bool { | ||
self.ivars().handling_send_event.get() | ||
} | ||
} | ||
|
||
unsafe impl CefAppProtocol for SimpleApplication {} | ||
); | ||
|
||
impl SimpleApplication { | ||
/// Initializes the global `NSApplication` instance with our custom `SimpleApplication`. | ||
/// | ||
/// This function must be called on the main thread before any other UI code | ||
/// creates the shared application instance. It ensures that CEF's event | ||
/// handling requirements are met. | ||
/// | ||
/// If other UI code has already created a shared application instance, | ||
/// this will return an error. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function interacts with UI and should only be called once | ||
/// at the beginning | ||
pub fn init() -> Result<Retained<Self>, ()> { | ||
let mtm = MainThreadMarker::new() | ||
.expect("`SimpleApplication::init` must be called on the main thread"); | ||
|
||
unsafe { | ||
// Initialize mac application instance. | ||
// SAFETY: mtm ensure that here is the main thread. | ||
let _: Retained<AnyObject> = msg_send![SimpleApplication::class(), sharedApplication]; | ||
} | ||
|
||
// If there was an invocation to NSApp prior to here, | ||
// then the NSApp will not be a SimpleApplication. | ||
// objc2's downcast ensure that this doesn't happen. | ||
let app = NSApp(mtm); | ||
|
||
if let Ok(app) = app.downcast() { | ||
Ok(app) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This SimpleApplication should be move to examples?
As for projects integrate cef, they usually have their own NSApplication definition, We should leave it in examples code for reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @csmoe, thanks for taking the time to review my PR.
That is a fair point. My initial decision to place
SimpleApplication
in the cef crate was based on an earlier discussion with @wravery on the issue.He suggested the following, which led me to believe it would be a useful utility within the cef crate itself:
#96 (comment)
Your point that most users integrating cef will likely have their own
NSApplication
also makes a lot of sense. It's possible I mistakenly assumed his suggestion to "include these in the cef crate itself" also applied to the concreteSimpleApplication
implementation, and not just the protocol bindings.What are your thoughts on this, @wravery?
I'm happy to move the
SimpleApplication
code to examples if that's the preferred approach. I just want to make sure I'm following the best direction for the project.