Skip to content

Commit

Permalink
On macOS, add documentEdited APIs (#2550)
Browse files Browse the repository at this point in the history
* On macOS, add documentEdited APIs

Port of tauri-apps/tao@33fdeab

* Update src/platform/macos.rs

Co-authored-by: Mads Marquart <mads@marquart.dk>

* typo

Co-authored-by: Mads Marquart <mads@marquart.dk>
  • Loading branch information
amrbashir and madsmtm committed Nov 23, 2022
1 parent 6d0cf6a commit f77f858
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- On macOS, add `WindowExtMacOS::is_document_edited` and `WindowExtMacOS::set_document_edited` APIs.
- **Breaking:** Removed `WindowBuilderExtIOS::with_root_view_class`; instead, you should use `[[view layer] addSublayer: ...]` to add an instance of the desired layer class (e.g. `CAEAGLLayer` or `CAMetalLayer`). See `vulkano-win` or `wgpu` for examples of this.
- On MacOS and Windows, add `Window::set_content_protected`.
- On MacOS, add `EventLoopBuilderExtMacOS::with_activate_ignoring_other_apps`.
Expand Down
29 changes: 29 additions & 0 deletions src/platform/macos.rs
Expand Up @@ -36,6 +36,25 @@ pub trait WindowExtMacOS {

/// Sets whether or not the window has shadow.
fn set_has_shadow(&self, has_shadow: bool);

/// Get the window's edit state.
///
/// # Examples
///
/// ```ignore
/// WindowEvent::CloseRequested => {
/// if window.is_document_edited() {
/// // Show the user a save pop-up or similar
/// } else {
/// // Close the window
/// drop(window);
/// }
/// }
/// ```
fn is_document_edited(&self) -> bool;

/// Put the window in a state which indicates a file save is required.
fn set_document_edited(&self, edited: bool);
}

impl WindowExtMacOS for Window {
Expand Down Expand Up @@ -68,6 +87,16 @@ impl WindowExtMacOS for Window {
fn set_has_shadow(&self, has_shadow: bool) {
self.window.set_has_shadow(has_shadow)
}

#[inline]
fn is_document_edited(&self) -> bool {
self.window.is_document_edited()
}

#[inline]
fn set_document_edited(&self, edited: bool) {
self.window.set_document_edited(edited)
}
}

/// Corresponds to `NSApplicationActivationPolicy`.
Expand Down
6 changes: 6 additions & 0 deletions src/platform_impl/macos/appkit/window.rs
Expand Up @@ -168,6 +168,9 @@ extern_methods!(
#[sel(setLevel:)]
pub fn setLevel(&self, level: NSWindowLevel);

#[sel(setDocumentEdited:)]
pub fn setDocumentEdited(&self, val: bool);

#[sel(occlusionState)]
pub fn occlusionState(&self) -> NSWindowOcclusionState;

Expand All @@ -186,6 +189,9 @@ extern_methods!(
#[sel(isZoomed)]
pub fn isZoomed(&self) -> bool;

#[sel(isDocumentEdited)]
pub fn isDocumentEdited(&self) -> bool;

#[sel(close)]
pub fn close(&self);

Expand Down
8 changes: 8 additions & 0 deletions src/platform_impl/macos/window.rs
Expand Up @@ -1229,6 +1229,14 @@ impl WindowExtMacOS for WinitWindow {
fn set_has_shadow(&self, has_shadow: bool) {
self.setHasShadow(has_shadow)
}

fn is_document_edited(&self) -> bool {
self.isDocumentEdited()
}

fn set_document_edited(&self, edited: bool) {
self.setDocumentEdited(edited)
}
}

pub(super) fn get_ns_theme() -> Theme {
Expand Down

0 comments on commit f77f858

Please sign in to comment.