Skip to content

Commit dd4f13c

Browse files
1111mplucasfernog
andauthored
feat: add set_dock_visibility method (#13185)
* feat: add `set_dock_visibility` method Signed-off-by: The1111mp <The1111mp@outlook.com> * add api * retain focus * fmt * make SetDockVisibility message conditional (macos only) * lint --------- Signed-off-by: The1111mp <The1111mp@outlook.com> Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 7b14531 commit dd4f13c

13 files changed

Lines changed: 168 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
tauri: patch:feat
3+
tauri-runtime: patch:feat
4+
tauri-runtime-wry: patch:feat
5+
---
6+
7+
MacOS: Add `set_dock_visibility` method to support setting the visibility of the application in the dock.

.changes/setDockVisibility.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": minor:feat
3+
---
4+
5+
Added `app.setDockVisibility` for macOS.
6+

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,8 @@ pub enum Message<T: 'static> {
13631363
Task(Box<dyn FnOnce() + Send>),
13641364
#[cfg(target_os = "macos")]
13651365
SetActivationPolicy(ActivationPolicy),
1366+
#[cfg(target_os = "macos")]
1367+
SetDockVisibility(bool),
13661368
RequestExit(i32),
13671369
Application(ApplicationMessage),
13681370
Window(WindowId, WindowMessage),
@@ -2435,6 +2437,11 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
24352437
)
24362438
}
24372439

2440+
#[cfg(target_os = "macos")]
2441+
fn set_dock_visibility(&self, visible: bool) -> Result<()> {
2442+
send_user_message(&self.context, Message::SetDockVisibility(visible))
2443+
}
2444+
24382445
fn request_exit(&self, code: i32) -> Result<()> {
24392446
// NOTE: request_exit cannot use the `send_user_message` function because it accesses the event loop callback
24402447
self
@@ -2844,6 +2851,11 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
28442851
.set_activation_policy(tao_activation_policy(activation_policy));
28452852
}
28462853

2854+
#[cfg(target_os = "macos")]
2855+
fn set_dock_visibility(&mut self, visible: bool) {
2856+
self.event_loop.set_dock_visibility(visible);
2857+
}
2858+
28472859
#[cfg(target_os = "macos")]
28482860
fn show(&self) {
28492861
self.event_loop.show_application();
@@ -3018,6 +3030,8 @@ fn handle_user_message<T: UserEvent>(
30183030
Message::SetActivationPolicy(activation_policy) => {
30193031
event_loop.set_activation_policy_at_runtime(tao_activation_policy(activation_policy))
30203032
}
3033+
#[cfg(target_os = "macos")]
3034+
Message::SetDockVisibility(visible) => event_loop.set_dock_visibility(visible),
30213035
Message::RequestExit(_code) => panic!("cannot handle RequestExit on the main thread"),
30223036
Message::Application(application_message) => match application_message {
30233037
#[cfg(target_os = "macos")]

crates/tauri-runtime/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'st
288288
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
289289
fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()>;
290290

291+
/// Sets the dock visibility for the application.
292+
///
293+
#[cfg(target_os = "macos")]
294+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
295+
fn set_dock_visibility(&self, visible: bool) -> Result<()>;
296+
291297
/// Requests an exit of the event loop.
292298
fn request_exit(&self, code: i32) -> Result<()>;
293299

@@ -431,6 +437,12 @@ pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
431437
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
432438
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);
433439

440+
/// Sets the dock visibility for the application.
441+
///
442+
#[cfg(target_os = "macos")]
443+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
444+
fn set_dock_visibility(&mut self, visible: bool);
445+
434446
/// Shows the application, but does not automatically focus it.
435447
#[cfg(target_os = "macos")]
436448
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]

crates/tauri/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
159159
("remove_data_store", false),
160160
("default_window_icon", false),
161161
("set_app_theme", false),
162+
("set_dock_visibility", false),
162163
],
163164
),
164165
(

crates/tauri/permissions/app/autogenerated/reference.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ Denies the set_app_theme command without any pre-configured scope.
229229
<tr>
230230
<td>
231231

232+
`core:app:allow-set-dock-visibility`
233+
234+
</td>
235+
<td>
236+
237+
Enables the set_dock_visibility command without any pre-configured scope.
238+
239+
</td>
240+
</tr>
241+
242+
<tr>
243+
<td>
244+
245+
`core:app:deny-set-dock-visibility`
246+
247+
</td>
248+
<td>
249+
250+
Denies the set_dock_visibility command without any pre-configured scope.
251+
252+
</td>
253+
</tr>
254+
255+
<tr>
256+
<td>
257+
232258
`core:app:allow-tauri-version`
233259

234260
</td>

crates/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.

crates/tauri/src/app.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,26 @@ impl<R: Runtime> AppHandle<R> {
601601
.set_activation_policy(activation_policy)
602602
.map_err(Into::into)
603603
}
604+
605+
/// Sets the dock visibility for the application.
606+
///
607+
/// # Examples
608+
/// ```,no_run
609+
/// tauri::Builder::default()
610+
/// .setup(move |app| {
611+
/// #[cfg(target_os = "macos")]
612+
/// app.handle().set_dock_visibility(false);
613+
/// Ok(())
614+
/// });
615+
/// ```
616+
#[cfg(target_os = "macos")]
617+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
618+
pub fn set_dock_visibility(&self, visible: bool) -> crate::Result<()> {
619+
self
620+
.runtime_handle
621+
.set_dock_visibility(visible)
622+
.map_err(Into::into)
623+
}
604624
}
605625

