On Windows and macOS, add API to enable/disable window controls (#2537)

* On Windows and macOS, add API to enable/disable window controls

* fix build

* missing import

* use `WindowButtons` flags

* rename to `[set_]enabled_buttons`

* add example, fix windows impl for minimize

* macOS: Fix button enabling close/minimize while disabling maximized

* Update src/platform_impl/windows/window.rs

Co-authored-by: Kirill Chibisov <contact@kchibisov.com>

* compose the flags on a sep line, use `bool::then`

Co-authored-by: Mads Marquart <mads@marquart.dk>
Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
This commit is contained in:
Amr Bashir 2022-11-29 12:03:51 +02:00 committed by GitHub
parent 28e34c2e1b
commit 94688a62f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 343 additions and 36 deletions

View file

@ -126,6 +126,7 @@ pub(crate) struct WindowAttributes {
pub max_inner_size: Option<Size>,
pub position: Option<Position>,
pub resizable: bool,
pub enabled_buttons: WindowButtons,
pub title: String,
pub fullscreen: Option<platform_impl::Fullscreen>,
pub maximized: bool,
@ -148,6 +149,7 @@ impl Default for WindowAttributes {
max_inner_size: None,
position: None,
resizable: true,
enabled_buttons: WindowButtons::all(),
title: "winit window".to_owned(),
maximized: false,
fullscreen: None,
@ -244,6 +246,17 @@ impl WindowBuilder {
self
}
/// Sets the enabled window buttons.
///
/// The default is [`WindowButtons::all`]
///
/// See [`Window::set_enabled_buttons`] for details.
#[inline]
pub fn with_enabled_buttons(mut self, buttons: WindowButtons) -> Self {
self.window.enabled_buttons = buttons;
self
}
/// Sets the initial title of the window in the title bar.
///
/// The default is `"winit window"`.
@ -755,6 +768,26 @@ impl Window {
self.window.is_resizable()
}
/// Sets the enabled window buttons.
///
/// ## Platform-specific
///
/// - **Wayland / X11:** Not implemented.
/// - **Web / iOS / Android:** Unsupported.
pub fn set_enabled_buttons(&self, buttons: WindowButtons) {
self.window.set_enabled_buttons(buttons)
}
/// Gets the enabled window buttons.
///
/// ## Platform-specific
///
/// - **Wayland / X11:** Not implemented. Always returns [`WindowButtons::all`].
/// - **Web / iOS / Android:** Unsupported. Always returns [`WindowButtons::all`].
pub fn enabled_buttons(&self) -> WindowButtons {
self.window.enabled_buttons()
}
/// Sets the window to minimized or back
///
/// ## Platform-specific
@ -1441,6 +1474,14 @@ impl Default for UserAttentionType {
}
}
bitflags! {
pub struct WindowButtons: u32 {
const CLOSE = 1 << 0;
const MINIMIZE = 1 << 1;
const MAXIMIZE = 1 << 2;
}
}
/// A window level groups windows with respect to their z-position.
///
/// The relative ordering between windows in different window levels is fixed.