Skip to content

Commit

Permalink
fix(Windows): conflict between taskbar and visible (rust-windowing#172)
Browse files Browse the repository at this point in the history
* Fix conflict between taskbar and visible on Windows

* Move skip_taskbar into window state

* Add more comments about skip_taskbar and visible

* Add change file
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) authored Aug 6, 2021
1 parent 1c0f527 commit 226e661
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changes/windows-taskbar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tao": minor
---

Fix confliction between `set_skip_taksbar(true)` and `set_visible(false)`.

2 changes: 1 addition & 1 deletion src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl WindowExtWindows for Window {

#[inline]
fn set_skip_taskbar(&self, skip: bool) {
self.window.set_skip_taskbar(skip);
self.window.set_skip_taskbar(skip, true);
}
}

Expand Down
71 changes: 48 additions & 23 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,29 @@ impl Window {

#[inline]
pub fn set_visible(&self, visible: bool) {
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::VISIBLE, visible)
let prev = self.is_visible();
let skip_taskbar = self.window_state.lock().skip_taskbar;
// Hidden window also skips taskbar, we need to check if it conflicts with skip_taskbar state
// If it's moving from visible to hidden, we need to unset skip_taskbar
if prev && !visible && skip_taskbar {
self.set_skip_taskbar(false, false);
}

// If it's still the same, there's no need to set it again
if prev != visible {
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::VISIBLE, visible)
});
});
});
}

// If it's moving from hidden to visible, we set skip_taskbar back
if !prev && visible && skip_taskbar {
self.set_skip_taskbar(true, false);
}
}

#[inline]
Expand Down Expand Up @@ -760,22 +776,30 @@ impl Window {
}

#[inline]
pub(crate) fn set_skip_taskbar(&self, skip: bool) {
unsafe {
let mut taskbar_list: *mut ITaskbarList = std::mem::zeroed();
CoCreateInstance(
&CLSID_TaskbarList,
std::ptr::null_mut(),
CLSCTX_SERVER,
&ITaskbarList::uuidof(),
&mut taskbar_list as *mut _ as *mut _,
);
if skip {
(*taskbar_list).DeleteTab(self.hwnd() as _);
} else {
(*taskbar_list).AddTab(self.hwnd() as _);
pub(crate) fn set_skip_taskbar(&self, skip: bool, state: bool) {
// Update self skip_taskbar state if true
if state {
let mut window_state = self.window_state.lock();
window_state.skip_taskbar = skip;
}

if self.is_visible() {
unsafe {
let mut taskbar_list: *mut ITaskbarList = std::mem::zeroed();
CoCreateInstance(
&CLSID_TaskbarList,
std::ptr::null_mut(),
CLSCTX_SERVER,
&ITaskbarList::uuidof(),
&mut taskbar_list as *mut _ as *mut _,
);
if skip {
(*taskbar_list).DeleteTab(self.hwnd() as _);
} else {
(*taskbar_list).AddTab(self.hwnd() as _);
}
(*taskbar_list).Release();
}
(*taskbar_list).Release();
}
}
}
Expand Down Expand Up @@ -906,6 +930,7 @@ unsafe fn init<T: 'static>(
scale_factor,
current_theme,
pl_attribs.preferred_theme,
pl_attribs.skip_taskbar,
);
let window_state = Arc::new(Mutex::new(window_state));
WindowState::set_window_flags(window_state.lock(), real_window.0, |f| *f = window_flags);
Expand All @@ -919,6 +944,8 @@ unsafe fn init<T: 'static>(
menu: None,
};

win.set_skip_taskbar(pl_attribs.skip_taskbar, false);

let dimensions = attributes
.inner_size
.unwrap_or_else(|| PhysicalSize::new(800, 600).into());
Expand Down Expand Up @@ -956,8 +983,6 @@ unsafe fn init<T: 'static>(
win.menu = menu::initialize(window_menu, window_handle, menu_handler).map(HMenuWrapper);
}

win.set_skip_taskbar(pl_attribs.skip_taskbar);

Ok(win)
}

Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/windows/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct WindowState {
pub ime_handler: MinimalIme,

pub window_flags: WindowFlags,

pub skip_taskbar: bool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -104,6 +106,7 @@ impl WindowState {
scale_factor: f64,
current_theme: Theme,
preferred_theme: Option<Theme>,
skip_taskbar: bool,
) -> WindowState {
WindowState {
mouse: MouseProperties {
Expand All @@ -130,6 +133,8 @@ impl WindowState {
key_event_builder: KeyEventBuilder::default(),
ime_handler: MinimalIme::default(),
window_flags: WindowFlags::empty(),

skip_taskbar,
}
}

Expand Down

0 comments on commit 226e661

Please sign in to comment.