606626
impl<R: Runtime> Manager<R> for AppHandle<R> {
@@ -1130,6 +1150,27 @@ impl<R: Runtime> App<R> {
11301150
}
11311151
}
11321152

1153+
/// Sets the dock visibility for the application.
1154+
///
1155+
/// # Examples
1156+
/// ```,no_run
1157+
/// tauri::Builder::default()
1158+
/// .setup(move |app| {
1159+
/// #[cfg(target_os = "macos")]
1160+
/// app.set_dock_visibility(false);
1161+
/// Ok(())
1162+
/// });
1163+
/// ```
1164+
#[cfg(target_os = "macos")]
1165+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
1166+
pub fn set_dock_visibility(&mut self, visible: bool) {
1167+
if let Some(runtime) = self.runtime.as_mut() {
1168+
runtime.set_dock_visibility(visible);
1169+
} else {
1170+
let _ = self.app_handle().set_dock_visibility(visible);
1171+
}
1172+
}
1173+
11331174
/// Change the device event filter mode.
11341175
///
11351176
/// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]

crates/tauri/src/app/plugin.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ pub async fn set_app_theme<R: Runtime>(app: AppHandle<R>, theme: Option<Theme>)
8383
app.set_theme(theme);
8484
}
8585

86+
#[command(root = "crate")]
87+
pub async fn set_dock_visibility<R: Runtime>(
88+
app: AppHandle<R>,
89+
visible: bool,
90+
) -> crate::Result<()> {
91+
#[cfg(target_os = "macos")]
92+
{
93+
let mut focused_window = None;
94+
for window in app.manager.windows().into_values() {
95+
if window.is_focused().unwrap_or_default() {
96+
focused_window.replace(window);
97+
break;
98+
}
99+
}
100+
101+
app.set_dock_visibility(visible)?;
102+
103+
// retain focus
104+
if let Some(focused_window) = focused_window {
105+
let _ = focused_window.set_focus();
106+
}
107+
}
108+
#[cfg(not(target_os = "macos"))]
109+
let (_app, _visible) = (app, visible);
110+
Ok(())
111+
}
112+
86113
pub fn init<R: Runtime>() -> TauriPlugin<R> {
87114
Builder::new("app")
88115
.invoke_handler(crate::generate_handler![
@@ -97,6 +124,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
97124
remove_data_store,
98125
default_window_icon,
99126
set_app_theme,
127+
set_dock_visibility,
100128
])
101129
.build()
102130
}

crates/tauri/src/test/mock_runtime.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
134134
Ok(())
135135
}
136136

137+
#[cfg(target_os = "macos")]
138+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
139+
fn set_dock_visibility(&self, visible: bool) -> Result<()> {
140+
Ok(())
141+
}
142+
137143
fn request_exit(&self, code: i32) -> Result<()> {
138144
unimplemented!()
139145
}
@@ -1209,6 +1215,10 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
12091215
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
12101216
fn set_activation_policy(&mut self, activation_policy: tauri_runtime::ActivationPolicy) {}
12111217

1218+
#[cfg(target_os = "macos")]
1219+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
1220+
fn set_dock_visibility(&mut self, visible: bool) {}
1221+
12121222
#[cfg(target_os = "macos")]
12131223
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
12141224
fn show(&self) {}

0 commit comments

Comments
 (0)