From 4b04e580471df4d1f9e22e8ac9937e691b74403e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=E8=B4=A6=E6=88=B7=E5=B7=B2=E6=B3=A8=E9=94=80I?= Date: Fri, 19 Jun 2026 19:06:00 +0800 Subject: [PATCH] Add setMinSize() and setMaxSize() methods --- index.d.ts | 8 ++++++++ src/browser_window.rs | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 7c63bf2..374f9c1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -57,6 +57,14 @@ export declare class BrowserWindow { setResizable(resizable: boolean): void /** Sets the window inner size (width and height). */ setSize(width: number, height: number): void + /** Sets the minimum inner size (width and height) for the window. + * Pass `0` for both width and height to remove the constraint. + */ + setMinSize(width: number, height: number, logical?: boolean | undefined | null): void + /** Sets the maximum inner size (width and height) for the window. + * Pass `0` for both width and height to remove the constraint. + */ + setMaxSize(width: number, height: number, logical?: boolean | undefined | null): void /** Gets the window ID. */ id(): number /** Gets whether the window has a menu. */ diff --git a/src/browser_window.rs b/src/browser_window.rs index 108954d..6a1f25b 100644 --- a/src/browser_window.rs +++ b/src/browser_window.rs @@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex}; use std::hash::{Hash, Hasher}; use std::collections::hash_map::DefaultHasher; use tao::{ - dpi::{LogicalPosition, LogicalSize, PhysicalSize}, + dpi::{LogicalPosition, LogicalSize, PhysicalSize, Size}, event_loop::EventLoop, window::{Fullscreen, ProgressBarState, Window, WindowBuilder, WindowId}, }; @@ -444,6 +444,44 @@ impl BrowserWindow { self.window.set_inner_size(LogicalSize::new(width, height)); } + #[napi] + /// Sets the minimum inner size (width and height) for the window. + /// Pass `0` for both width and height to remove the constraint. + pub fn set_min_size(&self, width: u32, height: u32, logical: Option) { + if width == 0 && height == 0 { + self.window.set_min_inner_size(None::); + } else { + if let Some(logical) = logical { + if logical { + self.window.set_min_inner_size(Some(LogicalSize::new(width, height))); + } else { + self.window.set_min_inner_size(Some(PhysicalSize::new(width, height))); + } + } else { + self.window.set_min_inner_size(Some(PhysicalSize::new(width, height))); + } + } + } + + #[napi] + /// Sets the maximum inner size (width and height) for the window. + /// Pass `0` for both width and height to remove the constraint. + pub fn set_max_size(&self, width: u32, height: u32, logical: Option) { + if width == 0 && height == 0 { + self.window.set_max_inner_size(None::); + } else { + if let Some(logical) = logical { + if logical { + self.window.set_max_inner_size(Some(LogicalSize::new(width, height))); + } else { + self.window.set_max_inner_size(Some(PhysicalSize::new(width, height))); + } + } else { + self.window.set_max_inner_size(Some(PhysicalSize::new(width, height))); + } + } + } + #[napi] /// Gets the window ID. pub fn id(&self) -> u32 {