Merge pull request #40 from dotCooCoo/fix/windows-tool-window

Windows: add an optional tool-window creation attribute
This commit is contained in:
BFlat authored and GitHub committed 2026-11-30 10:46:40 +07:00
commit 664a77cf49
5 files changed
+95 -5

No files matched your search

+1
View File
@@ -119,6 +119,7 @@ If your PR makes notable changes to Winit's features, please update this section
### Windows
* Setting the name of the internal window class
* Setting the taskbar icon
* `WindowAttributesExtWindows::with_tool_window` requests a tool window without taskbar or Alt-Tab entries.
* Setting the parent window
* Setting a menu bar
* `WS_EX_NOREDIRECTIONBITMAP` support
+20
View File
@@ -271,6 +271,9 @@ pub trait WindowExtWindows {
fn set_taskbar_icon(&self, taskbar_icon: Option<Icon>);
/// Whether to show or hide the window icon in the taskbar.
///
/// A window created with [`WindowAttributesExtWindows::with_tool_window`] stays off the
/// taskbar, and `set_skip_taskbar(false)` does not add it.
fn set_skip_taskbar(&self, skip: bool);
/// Shows or hides the background drop shadow for undecorated windows.
@@ -497,8 +500,19 @@ pub trait WindowAttributesExtWindows {
fn with_drag_and_drop(self, flag: bool) -> Self;
/// Whether show or hide the window icon in the taskbar.
///
/// A tool window ([`WindowAttributesExtWindows::with_tool_window`]) is kept off the taskbar
/// whatever this is set to.
fn with_skip_taskbar(self, skip: bool) -> Self;
/// Requests the Windows tool-window style (`WS_EX_TOOLWINDOW`).
///
/// Tool windows are created without taskbar or Alt-Tab entries. This defaults to `false`.
/// A tool window stays off the taskbar for its whole life: `with_skip_taskbar(false)` and
/// [`WindowExtWindows::set_skip_taskbar`]`(false)` do not add it.
/// See <https://learn.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles>.
fn with_tool_window(self, tool_window: bool) -> Self;
/// Customize the window class name.
fn with_class_name<S: Into<String>>(self, class_name: S) -> Self;
@@ -574,6 +588,12 @@ impl WindowAttributesExtWindows for WindowAttributes {
self
}
#[inline]
fn with_tool_window(mut self, tool_window: bool) -> Self {
self.platform_specific.tool_window = tool_window;
self
}
#[inline]
fn with_class_name<S: Into<String>>(mut self, class_name: S) -> Self {
self.platform_specific.class_name = class_name.into();
+2
View File
@@ -29,6 +29,7 @@ pub struct PlatformSpecificWindowAttributes {
pub no_redirection_bitmap: bool,
pub drag_and_drop: bool,
pub skip_taskbar: bool,
pub tool_window: bool,
pub class_name: String,
pub decoration_shadow: bool,
pub backdrop_type: BackdropType,
@@ -48,6 +49,7 @@ impl Default for PlatformSpecificWindowAttributes {
no_redirection_bitmap: false,
drag_and_drop: true,
skip_taskbar: false,
tool_window: false,
class_name: "Window Class".to_string(),
decoration_shadow: false,
backdrop_type: BackdropType::default(),
+11 -2
View File
@@ -1019,7 +1019,12 @@ impl Window {
#[inline]
pub fn set_skip_taskbar(&self, skip: bool) {
self.window_state_lock().skip_taskbar = skip;
let skip = {
let mut window_state = self.window_state_lock();
let skip = window_state.window_flags.skip_taskbar(skip);
window_state.skip_taskbar = skip;
skip
};
unsafe { set_skip_taskbar(self.hwnd(), skip) };
}
@@ -1290,7 +1295,10 @@ impl InitData<'_> {
unsafe { DeleteObject(region) };
}
win.set_skip_taskbar(self.attributes.platform_specific.skip_taskbar);
win.set_skip_taskbar(
self.attributes.platform_specific.skip_taskbar
|| self.attributes.platform_specific.tool_window,
);
win.set_window_icon(self.attributes.window_icon.clone());
win.set_taskbar_icon(self.attributes.platform_specific.taskbar_icon.clone());
@@ -1367,6 +1375,7 @@ unsafe fn init(
.set(WindowFlags::NO_BACK_BUFFER, attributes.platform_specific.no_redirection_bitmap);
window_flags.set(WindowFlags::MARKER_ACTIVATE, attributes.active);
window_flags.set(WindowFlags::TRANSPARENT, attributes.transparent);
window_flags.set(WindowFlags::TOOL_WINDOW, attributes.platform_specific.tool_window);
// WindowFlags::VISIBLE and MAXIMIZED are set down below after the window has been configured.
window_flags.set(WindowFlags::RESIZABLE, attributes.resizable);
// Will be changed later using `window.set_enabled_buttons` but we need to set a default here
+61 -3
View File
@@ -17,8 +17,9 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{
SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, SW_RESTORE, SW_SHOW, SW_SHOWNOACTIVATE, WINDOWPLACEMENT,
WINDOW_EX_STYLE, WINDOW_STYLE, WS_BORDER, WS_CAPTION, WS_CHILD, WS_CLIPCHILDREN,
WS_CLIPSIBLINGS, WS_EX_ACCEPTFILES, WS_EX_APPWINDOW, WS_EX_LAYERED, WS_EX_NOREDIRECTIONBITMAP,
WS_EX_TOPMOST, WS_EX_TRANSPARENT, WS_EX_WINDOWEDGE, WS_MAXIMIZE, WS_MAXIMIZEBOX, WS_MINIMIZE,
WS_MINIMIZEBOX, WS_OVERLAPPEDWINDOW, WS_POPUP, WS_SIZEBOX, WS_SYSMENU, WS_VISIBLE,
WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT, WS_EX_WINDOWEDGE, WS_MAXIMIZE,
WS_MAXIMIZEBOX, WS_MINIMIZE, WS_MINIMIZEBOX, WS_OVERLAPPEDWINDOW, WS_POPUP, WS_SIZEBOX,
WS_SYSMENU, WS_VISIBLE,
};
/// Contains information about states and the window that the callback is going to use.
@@ -125,6 +126,7 @@ bitflags! {
const MARKER_ACTIVATE = 1 << 21;
const CLIP_CHILDREN = 1 << 22;
const TOOL_WINDOW = 1 << 23;
const EXCLUSIVE_FULLSCREEN_OR_MASK = WindowFlags::ALWAYS_ON_TOP.bits();
}
@@ -249,6 +251,13 @@ impl MouseProperties {
}
impl WindowFlags {
/// Returns whether a window with these flags is kept off the taskbar when `skip` is requested.
///
/// A tool window always stays off the taskbar, so `skip == false` does not add it.
pub fn skip_taskbar(self, skip: bool) -> bool {
skip || self.contains(WindowFlags::TOOL_WINDOW)
}
fn mask(mut self) -> WindowFlags {
if self.contains(WindowFlags::MARKER_EXCLUSIVE_FULLSCREEN) {
self |= WindowFlags::EXCLUSIVE_FULLSCREEN_OR_MASK;
@@ -273,7 +282,9 @@ impl WindowFlags {
if self.contains(WindowFlags::VISIBLE) {
style |= WS_VISIBLE;
}
if self.contains(WindowFlags::ON_TASKBAR) {
if self.contains(WindowFlags::TOOL_WINDOW) {
style_ex |= WS_EX_TOOLWINDOW;
} else if self.contains(WindowFlags::ON_TASKBAR) {
style_ex |= WS_EX_APPWINDOW;
}
if self.contains(WindowFlags::ALWAYS_ON_TOP) {
@@ -557,3 +568,50 @@ impl CursorFlags {
Ok(())
}
}
#[cfg(test)]
mod tool_window_tests {
use super::WindowFlags;
use crate::platform::windows::WindowAttributesExtWindows;
use crate::window::WindowAttributes;
use windows_sys::Win32::UI::WindowsAndMessaging::{
WS_EX_APPWINDOW, WS_EX_TOOLWINDOW, WS_SIZEBOX,
};
#[test]
fn tool_window_attributes_are_opt_in() {
let defaults = WindowAttributes::default();
assert!(!defaults.platform_specific.tool_window);
let attributes = defaults.with_tool_window(true).with_resizable(true);
assert!(attributes.platform_specific.tool_window);
assert!(attributes.resizable);
assert!(!attributes.with_tool_window(false).platform_specific.tool_window);
}
#[test]
fn tool_window_styles_keep_resizing_without_forcing_a_taskbar_entry() {
let normal = WindowFlags::ON_TASKBAR | WindowFlags::RESIZABLE;
let (style, extended) = (normal | WindowFlags::TOOL_WINDOW).to_window_styles();
assert_ne!(extended & WS_EX_TOOLWINDOW, 0);
assert_eq!(extended & WS_EX_APPWINDOW, 0);
assert_ne!(style & WS_SIZEBOX, 0);
let (style, extended) = normal.to_window_styles();
assert_eq!(extended & WS_EX_TOOLWINDOW, 0);
assert_ne!(extended & WS_EX_APPWINDOW, 0);
assert_ne!(style & WS_SIZEBOX, 0);
}
#[test]
fn tool_window_stays_off_the_taskbar_after_skip_taskbar_false() {
let tool = WindowFlags::ON_TASKBAR | WindowFlags::TOOL_WINDOW;
assert!(tool.skip_taskbar(false));
assert!(tool.skip_taskbar(true));
}
#[test]
fn normal_window_toggles_skip_taskbar() {
let normal = WindowFlags::ON_TASKBAR | WindowFlags::RESIZABLE;
assert!(!normal.skip_taskbar(false));
assert!(normal.skip_taskbar(true));
}
}