Add get_latest and get_oldest tasks in window

This commit is contained in:
Héctor Ramón Jiménez 2024-06-20 01:13:09 +02:00
parent bdd30f7ab8
commit 92e08c8f07
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 64 additions and 36 deletions

View file

@ -26,6 +26,12 @@ pub enum Action {
/// Close the window and exits the application. /// Close the window and exits the application.
Close(Id), Close(Id),
/// Gets the [`Id`] of the oldest window.
GetOldest(oneshot::Sender<Option<Id>>),
/// Gets the [`Id`] of the latest window.
GetLatest(oneshot::Sender<Option<Id>>),
/// Move the window with the left mouse button until the button is /// Move the window with the left mouse button until the button is
/// released. /// released.
/// ///
@ -36,26 +42,26 @@ pub enum Action {
/// Resize the window to the given logical dimensions. /// Resize the window to the given logical dimensions.
Resize(Id, Size), Resize(Id, Size),
/// Fetch the current logical dimensions of the window. /// Get the current logical dimensions of the window.
FetchSize(Id, oneshot::Sender<Size>), GetSize(Id, oneshot::Sender<Size>),
/// Fetch if the current window is maximized or not. /// Get if the current window is maximized or not.
FetchMaximized(Id, oneshot::Sender<bool>), GetMaximized(Id, oneshot::Sender<bool>),
/// Set the window to maximized or back /// Set the window to maximized or back
Maximize(Id, bool), Maximize(Id, bool),
/// Fetch if the current window is minimized or not. /// Get if the current window is minimized or not.
/// ///
/// ## Platform-specific /// ## Platform-specific
/// - **Wayland:** Always `None`. /// - **Wayland:** Always `None`.
FetchMinimized(Id, oneshot::Sender<Option<bool>>), GetMinimized(Id, oneshot::Sender<Option<bool>>),
/// Set the window to minimized or back /// Set the window to minimized or back
Minimize(Id, bool), Minimize(Id, bool),
/// Fetch the current logical coordinates of the window. /// Get the current logical coordinates of the window.
FetchPosition(Id, oneshot::Sender<Option<Point>>), GetPosition(Id, oneshot::Sender<Option<Point>>),
/// Move the window to the given logical coordinates. /// Move the window to the given logical coordinates.
/// ///
@ -65,8 +71,8 @@ pub enum Action {
/// Change the [`Mode`] of the window. /// Change the [`Mode`] of the window.
ChangeMode(Id, Mode), ChangeMode(Id, Mode),
/// Fetch the current [`Mode`] of the window. /// Get the current [`Mode`] of the window.
FetchMode(Id, oneshot::Sender<Mode>), GetMode(Id, oneshot::Sender<Mode>),
/// Toggle the window to maximized or back /// Toggle the window to maximized or back
ToggleMaximize(Id), ToggleMaximize(Id),
@ -114,8 +120,8 @@ pub enum Action {
/// Android / iOS / macOS / Orbital / Web / X11: Unsupported. /// Android / iOS / macOS / Orbital / Web / X11: Unsupported.
ShowSystemMenu(Id), ShowSystemMenu(Id),
/// Fetch the raw identifier unique to the window. /// Get the raw identifier unique to the window.
FetchRawId(Id, oneshot::Sender<u64>), GetRawId(Id, oneshot::Sender<u64>),
/// Change the window [`Icon`]. /// Change the window [`Icon`].
/// ///
@ -214,6 +220,16 @@ pub fn close<T>(id: Id) -> Task<T> {
Task::effect(crate::Action::Window(Action::Close(id))) Task::effect(crate::Action::Window(Action::Close(id)))
} }
/// Gets the window [`Id`] of the oldest window.
pub fn get_oldest() -> Task<Option<Id>> {
Task::oneshot(|channel| crate::Action::Window(Action::GetOldest(channel)))
}
/// Gets the window [`Id`] of the latest window.
pub fn get_latest() -> Task<Option<Id>> {
Task::oneshot(|channel| crate::Action::Window(Action::GetLatest(channel)))
}
/// Begins dragging the window while the left mouse button is held. /// Begins dragging the window while the left mouse button is held.
pub fn drag<T>(id: Id) -> Task<T> { pub fn drag<T>(id: Id) -> Task<T> {
Task::effect(crate::Action::Window(Action::Drag(id))) Task::effect(crate::Action::Window(Action::Drag(id)))
@ -224,17 +240,17 @@ pub fn resize<T>(id: Id, new_size: Size) -> Task<T> {
Task::effect(crate::Action::Window(Action::Resize(id, new_size))) Task::effect(crate::Action::Window(Action::Resize(id, new_size)))
} }
/// Fetches the window's size in logical dimensions. /// Get the window's size in logical dimensions.
pub fn fetch_size(id: Id) -> Task<Size> { pub fn get_size(id: Id) -> Task<Size> {
Task::oneshot(move |channel| { Task::oneshot(move |channel| {
crate::Action::Window(Action::FetchSize(id, channel)) crate::Action::Window(Action::GetSize(id, channel))
}) })
} }
/// Fetches if the window is maximized. /// Gets the maximized state of the window with the given [`Id`].
pub fn fetch_maximized(id: Id) -> Task<bool> { pub fn get_maximized(id: Id) -> Task<bool> {
Task::oneshot(move |channel| { Task::oneshot(move |channel| {
crate::Action::Window(Action::FetchMaximized(id, channel)) crate::Action::Window(Action::GetMaximized(id, channel))
}) })
} }
@ -243,10 +259,10 @@ pub fn maximize<T>(id: Id, maximized: bool) -> Task<T> {
Task::effect(crate::Action::Window(Action::Maximize(id, maximized))) Task::effect(crate::Action::Window(Action::Maximize(id, maximized)))
} }
/// Fetches if the window is minimized. /// Gets the minimized state of the window with the given [`Id`].
pub fn fetch_minimized(id: Id) -> Task<Option<bool>> { pub fn get_minimized(id: Id) -> Task<Option<bool>> {
Task::oneshot(move |channel| { Task::oneshot(move |channel| {
crate::Action::Window(Action::FetchMinimized(id, channel)) crate::Action::Window(Action::GetMinimized(id, channel))
}) })
} }
@ -255,10 +271,10 @@ pub fn minimize<T>(id: Id, minimized: bool) -> Task<T> {
Task::effect(crate::Action::Window(Action::Minimize(id, minimized))) Task::effect(crate::Action::Window(Action::Minimize(id, minimized)))
} }
/// Fetches the current window position in logical coordinates. /// Gets the position in logical coordinates of the window with the given [`Id`].
pub fn fetch_position(id: Id) -> Task<Option<Point>> { pub fn get_position(id: Id) -> Task<Option<Point>> {
Task::oneshot(move |channel| { Task::oneshot(move |channel| {
crate::Action::Window(Action::FetchPosition(id, channel)) crate::Action::Window(Action::GetPosition(id, channel))
}) })
} }
@ -272,10 +288,10 @@ pub fn change_mode<T>(id: Id, mode: Mode) -> Task<T> {
Task::effect(crate::Action::Window(Action::ChangeMode(id, mode))) Task::effect(crate::Action::Window(Action::ChangeMode(id, mode)))
} }
/// Fetches the current [`Mode`] of the window. /// Gets the current [`Mode`] of the window.
pub fn fetch_mode(id: Id) -> Task<Mode> { pub fn get_mode(id: Id) -> Task<Mode> {
Task::oneshot(move |channel| { Task::oneshot(move |channel| {
crate::Action::Window(Action::FetchMode(id, channel)) crate::Action::Window(Action::GetMode(id, channel))
}) })
} }
@ -327,11 +343,11 @@ pub fn show_system_menu<T>(id: Id) -> Task<T> {
Task::effect(crate::Action::Window(Action::ShowSystemMenu(id))) Task::effect(crate::Action::Window(Action::ShowSystemMenu(id)))
} }
/// Fetches an identifier unique to the window, provided by the underlying windowing system. This is /// Gets an identifier unique to the window, provided by the underlying windowing system. This is
/// not to be confused with [`Id`]. /// not to be confused with [`Id`].
pub fn fetch_raw_id<Message>(id: Id) -> Task<u64> { pub fn get_raw_id<Message>(id: Id) -> Task<u64> {
Task::oneshot(|channel| { Task::oneshot(|channel| {
crate::Action::Window(Action::FetchRawId(id, channel)) crate::Action::Window(Action::GetRawId(id, channel))
}) })
} }

