Skip to content
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
36 changes: 36 additions & 0 deletions crates/core/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,42 @@ mod tests {
}
}

/// In the window list these cover both displays, so the Character fades
/// and Come back cannot bring it back while that rule stays true.
#[test]
fn display_sized_overlays_hide_the_character_until_they_leave_the_list() {
let displays = [
rect(0.0, 0.0, 3440.0, 1440.0),
rect(-1200.0, -209.0, 1200.0, 1920.0),
];
assert!(
fullscreen_frontmost(&displays, &displays),
"each overlay covers the display it was built for"
);

let mut rules = HideRules::default();
assert_eq!(
rules.update(Desktop {
fullscreen_frontmost: true,
}),
faded_out()
);
rules.toggle();
rules.toggle();
assert_eq!(
rules.update(Desktop {
fullscreen_frontmost: true,
}),
None,
"come back does not outrank a fullscreen rule that is still true"
);
assert!(!rules.presence().visible);

assert!(!fullscreen_frontmost(&[], &displays));
assert_eq!(rules.update(Desktop::default()), faded_in());
assert!(rules.presence().visible);
}

#[test]
fn a_window_covering_its_whole_display_is_a_fullscreen_application() {
assert!(fullscreen_frontmost(
Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/frame_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,7 @@ pub(crate) fn run_frame_loop(
&& ignoring[index] != Some(false)
{
flipped = true;
if window.set_ignore_cursor_events(false).is_ok() {
if platform::set_overlay_click_through(&window, false).is_ok() {
ignoring[index] = Some(false);
}
}
Expand Down Expand Up @@ -2132,7 +2132,7 @@ pub(crate) fn run_frame_loop(

if ignoring[index] != Some(true) {
flipped = true;
if window.set_ignore_cursor_events(true).is_ok() {
if platform::set_overlay_click_through(&window, true).is_ok() {
ignoring[index] = Some(true);
}
}
Expand Down Expand Up @@ -2161,7 +2161,7 @@ pub(crate) fn run_frame_loop(

if ignoring[index] != Some(true) {
flipped = true;
if window.set_ignore_cursor_events(true).is_ok() {
if platform::set_overlay_click_through(&window, true).is_ok() {
ignoring[index] = Some(true);
}
}
Expand All @@ -2175,7 +2175,7 @@ pub(crate) fn run_frame_loop(
// forever, leaving click-through stuck.
if ignoring[index] != Some(ignore) {
flipped = true;
if window.set_ignore_cursor_events(ignore).is_ok() {
if platform::set_overlay_click_through(&window, ignore).is_ok() {
ignoring[index] = Some(ignore);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ fn build_overlay(
// None until realize. The frame loop sets ignore-cursor on the first
// frame. On macOS, NSWindow exists while hidden, so the call is safe here.
#[cfg(not(all(unix, not(target_os = "macos"))))]
window.set_ignore_cursor_events(true)?;
platform::set_overlay_click_through(&window, true)?;

cover_display(&window, display)?;
// Show the window first so GTK realizes it and creates the native handle.
Expand Down
19 changes: 19 additions & 0 deletions src-tauri/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,25 @@ pub fn configure_overlay(window: &tauri::WebviewWindow) -> Result<(), String> {
windows::configure_overlay(window)
}

/// Click-through for one overlay.
/// On Windows the tool-window bits are put back after tao's style rewrite,
/// on the event-loop thread, or that rewrite lands last and clears them.
pub fn set_overlay_click_through(
window: &tauri::WebviewWindow,
ignore: bool,
) -> Result<(), String> {
#[cfg(unix)]
{
window
.set_ignore_cursor_events(ignore)
.map_err(|e| e.to_string())
}
#[cfg(not(unix))]
{
windows::set_click_through(window, ignore)
}
}

/// Update the input region for the overlay window based on the sprite's alpha mask.
/// X11 and Windows then union hotspot rects so a control drawn outside the
/// art still receives clicks. macOS uses `set_ignore_cursor_events`.
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod settings_raise;
mod window_source;

pub(super) use console::{restore_ctrl_c, suppress_ctrl_c_for_children};
pub use overlay::{configure_overlay, update_input_region};
pub use overlay::{configure_overlay, set_click_through, update_input_region};
pub use pointer::{buttons_down, double_click_interval_ms};
pub use sensing::WindowsActivitySource;
pub(super) use settings_raise::{lower_to_normal_level, raise_above_overlay};
Expand Down
166 changes: 113 additions & 53 deletions src-tauri/src/platform/windows/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,88 @@
//! outside the art still receives clicks. WDA_EXCLUDEFROMCAPTURE applies only
//! when the capturable setting, on by default (ADR-0024), is turned off.

use std::sync::Mutex;
use std::time::Instant;

use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::Graphics::Gdi::{CreateRectRgn, DeleteObject, SetWindowRgn, HRGN, RGN_OR};
use windows_sys::Win32::System::Threading::GetCurrentThreadId;
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetWindowLongW, SetWindowDisplayAffinity, SetWindowLongW, SetWindowPos, GWL_EXSTYLE,
HWND_TOPMOST, SWP_FRAMECHANGED, SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER, WDA_EXCLUDEFROMCAPTURE,
WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT,
HWND_TOPMOST, SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER,
WDA_EXCLUDEFROMCAPTURE, WS_EX_TRANSPARENT,
};

/// Float above other windows, non-activating. Capturable unless Presence or
/// `FIDGET_CAPTURABLE=0` excludes it from shares.
/// Returns Err when the handle is not realized yet, so the caller can retry.
pub fn configure_overlay(window: &tauri::WebviewWindow) -> Result<(), String> {
let raw_window_handle = match window.window_handle() {
Ok(handle) => handle,
Err(e) => {
return Err(format!("Window handle not available yet: {}", e));
}
};

let hwnd = match raw_window_handle.as_raw() {
RawWindowHandle::Win32(win32_window) => win32_window.hwnd.get() as HWND,
_ => {
return Err("Not a Windows window handle".to_string());
}
};

let hwnd = overlay_hwnd(window)?;
set_window_styles(hwnd)?;
set_window_topmost(hwnd)?;
apply_capture_exclusion(hwnd)?;
note_overlay(hwnd as u64);

Ok(())
}

/// Click-through, then put the tool-window bits back.
/// Off the event-loop thread tao posts the style rewrite and returns first,
/// so both steps run there, rewrite first.
pub fn set_click_through(window: &tauri::WebviewWindow, ignore: bool) -> Result<(), String> {
if event_loop_thread() {
return apply_click_through(window, ignore);
}
let window = window.clone();
window
.clone()
.run_on_main_thread(move || {
if let Err(why) = apply_click_through(&window, ignore) {
eprintln!("overlay: click-through restore failed: {why}");
}
})
.map_err(|e| e.to_string())
}

fn apply_click_through(window: &tauri::WebviewWindow, ignore: bool) -> Result<(), String> {
window
.set_ignore_cursor_events(ignore)
.map_err(|e| e.to_string())?;
reinforce_overlay(window)
}

/// Setup builds overlays on the event-loop thread, before the frame loop.
fn event_loop_thread() -> bool {
static EVENT_LOOP: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
// SAFETY: GetCurrentThreadId only reads the calling thread.
let current = unsafe { GetCurrentThreadId() };
*EVENT_LOOP.get_or_init(|| current) == current
}

/// Overlay hwnds. The window-list poll runs off this thread and has no other
/// handle for the windows built here.
static OVERLAY_HWNDS: Mutex<Vec<u64>> = Mutex::new(Vec::new());

pub(super) fn overlay_hwnds() -> Vec<u64> {
OVERLAY_HWNDS
.lock()
.map(|hwnds| hwnds.clone())
.unwrap_or_default()
}

fn note_overlay(hwnd: u64) {
if hwnd == 0 {
return;
}
let Ok(mut hwnds) = OVERLAY_HWNDS.lock() else {
return;
};
if !hwnds.contains(&hwnd) {
hwnds.push(hwnd);
}
}

/// SetWindowRgn carves the click-through region from the sprite's alpha mask.
/// `None` is WS_EX_TRANSPARENT (whole window). `Some` is the opaque pixels
/// plus hotspots, with WS_EX_TRANSPARENT removed so clicks hit that region.
Expand All @@ -55,19 +101,7 @@ pub fn update_input_region(
scale: i32,
hotspot_rects: &[[i32; 4]],
) -> Result<(), String> {
let raw_window_handle = match window.window_handle() {
Ok(handle) => handle,
Err(e) => {
return Err(format!("Window handle not available yet: {}", e));
}
};

let hwnd = match raw_window_handle.as_raw() {
RawWindowHandle::Win32(win32_window) => win32_window.hwnd.get() as HWND,
_ => {
return Err("Not a Windows window handle".to_string());
}
};
let hwnd = overlay_hwnd(window)?;

if let Some(mask) = mask_data {
apply_input_mask(
Expand All @@ -87,35 +121,61 @@ pub fn update_input_region(
}

fn set_window_styles(hwnd: HWND) -> Result<(), String> {
// SAFETY: hwnd is a valid HWND from Tauri. SetWindowLongW returns 0 on
// error or when the previous value was 0; disambiguate by checking equality.
// SAFETY: hwnd is a live HWND from Tauri.
unsafe {
let current_style = GetWindowLongW(hwnd, GWL_EXSTYLE);
let new_style = current_style
| (WS_EX_NOACTIVATE as i32)
| (WS_EX_TOPMOST as i32)
| (WS_EX_TOOLWINDOW as i32)
| (WS_EX_TRANSPARENT as i32)
| (WS_EX_LAYERED as i32);

if SetWindowLongW(hwnd, GWL_EXSTYLE, new_style) == 0 && current_style != new_style {
return Err("Failed to set extended window styles".to_string());
}
let new_style = super::super::windows_perch::restore_overlay_exstyle(current_style)
| (WS_EX_TRANSPARENT as i32);
apply_exstyle(hwnd, current_style, new_style)?;
}
Ok(())
}

if SetWindowPos(
hwnd,
HWND_TOPMOST,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED,
) == 0
{
return Err("Failed to apply topmost flag".to_string());
}
/// Put the tool-window bits back after a click-through rewrite drops them.
fn reinforce_overlay(window: &tauri::WebviewWindow) -> Result<(), String> {
let hwnd = overlay_hwnd(window)?;
// SAFETY: hwnd comes from the window's raw handle, valid for this call.
unsafe {
let current_style = GetWindowLongW(hwnd, GWL_EXSTYLE);
let new_style = super::super::windows_perch::restore_overlay_exstyle(current_style);
apply_exstyle(hwnd, current_style, new_style)?;
}
note_overlay(hwnd as u64);
Ok(())
}

fn overlay_hwnd(window: &tauri::WebviewWindow) -> Result<HWND, String> {
let raw_window_handle = window
.window_handle()
.map_err(|e| format!("Window handle not available yet: {e}"))?;
match raw_window_handle.as_raw() {
RawWindowHandle::Win32(win32_window) => Ok(win32_window.hwnd.get() as HWND),
_ => Err("Not a Windows window handle".to_string()),
}
}

/// SAFETY: caller holds a live HWND. SetWindowLongW returns 0 both on error
/// and when the previous value was 0.
unsafe fn apply_exstyle(hwnd: HWND, current_style: i32, new_style: i32) -> Result<(), String> {
if new_style == current_style {
return Ok(());
}
if SetWindowLongW(hwnd, GWL_EXSTYLE, new_style) == 0 && current_style != 0 {
return Err("Failed to set extended window styles".to_string());
}
// FRAMECHANGED is what makes the extended-style write take effect.
if SetWindowPos(
hwnd,
HWND_TOPMOST,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_FRAMECHANGED,
) == 0
{
return Err("Failed to apply overlay extended styles".to_string());
}
Ok(())
}

Expand Down
15 changes: 8 additions & 7 deletions src-tauri/src/platform/windows/window_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{
GWL_STYLE,
};

use super::super::windows_perch::perch_candidate;
use super::super::windows_perch::counts_for_desktop;
use super::overlay::overlay_hwnds;
use super::process::window_owner;

/// The Windows window manager's view of the desktop.
Expand Down Expand Up @@ -82,7 +83,7 @@ impl WindowSource for WindowsWindowSource {

/// Visible application windows, frontmost first (z-order).
fn visible_windows(can_read_names: bool) -> Vec<WindowRect> {
let state = Mutex::new((Vec::new(), can_read_names));
let state = Mutex::new((Vec::new(), can_read_names, overlay_hwnds()));

// SAFETY: EnumWindows takes a callback and a pointer-sized parameter. The
// callback's signature matches the required WNDENUMPROC ABI. The state
Expand Down Expand Up @@ -110,19 +111,19 @@ pub fn visible_window_titles() -> Vec<WindowTitle> {
/// SAFETY: hwnd is valid for the call; lparam is the pointer
/// `visible_windows` passed in, still live and pointing at the Mutex.
unsafe extern "system" fn enum_window_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let state = &*(lparam as *const Mutex<(Vec<WindowRect>, bool)>);
let state = &*(lparam as *const Mutex<(Vec<WindowRect>, bool, Vec<u64>)>);

if let Ok(mut locked) = state.lock() {
let (ref mut windows, can_read_names) = *locked;
if let Some(window_rect) = window_rect(hwnd, can_read_names) {
let (ref mut windows, can_read_names, ref overlays) = *locked;
if let Some(window_rect) = window_rect(hwnd, can_read_names, overlays) {
windows.push(window_rect);
}
}

TRUE
}

fn window_rect(hwnd: HWND, can_read_names: bool) -> Option<WindowRect> {
fn window_rect(hwnd: HWND, can_read_names: bool, overlays: &[u64]) -> Option<WindowRect> {
// SAFETY: hwnd comes from EnumWindows, which guarantees it is valid for
// the callback's execution. IsWindowVisible is a simple read.
let is_window_visible = unsafe { IsWindowVisible(hwnd) } != 0;
Expand All @@ -134,7 +135,7 @@ fn window_rect(hwnd: HWND, can_read_names: bool) -> Option<WindowRect> {
// SAFETY: GetWindowLongW on GWL_EXSTYLE reads the extended style bits.
let ex_style = unsafe { GetWindowLongW(hwnd, GWL_EXSTYLE) };

if !perch_candidate(is_window_visible, style, ex_style) {
if !counts_for_desktop(is_window_visible, style, ex_style, hwnd as u64, overlays) {
return None;
}

Expand Down
Loading
Loading