feat: adds monitor_from_point method (#9770)

This commit is contained in:
Siddharth 2024-05-14 22:18:51 +05:30 committed by GitHub
parent 31aa90f514
commit ec0e092ecd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 122 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'patch:feat'
---
Add `monitorFromPoint` function in `window` module to get the monitor from a given point.

View File

@ -0,0 +1,5 @@
---
'tauri': 'patch:feat'
---
Add `App/AppHandle/Window/Webview/WebviewWindow::monitor_from_point(x, y)` getter to get the monitor from a given point.

View File

@ -1092,6 +1092,7 @@ pub enum WindowMessage {
Title(Sender<String>), Title(Sender<String>),
CurrentMonitor(Sender<Option<MonitorHandle>>), CurrentMonitor(Sender<Option<MonitorHandle>>),
PrimaryMonitor(Sender<Option<MonitorHandle>>), PrimaryMonitor(Sender<Option<MonitorHandle>>),
MonitorFromPoint(Sender<Option<MonitorHandle>>, (f64, f64)),
AvailableMonitors(Sender<Vec<MonitorHandle>>), AvailableMonitors(Sender<Vec<MonitorHandle>>),
#[cfg(any( #[cfg(any(
target_os = "linux", target_os = "linux",
@ -1578,6 +1579,21 @@ impl<T: UserEvent> WindowDispatch<T> for WryWindowDispatcher<T> {
Ok(window_getter!(self, WindowMessage::PrimaryMonitor)?.map(|m| MonitorHandleWrapper(m).into())) Ok(window_getter!(self, WindowMessage::PrimaryMonitor)?.map(|m| MonitorHandleWrapper(m).into()))
} }
fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
let (tx, rx) = channel();
let _ = send_user_message(
&self.context,
Message::Window(self.window_id, WindowMessage::MonitorFromPoint(tx, (x, y))),
);
Ok(
rx.recv()
.map_err(|_| crate::Error::FailedToReceiveMessage)?
.map(|m| MonitorHandleWrapper(m).into()),
)
}
fn available_monitors(&self) -> Result<Vec<Monitor>> { fn available_monitors(&self) -> Result<Vec<Monitor>> {
Ok( Ok(
window_getter!(self, WindowMessage::AvailableMonitors)? window_getter!(self, WindowMessage::AvailableMonitors)?
@ -2149,6 +2165,15 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
.map(|m| MonitorHandleWrapper(m).into()) .map(|m| MonitorHandleWrapper(m).into())
} }
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
self
.context
.main_thread
.window_target
.monitor_from_point(x, y)
.map(|m| MonitorHandleWrapper(m).into())
}
fn available_monitors(&self) -> Vec<Monitor> { fn available_monitors(&self) -> Vec<Monitor> {
self self
.context .context
@ -2407,6 +2432,15 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
.map(|m| MonitorHandleWrapper(m).into()) .map(|m| MonitorHandleWrapper(m).into())
} }
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
self
.context
.main_thread
.window_target
.monitor_from_point(x, y)
.map(|m| MonitorHandleWrapper(m).into())
}
fn available_monitors(&self) -> Vec<Monitor> { fn available_monitors(&self) -> Vec<Monitor> {
self self
.context .context
@ -2643,6 +2677,9 @@ fn handle_user_message<T: UserEvent>(
WindowMessage::Title(tx) => tx.send(window.title()).unwrap(), WindowMessage::Title(tx) => tx.send(window.title()).unwrap(),
WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(), WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(), WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
WindowMessage::MonitorFromPoint(tx, (x, y)) => {
tx.send(window.monitor_from_point(x, y)).unwrap()
}
WindowMessage::AvailableMonitors(tx) => { WindowMessage::AvailableMonitors(tx) => {
tx.send(window.available_monitors().collect()).unwrap() tx.send(window.available_monitors().collect()).unwrap()
} }

View File

@ -300,6 +300,7 @@ pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'st
fn display_handle(&self) -> std::result::Result<DisplayHandle, raw_window_handle::HandleError>; fn display_handle(&self) -> std::result::Result<DisplayHandle, raw_window_handle::HandleError>;
fn primary_monitor(&self) -> Option<Monitor>; fn primary_monitor(&self) -> Option<Monitor>;
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor>;
fn available_monitors(&self) -> Vec<Monitor>; fn available_monitors(&self) -> Vec<Monitor>;
fn cursor_position(&self) -> Result<PhysicalPosition<f64>>; fn cursor_position(&self) -> Result<PhysicalPosition<f64>>;
@ -382,6 +383,7 @@ pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
) -> Result<DetachedWebview<T, Self>>; ) -> Result<DetachedWebview<T, Self>>;
fn primary_monitor(&self) -> Option<Monitor>; fn primary_monitor(&self) -> Option<Monitor>;
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor>;
fn available_monitors(&self) -> Vec<Monitor>; fn available_monitors(&self) -> Vec<Monitor>;
fn cursor_position(&self) -> Result<PhysicalPosition<f64>>; fn cursor_position(&self) -> Result<PhysicalPosition<f64>>;
@ -587,6 +589,9 @@ pub trait WindowDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 's
/// Returns None if it can't identify any monitor as a primary one. /// Returns None if it can't identify any monitor as a primary one.
fn primary_monitor(&self) -> Result<Option<Monitor>>; fn primary_monitor(&self) -> Result<Option<Monitor>>;
/// Returns the monitor that contains the given point.
fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>>;
/// Returns the list of all the monitors available on the system. /// Returns the list of all the monitors available on the system.
fn available_monitors(&self) -> Result<Vec<Monitor>>; fn available_monitors(&self) -> Result<Vec<Monitor>>;

View File

@ -63,6 +63,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
("title", true), ("title", true),
("current_monitor", true), ("current_monitor", true),
("primary_monitor", true), ("primary_monitor", true),
("monitor_from_point", true),
("available_monitors", true), ("available_monitors", true),
("cursor_position", true), ("cursor_position", true),
("theme", true), ("theme", true),

View File

@ -46,6 +46,8 @@
|`deny-maximize`|Denies the maximize command without any pre-configured scope.| |`deny-maximize`|Denies the maximize command without any pre-configured scope.|
|`allow-minimize`|Enables the minimize command without any pre-configured scope.| |`allow-minimize`|Enables the minimize command without any pre-configured scope.|
|`deny-minimize`|Denies the minimize command without any pre-configured scope.| |`deny-minimize`|Denies the minimize command without any pre-configured scope.|
|`allow-monitor-from-point`|Enables the monitor_from_point command without any pre-configured scope.|
|`deny-monitor-from-point`|Denies the monitor_from_point command without any pre-configured scope.|
|`allow-outer-position`|Enables the outer_position command without any pre-configured scope.| |`allow-outer-position`|Enables the outer_position command without any pre-configured scope.|
|`deny-outer-position`|Denies the outer_position command without any pre-configured scope.| |`deny-outer-position`|Denies the outer_position command without any pre-configured scope.|
|`allow-outer-size`|Enables the outer_size command without any pre-configured scope.| |`allow-outer-size`|Enables the outer_size command without any pre-configured scope.|

File diff suppressed because one or more lines are too long

View File

@ -592,6 +592,15 @@ macro_rules! shared_app_impl {
}) })
} }
/// Returns the monitor that contains the given point.
pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
Ok(match self.runtime() {
RuntimeOrDispatch::Runtime(h) => h.monitor_from_point(x, y).map(Into::into),
RuntimeOrDispatch::RuntimeHandle(h) => h.monitor_from_point(x, y).map(Into::into),
_ => unreachable!(),
})
}
/// Returns the list of all the monitors available on the system. /// Returns the list of all the monitors available on the system.
pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> { pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
Ok(match self.runtime() { Ok(match self.runtime() {

View File

@ -235,6 +235,10 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
unimplemented!() unimplemented!()
} }
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
unimplemented!()
}
fn available_monitors(&self) -> Vec<Monitor> { fn available_monitors(&self) -> Vec<Monitor> {
unimplemented!() unimplemented!()
} }
@ -650,6 +654,10 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
Ok(None) Ok(None)
} }
fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
Ok(None)
}
fn available_monitors(&self) -> Result<Vec<Monitor>> { fn available_monitors(&self) -> Result<Vec<Monitor>> {
Ok(Vec::new()) Ok(Vec::new())
} }
@ -1054,6 +1062,10 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
unimplemented!() unimplemented!()
} }
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
unimplemented!()
}
fn available_monitors(&self) -> Vec<Monitor> { fn available_monitors(&self) -> Vec<Monitor> {
unimplemented!() unimplemented!()
} }

