Skip to content

Commit

Permalink
Merge pull request #2633 from JL710/window-tasks
Browse files Browse the repository at this point in the history
more window tasks
  • Loading branch information
hecrj authored Jan 6, 2025
2 parents 717b296 + 5b70754 commit e722c4e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 18 deletions.
4 changes: 1 addition & 3 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};

Expand Down
60 changes: 48 additions & 12 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mode>),
Expand Down Expand Up @@ -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.
///
Expand All @@ -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<dyn FnOnce(WindowHandle<'_>) + Send>),
Expand All @@ -155,6 +155,18 @@ 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<Size>),

/// Set the maximum inner window size.
SetMaxSize(Id, Option<Size>),

/// Set the window to be resizable or not.
SetResizable(Id, bool),

/// Set the window size increment.
SetResizeIncrements(Id, Option<Size>),
}

/// Subscribes to the frames of the window of the running application.
Expand Down Expand Up @@ -265,6 +277,30 @@ pub fn resize<T>(id: Id, new_size: Size) -> Task<T> {
task::effect(crate::Action::Window(Action::Resize(id, new_size)))
}

/// Set the window to be resizable or not.
pub fn set_resizable<T>(id: Id, resizable: bool) -> Task<T> {
task::effect(crate::Action::Window(Action::SetResizable(id, resizable)))
}

/// Set the inner maximum size of the window.
pub fn set_max_size<T>(id: Id, size: Option<Size>) -> Task<T> {
task::effect(crate::Action::Window(Action::SetMaxSize(id, size)))
}

/// Set the inner minimum size of the window.
pub fn set_min_size<T>(id: Id, size: Option<Size>) -> Task<T> {
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<T>(id: Id, increments: Option<Size>) -> Task<T> {
task::effect(crate::Action::Window(Action::SetResizeIncrements(
id, increments,
)))
}

/// Get the window's size in logical dimensions.
pub fn get_size(id: Id) -> Task<Size> {
task::oneshot(move |channel| {
Expand Down Expand Up @@ -315,18 +351,18 @@ pub fn move_to<T>(id: Id, position: Point) -> Task<T> {
task::effect(crate::Action::Window(Action::Move(id, position)))
}

/// Changes the [`Mode`] of the window.
pub fn change_mode<T>(id: Id, mode: Mode) -> Task<T> {
task::effect(crate::Action::Window(Action::ChangeMode(id, mode)))
}

/// Gets the current [`Mode`] of the window.
pub fn get_mode(id: Id) -> Task<Mode> {
task::oneshot(move |channel| {
crate::Action::Window(Action::GetMode(id, channel))
})
}

/// Changes the [`Mode`] of the window.
pub fn set_mode<T>(id: Id, mode: Mode) -> Task<T> {
task::effect(crate::Action::Window(Action::SetMode(id, mode)))
}

/// Toggles the window to maximized or back.
pub fn toggle_maximize<T>(id: Id) -> Task<T> {
task::effect(crate::Action::Window(Action::ToggleMaximize(id)))
Expand Down Expand Up @@ -364,8 +400,8 @@ pub fn gain_focus<T>(id: Id) -> Task<T> {
}

/// Changes the window [`Level`].
pub fn change_level<T>(id: Id, level: Level) -> Task<T> {
task::effect(crate::Action::Window(Action::ChangeLevel(id, level)))
pub fn set_level<T>(id: Id, level: Level) -> Task<T> {
task::effect(crate::Action::Window(Action::SetLevel(id, level)))
}

/// Show the [system menu] at cursor position.
Expand All @@ -384,8 +420,8 @@ pub fn get_raw_id<Message>(id: Id) -> Task<u64> {
}

/// Changes the [`Icon`] of the window.
pub fn change_icon<T>(id: Id, icon: Icon) -> Task<T> {
task::effect(crate::Action::Window(Action::ChangeIcon(id, icon)))
pub fn set_icon<T>(id: Id, icon: Icon) -> Task<T> {
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.
Expand Down
41 changes: 38 additions & 3 deletions winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,41 @@ fn run_action<P, C>(
);
}
}
window::Action::SetMinSize(id, size) => {
if let Some(window) = window_manager.get_mut(id) {
window.raw.set_min_inner_size(size.map(|size| {
winit::dpi::LogicalSize {
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(|size| {
winit::dpi::LogicalSize {
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(|size| {
winit::dpi::LogicalSize {
width: size.width,
height: size.height,
}
}));
}
}
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
Expand Down Expand Up @@ -1354,7 +1389,7 @@ fn run_action<P, C>(
);
}
}
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(
Expand All @@ -1363,7 +1398,7 @@ fn run_action<P, C>(
));
}
}
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));
}
Expand Down Expand Up @@ -1401,7 +1436,7 @@ fn run_action<P, C>(
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
Expand Down

0 comments on commit e722c4e

Please sign in to comment.