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

feat: ensure windows overlap the taskbar when fullscreen #940

Merged
merged 2 commits into from
Jan 23, 2025
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
80 changes: 55 additions & 25 deletions packages/wm-platform/src/com.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::Context;
use windows::{
core::{ComInterface, IUnknown, IUnknown_Vtbl, GUID, HRESULT},
Win32::System::Com::{
CoCreateInstance, CoInitializeEx, CoUninitialize, IServiceProvider,
CLSCTX_ALL, COINIT_APARTMENTTHREADED,
Win32::{
System::Com::{
CoCreateInstance, CoInitializeEx, CoUninitialize, IServiceProvider,
CLSCTX_ALL, CLSCTX_SERVER, COINIT_APARTMENTTHREADED,
},
UI::Shell::{ITaskbarList2, TaskbarList},
},
};

Expand All @@ -19,7 +22,11 @@ thread_local! {
pub static COM_INIT: ComInit = ComInit::new();
}

pub struct ComInit();
pub struct ComInit {
service_provider: Option<IServiceProvider>,
application_view_collection: Option<IApplicationViewCollection>,
taskbar_list: Option<ITaskbarList2>,
}

impl ComInit {
/// Initializes COM on the current thread with apartment threading model.
Expand All @@ -34,7 +41,50 @@ impl ComInit {
unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }
.expect("Unable to initialize COM.");

Self()
let service_provider = unsafe {
CoCreateInstance(&CLSID_IMMERSIVE_SHELL, None, CLSCTX_ALL)
}
.ok();

let application_view_collection = service_provider.as_ref().and_then(
|provider: &IServiceProvider| unsafe {
provider.QueryService(&IApplicationViewCollection::IID).ok()
},
);

let taskbar_list =
unsafe { CoCreateInstance(&TaskbarList, None, CLSCTX_SERVER) }.ok();

Self {
service_provider,
application_view_collection,
taskbar_list,
}
}

/// Returns an instance of `IServiceProvider`.
pub fn service_provider(&self) -> anyhow::Result<IServiceProvider> {
self
.service_provider
.clone()
.context("Unable to create `IServiceProvider` instance.")
}

/// Returns an instance of `IApplicationViewCollection`.
pub fn application_view_collection(
&self,
) -> anyhow::Result<IApplicationViewCollection> {
self.application_view_collection.clone().context(
"Failed to query for `IApplicationViewCollection` instance.",
)
}

/// Returns an instance of `ITaskbarList2`.
pub fn taskbar_list(&self) -> anyhow::Result<ITaskbarList2> {
self
.taskbar_list
.clone()
.context("Unable to create `ITaskbarList2` instance.")
}
}

Expand All @@ -50,26 +100,6 @@ impl Drop for ComInit {
}
}

/// Returns an instance of `IServiceProvider`.
pub fn iservice_provider() -> anyhow::Result<IServiceProvider> {
COM_INIT.with(|_| unsafe {
CoCreateInstance(&CLSID_IMMERSIVE_SHELL, None, CLSCTX_ALL)
.context("Unable to create `IServiceProvider` instance.")
})
}

/// Returns an instance of `IApplicationViewCollection`.
pub fn iapplication_view_collection(
provider: &IServiceProvider,
) -> anyhow::Result<IApplicationViewCollection> {
COM_INIT.with(|_| {
unsafe { provider.QueryService(&IApplicationViewCollection::IID) }
.context(
"Failed to query for `IApplicationViewCollection` instance.",
)
})
}