View File

@ -1160,6 +1160,11 @@ impl<R: Runtime> WebviewWindow<R> {
self.webview.window().primary_monitor() self.webview.window().primary_monitor()
} }
/// Returns the monitor that contains the given point.
pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
self.webview.window().monitor_from_point(x, y)
}
/// Returns the list of all the monitors available on the system. /// Returns the list of all the monitors available on the system.
pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> { pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
self.webview.window().available_monitors() self.webview.window().available_monitors()

View File

@ -1437,6 +1437,16 @@ impl<R: Runtime> Window<R> {
.map_err(Into::into) .map_err(Into::into)
} }
/// Returns the monitor that contains the given point.
pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
self
.window
.dispatcher
.monitor_from_point(x, y)
.map(|m| m.map(Into::into))
.map_err(Into::into)
}
/// Returns the primary monitor of the system. /// Returns the primary monitor of the system.
/// ///
/// Returns None if it can't identify any monitor as a primary one. /// Returns None if it can't identify any monitor as a primary one.

View File

@ -172,6 +172,17 @@ mod desktop_commands {
} }
Ok(()) Ok(())
} }
#[command(root = "crate")]
pub async fn monitor_from_point<R: Runtime>(
window: Window<R>,
label: Option<String>,
x: f64,
y: f64,
) -> crate::Result<Option<Monitor>> {
let window = get_window(window, label)?;
window.monitor_from_point(x, y)
}
} }
/// Initializes the plugin. /// Initializes the plugin.
@ -222,6 +233,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
desktop_commands::title, desktop_commands::title,
desktop_commands::current_monitor, desktop_commands::current_monitor,
desktop_commands::primary_monitor, desktop_commands::primary_monitor,
desktop_commands::monitor_from_point,
desktop_commands::available_monitors, desktop_commands::available_monitors,
desktop_commands::cursor_position, desktop_commands::cursor_position,
desktop_commands::theme, desktop_commands::theme,

View File

@ -2240,6 +2240,23 @@ async function primaryMonitor(): Promise<Monitor | null> {
) )
} }
/**
* Returns the monitor that contains the given point. Returns `null` if can't find any.
* @example
* ```typescript
* import { monitorFromPoint } from '@tauri-apps/api/window';
* const monitor = monitorFromPoint();
* ```
*
* @since 1.0.0
*/
async function monitorFromPoint(x: number, y: number): Promise<Monitor | null> {
return invoke<Monitor | null>('plugin:window|monitor_from_point', {
x,
y
}).then(mapMonitor)
}
/** /**
* Returns the list of all the monitors available on the system. * Returns the list of all the monitors available on the system.
* @example * @example
@ -2285,6 +2302,7 @@ export {
Effect, Effect,
EffectState, EffectState,
currentMonitor, currentMonitor,
monitorFromPoint,
primaryMonitor, primaryMonitor,
availableMonitors, availableMonitors,
cursorPosition cursorPosition