Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve set_skip_taskbar impl on Windows #250

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
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