/// Undocumented COM interface for Windows shell functionality.
///
/// Note that filler methods are added to match the vtable layout.
Expand Down
38 changes: 24 additions & 14 deletions packages/wm-platform/src/native_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ use windows::{
DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DEFAULT, DWMWCP_DONOTROUND,
DWMWCP_ROUND, DWMWCP_ROUNDSMALL,
},
System::{
Com::{CoCreateInstance, CLSCTX_SERVER},
Threading::{
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,
PROCESS_QUERY_LIMITED_INFORMATION,
},
System::Threading::{
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,
PROCESS_QUERY_LIMITED_INFORMATION,
},
UI::{
Input::KeyboardAndMouse::{
SendInput, INPUT, INPUT_0, INPUT_MOUSE, MOUSEINPUT,
},
Shell::{ITaskbarList, TaskbarList},
WindowsAndMessaging::{
EnumWindows, GetClassNameW, GetLayeredWindowAttributes, GetWindow,
GetWindowLongPtrW, GetWindowRect, GetWindowTextW,
Expand All @@ -47,7 +43,7 @@ use wm_common::{
Rect, RectDelta, WindowState,
};

use super::{iapplication_view_collection, iservice_provider, COM_INIT};
use super::COM_INIT;

/// Magic number used to identify programmatic mouse inputs from our own
/// process.
Expand Down Expand Up @@ -664,9 +660,8 @@ impl NativeWindow {
}

pub fn set_cloaked(&self, cloaked: bool) -> anyhow::Result<()> {
COM_INIT.with(|_| -> anyhow::Result<()> {
let view_collection =
iapplication_view_collection(&iservice_provider()?)?;
COM_INIT.with(|com_init| -> anyhow::Result<()> {
let view_collection = com_init.application_view_collection()?;

let mut view = None;
unsafe { view_collection.get_view_for_hwnd(self.handle, &mut view) }
Expand All @@ -691,9 +686,8 @@ impl NativeWindow {
&self,
visible: bool,
) -> anyhow::Result<()> {
COM_INIT.with(|_| -> anyhow::Result<()> {
let taskbar_list: ITaskbarList =
unsafe { CoCreateInstance(&TaskbarList, None, CLSCTX_SERVER)? };
COM_INIT.with(|com_init| -> anyhow::Result<()> {
let taskbar_list = com_init.taskbar_list()?;

if visible {
unsafe { taskbar_list.AddTab(HWND(self.handle))? };
Expand Down Expand Up @@ -814,6 +808,22 @@ impl NativeWindow {
Ok(())
}

/// Marks the window as fullscreen.
///
/// Causes the native Windows taskbar to be moved to the bottom of the
/// z-order when this window is active.
pub fn mark_fullscreen(&self, fullscreen: bool) -> anyhow::Result<()> {
COM_INIT.with(|com_init| -> anyhow::Result<()> {
let taskbar_list = com_init.taskbar_list()?;

unsafe {
taskbar_list.MarkFullscreenWindow(HWND(self.handle), fullscreen)
}?;

Ok(())
})
}

pub fn set_z_order(&self, z_order: &ZOrder) -> anyhow::Result<()> {
let z_order = match z_order {
ZOrder::TopMost => HWND_TOPMOST,
Expand Down
20 changes: 20 additions & 0 deletions packages/wm/src/commands/general/platform_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ fn windows_to_bring_to_front(
Ok(windows_to_bring_to_front)
}

#[allow(clippy::too_many_lines)]
fn redraw_containers(
focused_container: &Container,
state: &mut WmState,
Expand Down Expand Up @@ -278,6 +279,25 @@ fn redraw_containers(
warn!("Failed to set window position: {}", err);
}

// Whether the window is either transitioning to or from fullscreen.
// TODO: This check can be improved since `prev_state` can be
// fullscreen without it needing to be marked as not fullscreen.
let is_transitioning_fullscreen =
match (window.prev_state(), window.state()) {
(Some(_), WindowState::Fullscreen(s)) if !s.maximized => true,
(Some(WindowState::Fullscreen(_)), _) => true,
_ => false,
};

if is_transitioning_fullscreen {
if let Err(err) = window.native().mark_fullscreen(matches!(
window.state(),
WindowState::Fullscreen(_)
)) {
warn!("Failed to mark window as fullscreen: {}", err);
}
}

// Skip setting taskbar visibility if the window is hidden (has no
// effect). Since cloaked windows are normally always visible in the
// taskbar, we only need to set visibility if `show_all_in_taskbar` is
Expand Down
Loading