View file

@ -1165,6 +1165,18 @@ fn run_action<P, C>(
let _ = window_manager.remove(id); let _ = window_manager.remove(id);
let _ = ui_caches.remove(&id); let _ = ui_caches.remove(&id);
} }
window::Action::GetOldest(channel) => {
let id =
window_manager.iter_mut().next().map(|(id, _window)| id);
let _ = channel.send(id);
}
window::Action::GetLatest(channel) => {
let id =
window_manager.iter_mut().last().map(|(id, _window)| id);
let _ = channel.send(id);
}
window::Action::Drag(id) => { window::Action::Drag(id) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let _ = window.raw.drag_window(); let _ = window.raw.drag_window();
@ -1180,7 +1192,7 @@ fn run_action<P, C>(
); );
} }
} }
window::Action::FetchSize(id, channel) => { window::Action::GetSize(id, channel) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let size = window let size = window
.raw .raw
@ -1190,7 +1202,7 @@ fn run_action<P, C>(
let _ = channel.send(Size::new(size.width, size.height)); let _ = channel.send(Size::new(size.width, size.height));
} }
} }
window::Action::FetchMaximized(id, channel) => { window::Action::GetMaximized(id, channel) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let _ = channel.send(window.raw.is_maximized()); let _ = channel.send(window.raw.is_maximized());
} }
@ -1200,7 +1212,7 @@ fn run_action<P, C>(
window.raw.set_maximized(maximized); window.raw.set_maximized(maximized);
} }
} }
window::Action::FetchMinimized(id, channel) => { window::Action::GetMinimized(id, channel) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let _ = channel.send(window.raw.is_minimized()); let _ = channel.send(window.raw.is_minimized());
} }
@ -1210,7 +1222,7 @@ fn run_action<P, C>(
window.raw.set_minimized(minimized); window.raw.set_minimized(minimized);
} }
} }
window::Action::FetchPosition(id, channel) => { window::Action::GetPosition(id, channel) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let position = window let position = window
.raw .raw
@ -1250,7 +1262,7 @@ fn run_action<P, C>(
window.raw.set_window_icon(conversion::icon(icon)); window.raw.set_window_icon(conversion::icon(icon));
} }
} }
window::Action::FetchMode(id, channel) => { window::Action::GetMode(id, channel) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let mode = if window.raw.is_visible().unwrap_or(true) { let mode = if window.raw.is_visible().unwrap_or(true) {
conversion::mode(window.raw.fullscreen()) conversion::mode(window.raw.fullscreen())
@ -1304,7 +1316,7 @@ fn run_action<P, C>(
} }
} }
} }
window::Action::FetchRawId(id, channel) => { window::Action::GetRawId(id, channel) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let _ = channel.send(window.raw.id().into()); let _ = channel.send(window.raw.id().into());
} }