Skip to content

Commit

Permalink
fix(Windows): fix icons specified on WindowBuilder not taking effec…
Browse files Browse the repository at this point in the history
…t for windows created after the first one (#604)

* fix(Windows): fix `WindowBuilder::with_icons` for second windows, closes #603

* Update second-window-icon-builder.md
  • Loading branch information
amrbashir committed Oct 27, 2022
1 parent 2edc741 commit d72b1e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changes/second-window-icon-builder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

On Windows, fix icons specified on `WindowBuilder` not taking effect for windows created after the firt one.
24 changes: 7 additions & 17 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ unsafe fn init<T: 'static>(
event_loop: &EventLoopWindowTarget<T>,
) -> Result<Window, RootOsError> {
// registering the window class
let class_name = register_window_class(&attributes.window_icon, &pl_attribs.taskbar_icon);
let class_name = register_window_class();

let mut window_flags = WindowFlags::empty();
window_flags.set(WindowFlags::DECORATIONS, attributes.decorations);
Expand Down Expand Up @@ -1026,7 +1026,7 @@ unsafe fn init<T: 'static>(
let window_state = {
let window_state = WindowState::new(
&attributes,
pl_attribs.taskbar_icon,
None,
scale_factor,
current_theme,
pl_attribs.preferred_theme,
Expand All @@ -1048,6 +1048,8 @@ unsafe fn init<T: 'static>(
.insert(win.id(), KeyEventBuilder::default());

win.set_skip_taskbar(pl_attribs.skip_taskbar);
win.set_window_icon(attributes.window_icon);
win.set_taskbar_icon(pl_attribs.taskbar_icon);

if attributes.fullscreen.is_some() {
win.set_fullscreen(attributes.fullscreen);
Expand Down Expand Up @@ -1102,34 +1104,22 @@ unsafe fn init<T: 'static>(
Ok(win)
}

unsafe fn register_window_class(
window_icon: &Option<Icon>,
taskbar_icon: &Option<Icon>,
) -> Vec<u16> {
unsafe fn register_window_class() -> Vec<u16> {
let class_name = util::encode_wide("Window Class");

let h_icon = taskbar_icon
.as_ref()
.map(|icon| icon.inner.as_raw_handle())
.unwrap_or_default();
let h_icon_small = window_icon
.as_ref()
.map(|icon| icon.inner.as_raw_handle())
.unwrap_or_default();

let class = WNDCLASSEXW {
cbSize: mem::size_of::<WNDCLASSEXW>() as u32,
style: CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
lpfnWndProc: Some(window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: GetModuleHandleW(PCWSTR::null()).unwrap_or_default(),
hIcon: h_icon,
hIcon: HICON::default(),
hCursor: HCURSOR::default(), // must be null in order for cursor state to work properly
hbrBackground: HBRUSH::default(),
lpszMenuName: PCWSTR::null(),
lpszClassName: PCWSTR::from_raw(class_name.as_ptr()),
hIconSm: h_icon_small,
hIconSm: HICON::default(),
};

// We ignore errors because registering the same window class twice would trigger
Expand Down

0 comments on commit d72b1e1

Please sign in to comment.