Skip to content

Commit

Permalink
On Windows, add option to customize window class name (rust-windowing…
Browse files Browse the repository at this point in the history
  • Loading branch information
geraudloup authored and kchibisov committed Aug 14, 2023
1 parent 4c9522d commit 416fc45
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- On Windows, added `WindowBuilderExtWindows::with_class_name` to customize the internal class name.
- On iOS, always wake the event loop when transitioning from `ControlFlow::Poll` to `ControlFlow::Poll`.
- **Breaking:** `ActivationTokenDone` event which could be requested with the new `startup_notify` module, see its docs for more.
- On Wayland, make double clicking and moving the CSD frame more reliable.
Expand Down
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ If your PR makes notable changes to Winit's features, please update this section

## Platform
### Windows
* Setting the name of the internal window class
* Setting the taskbar icon
* Setting the parent window
* Setting a menu bar
Expand Down
9 changes: 9 additions & 0 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ pub trait WindowBuilderExtWindows {
/// Whether show or hide the window icon in the taskbar.
fn with_skip_taskbar(self, skip: bool) -> WindowBuilder;

/// Customize the window class name.
fn with_class_name<S: Into<String>>(self, class_name: S) -> WindowBuilder;

/// Shows or hides the background drop shadow for undecorated windows.
///
/// The shadow is hidden by default.
Expand Down Expand Up @@ -267,6 +270,12 @@ impl WindowBuilderExtWindows for WindowBuilder {
self
}

#[inline]
fn with_class_name<S: Into<String>>(mut self, class_name: S) -> WindowBuilder {
self.platform_specific.class_name = class_name.into();
self
}

#[inline]
fn with_undecorated_shadow(mut self, shadow: bool) -> WindowBuilder {
self.platform_specific.decoration_shadow = shadow;
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub no_redirection_bitmap: bool,
pub drag_and_drop: bool,
pub skip_taskbar: bool,
pub class_name: String,
pub decoration_shadow: bool,
}

Expand All @@ -42,6 +43,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
no_redirection_bitmap: false,
drag_and_drop: true,
skip_taskbar: false,
class_name: "Window Class".to_string(),
decoration_shadow: false,
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,8 @@ where
{
let title = util::encode_wide(&attributes.title);

let class_name = register_window_class::<T>();
let class_name = util::encode_wide(&pl_attribs.class_name);
register_window_class::<T>(&class_name);

let mut window_flags = WindowFlags::empty();
window_flags.set(WindowFlags::MARKER_DECORATIONS, attributes.decorations);
Expand Down Expand Up @@ -1187,9 +1188,7 @@ where
Ok(initdata.window.unwrap())
}

unsafe fn register_window_class<T: 'static>() -> Vec<u16> {
let class_name = util::encode_wide("Window Class");

unsafe fn register_window_class<T: 'static>(class_name: &[u16]) {
let class = WNDCLASSEXW {
cbSize: mem::size_of::<WNDCLASSEXW>() as u32,
style: CS_HREDRAW | CS_VREDRAW,
Expand All @@ -1210,8 +1209,6 @@ unsafe fn register_window_class<T: 'static>() -> Vec<u16> {
// Also since there is no weird element in the struct, there is no reason for this
// call to fail.
RegisterClassExW(&class);

class_name
}

struct ComInitialized(*mut ());
Expand Down

0 comments on commit 416fc45

Please sign in to comment.