Skip to content

Commit 84c4159

Browse files
feat(core): add monitor functions for App/AppHandle, closes #6394 (#6403)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent f280dcf commit 84c4159

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

.changes/core-app-montior.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'minor:feat'
3+
---
4+
5+
Add `App::primary_monitor`, `App::available_monitors`, `AppHandle::primary_monitor`, and `AppHandle::available_monitors`

.changes/runtime-monitor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": "minor:feat"
3+
"tauri-runtime-wry": "minor:feat"
4+
---
5+
6+
Added `primary_monitor` and `available_monitors` to `Runtime` and `RuntimeHandle`.

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,25 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
19121912
self.context.main_thread.window_target.raw_display_handle()
19131913
}
19141914

1915+
fn primary_monitor(&self) -> Option<Monitor> {
1916+
self
1917+
.context
1918+
.main_thread
1919+
.window_target
1920+
.primary_monitor()
1921+
.map(|m| MonitorHandleWrapper(m).into())
1922+
}
1923+
1924+
fn available_monitors(&self) -> Vec<Monitor> {
1925+
self
1926+
.context
1927+
.main_thread
1928+
.window_target
1929+
.available_monitors()
1930+
.map(|m| MonitorHandleWrapper(m).into())
1931+
.collect()
1932+
}
1933+
19151934
#[cfg(target_os = "macos")]
19161935
fn show(&self) -> tauri_runtime::Result<()> {
19171936
send_user_message(
@@ -2090,6 +2109,25 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
20902109
.push(Arc::new(Box::new(f)));
20912110
}
20922111

2112+
fn primary_monitor(&self) -> Option<Monitor> {
2113+
self
2114+
.context
2115+
.main_thread
2116+
.window_target
2117+
.primary_monitor()
2118+
.map(|m| MonitorHandleWrapper(m).into())
2119+
}
2120+
2121+
fn available_monitors(&self) -> Vec<Monitor> {
2122+
self
2123+
.context
2124+
.main_thread
2125+
.window_target
2126+
.available_monitors()
2127+
.map(|m| MonitorHandleWrapper(m).into())
2128+
.collect()
2129+
}
2130+
20932131
#[cfg(target_os = "macos")]
20942132
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
20952133
self

core/tauri-runtime/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'st
387387

388388
fn raw_display_handle(&self) -> RawDisplayHandle;
389389

390+
fn primary_monitor(&self) -> Option<Monitor>;
391+
fn available_monitors(&self) -> Vec<Monitor>;
392+
390393
/// Shows the application, but does not automatically focus it.
391394
#[cfg(target_os = "macos")]
392395
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
@@ -460,6 +463,9 @@ pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
460463
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
461464
fn on_system_tray_event<F: Fn(TrayId, &SystemTrayEvent) + Send + 'static>(&mut self, f: F);
462465

466+
fn primary_monitor(&self) -> Option<Monitor>;
467+
fn available_monitors(&self) -> Vec<Monitor>;
468+
463469
/// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
464470
#[cfg(target_os = "macos")]
465471
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]

core/tauri/src/app.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
utils::config::Config,
2525
utils::{assets::Assets, Env},
2626
Context, DeviceEventFilter, EventLoopMessage, Icon, Invoke, InvokeError, InvokeResponse, Manager,
27-
Runtime, Scopes, StateManager, Theme, Window,
27+
Monitor, Runtime, Scopes, StateManager, Theme, Window,
2828
};
2929

3030
#[cfg(feature = "protocol-asset")]
@@ -570,6 +570,35 @@ macro_rules! shared_app_impl {
570570
}
571571
}
572572

573+
/// Returns the primary monitor of the system.
574+
///
575+
/// Returns None if it can't identify any monitor as a primary one.
576+
pub fn primary_monitor(&self) -> crate::Result<Option<Monitor>> {
577+
Ok(match self.runtime() {
578+
RuntimeOrDispatch::Runtime(h) => h
579+
.primary_monitor().map(Into::into),
580+
RuntimeOrDispatch::RuntimeHandle(h) => h
581+
.primary_monitor().map(Into::into),
582+
_ => unreachable!()
583+
})
584+
}
585+
586+
/// Returns the list of all the monitors available on the system.
587+
pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
588+
Ok(match self.runtime() {
589+
RuntimeOrDispatch::Runtime(h) => h
590+
.available_monitors()
591+
.into_iter()
592+
.map(Into::into)
593+
.collect(),
594+
RuntimeOrDispatch::RuntimeHandle(h) => h
595+
.available_monitors()
596+
.into_iter()
597+
.map(Into::into)
598+
.collect(),
599+
_ => unreachable!()
600+
})
601+
}
573602
/// Returns the default window icon.
574603
pub fn default_window_icon(&self) -> Option<&Icon> {
575604
self.manager.inner.default_window_icon.as_ref()

core/tauri/src/test/mock_runtime.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
154154
return unimplemented!();
155155
}
156156

157+
fn primary_monitor(&self) -> Option<Monitor> {
158+
unimplemented!()
159+
}
160+
161+
fn available_monitors(&self) -> Vec<Monitor> {
162+
unimplemented!()
163+
}
164+
157165
/// Shows the application, but does not automatically focus it.
158166
#[cfg(target_os = "macos")]
159167
fn show(&self) -> Result<()> {
@@ -815,6 +823,14 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
815823
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
816824
fn on_system_tray_event<F: Fn(TrayId, &SystemTrayEvent) + Send + 'static>(&mut self, f: F) {}
817825

826+
fn primary_monitor(&self) -> Option<Monitor> {
827+
unimplemented!()
828+
}
829+
830+
fn available_monitors(&self) -> Vec<Monitor> {
831+
unimplemented!()
832+
}
833+
818834
#[cfg(target_os = "macos")]
819835
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
820836
fn set_activation_policy(&mut self, activation_policy: tauri_runtime::ActivationPolicy) {}

0 commit comments

Comments
 (0)