Skip to content
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

On macOS, add documentEdited APIs #2550

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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.
- On Windows, fix icons specified on `WindowBuilder` not taking effect for windows created after the first one.
- On Windows and macOS, add `Window::title` to query the current window title.
- On Windows, fix focusing menubar when pressing `Alt`.
Expand Down
29 changes: 29 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
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.
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
///
/// # 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
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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 @@ -183,6 +186,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
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,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