diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e0d44c7..2d357e0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 37a44cfb..c2be433f 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -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 { @@ -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`. diff --git a/src/platform_impl/macos/appkit/window.rs b/src/platform_impl/macos/appkit/window.rs index e717695b..1f34e435 100644 --- a/src/platform_impl/macos/appkit/window.rs +++ b/src/platform_impl/macos/appkit/window.rs @@ -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; @@ -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); diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index c06eb438..c3375264 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -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 {