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

Window Button Styles #6675

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions docs/reference/src/language/builtins/elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -1064,3 +1064,6 @@ or smaller. The initial width can be controlled with the `preferred-width` prope
- **`no-frame`** (_in_ _bool_): Whether the window should be borderless/frameless or not.
- **`resize-border-width`** (_in_ _length_): Size of the resize border in borderless/frameless windows (winit only for now).
- **`title`** (_in_ _string_): The window title that is shown in the title bar.
- **`no-minimize-button`** (_in_ _bool_): Whether the window should have a minimize button in the title bar. Default is `false` to show the minimize button.
- **`no-maximize-button`** (_in_ _bool_): Whether the window should have a maximize button in the title bar. Default is `false` to show the maximize button.
- **`no-close-button`** (_in_ _bool_): Whether the window should have a close button in the title bar. Default is `false` to show the close button.
Comment on lines +1067 to +1069
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Enyium has a point. It probably should be has-maximize-button or maximize-button-visible or maximize-button-enabled (and default to true which can be done in the builtins.slint)

31 changes: 30 additions & 1 deletion internal/backends/winit/winitwindowadapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::pin::Pin;
use std::rc::Rc;
use std::rc::Weak;

use i_slint_core::window::WindowButtonState;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::WindowExtWebSys;
#[cfg(target_family = "windows")]
Expand Down Expand Up @@ -39,7 +40,7 @@ use i_slint_core::{self as corelib, OpenGLAPI};
use once_cell::unsync::OnceCell;
#[cfg(enable_accesskit)]
use winit::event_loop::EventLoopProxy;
use winit::window::WindowAttributes;
use winit::window::{WindowAttributes, WindowButtons};

fn position_to_winit(pos: &corelib::api::WindowPosition) -> winit::dpi::Position {
match pos {
Expand Down Expand Up @@ -195,6 +196,27 @@ impl WinitWindowOrNone {
}
}

fn set_window_buttons_state(&self, window_buttons_state: WindowButtonState) {
match self {
Self::HasWindow(window) => {
let mut enabled_buttons = WindowButtons::empty();
if !window_buttons_state.minimize {
enabled_buttons |= WindowButtons::MINIMIZE;
}
if !window_buttons_state.maximize {
enabled_buttons |= WindowButtons::MAXIMIZE;
}
if !window_buttons_state.close {
enabled_buttons |= WindowButtons::CLOSE;
}
window.set_enabled_buttons(enabled_buttons);
}
Self::None(attributes) => {
attributes.borrow_mut().enabled_buttons = WindowButtons::all()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why set all buttons here?

}
}
}

fn set_minimized(&self, minimized: bool) {
match self {
Self::HasWindow(window) => window.set_minimized(minimized),
Expand Down Expand Up @@ -764,6 +786,13 @@ impl WindowAdapter for WinitWindowAdapter {
winit_window_or_none.set_window_level(new_window_level);
}

// Get the associated window button states
let mut win_props = properties.window_buttons_enabled();
win_props.close = window_item.no_close_button();
win_props.minimize = window_item.no_minimize_button();
win_props.maximize = window_item.no_maximize_button();
winit_window_or_none.set_window_buttons_state(win_props);

// Use our scale factor instead of winit's logical size to take a scale factor override into account.
let sf = self.window().scale_factor();

Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/builtins.slint
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ component WindowItem {
in-out property <length> default-font-size; // <=> StyleMetrics.default-font-size set in apply_default_properties_from_style
in property <int> default-font-weight;
in property <image> icon;
in property <bool> no-minimize-button;
in property <bool> no-maximize-button;
in property <bool> no-close-button;
}

export component Window inherits WindowItem { }
Expand Down
3 changes: 3 additions & 0 deletions internal/core/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,9 @@ pub struct WindowItem {
pub default_font_size: Property<LogicalLength>,
pub default_font_weight: Property<i32>,
pub cached_rendering_data: CachedRenderingData,
pub no_minimize_button: Property<bool>,
pub no_maximize_button: Property<bool>,
pub no_close_button: Property<bool>,
}

impl Item for WindowItem {
Expand Down
23 changes: 23 additions & 0 deletions internal/core/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ impl<'a> WindowProperties<'a> {
pub fn is_minimized(&self) -> bool {
self.0.minimized.get()
}

/// The widow style
/// Returns a tuple of three booleans: (minimize, maximize, close)
pub fn window_buttons_enabled(&self) -> WindowButtonState {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it maybe visibility rather than enabled

self.0.window_button_state.get()
}
}

struct WindowPropertiesTracker {
Expand Down Expand Up @@ -407,6 +413,17 @@ struct WindowPinnedFields {
text_input_focused: Property<bool>,
}

#[derive(Debug, Copy, Clone, PartialEq)]
/// The state of each window button
pub struct WindowButtonState {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we consider marking this struct as #[non_exhaustive]?

Naming-wise, i believe the struct should probably be called WindowButtonVisibility as it is about the visibility of these buttons rather than their state. Or the value should say minimize_button_visible or has_minimize_button

/// The minimize button state
pub minimize: bool,
/// The maximize button state
pub maximize: bool,
/// The close button state
pub close: bool,
}

/// Inner datastructure for the [`crate::api::Window`]
pub struct WindowInner {
window_adapter_weak: Weak<dyn WindowAdapter>,
Expand All @@ -431,6 +448,7 @@ pub struct WindowInner {
fullscreen: Cell<bool>,
maximized: Cell<bool>,
minimized: Cell<bool>,
window_button_state: Cell<WindowButtonState>,

/// Stack of currently active popups
active_popups: RefCell<Vec<PopupWindow>>,
Expand Down Expand Up @@ -491,6 +509,11 @@ impl WindowInner {
fullscreen: Cell::new(false),
maximized: Cell::new(false),
minimized: Cell::new(false),
window_button_state: Cell::new(WindowButtonState {
minimize: true,
maximize: true,
close: true,
}),
focus_item: Default::default(),
last_ime_text: Default::default(),
cursor_blinker: Default::default(),
Expand Down
Loading