Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Fix multi-monitor window placement on mixed-DPI setups

Richie McIlroy92d82f8c39bb
diff --git a/apps/desktop/src-tauri/src/fake_window.rs b/apps/desktop/src-tauri/src/fake_window.rs
index 773d882fe..568094ebf 100644
--- a/apps/desktop/src-tauri/src/fake_window.rs
+++ b/apps/desktop/src-tauri/src/fake_window.rs
@@ -355,8 +355,10 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
None => true,
};
if changed {
- let _ =
- window.set_position(tauri::LogicalPosition::new(px, py));
+ let _ = window
+ .set_position(crate::windows::logical_point_position(
+ px, py,
+ ));
last_target_pos = Some((px, py));
}
handled = true;
@@ -376,7 +378,8 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
&& let Some(display) = get_display_by_id(&cursor_display_id)
&& let Some((pos_x, pos_y)) = calculate_bottom_center_position(&display)
{
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ =
+ window.set_position(crate::windows::logical_point_position(pos_x, pos_y));
current_display_id = Some(cursor_display_id);
}
}
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index f17290e05..826fb2bfd 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -1563,23 +1563,38 @@ fn display_for_position(pos_x: f64, pos_y: f64) -> Option<Display> {
})
}
-fn display_id_for_position(pos_x: f64, pos_y: f64) -> Option<DisplayId> {
- display_for_position(pos_x, pos_y).map(|display| display.id())
-}
-
-fn monitor_name_for_position(pos_x: f64, pos_y: f64) -> Option<String> {
- display_for_position(pos_x, pos_y)
- .and_then(|display| display.name())
- .filter(|name| !name.trim().is_empty())
+// On Windows the per-monitor logical rects overlap in mixed-DPI layouts, so a
+// window's display must be resolved from its physical position; logical
+// containment (display_for_position) can match the wrong monitor there.
+#[cfg(windows)]
+fn display_for_physical_position(pos_x: f64, pos_y: f64) -> Option<Display> {
+ Display::list().into_iter().find(|display| {
+ display
+ .raw_handle()
+ .physical_bounds()
+ .is_some_and(|bounds| {
+ pos_x >= bounds.position().x()
+ && pos_x < bounds.position().x() + bounds.size().width()
+ && pos_y >= bounds.position().y()
+ && pos_y < bounds.position().y() + bounds.size().height()
+ })
+ })
}
pub(crate) fn update_camera_window_position_settings(
settings: &mut GeneralSettingsStore,
x: f64,
y: f64,
+ display_id: Option<DisplayId>,
) {
- let display_id = display_id_for_position(x, y);
- let monitor_name = monitor_name_for_position(x, y);
+ let display = display_id
+ .and_then(|id| Display::from_id(&id))
+ .or_else(|| display_for_position(x, y));
+ let display_id = display.as_ref().map(|display| display.id());
+ let monitor_name = display
+ .as_ref()
+ .and_then(|display| display.name())
+ .filter(|name| !name.trim().is_empty());
let position = general_settings::WindowPosition { x, y, display_id };
settings.camera_window_position = Some(position.clone());
if let Some(monitor_name) = monitor_name {
@@ -4507,7 +4522,7 @@ fn set_camera_window_position(app: AppHandle, x: f64, y: f64) -> Result<(), Stri
}
GeneralSettingsStore::update(&app, |settings| {
- update_camera_window_position_settings(settings, x, y);
+ update_camera_window_position_settings(settings, x, y, None);
})?;
Ok(())
@@ -5789,10 +5804,20 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
if let Ok(window_id) = CapWindowId::from_str(label) {
let scale_factor = window.scale_factor().unwrap_or(1.0);
let logical_pos = position.to_logical::<f64>(scale_factor);
+
+ // Resolve which display the window landed on. On Windows this
+ // must use the physical position: mixed-DPI logical rects
+ // overlap, so logical containment can pick the wrong display.
+ #[cfg(windows)]
+ let moved_display =
+ display_for_physical_position(position.x as f64, position.y as f64);
+ #[cfg(not(windows))]
+ let moved_display = display_for_position(logical_pos.x, logical_pos.y);
+
match window_id {
CapWindowId::Main => {
let display_id =
- display_id_for_position(logical_pos.x, logical_pos.y);
+ moved_display.as_ref().map(|display| display.id());
window_position_persistence::queue_main_position(
app,
general_settings::WindowPosition {
@@ -5813,6 +5838,7 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
app,
logical_pos.x,
logical_pos.y,
+ moved_display.map(|display| display.id()),
);
}
_ => {}
diff --git a/apps/desktop/src-tauri/src/window_position_persistence.rs b/apps/desktop/src-tauri/src/window_position_persistence.rs
index 55b4b802f..2831a1bb5 100644
--- a/apps/desktop/src-tauri/src/window_position_persistence.rs
+++ b/apps/desktop/src-tauri/src/window_position_persistence.rs
@@ -1,3 +1,4 @@
+use scap_targets::DisplayId;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tauri::{AppHandle, Manager};
@@ -8,7 +9,7 @@ use crate::general_settings::{GeneralSettingsStore, WindowPosition};
#[derive(Default)]
struct PendingState {
main: Option<WindowPosition>,
- camera_position: Option<(f64, f64)>,
+ camera_position: Option<(f64, f64, Option<DisplayId>)>,
}
pub struct WindowPositionPersistence {
@@ -32,10 +33,10 @@ impl WindowPositionPersistence {
self.notify.notify_one();
}
- pub fn queue_camera(&self, x: f64, y: f64) {
+ pub fn queue_camera(&self, x: f64, y: f64, display_id: Option<DisplayId>) {
{
let mut guard = self.pending.lock().unwrap_or_else(|e| e.into_inner());
- guard.camera_position = Some((x, y));
+ guard.camera_position = Some((x, y, display_id));
}
self.notify.notify_one();
}
@@ -94,8 +95,8 @@ pub fn install(app: &AppHandle) {
if let Some(main) = pending.main {
settings.main_window_position = Some(main);
}
- if let Some((x, y)) = pending.camera_position {
- crate::update_camera_window_position_settings(settings, x, y);
+ if let Some((x, y, display_id)) = pending.camera_position {
+ crate::update_camera_window_position_settings(settings, x, y, display_id);
}
})
})
@@ -118,8 +119,8 @@ pub fn queue_main_position(app: &AppHandle, position: WindowPosition) {
}
}
-pub fn queue_camera_position(app: &AppHandle, x: f64, y: f64) {
+pub fn queue_camera_position(app: &AppHandle, x: f64, y: f64, display_id: Option<DisplayId>) {
if let Some(persistence) = app.try_state::<Arc<WindowPositionPersistence>>() {
- persistence.queue_camera(x, y);
+ persistence.queue_camera(x, y, display_id);
}
}
diff --git a/apps/desktop/src-tauri/src/windows.rs b/apps/desktop/src-tauri/src/windows.rs
index 2eecff2b2..96f644a50 100644
--- a/apps/desktop/src-tauri/src/windows.rs
+++ b/apps/desktop/src-tauri/src/windows.rs
@@ -575,12 +575,36 @@ struct CursorMonitorInfo {
y: f64,
width: f64,
height: f64,
+ // On Windows each monitor's "logical" rect is its physical rect divided by
+ // its own scale, so logical rects of mixed-DPI monitors overlap and tao's
+ // LogicalPosition conversion (which uses whatever monitor the window
+ // currently occupies) can land a window on the wrong monitor. Positioning
+ // must go through this monitor's own scale, as a physical position.
+ #[cfg(windows)]
+ scale: f64,
}
impl CursorMonitorInfo {
fn get() -> Self {
- let display = Display::get_containing_cursor().unwrap_or_else(Display::primary);
+ Self::from_display(&Display::get_containing_cursor().unwrap_or_else(Display::primary))
+ }
+
+ fn from_display(display: &Display) -> Self {
let bounds = display.raw_handle().logical_bounds();
+
+ #[cfg(windows)]
+ let scale = bounds
+ .as_ref()
+ .map(|b| b.size().width())
+ .filter(|width| *width > 0.0)
+ .and_then(|logical_width| {
+ display
+ .physical_size()
+ .map(|physical| physical.width() / logical_width)
+ })
+ .filter(|scale| scale.is_finite() && *scale > 0.0)
+ .unwrap_or(1.0);
+
let (x, y, width, height) = bounds
.map(|b| {
(
@@ -602,9 +626,26 @@ impl CursorMonitorInfo {
y,
width,
height,
+ #[cfg(windows)]
+ scale,
}
}
+ /// Converts a global-logical point on this monitor into a `Position` that
+ /// lands exactly there regardless of which monitor the window currently
+ /// occupies. Logical on macOS/Linux (a true global space there), physical
+ /// on Windows.
+ fn position(&self, x: f64, y: f64) -> tauri::Position {
+ #[cfg(windows)]
+ return tauri::Position::Physical(tauri::PhysicalPosition::new(
+ (x * self.scale).round() as i32,
+ (y * self.scale).round() as i32,
+ ));
+
+ #[cfg(not(windows))]
+ tauri::Position::Logical(tauri::LogicalPosition::new(x, y))
+ }
+
fn center_position(&self, window_width: f64, window_height: f64) -> (f64, f64) {
let pos_x = self.x + (self.width - window_width) / 2.0;
let pos_y = self.y + (self.height - window_height) / 2.0;
@@ -623,40 +664,100 @@ impl CursorMonitorInfo {
}
fn from_window(window: &tauri::WebviewWindow) -> Self {
- let window_pos = window
- .outer_position()
- .ok()
- .map(|p| (p.x as f64, p.y as f64))
- .unwrap_or((0.0, 0.0));
+ let Ok(window_pos) = window.outer_position() else {
+ return Self::get();
+ };
- for display in Display::list() {
- if let Some(bounds) = display.raw_handle().logical_bounds() {
- let (x, y, width, height) = (
- bounds.position().x(),
- bounds.position().y(),
- bounds.size().width(),
- bounds.size().height(),
- );
+ // outer_position is physical. On Windows, resolve the display in
+ // physical space (per-monitor logical rects overlap in mixed-DPI
+ // layouts); elsewhere, convert to logical, which is a global space.
+ #[cfg(windows)]
+ {
+ let (pos_x, pos_y) = (window_pos.x as f64, window_pos.y as f64);
+ for display in Display::list() {
+ if let Some(bounds) = display.raw_handle().physical_bounds() {
+ let (x, y, width, height) = (
+ bounds.position().x(),
+ bounds.position().y(),
+ bounds.size().width(),
+ bounds.size().height(),
+ );
- if window_pos.0 >= x
- && window_pos.0 < x + width
- && window_pos.1 >= y
- && window_pos.1 < y + height
- {
- return Self {
- x,
- y,
- width,
- height,
- };
+ if pos_x >= x && pos_x < x + width && pos_y >= y && pos_y < y + height {
+ return Self::from_display(&display);
+ }
}
}
+
+ Self::get()
}
- Self::get()
+ #[cfg(not(windows))]
+ {
+ let scale = window.scale_factor().unwrap_or(1.0);
+ let pos = window_pos.to_logical::<f64>(scale);
+
+ for display in Display::list() {
+ if display_contains_logical(&display, pos.x, pos.y) {
+ return Self::from_display(&display);
+ }
+ }
+
+ Self::get()
+ }
}
}
+fn display_contains_logical(display: &Display, pos_x: f64, pos_y: f64) -> bool {
+ display
+ .raw_handle()
+ .logical_bounds()
+ .map(|bounds| {
+ let (x, y, width, height) = (
+ bounds.position().x(),
+ bounds.position().y(),
+ bounds.size().width(),
+ bounds.size().height(),
+ );
+
+ pos_x >= x && pos_x < x + width && pos_y >= y && pos_y < y + height
+ })
+ .unwrap_or(false)
+}
+
+fn display_containing_logical(pos_x: f64, pos_y: f64) -> Option<Display> {
+ Display::list()
+ .into_iter()
+ .find(|display| display_contains_logical(display, pos_x, pos_y))
+}
+
+/// Resolves the display a persisted window position belongs to, preferring the
+/// display it was saved on. On Windows the saved logical coordinates are only
+/// meaningful relative to that display (mixed-DPI logical rects overlap), so
+/// restores must convert through its scale rather than the window's current one.
+fn display_for_saved_position(
+ pos_x: f64,
+ pos_y: f64,
+ display_id: Option<&DisplayId>,
+) -> Option<Display> {
+ display_id
+ .and_then(Display::from_id)
+ .filter(|display| display_contains_logical(display, pos_x, pos_y))
+ .or_else(|| display_containing_logical(pos_x, pos_y))
+}
+
+/// Converts a global-logical point into a `Position` that lands exactly there,
+/// resolving the owning display by containment when the caller doesn't know it.
+/// Falls back to a plain logical position when no display contains the point.
+pub fn logical_point_position(pos_x: f64, pos_y: f64) -> tauri::Position {
+ #[cfg(windows)]
+ if let Some(display) = display_containing_logical(pos_x, pos_y) {
+ return CursorMonitorInfo::from_display(&display).position(pos_x, pos_y);
+ }
+
+ tauri::Position::Logical(tauri::LogicalPosition::new(pos_x, pos_y))
+}
+
fn center_camera_window(app: &AppHandle, window: &WebviewWindow) {
let camera_state = match app.try_state::<ArcLock<crate::App>>() {
Some(state) => state
@@ -682,7 +783,7 @@ fn center_camera_window(app: &AppHandle, window: &WebviewWindow) {
if let Some(guard) = app.try_state::<CameraWindowPositionGuard>() {
guard.ignore_for(1000);
}
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(monitor_info.position(pos_x, pos_y));
if let Some(state) = app.try_state::<ArcLock<crate::App>>()
&& let Ok(guard) = state.try_read()
@@ -789,7 +890,7 @@ fn recenter_window_if_offscreen(window: &WebviewWindow) {
let monitor = CursorMonitorInfo::get();
let (pos_x, pos_y) =
monitor.center_position(size.width as f64 / scale, size.height as f64 / scale);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(monitor.position(pos_x, pos_y));
}
fn ensure_settings_window_bounds(window: &WebviewWindow) {
@@ -1348,7 +1449,7 @@ impl ShowCapWindow {
.unwrap_or_else(|| {
CursorMonitorInfo::get().bottom_center_position(width, height, 120.0)
});
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(logical_point_position(pos_x, pos_y));
window.show().ok();
window.set_focus().ok();
fake_window::spawn_fake_window_listener(app.clone(), window.clone());
@@ -1480,10 +1581,16 @@ impl ShowCapWindow {
.and_then(|s| s.main_window_position)
.filter(|pos| is_position_on_any_screen(pos.x, pos.y));
- let (pos_x, pos_y) = if let Some(pos) = saved_position {
- (pos.x, pos.y)
+ let main_position = if let Some(pos) = saved_position {
+ match display_for_saved_position(pos.x, pos.y, pos.display_id.as_ref()) {
+ Some(display) => {
+ CursorMonitorInfo::from_display(&display).position(pos.x, pos.y)
+ }
+ None => tauri::Position::Logical(tauri::LogicalPosition::new(pos.x, pos.y)),
+ }
} else {
- cursor_monitor.center_position(330.0, 395.0)
+ let (pos_x, pos_y) = cursor_monitor.center_position(330.0, 395.0);
+ cursor_monitor.position(pos_x, pos_y)
};
#[cfg(target_os = "macos")]
@@ -1525,7 +1632,7 @@ impl ShowCapWindow {
panel.set_level(MAIN_PANEL_LEVEL);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(main_position);
crate::platform::apply_squircle_corners(&window, 16.0);
@@ -1537,16 +1644,14 @@ impl ShowCapWindow {
#[cfg(not(target_os = "macos"))]
{
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(main_position);
#[cfg(windows)]
{
if let Err(e) = window.set_size(LogicalSize::new(330.0, 395.0)) {
warn!("Failed to set Main window size on Windows: {}", e);
}
- if let Err(e) =
- window.set_position(tauri::LogicalPosition::new(pos_x, pos_y))
- {
+ if let Err(e) = window.set_position(main_position) {
warn!("Failed to position Main window on Windows: {}", e);
}
}
@@ -1776,14 +1881,14 @@ impl ShowCapWindow {
lock_window_text_scale(&window);
let (pos_x, pos_y) = cursor_monitor.center_position(782.0, 775.0);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(cursor_monitor.position(pos_x, pos_y));
#[cfg(windows)]
{
if let Err(e) = window.set_size(LogicalSize::new(782.0, 775.0)) {
warn!("Failed to set Settings window size on Windows: {}", e);
}
- if let Err(e) = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y)) {
+ if let Err(e) = window.set_position(cursor_monitor.position(pos_x, pos_y)) {
warn!("Failed to position Settings window on Windows: {}", e);
}
}
@@ -1819,7 +1924,7 @@ impl ShowCapWindow {
lock_window_text_scale(&window);
let (pos_x, pos_y) = cursor_monitor.center_position(1275.0, 800.0);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(cursor_monitor.position(pos_x, pos_y));
#[cfg(windows)]
{
@@ -1827,7 +1932,7 @@ impl ShowCapWindow {
if let Err(e) = window.set_size(LogicalSize::new(1275.0, 800.0)) {
warn!("Failed to set Editor window size on Windows: {}", e);
}
- if let Err(e) = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y)) {
+ if let Err(e) = window.set_position(cursor_monitor.position(pos_x, pos_y)) {
warn!("Failed to position Editor window on Windows: {}", e);
}
}
@@ -1886,7 +1991,7 @@ impl ShowCapWindow {
lock_window_text_scale(&window);
let (pos_x, pos_y) = cursor_monitor.center_position(1240.0, 800.0);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(cursor_monitor.position(pos_x, pos_y));
#[cfg(windows)]
{
@@ -1897,7 +2002,7 @@ impl ShowCapWindow {
e
);
}
- if let Err(e) = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y)) {
+ if let Err(e) = window.set_position(cursor_monitor.position(pos_x, pos_y)) {
warn!(
"Failed to position ScreenshotEditor window on Windows: {}",
e
@@ -1928,7 +2033,7 @@ impl ShowCapWindow {
lock_window_text_scale(&window);
let (pos_x, pos_y) = cursor_monitor.center_position(950.0, 850.0);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(cursor_monitor.position(pos_x, pos_y));
#[cfg(windows)]
{
@@ -1936,7 +2041,7 @@ impl ShowCapWindow {
if let Err(e) = window.set_size(LogicalSize::new(950.0, 850.0)) {
warn!("Failed to set Upgrade window size on Windows: {}", e);
}
- if let Err(e) = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y)) {
+ if let Err(e) = window.set_position(cursor_monitor.position(pos_x, pos_y)) {
warn!("Failed to position Upgrade window on Windows: {}", e);
}
}
@@ -1964,7 +2069,7 @@ impl ShowCapWindow {
lock_window_text_scale(&window);
let (pos_x, pos_y) = cursor_monitor.center_position(580.0, 340.0);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(cursor_monitor.position(pos_x, pos_y));
#[cfg(windows)]
{
@@ -1972,7 +2077,7 @@ impl ShowCapWindow {
if let Err(e) = window.set_size(LogicalSize::new(580.0, 340.0)) {
warn!("Failed to set ModeSelect window size on Windows: {}", e);
}
- if let Err(e) = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y)) {
+ if let Err(e) = window.set_position(cursor_monitor.position(pos_x, pos_y)) {
warn!("Failed to position ModeSelect window on Windows: {}", e);
}
}
@@ -2004,7 +2109,7 @@ impl ShowCapWindow {
lock_window_text_scale(&window);
let (pos_x, pos_y) = cursor_monitor.center_position(width, height);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(cursor_monitor.position(pos_x, pos_y));
let _ = window.set_ignore_cursor_events(false);
#[cfg(windows)]
@@ -2013,7 +2118,7 @@ impl ShowCapWindow {
if let Err(e) = window.set_size(LogicalSize::new(width, height)) {
warn!("Failed to set Onboarding window size on Windows: {}", e);
}
- if let Err(e) = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y)) {
+ if let Err(e) = window.set_position(cursor_monitor.position(pos_x, pos_y)) {
warn!("Failed to position Onboarding window on Windows: {}", e);
}
}
@@ -2188,20 +2293,29 @@ impl ShowCapWindow {
}
});
- let (camera_pos_x, camera_pos_y) = if let Some(pos) = saved_position {
- (pos.x, pos.y)
+ let camera_position = if let Some(pos) = saved_position {
+ match display_for_saved_position(pos.x, pos.y, pos.display_id.as_ref()) {
+ Some(display) => {
+ CursorMonitorInfo::from_display(&display).position(pos.x, pos.y)
+ }
+ None => {
+ tauri::Position::Logical(tauri::LogicalPosition::new(pos.x, pos.y))
+ }
+ }
} else if *centered {
let aspect_ratio = crate::camera::WIDE_CAMERA_ASPECT_RATIO as f64;
let toolbar_height = 56.0;
let window_width = CENTERED_WINDOW_SIZE * aspect_ratio;
let window_height = CENTERED_WINDOW_SIZE + toolbar_height;
- camera_monitor.center_position(window_width, window_height)
+ let (camera_pos_x, camera_pos_y) =
+ camera_monitor.center_position(window_width, window_height);
+ camera_monitor.position(camera_pos_x, camera_pos_y)
} else {
let camera_pos_x =
camera_monitor.x + camera_monitor.width - DEFAULT_WINDOW_SIZE - 100.0;
let camera_pos_y =
camera_monitor.y + camera_monitor.height - DEFAULT_WINDOW_SIZE - 100.0;
- (camera_pos_x, camera_pos_y)
+ camera_monitor.position(camera_pos_x, camera_pos_y)
};
#[cfg(not(target_os = "macos"))]
@@ -2209,8 +2323,7 @@ impl ShowCapWindow {
if let Some(guard) = app.try_state::<CameraWindowPositionGuard>() {
guard.ignore_for(1000);
}
- let _ = window
- .set_position(tauri::LogicalPosition::new(camera_pos_x, camera_pos_y));
+ let _ = window.set_position(camera_position);
}
ensure_camera_input_active(&mut state).await;
@@ -2270,10 +2383,7 @@ impl ShowCapWindow {
if let Some(guard) = app.try_state::<CameraWindowPositionGuard>() {
guard.ignore_for(1000);
}
- let _ = window.set_position(tauri::LogicalPosition::new(
- camera_pos_x,
- camera_pos_y,
- ));
+ let _ = window.set_position(camera_position);
panel.order_front_regardless();
panel.show();
@@ -2328,23 +2438,6 @@ impl ShowCapWindow {
.title();
let should_protect = should_protect_window(app, &title);
- #[cfg(target_os = "macos")]
- let position = display.raw_handle().logical_position();
-
- #[cfg(windows)]
- let Some(position) = display.raw_handle().physical_position() else {
- warn!(screen_id = %screen_id, "Missing display position for window capture occluder");
- return Err(tauri::Error::WindowNotFound);
- };
-
- #[cfg(target_os = "linux")]
- let position = display.raw_handle().physical_position().unwrap();
-
- let Some(bounds) = display.physical_size() else {
- warn!(screen_id = %screen_id, "Missing display size for window capture occluder");
- return Err(tauri::Error::WindowNotFound);
- };
-
let mut window_builder = self
.window_builder(app, "/window-capture-occluder")
.maximized(false)
@@ -2355,13 +2448,101 @@ impl ShowCapWindow {
.visible_on_all_workspaces(true)
.content_protected(should_protect)
.skip_taskbar(true)
- .inner_size(bounds.width(), bounds.height())
- .position(position.x(), position.y())
.transparent(true);
+ #[cfg(target_os = "macos")]
+ {
+ let position = display.raw_handle().logical_position();
+ let Some(size) = display.logical_size() else {
+ warn!(screen_id = %screen_id, "Missing display logical size for window capture occluder");
+ return Err(tauri::Error::WindowNotFound);
+ };
+
+ window_builder = window_builder
+ .inner_size(size.width(), size.height())
+ .position(position.x(), position.y());
+ }
+
+ // On Windows a window's DPI scale isn't known until it's placed on a
+ // monitor, so sizing/positioning from display bounds at build time is
+ // unreliable across monitors with different DPIs. Build a placeholder
+ // and fix the geometry up after the window exists (below), mirroring
+ // the TargetSelectOverlay path.
+ #[cfg(windows)]
+ {
+ window_builder = window_builder.inner_size(100.0, 100.0).position(0.0, 0.0);
+ }
+
+ #[cfg(target_os = "linux")]
+ {
+ let position = display.raw_handle().physical_position().unwrap();
+ let Some(size) = display.physical_size() else {
+ warn!(screen_id = %screen_id, "Missing display size for window capture occluder");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ window_builder = window_builder
+ .inner_size(size.width(), size.height())
+ .position(position.x(), position.y());
+ }
+
let window = window_builder.build()?;
lock_window_text_scale(&window);
+ #[cfg(target_os = "linux")]
+ {
+ use tauri::{LogicalSize, PhysicalPosition};
+ let position = display.raw_handle().physical_position().unwrap();
+ if let Some(size) = display.physical_size() {
+ let _ =
+ window.set_position(PhysicalPosition::new(position.x(), position.y()));
+ let _ = window.set_size(LogicalSize::new(size.width(), size.height()));
+ }
+ }
+
+ // Fix up the occluder geometry now that the window exists and its real
+ // per-monitor DPI is known: position with physical coordinates (which
+ // are unambiguous across monitors), then set the logical size so the
+ // window covers the full display. Verify the resulting physical size
+ // matches the display and re-apply once if the initial placement raced
+ // the DPI change.
+ #[cfg(windows)]
+ {
+ let Some(position) = display.raw_handle().physical_position() else {
+ warn!(screen_id = %screen_id, "Missing display position for window capture occluder");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ let Some(logical_size) = display.logical_size() else {
+ warn!(screen_id = %screen_id, "Missing display logical size for window capture occluder");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ let Some(physical_size) = display.physical_size() else {
+ warn!(screen_id = %screen_id, "Missing display physical size for window capture occluder");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ use tauri::{LogicalSize, PhysicalPosition};
+ let _ = window.set_size(LogicalSize::new(
+ logical_size.width(),
+ logical_size.height(),
+ ));
+ let _ = window.set_position(PhysicalPosition::new(position.x(), position.y()));
+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;
+
+ match window.inner_size() {
+ Ok(actual_physical_size)
+ if physical_size.width() != actual_physical_size.width as f64 =>
+ {
+ let _ = window.set_size(LogicalSize::new(
+ logical_size.width(),
+ logical_size.height(),
+ ));
+ }
+ Ok(_) => {}
+ Err(err) => {
+ warn!(%err, "Failed to read window capture occluder inner size");
+ }
+ }
+ }
+
if let Err(err) = window.set_ignore_cursor_events(true) {
warn!(%err, "Failed to ignore cursor events for window capture occluder");
}
@@ -2578,7 +2759,7 @@ impl ShowCapWindow {
.as_ref()
.and_then(fake_window::calculate_recording_controls_position_for_target)
.unwrap_or_else(|| cursor_monitor.bottom_center_position(width, height, 120.0));
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ let _ = window.set_position(logical_point_position(pos_x, pos_y));
debug!(
"InProgressRecording window: cursor_monitor=({}, {}, {}, {}), pos=({}, {})",
@@ -2688,10 +2869,38 @@ impl ShowCapWindow {
.build()?;
lock_window_text_scale(&window);
- let _ = window.set_position(tauri::LogicalPosition::new(
- cursor_monitor.x,
- cursor_monitor.y,
- ));
+ let _ =
+ window.set_position(cursor_monitor.position(cursor_monitor.x, cursor_monitor.y));
+
+ // The build-time inner_size above was interpreted with the DPI of
+ // whatever monitor the window materialized on; now that it sits on the
+ // cursor monitor, re-apply the logical size so it converts with that
+ // monitor's scale, then verify against the expected physical size.
+ #[cfg(windows)]
+ {
+ let _ = window.set_size(LogicalSize::new(
+ cursor_monitor.width,
+ cursor_monitor.height,
+ ));
+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;
+
+ let expected_physical_width = (cursor_monitor.width * cursor_monitor.scale)
+ .round();
+ match window.inner_size() {
+ Ok(actual_physical_size)
+ if expected_physical_width != actual_physical_size.width as f64 =>
+ {
+ let _ = window.set_size(LogicalSize::new(
+ cursor_monitor.width,
+ cursor_monitor.height,
+ ));
+ }
+ Ok(_) => {}
+ Err(err) => {
+ warn!(%err, "Failed to read recordings overlay inner size");
+ }
+ }
+ }
#[cfg(target_os = "macos")]
{
diff --git a/apps/desktop/src/routes/target-select-overlay.tsx b/apps/desktop/src/routes/target-select-overlay.tsx
index 925707752..fd62eae8d 100644
--- a/apps/desktop/src/routes/target-select-overlay.tsx
+++ b/apps/desktop/src/routes/target-select-overlay.tsx
@@ -940,11 +940,27 @@ function Inner() {
bounds.y + bounds.height - newHeight - padding,
);
+ // The command applies these as raw device pixels. On Windows
+ // that means converting with the scale of the display the
+ // target rect is on (the overlay's display) — the camera
+ // window's own scale is wrong when it starts on a monitor
+ // with different DPI. On macOS physical coordinates are
+ // interpreted relative to the camera window's scale, so its
+ // own factor is the correct (self-canceling) one.
+ const targetScale = (() => {
+ if (ostype() === "macos") return scaleFactor;
+ const physicalWidth = displayInfo?.physical_size?.width;
+ const logicalWidth = displayInfo?.logical_size?.width;
+ return physicalWidth && logicalWidth && logicalWidth > 0
+ ? physicalWidth / logicalWidth
+ : scaleFactor;
+ })();
+
setTargetState({
- x: (newX + displayOriginX) * scaleFactor,
- y: (newY + displayOriginY) * scaleFactor,
- width: newWidth * scaleFactor,
- height: newHeight * scaleFactor,
+ x: (newX + displayOriginX) * targetScale,
+ y: (newY + displayOriginY) * targetScale,
+ width: newWidth * targetScale,
+ height: newHeight * targetScale,
});
}
});