From 08eb729af2550bf3abee809e4944a55ff216ed33 Mon Sep 17 00:00:00 2001 From: majiayu000 <1835304752@qq.com> Date: Fri, 26 Dec 2025 11:45:09 +0800 Subject: [PATCH] feat(window): add physical coordinate methods to WindowBuilder (fix #5228) Add methods to set window position and size in physical coordinates: - `position_physical(x: i32, y: i32)` - `inner_size_physical(width: u32, height: u32)` - `min_inner_size_physical(min_width: u32, min_height: u32)` - `max_inner_size_physical(max_width: u32, max_height: u32)` This allows positioning windows on specific monitors using physical pixels. Signed-off-by: majiayu000 <1835304752@qq.com> --- .changes/window-builder-physical-size.md | 7 ++++++ crates/tauri-runtime-wry/src/lib.rs | 26 ++++++++++++++++++++++ crates/tauri-runtime/src/window.rs | 16 ++++++++++++++ crates/tauri/src/window/mod.rs | 28 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 .changes/window-builder-physical-size.md diff --git a/.changes/window-builder-physical-size.md b/.changes/window-builder-physical-size.md new file mode 100644 index 000000000..7008e41e7 --- /dev/null +++ b/.changes/window-builder-physical-size.md @@ -0,0 +1,7 @@ +--- +'tauri': 'minor:feat' +'tauri-runtime': 'minor:feat' +'tauri-runtime-wry': 'minor:feat' +--- + +Add physical coordinate methods to WindowBuilder: `position_physical`, `inner_size_physical`, `min_inner_size_physical`, and `max_inner_size_physical` (fix #5228). diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 821fbacec..a4942dbf6 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -945,6 +945,11 @@ impl WindowBuilder for WindowBuilderWrapper { self } + fn position_physical(mut self, x: i32, y: i32) -> Self { + self.inner = self.inner.with_position(TaoPhysicalPosition::new(x, y)); + self + } + fn inner_size(mut self, width: f64, height: f64) -> Self { self.inner = self .inner @@ -952,6 +957,13 @@ impl WindowBuilder for WindowBuilderWrapper { self } + fn inner_size_physical(mut self, width: u32, height: u32) -> Self { + self.inner = self + .inner + .with_inner_size(TaoPhysicalSize::new(width, height)); + self + } + fn min_inner_size(mut self, min_width: f64, min_height: f64) -> Self { self.inner = self .inner @@ -959,6 +971,13 @@ impl WindowBuilder for WindowBuilderWrapper { self } + fn min_inner_size_physical(mut self, min_width: u32, min_height: u32) -> Self { + self.inner = self + .inner + .with_min_inner_size(TaoPhysicalSize::new(min_width, min_height)); + self + } + fn max_inner_size(mut self, max_width: f64, max_height: f64) -> Self { self.inner = self .inner @@ -966,6 +985,13 @@ impl WindowBuilder for WindowBuilderWrapper { self } + fn max_inner_size_physical(mut self, max_width: u32, max_height: u32) -> Self { + self.inner = self + .inner + .with_max_inner_size(TaoPhysicalSize::new(max_width, max_height)); + self + } + fn inner_size_constraints(mut self, constraints: WindowSizeConstraints) -> Self { self.inner.window.inner_size_constraints = tao::window::WindowSizeConstraints { min_width: constraints.min_width, diff --git a/crates/tauri-runtime/src/window.rs b/crates/tauri-runtime/src/window.rs index 652832c1e..86a8cb28d 100644 --- a/crates/tauri-runtime/src/window.rs +++ b/crates/tauri-runtime/src/window.rs @@ -251,18 +251,34 @@ pub trait WindowBuilder: WindowBuilderBase { #[must_use] fn position(self, x: f64, y: f64) -> Self; + /// The initial position of the window in physical pixels. + #[must_use] + fn position_physical(self, x: i32, y: i32) -> Self; + /// Window size in logical pixels. #[must_use] fn inner_size(self, width: f64, height: f64) -> Self; + /// Window size in physical pixels. + #[must_use] + fn inner_size_physical(self, width: u32, height: u32) -> Self; + /// Window min inner size in logical pixels. #[must_use] fn min_inner_size(self, min_width: f64, min_height: f64) -> Self; + /// Window min inner size in physical pixels. + #[must_use] + fn min_inner_size_physical(self, min_width: u32, min_height: u32) -> Self; + /// Window max inner size in logical pixels. #[must_use] fn max_inner_size(self, max_width: f64, max_height: f64) -> Self; + /// Window max inner size in physical pixels. + #[must_use] + fn max_inner_size_physical(self, max_width: u32, max_height: u32) -> Self; + /// Window inner size constraints. #[must_use] fn inner_size_constraints(self, constraints: WindowSizeConstraints) -> Self; diff --git a/crates/tauri/src/window/mod.rs b/crates/tauri/src/window/mod.rs index a67784cc3..4679025ab 100644 --- a/crates/tauri/src/window/mod.rs +++ b/crates/tauri/src/window/mod.rs @@ -450,6 +450,13 @@ impl<'a, R: Runtime, M: Manager> WindowBuilder<'a, R, M> { self } + /// The initial position of the window in physical pixels. + #[must_use] + pub fn position_physical(mut self, x: i32, y: i32) -> Self { + self.window_builder = self.window_builder.position_physical(x, y); + self + } + /// Window size in logical pixels. #[must_use] pub fn inner_size(mut self, width: f64, height: f64) -> Self { @@ -457,6 +464,13 @@ impl<'a, R: Runtime, M: Manager> WindowBuilder<'a, R, M> { self } + /// Window size in physical pixels. + #[must_use] + pub fn inner_size_physical(mut self, width: u32, height: u32) -> Self { + self.window_builder = self.window_builder.inner_size_physical(width, height); + self + } + /// Window min inner size in logical pixels. #[must_use] pub fn min_inner_size(mut self, min_width: f64, min_height: f64) -> Self { @@ -464,6 +478,13 @@ impl<'a, R: Runtime, M: Manager> WindowBuilder<'a, R, M> { self } + /// Window min inner size in physical pixels. + #[must_use] + pub fn min_inner_size_physical(mut self, min_width: u32, min_height: u32) -> Self { + self.window_builder = self.window_builder.min_inner_size_physical(min_width, min_height); + self + } + /// Window max inner size in logical pixels. #[must_use] pub fn max_inner_size(mut self, max_width: f64, max_height: f64) -> Self { @@ -471,6 +492,13 @@ impl<'a, R: Runtime, M: Manager> WindowBuilder<'a, R, M> { self } + /// Window max inner size in physical pixels. + #[must_use] + pub fn max_inner_size_physical(mut self, max_width: u32, max_height: u32) -> Self { + self.window_builder = self.window_builder.max_inner_size_physical(max_width, max_height); + self + } + /// Window inner size constraints. #[must_use] pub fn inner_size_constraints(