Skip to content

Commit ec0e092

Browse files
authored
feat: adds monitor_from_point method (#9770)
1 parent 31aa90f commit ec0e092

File tree

13 files changed

+122
-1
lines changed

13 files changed

+122
-1
lines changed

.changes/monitor-from-point-js.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'patch:feat'
3+
---
4+
5+
Add `monitorFromPoint` function in `window` module to get the monitor from a given point.

.changes/monitor-from-point.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:feat'
3+
---
4+
5+
Add `App/AppHandle/Window/Webview/WebviewWindow::monitor_from_point(x, y)` getter to get the monitor from a given point.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ pub enum WindowMessage {
10921092
Title(Sender<String>),
10931093
CurrentMonitor(Sender<Option<MonitorHandle>>),
10941094
PrimaryMonitor(Sender<Option<MonitorHandle>>),
1095+
MonitorFromPoint(Sender<Option<MonitorHandle>>, (f64, f64)),
10951096
AvailableMonitors(Sender<Vec<MonitorHandle>>),
10961097
#[cfg(any(
10971098
target_os = "linux",
@@ -1578,6 +1579,21 @@ impl<T: UserEvent> WindowDispatch<T> for WryWindowDispatcher<T> {
15781579
Ok(window_getter!(self, WindowMessage::PrimaryMonitor)?.map(|m| MonitorHandleWrapper(m).into()))
15791580
}
15801581

1582+
fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
1583+
let (tx, rx) = channel();
1584+
1585+
let _ = send_user_message(
1586+
&self.context,
1587+
Message::Window(self.window_id, WindowMessage::MonitorFromPoint(tx, (x, y))),
1588+
);
1589+
1590+
Ok(
1591+
rx.recv()
1592+
.map_err(|_| crate::Error::FailedToReceiveMessage)?
1593+
.map(|m| MonitorHandleWrapper(m).into()),
1594+
)
1595+
}
1596+
15811597
fn available_monitors(&self) -> Result<Vec<Monitor>> {
15821598
Ok(
15831599
window_getter!(self, WindowMessage::AvailableMonitors)?
@@ -2149,6 +2165,15 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
21492165
.map(|m| MonitorHandleWrapper(m).into())
21502166
}
21512167

2168+
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
2169+
self
2170+
.context
2171+
.main_thread
2172+
.window_target
2173+
.monitor_from_point(x, y)
2174+
.map(|m| MonitorHandleWrapper(m).into())
2175+
}
2176+
21522177
fn available_monitors(&self) -> Vec<Monitor> {
21532178
self
21542179
.context
@@ -2407,6 +2432,15 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
24072432
.map(|m| MonitorHandleWrapper(m).into())
24082433
}
24092434

2435+
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
2436+
self
2437+
.context
2438+
.main_thread
2439+
.window_target
2440+
.monitor_from_point(x, y)
2441+
.map(|m| MonitorHandleWrapper(m).into())
2442+
}
2443+
24102444
fn available_monitors(&self) -> Vec<Monitor> {
24112445
self
24122446
.context
@@ -2643,6 +2677,9 @@ fn handle_user_message<T: UserEvent>(
26432677
WindowMessage::Title(tx) => tx.send(window.title()).unwrap(),
26442678
WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
26452679
WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
2680+
WindowMessage::MonitorFromPoint(tx, (x, y)) => {
2681+
tx.send(window.monitor_from_point(x, y)).unwrap()
2682+
}
26462683
WindowMessage::AvailableMonitors(tx) => {
26472684
tx.send(window.available_monitors().collect()).unwrap()
26482685
}

core/tauri-runtime/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'st
300300
fn display_handle(&self) -> std::result::Result<DisplayHandle, raw_window_handle::HandleError>;
301301

302302
fn primary_monitor(&self) -> Option<Monitor>;
303+
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor>;
303304
fn available_monitors(&self) -> Vec<Monitor>;
304305

305306
fn cursor_position(&self) -> Result<PhysicalPosition<f64>>;
@@ -382,6 +383,7 @@ pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
382383
) -> Result<DetachedWebview<T, Self>>;
383384

384385
fn primary_monitor(&self) -> Option<Monitor>;
386+
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor>;
385387
fn available_monitors(&self) -> Vec<Monitor>;
386388

387389
fn cursor_position(&self) -> Result<PhysicalPosition<f64>>;
@@ -587,6 +589,9 @@ pub trait WindowDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 's
587589
/// Returns None if it can't identify any monitor as a primary one.
588590
fn primary_monitor(&self) -> Result<Option<Monitor>>;
589591

592+
/// Returns the monitor that contains the given point.
593+
fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>>;
594+
590595
/// Returns the list of all the monitors available on the system.
591596
fn available_monitors(&self) -> Result<Vec<Monitor>>;
592597

core/tauri/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
6363
("title", true),
6464
("current_monitor", true),
6565
("primary_monitor", true),
66+
("monitor_from_point", true),
6667
("available_monitors", true),
6768
("cursor_position", true),
6869
("theme", true),

core/tauri/permissions/window/autogenerated/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
|`deny-maximize`|Denies the maximize command without any pre-configured scope.|
4747
|`allow-minimize`|Enables the minimize command without any pre-configured scope.|
4848
|`deny-minimize`|Denies the minimize command without any pre-configured scope.|
49+
|`allow-monitor-from-point`|Enables the monitor_from_point command without any pre-configured scope.|
50+
|`deny-monitor-from-point`|Denies the monitor_from_point command without any pre-configured scope.|
4951
|`allow-outer-position`|Enables the outer_position command without any pre-configured scope.|
5052
|`deny-outer-position`|Denies the outer_position command without any pre-configured scope.|
5153
|`allow-outer-size`|Enables the outer_size command without any pre-configured scope.|

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/app.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,15 @@ macro_rules! shared_app_impl {
592592
})
593593
}
594594

595+
/// Returns the monitor that contains the given point.
596+
pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
597+
Ok(match self.runtime() {
598+
RuntimeOrDispatch::Runtime(h) => h.monitor_from_point(x, y).map(Into::into),
599+
RuntimeOrDispatch::RuntimeHandle(h) => h.monitor_from_point(x, y).map(Into::into),
600+
_ => unreachable!(),
601+
})
602+
}
603+
595604
/// Returns the list of all the monitors available on the system.
596605
pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
597606
Ok(match self.runtime() {

core/tauri/src/test/mock_runtime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
235235
unimplemented!()
236236
}
237237

238+
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
239+
unimplemented!()
240+
}
241+
238242
fn available_monitors(&self) -> Vec<Monitor> {
239243
unimplemented!()
240244
}
@@ -650,6 +654,10 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
650654
Ok(None)
651655
}
652656

657+
fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
658+
Ok(None)
659+
}
660+
653661
fn available_monitors(&self) -> Result<Vec<Monitor>> {
654662
Ok(Vec::new())
655663
}
@@ -1054,6 +1062,10 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
10541062
unimplemented!()
10551063
}
10561064

1065+
fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
1066+
unimplemented!()
1067+
}
1068+
10571069
fn available_monitors(&self) -> Vec<Monitor> {
10581070
unimplemented!()
10591071
}

core/tauri/src/webview/webview_window.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,11 @@ impl<R: Runtime> WebviewWindow<R> {
11601160
self.webview.window().primary_monitor()
11611161
}
11621162

1163+
/// Returns the monitor that contains the given point.
1164+
pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
1165+
self.webview.window().monitor_from_point(x, y)
1166+
}
1167+
11631168
/// Returns the list of all the monitors available on the system.
11641169
pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
11651170
self.webview.window().available_monitors()

0 commit comments

Comments
 (0)