From ca8aaf9b8daa3d8b925165fb912af425438d4e79 Mon Sep 17 00:00:00 2001 From: JL710 <76447362+JL710@users.noreply.github.com> Date: Fri, 11 Oct 2024 11:51:44 +0200 Subject: [PATCH 1/8] add Task and Action for changing a window title --- runtime/src/window.rs | 8 ++++++++ winit/src/program.rs | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 0ebdba2f5c..b5c6ee506c 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -73,6 +73,9 @@ pub enum Action { /// Get the current [`Mode`] of the window. GetMode(Id, oneshot::Sender), + /// Change the title of the window. + ChangeTitle(Id, String), + /// Toggle the window to maximized or back ToggleMaximize(Id), @@ -368,6 +371,11 @@ pub fn change_level(id: Id, level: Level) -> Task { task::effect(crate::Action::Window(Action::ChangeLevel(id, level))) } +/// Changes the title of the window. +pub fn change_title(id: Id, title: String) -> Task { + task::effect(crate::Action::Window(Action::ChangeTitle(id, title))) +} + /// Show the [system menu] at cursor position. /// /// [system menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu diff --git a/winit/src/program.rs b/winit/src/program.rs index cc19a4e040..052b36b0df 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1291,6 +1291,11 @@ fn run_action( ); } } + window::Action::ChangeTitle(id, title) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_title(&title); + } + } window::Action::GetSize(id, channel) => { if let Some(window) = window_manager.get_mut(id) { let size = window From 8ebbfa9767bcb8f8efd43cc1c7d40e9c61f6c461 Mon Sep 17 00:00:00 2001 From: JL710 <76447362+JL710@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:10:30 +0200 Subject: [PATCH 2/8] window tasks for setting min and max size --- runtime/src/window.rs | 16 ++++++++++++++++ winit/src/program.rs | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index b5c6ee506c..598ee49155 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -158,6 +158,12 @@ pub enum Action { /// This enables mouse events for the window and stops mouse events /// from being passed to whatever is underneath. DisableMousePassthrough(Id), + + /// Set the minimum inner window size. + SetMinSize(Id, Option), + + /// Set the maximum inner window size. + SetMaxSize(Id, Option), } /// Subscribes to the frames of the window of the running application. @@ -376,6 +382,16 @@ pub fn change_title(id: Id, title: String) -> Task { task::effect(crate::Action::Window(Action::ChangeTitle(id, title))) } +/// Set the inner maximum size of the window. +pub fn set_max_size(id: Id, size: Option) -> Task { + task::effect(crate::Action::Window(Action::SetMaxSize(id, size))) +} + +/// Set the inner minimum size of the window. +pub fn set_min_size(id: Id, size: Option) -> Task { + task::effect(crate::Action::Window(Action::SetMinSize(id, size))) +} + /// Show the [system menu] at cursor position. /// /// [system menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu diff --git a/winit/src/program.rs b/winit/src/program.rs index 052b36b0df..58fec9df65 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1291,6 +1291,26 @@ fn run_action( ); } } + window::Action::SetMinSize(id, size) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_min_inner_size(size.map(|x| { + winit::dpi::LogicalSize { + width: x.width, + height: x.height, + } + })); + } + } + window::Action::SetMaxSize(id, size) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_max_inner_size(size.map(|x| { + winit::dpi::LogicalSize { + width: x.width, + height: x.height, + } + })); + } + } window::Action::ChangeTitle(id, title) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_title(&title); From f5f075e5cdff84a58c181c498653b7ab66c9531a Mon Sep 17 00:00:00 2001 From: JL710 <76447362+JL710@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:16:52 +0200 Subject: [PATCH 3/8] window resizable task --- runtime/src/window.rs | 8 ++++++++ winit/src/program.rs | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 598ee49155..cdef70e39d 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -164,6 +164,9 @@ pub enum Action { /// Set the maximum inner window size. SetMaxSize(Id, Option), + + /// Set the window to be resizable or not. + SetResizable(Id, bool), } /// Subscribes to the frames of the window of the running application. @@ -274,6 +277,11 @@ pub fn resize(id: Id, new_size: Size) -> Task { task::effect(crate::Action::Window(Action::Resize(id, new_size))) } +/// Set the window to be resizable or not. +pub fn resizable(id: Id, resizable: bool) -> Task { + task::effect(crate::Action::Window(Action::SetResizable(id, resizable))) +} + /// Get the window's size in logical dimensions. pub fn get_size(id: Id) -> Task { task::oneshot(move |channel| { diff --git a/winit/src/program.rs b/winit/src/program.rs index 58fec9df65..416c8e7016 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1316,6 +1316,11 @@ fn run_action( window.raw.set_title(&title); } } + window::Action::SetResizable(id, resizable) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_resizable(resizable); + } + } window::Action::GetSize(id, channel) => { if let Some(window) = window_manager.get_mut(id) { let size = window From 00b60d819b25abe14790a26fe0eb5818c3c6bc4f Mon Sep 17 00:00:00 2001 From: JL710 <76447362+JL710@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:24:05 +0200 Subject: [PATCH 4/8] window task for setting resize increments --- runtime/src/window.rs | 12 ++++++++++++ winit/src/program.rs | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index cdef70e39d..e82d450584 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -167,6 +167,9 @@ pub enum Action { /// Set the window to be resizable or not. SetResizable(Id, bool), + + /// Set the window size increment. + SetResizeIncrements(Id, Option), } /// Subscribes to the frames of the window of the running application. @@ -400,6 +403,15 @@ pub fn set_min_size(id: Id, size: Option) -> Task { task::effect(crate::Action::Window(Action::SetMinSize(id, size))) } +/// Set the window size increment. +/// +/// This is usually used by apps such as terminal emulators that need "blocky" resizing. +pub fn set_resize_increments(id: Id, increments: Option) -> Task { + task::effect(crate::Action::Window(Action::SetResizeIncrements( + id, increments, + ))) +} + /// Show the [system menu] at cursor position. /// /// [system menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu diff --git a/winit/src/program.rs b/winit/src/program.rs index 416c8e7016..58f4d96d24 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1311,6 +1311,16 @@ fn run_action( })); } } + window::Action::SetResizeIncrements(id, increments) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_resize_increments(increments.map(|x| { + winit::dpi::LogicalSize { + width: x.width, + height: x.height, + } + })); + } + } window::Action::ChangeTitle(id, title) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_title(&title); From 91fd6d395fad08bcf5d068be95575865ccb305cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 6 Jan 2025 23:20:12 +0100 Subject: [PATCH 5/8] Remove `window::change_title` since it's redundant Applications can change title declaratively. --- runtime/src/window.rs | 8 -------- winit/src/program.rs | 5 ----- 2 files changed, 13 deletions(-) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index e82d450584..9be613156f 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -73,9 +73,6 @@ pub enum Action { /// Get the current [`Mode`] of the window. GetMode(Id, oneshot::Sender), - /// Change the title of the window. - ChangeTitle(Id, String), - /// Toggle the window to maximized or back ToggleMaximize(Id), @@ -388,11 +385,6 @@ pub fn change_level(id: Id, level: Level) -> Task { task::effect(crate::Action::Window(Action::ChangeLevel(id, level))) } -/// Changes the title of the window. -pub fn change_title(id: Id, title: String) -> Task { - task::effect(crate::Action::Window(Action::ChangeTitle(id, title))) -} - /// Set the inner maximum size of the window. pub fn set_max_size(id: Id, size: Option) -> Task { task::effect(crate::Action::Window(Action::SetMaxSize(id, size))) diff --git a/winit/src/program.rs b/winit/src/program.rs index 58f4d96d24..2bb0f9103c 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1321,11 +1321,6 @@ fn run_action( })); } } - window::Action::ChangeTitle(id, title) => { - if let Some(window) = window_manager.get_mut(id) { - window.raw.set_title(&title); - } - } window::Action::SetResizable(id, resizable) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_resizable(resizable); From 82ac0e7bf96d7c130108159a8b933ddfb39233fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 6 Jan 2025 23:24:01 +0100 Subject: [PATCH 6/8] Rename `window::resizable` to `window::set_resizable` --- runtime/src/window.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 9be613156f..ab805e52e8 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -278,10 +278,29 @@ pub fn resize(id: Id, new_size: Size) -> Task { } /// Set the window to be resizable or not. -pub fn resizable(id: Id, resizable: bool) -> Task { +pub fn set_resizable(id: Id, resizable: bool) -> Task { task::effect(crate::Action::Window(Action::SetResizable(id, resizable))) } +/// Set the inner maximum size of the window. +pub fn set_max_size(id: Id, size: Option) -> Task { + task::effect(crate::Action::Window(Action::SetMaxSize(id, size))) +} + +/// Set the inner minimum size of the window. +pub fn set_min_size(id: Id, size: Option) -> Task { + task::effect(crate::Action::Window(Action::SetMinSize(id, size))) +} + +/// Set the window size increment. +/// +/// This is usually used by apps such as terminal emulators that need "blocky" resizing. +pub fn set_resize_increments(id: Id, increments: Option) -> Task { + task::effect(crate::Action::Window(Action::SetResizeIncrements( + id, increments, + ))) +} + /// Get the window's size in logical dimensions. pub fn get_size(id: Id) -> Task { task::oneshot(move |channel| { @@ -385,25 +404,6 @@ pub fn change_level(id: Id, level: Level) -> Task { task::effect(crate::Action::Window(Action::ChangeLevel(id, level))) } -/// Set the inner maximum size of the window. -pub fn set_max_size(id: Id, size: Option) -> Task { - task::effect(crate::Action::Window(Action::SetMaxSize(id, size))) -} - -/// Set the inner minimum size of the window. -pub fn set_min_size(id: Id, size: Option) -> Task { - task::effect(crate::Action::Window(Action::SetMinSize(id, size))) -} - -/// Set the window size increment. -/// -/// This is usually used by apps such as terminal emulators that need "blocky" resizing. -pub fn set_resize_increments(id: Id, increments: Option) -> Task { - task::effect(crate::Action::Window(Action::SetResizeIncrements( - id, increments, - ))) -} - /// Show the [system menu] at cursor position. /// /// [system menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu From ed199e5e8f5bc210ffc923553983e5d3aa6ee5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 6 Jan 2025 23:24:12 +0100 Subject: [PATCH 7/8] Fix unintuitive variable name in `winit::program` --- winit/src/program.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/winit/src/program.rs b/winit/src/program.rs index 2bb0f9103c..95e8a28114 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1293,30 +1293,30 @@ fn run_action( } window::Action::SetMinSize(id, size) => { if let Some(window) = window_manager.get_mut(id) { - window.raw.set_min_inner_size(size.map(|x| { + window.raw.set_min_inner_size(size.map(|size| { winit::dpi::LogicalSize { - width: x.width, - height: x.height, + width: size.width, + height: size.height, } })); } } window::Action::SetMaxSize(id, size) => { if let Some(window) = window_manager.get_mut(id) { - window.raw.set_max_inner_size(size.map(|x| { + window.raw.set_max_inner_size(size.map(|size| { winit::dpi::LogicalSize { - width: x.width, - height: x.height, + width: size.width, + height: size.height, } })); } } window::Action::SetResizeIncrements(id, increments) => { if let Some(window) = window_manager.get_mut(id) { - window.raw.set_resize_increments(increments.map(|x| { + window.raw.set_resize_increments(increments.map(|size| { winit::dpi::LogicalSize { - width: x.width, - height: x.height, + width: size.width, + height: size.height, } })); } From 5b70754809df9d9f4f3a2f7d4d348dd58bd86aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 6 Jan 2025 23:25:57 +0100 Subject: [PATCH 8/8] Rename `window::change_*` tasks to `set_*` --- examples/todos/src/main.rs | 4 +--- runtime/src/window.rs | 24 ++++++++++++------------ winit/src/program.rs | 6 +++--- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index a5bca235be..e86e23b579 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -149,9 +149,7 @@ impl Todos { } } Message::ToggleFullscreen(mode) => window::get_latest() - .and_then(move |window| { - window::change_mode(window, mode) - }), + .and_then(move |window| window::set_mode(window, mode)), Message::Loaded(_) => Command::none(), }; diff --git a/runtime/src/window.rs b/runtime/src/window.rs index ab805e52e8..94ee0ae508 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -68,7 +68,7 @@ pub enum Action { Move(Id, Point), /// Change the [`Mode`] of the window. - ChangeMode(Id, Mode), + SetMode(Id, Mode), /// Get the current [`Mode`] of the window. GetMode(Id, oneshot::Sender), @@ -111,7 +111,7 @@ pub enum Action { GainFocus(Id), /// Change the window [`Level`]. - ChangeLevel(Id, Level), + SetLevel(Id, Level), /// Show the system menu at cursor position. /// @@ -136,7 +136,7 @@ pub enum Action { /// /// - **X11:** Has no universal guidelines for icon sizes, so you're at the whims of the WM. That /// said, it's usually in the same ballpark as on Windows. - ChangeIcon(Id, Icon), + SetIcon(Id, Icon), /// Runs the closure with the native window handle of the window with the given [`Id`]. RunWithHandle(Id, Box) + Send>), @@ -351,11 +351,6 @@ pub fn move_to(id: Id, position: Point) -> Task { task::effect(crate::Action::Window(Action::Move(id, position))) } -/// Changes the [`Mode`] of the window. -pub fn change_mode(id: Id, mode: Mode) -> Task { - task::effect(crate::Action::Window(Action::ChangeMode(id, mode))) -} - /// Gets the current [`Mode`] of the window. pub fn get_mode(id: Id) -> Task { task::oneshot(move |channel| { @@ -363,6 +358,11 @@ pub fn get_mode(id: Id) -> Task { }) } +/// Changes the [`Mode`] of the window. +pub fn set_mode(id: Id, mode: Mode) -> Task { + task::effect(crate::Action::Window(Action::SetMode(id, mode))) +} + /// Toggles the window to maximized or back. pub fn toggle_maximize(id: Id) -> Task { task::effect(crate::Action::Window(Action::ToggleMaximize(id))) @@ -400,8 +400,8 @@ pub fn gain_focus(id: Id) -> Task { } /// Changes the window [`Level`]. -pub fn change_level(id: Id, level: Level) -> Task { - task::effect(crate::Action::Window(Action::ChangeLevel(id, level))) +pub fn set_level(id: Id, level: Level) -> Task { + task::effect(crate::Action::Window(Action::SetLevel(id, level))) } /// Show the [system menu] at cursor position. @@ -420,8 +420,8 @@ pub fn get_raw_id(id: Id) -> Task { } /// Changes the [`Icon`] of the window. -pub fn change_icon(id: Id, icon: Icon) -> Task { - task::effect(crate::Action::Window(Action::ChangeIcon(id, icon))) +pub fn set_icon(id: Id, icon: Icon) -> Task { + task::effect(crate::Action::Window(Action::SetIcon(id, icon))) } /// Runs the given callback with the native window handle for the window with the given id. diff --git a/winit/src/program.rs b/winit/src/program.rs index 95e8a28114..499c625227 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1389,7 +1389,7 @@ fn run_action( ); } } - window::Action::ChangeMode(id, mode) => { + window::Action::SetMode(id, mode) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_visible(conversion::visible(mode)); window.raw.set_fullscreen(conversion::fullscreen( @@ -1398,7 +1398,7 @@ fn run_action( )); } } - window::Action::ChangeIcon(id, icon) => { + window::Action::SetIcon(id, icon) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_window_icon(conversion::icon(icon)); } @@ -1436,7 +1436,7 @@ fn run_action( window.raw.focus_window(); } } - window::Action::ChangeLevel(id, level) => { + window::Action::SetLevel(id, level) => { if let Some(window) = window_manager.get_mut(id) { window .raw