Skip to content

Commit

Permalink
feat(window): implement minimizable and closable for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
caesar committed Sep 29, 2022
1 parent 0637c60 commit e75a415
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ fn create_window(
masks &= !NSWindowStyleMask::NSResizableWindowMask;
}

if !attrs.minimizable {
masks &= !NSWindowStyleMask::NSMiniaturizableWindowMask;
}

if !attrs.closable {
masks &= !NSWindowStyleMask::NSClosableWindowMask;
}

if pl_attrs.fullsize_content_view {
masks |= NSWindowStyleMask::NSFullSizeContentViewWindowMask;
}
Expand Down Expand Up @@ -693,6 +701,28 @@ impl UnownedWindow {
} // Otherwise, we don't change the mask until we exit fullscreen.
}

#[inline]
pub fn set_minimizable(&self, minimizable: bool) {
let mut mask = unsafe { self.ns_window.styleMask() };
if minimizable {
mask |= NSWindowStyleMask::NSMiniaturizableWindowMask;
} else {
mask &= !NSWindowStyleMask::NSMiniaturizableWindowMask;
}
self.set_style_mask_async(mask);
}

#[inline]
pub fn set_closable(&self, closable: bool) {
let mut mask = unsafe { self.ns_window.styleMask() };
if closable {
mask |= NSWindowStyleMask::NSClosableWindowMask;
} else {
mask &= !NSWindowStyleMask::NSClosableWindowMask;
}
self.set_style_mask_async(mask);
}

pub fn set_cursor_icon(&self, cursor: CursorIcon) {
let cursor = util::Cursor::from(cursor);
if let Some(cursor_access) = self.cursor_state.upgrade() {
Expand Down Expand Up @@ -886,6 +916,18 @@ impl UnownedWindow {
is_resizable == YES
}

#[inline]
pub fn is_minimizable(&self) -> bool {
let is_minimizable: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturizable] };
is_minimizable == YES
}

#[inline]
pub fn is_closable(&self) -> bool {
let is_closable: BOOL = unsafe { msg_send![*self.ns_window, hasCloseBox] };
is_closable == YES
}

#[inline]
pub fn is_decorated(&self) -> bool {
self.decorations.load(Ordering::Acquire)
Expand Down
82 changes: 82 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ pub struct WindowAttributes {
/// The default is `true`.
pub resizable: bool,

/// Whether the window is minimizable or not.
///
/// The default is `true`.
pub minimizable: bool,

/// Whether the window is closable or not.
///
/// The default is `true`.
pub closable: bool,

/// Whether the window should be set as fullscreen upon creation.
///
/// The default is `None`.
Expand Down Expand Up @@ -219,6 +229,8 @@ impl Default for WindowAttributes {
max_inner_size: None,
position: None,
resizable: true,
minimizable: true,
closable: true,
title: "tao window".to_owned(),
maximized: false,
fullscreen: None,
Expand Down Expand Up @@ -296,6 +308,28 @@ impl WindowBuilder {
self
}

/// Sets whether the window is minimizable or not.
///
/// See [`Window::set_minimizable`] for details.
///
/// [`Window::set_minimizable`]: crate::window::Window::set_minimizable
#[inline]
pub fn with_minimizable(mut self, minimizable: bool) -> Self {
self.window.minimizable = minimizable;
self
}

/// Sets whether the window is closable or not.
///
/// See [`Window::set_closable`] for details.
///
/// [`Window::set_closable`]: crate::window::Window::set_closable
#[inline]
pub fn with_closable(mut self, closable: bool) -> Self {
self.window.closable = closable;
self
}

/// Requests a specific title for the window.
///
/// See [`Window::set_title`] for details.
Expand Down Expand Up @@ -694,6 +728,30 @@ impl Window {
self.window.set_resizable(resizable)
}

/// Sets whether the window is minimizable or not.
///
/// ## Platform-specific
///
/// - **Windows:** Unsupported
/// - **Linux:** Unsupported
/// - **iOS / Android:** Unsupported.
#[inline]
pub fn set_minimizable(&self, minimizable: bool) {
self.window.set_minimizable(minimizable)
}

/// Sets whether the window is closable or not.
///
/// ## Platform-specific
///
/// - **Windows:** Unsupported
/// - **Linux:** Unsupported
/// - **iOS / Android:** Unsupported.
#[inline]
pub fn set_closable(&self, closable: bool) {
self.window.set_closable(closable)
}

/// Sets the window to minimized or back
///
/// ## Platform-specific
Expand Down Expand Up @@ -754,6 +812,30 @@ impl Window {
self.window.is_resizable()
}

/// Gets the window's current minimizable state.
///
/// ## Platform-specific
///
/// - **Windows:** Unsupported.
/// - **Linux:** Unsupported.
/// - **iOS / Android:** Unsupported.
#[inline]
pub fn is_minimizable(&self) -> bool {
self.window.is_minimizable()
}

/// Gets the window's current closable state.
///
/// ## Platform-specific
///
/// - **Windows:** Unsupported.
/// - **Linux:** Unsupported.
/// - **iOS / Android:** Unsupported.
#[inline]
pub fn is_closable(&self) -> bool {
self.window.is_closable()
}

/// Gets the window's current decoration state.
///
/// ## Platform-specific
Expand Down

0 comments on commit e75a415

Please sign in to comment.