diff options
Diffstat (limited to 'runtime/src/window.rs')
-rw-r--r-- | runtime/src/window.rs | 112 |
1 files changed, 82 insertions, 30 deletions
diff --git a/runtime/src/window.rs b/runtime/src/window.rs index b04e5d59..cdf3d80a 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -11,7 +11,7 @@ use crate::core::{Point, Size}; use crate::futures::event; use crate::futures::futures::channel::oneshot; use crate::futures::Subscription; -use crate::Task; +use crate::task::{self, Task}; pub use raw_window_handle; @@ -63,6 +63,9 @@ pub enum Action { /// Get the current logical coordinates of the window. GetPosition(Id, oneshot::Sender<Option<Point>>), + /// Get the current scale factor (DPI) of the window. + GetScaleFactor(Id, oneshot::Sender<f32>), + /// Move the window to the given logical coordinates. /// /// Unsupported on Wayland. @@ -144,6 +147,18 @@ pub enum Action { /// Screenshot the viewport of the window. Screenshot(Id, oneshot::Sender<Screenshot>), + + /// Enables mouse passthrough for the given window. + /// + /// This disables mouse events for the window and passes mouse events + /// through to whatever window is underneath. + EnableMousePassthrough(Id), + + /// Disable mouse passthrough for the given window. + /// + /// This enables mouse events for the window and stops mouse events + /// from being passed to whatever is underneath. + DisableMousePassthrough(Id), } /// Subscribes to the frames of the window of the running application. @@ -175,7 +190,7 @@ pub fn events() -> Subscription<(Id, Event)> { /// Subscribes to all [`Event::Closed`] occurrences in the running application. pub fn open_events() -> Subscription<Id> { event::listen_with(|event, _status, id| { - if let crate::core::Event::Window(Event::Closed) = event { + if let crate::core::Event::Window(Event::Opened { .. }) = event { Some(id) } else { None @@ -194,6 +209,17 @@ pub fn close_events() -> Subscription<Id> { }) } +/// Subscribes to all [`Event::Resized`] occurrences in the running application. +pub fn resize_events() -> Subscription<(Id, Size)> { + event::listen_with(|event, _status, id| { + if let crate::core::Event::Window(Event::Resized(size)) = event { + Some((id, size)) + } else { + None + } + }) +} + /// Subscribes to all [`Event::CloseRequested`] occurences in the running application. pub fn close_requests() -> Subscription<Id> { event::listen_with(|event, _status, id| { @@ -207,102 +233,112 @@ pub fn close_requests() -> Subscription<Id> { /// Opens a new window with the given [`Settings`]; producing the [`Id`] /// of the new window on completion. -pub fn open(settings: Settings) -> Task<Id> { +pub fn open(settings: Settings) -> (Id, Task<Id>) { let id = Id::unique(); - Task::oneshot(|channel| { - crate::Action::Window(Action::Open(id, settings, channel)) - }) + ( + id, + task::oneshot(|channel| { + crate::Action::Window(Action::Open(id, settings, channel)) + }), + ) } /// Closes the window with `id`. 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))) + 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))) + task::oneshot(|channel| crate::Action::Window(Action::GetLatest(channel))) } /// Begins dragging the window while the left mouse button is held. pub fn drag<T>(id: Id) -> Task<T> { - Task::effect(crate::Action::Window(Action::Drag(id))) + task::effect(crate::Action::Window(Action::Drag(id))) } /// Resizes the window to the given logical dimensions. 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))) } /// Get the window's size in logical dimensions. pub fn get_size(id: Id) -> Task<Size> { - Task::oneshot(move |channel| { + task::oneshot(move |channel| { crate::Action::Window(Action::GetSize(id, channel)) }) } /// Gets the maximized state of the window with the given [`Id`]. pub fn get_maximized(id: Id) -> Task<bool> { - Task::oneshot(move |channel| { + task::oneshot(move |channel| { crate::Action::Window(Action::GetMaximized(id, channel)) }) } /// Maximizes the window. 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))) } /// Gets the minimized state of the window with the given [`Id`]. pub fn get_minimized(id: Id) -> Task<Option<bool>> { - Task::oneshot(move |channel| { + task::oneshot(move |channel| { crate::Action::Window(Action::GetMinimized(id, channel)) }) } /// Minimizes the window. 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))) } /// Gets the position in logical coordinates of the window with the given [`Id`]. pub fn get_position(id: Id) -> Task<Option<Point>> { - Task::oneshot(move |channel| { + task::oneshot(move |channel| { crate::Action::Window(Action::GetPosition(id, channel)) }) } +/// Gets the scale factor of the window with the given [`Id`]. +pub fn get_scale_factor(id: Id) -> Task<f32> { + task::oneshot(move |channel| { + crate::Action::Window(Action::GetScaleFactor(id, channel)) + }) +} + /// Moves the window to the given logical coordinates. pub fn move_to<T>(id: Id, position: Point) -> Task<T> { - Task::effect(crate::Action::Window(Action::Move(id, position))) + 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))) + 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| { + task::oneshot(move |channel| { crate::Action::Window(Action::GetMode(id, channel)) }) } /// Toggles the window to maximized or back. pub fn toggle_maximize<T>(id: Id) -> Task<T> { - Task::effect(crate::Action::Window(Action::ToggleMaximize(id))) + task::effect(crate::Action::Window(Action::ToggleMaximize(id))) } /// Toggles the window decorations. pub fn toggle_decorations<T>(id: Id) -> Task<T> { - Task::effect(crate::Action::Window(Action::ToggleDecorations(id))) + task::effect(crate::Action::Window(Action::ToggleDecorations(id))) } /// Request user attention to the window. This has no effect if the application @@ -315,7 +351,7 @@ pub fn request_user_attention<T>( id: Id, user_attention: Option<UserAttention>, ) -> Task<T> { - Task::effect(crate::Action::Window(Action::RequestUserAttention( + task::effect(crate::Action::Window(Action::RequestUserAttention( id, user_attention, ))) @@ -328,32 +364,32 @@ pub fn request_user_attention<T>( /// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive /// user experience. pub fn gain_focus<T>(id: Id) -> Task<T> { - Task::effect(crate::Action::Window(Action::GainFocus(id))) + task::effect(crate::Action::Window(Action::GainFocus(id))) } /// Changes the window [`Level`]. pub fn change_level<T>(id: Id, level: Level) -> Task<T> { - Task::effect(crate::Action::Window(Action::ChangeLevel(id, level))) + task::effect(crate::Action::Window(Action::ChangeLevel(id, level))) } /// Show the [system menu] at cursor position. /// /// [system menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu 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))) } /// Gets an identifier unique to the window, provided by the underlying windowing system. This is /// not to be confused with [`Id`]. pub fn get_raw_id<Message>(id: Id) -> Task<u64> { - Task::oneshot(|channel| { + task::oneshot(|channel| { crate::Action::Window(Action::GetRawId(id, channel)) }) } /// 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))) + task::effect(crate::Action::Window(Action::ChangeIcon(id, icon))) } /// Runs the given callback with the native window handle for the window with the given id. @@ -366,7 +402,7 @@ pub fn run_with_handle<T>( where T: Send + 'static, { - Task::oneshot(move |channel| { + task::oneshot(move |channel| { crate::Action::Window(Action::RunWithHandle( id, Box::new(move |handle| { @@ -378,7 +414,23 @@ where /// Captures a [`Screenshot`] from the window. pub fn screenshot(id: Id) -> Task<Screenshot> { - Task::oneshot(move |channel| { + task::oneshot(move |channel| { crate::Action::Window(Action::Screenshot(id, channel)) }) } + +/// Enables mouse passthrough for the given window. +/// +/// This disables mouse events for the window and passes mouse events +/// through to whatever window is underneath. +pub fn enable_mouse_passthrough<Message>(id: Id) -> Task<Message> { + task::effect(crate::Action::Window(Action::EnableMousePassthrough(id))) +} + +/// Disable mouse passthrough for the given window. +/// +/// This enables mouse events for the window and stops mouse events +/// from being passed to whatever is underneath. +pub fn disable_mouse_passthrough<Message>(id: Id) -> Task<Message> { + task::effect(crate::Action::Window(Action::DisableMousePassthrough(id))) +} |