Skip to content

Commit

Permalink
refactor: improve set_skip_taskbar impl on Windows (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Dec 2, 2021
1 parent 196f22a commit 5c583eb
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ parking_lot = "0.11"
unicode-segmentation = "1.8.0"

[target."cfg(target_os = \"windows\")".dependencies.windows]
version = "0.25.0"

This comment has been minimized.

Copy link
@lucasfernog

lucasfernog Dec 3, 2021

Member

Can we also update wry's windows to 0.28? we need to match it

This comment has been minimized.

Copy link
@amrbashir

amrbashir Dec 3, 2021

Author Member

I have a PR to update webview2-rs crate first but got stuck, I will revert in tao for now.

version = "0.28.0"
features = [
"alloc",
"build",
"Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/windows/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Clipboard {
println!(
"failed to set clipboard for fmt {}, error: {}",
&format.identifier,
windows::runtime::Error::from_win32().code().0
windows::core::Error::from_win32().code().0
);
}
}
Expand All @@ -89,7 +89,7 @@ fn register_identifier(ident: &str) -> Option<u32> {
unsafe {
let pb_format = RegisterClipboardFormatA(ident);
if pb_format == 0 {
let err = windows::runtime::Error::from_win32().code().0;
let err = windows::core::Error::from_win32().code().0;
println!(
"failed to register clipboard format '{}'; error {}.",
ident, err
Expand Down
10 changes: 5 additions & 5 deletions src/platform_impl/windows/drop_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf, ptr};

use windows::{
self as Windows,
runtime::implement,
core::implement,
Win32::{
Foundation::{self as win32f, HWND, POINTL, PWSTR},
System::{
Expand Down Expand Up @@ -46,7 +46,7 @@ impl FileDropHandler {
_grfKeyState: u32,
_pt: POINTL,
pdwEffect: *mut u32,
) -> windows::runtime::Result<()> {
) -> windows::core::Result<()> {
use crate::event::WindowEvent::HoveredFile;
let hdrop = Self::iterate_filenames(pDataObj, |filename| {
(self.send_event)(Event::WindowEvent {
Expand All @@ -69,12 +69,12 @@ impl FileDropHandler {
_grfKeyState: u32,
_pt: POINTL,
pdwEffect: *mut u32,
) -> windows::runtime::Result<()> {
) -> windows::core::Result<()> {
*pdwEffect = self.cursor_effect;
Ok(())
}

unsafe fn DragLeave(&self) -> windows::runtime::Result<()> {
unsafe fn DragLeave(&self) -> windows::core::Result<()> {
use crate::event::WindowEvent::HoveredFileCancelled;
if self.hovered_is_valid {
(self.send_event)(Event::WindowEvent {
Expand All @@ -91,7 +91,7 @@ impl FileDropHandler {
_grfKeyState: u32,
_pt: POINTL,
_pdwEffect: *mut u32,
) -> windows::runtime::Result<()> {
) -> windows::core::Result<()> {
use crate::event::WindowEvent::DroppedFile;
let hdrop = Self::iterate_filenames(pDataObj, |filename| {
(self.send_event)(Event::WindowEvent {
Expand Down
8 changes: 4 additions & 4 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use windows::Win32::{
System::{
LibraryLoader::GetModuleHandleW,
Ole::{IDropTarget, RevokeDragDrop},
Threading::{GetCurrentThreadId, MsgWaitForMultipleObjectsEx, MWMO_INPUTAVAILABLE},
Threading::GetCurrentThreadId,
WindowsProgramming::INFINITE,
},
UI::{
Expand Down Expand Up @@ -349,7 +349,7 @@ fn wait_thread(parent_thread_id: u32, msg_window_id: HWND) {
let mut wait_until_opt = None;
'main: loop {
// Zeroing out the message ensures that the `WaitUntilInstantBox` doesn't get
// double-freed if `MsgWaitForMultipleObjectsEx` returns early and there aren't
// double-freed if `WaitForMultipleObjectsEx` returns early and there aren't
// additional messages to process.
msg = MSG::default();

Expand Down Expand Up @@ -377,12 +377,12 @@ fn wait_thread(parent_thread_id: u32, msg_window_id: HWND) {
// MsgWaitForMultipleObjects tends to overshoot just a little bit. We subtract
// 1 millisecond from the requested time and spinlock for the remainder to
// compensate for that.
let resume_reason = WIN32_ERROR(MsgWaitForMultipleObjectsEx(
let resume_reason = WIN32_ERROR(win32wm::MsgWaitForMultipleObjectsEx(
0,
ptr::null(),
dur2timeout(wait_until - now).saturating_sub(1),
QS_ALLEVENTS,
MWMO_INPUTAVAILABLE,
win32wm::MWMO_INPUTAVAILABLE,
));
if resume_reason == WAIT_TIMEOUT {
PostMessageW(
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
use crate::{dpi::PhysicalSize, window::CursorIcon};

use windows::{
runtime::HRESULT,
core::HRESULT,
Win32::{
Foundation::{BOOL, FARPROC, HWND, LPARAM, LRESULT, POINT, PWSTR, RECT, WPARAM},
Globalization::lstrlenW,
Expand Down
36 changes: 11 additions & 25 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,13 @@ impl Window {

#[inline]
pub fn set_visible(&self, visible: bool) {
let skip_taskbar = self.window_state.lock().skip_taskbar;
let already_skipped = self.window_state.lock().already_skipped;

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 visible && skip_taskbar != already_skipped {
self.set_skip_taskbar(skip_taskbar);
}
}

#[inline]
Expand Down Expand Up @@ -756,23 +749,17 @@ impl Window {

#[inline]
pub(crate) fn set_skip_taskbar(&self, skip: bool) {
let mut window_state = self.window_state.lock();
window_state.skip_taskbar = skip;

if self.is_visible() {
unsafe {
let taskbar_list: ITaskbarList = CoCreateInstance(&TaskbarList, None, CLSCTX_SERVER)
.expect("failed to create TaskBarList");
if skip {
taskbar_list
.DeleteTab(self.hwnd())
.expect("DeleteTab failed");
} else {
taskbar_list.AddTab(self.hwnd()).expect("AddTab failed");
}
unsafe {
com_initialized();
let taskbar_list: ITaskbarList =
CoCreateInstance(&TaskbarList, None, CLSCTX_SERVER).expect("failed to create TaskBarList");
if skip {
taskbar_list
.DeleteTab(self.hwnd())
.expect("DeleteTab failed");
} else {
taskbar_list.AddTab(self.hwnd()).expect("AddTab failed");
}

window_state.already_skipped = skip
}
}
}
Expand Down Expand Up @@ -901,7 +888,6 @@ 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 Down Expand Up @@ -1075,7 +1061,7 @@ unsafe fn taskbar_mark_fullscreen(handle: HWND, fullscreen: bool) {
let mut task_bar_list = task_bar_list_ptr.borrow().clone();

if task_bar_list.is_none() {
let result: windows::runtime::Result<ITaskbarList2> =
let result: windows::core::Result<ITaskbarList2> =
CoCreateInstance(&TaskbarList, None, CLSCTX_ALL);
if let Ok(created) = result {
if let Ok(()) = created.HrInit() {
Expand Down
7 changes: 0 additions & 7 deletions src/platform_impl/windows/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ pub struct WindowState {
pub ime_handler: MinimalIme,

pub window_flags: WindowFlags,

pub skip_taskbar: bool,
pub already_skipped: bool,
}

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

skip_taskbar,
already_skipped: false,
}
}

Expand Down

0 comments on commit 5c583eb

Please sign in to comment.