Skip to content

Commit

Permalink
feat: expose set_title for MacOS tray, fixes tauri-apps#3322
Browse files Browse the repository at this point in the history
  • Loading branch information
vojty committed Sep 20, 2022
1 parent 447370f commit 120c2ac
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 2 deletions.
8 changes: 8 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ pub enum TrayMessage {
UpdateIcon(Icon),
#[cfg(target_os = "macos")]
UpdateIconAsTemplate(bool),
#[cfg(target_os = "macos")]
UpdateTitle(String),
Create(SystemTray, Sender<Result<()>>),
Destroy,
}
Expand Down Expand Up @@ -2477,6 +2479,12 @@ fn handle_user_message<T: UserEvent>(
tray.set_icon_as_template(is_template);
}
}
#[cfg(target_os = "macos")]
TrayMessage::UpdateTitle(title) => {
if let Some(tray) = &mut *tray_context.tray.lock().unwrap() {
tray.set_title(&title);
}
}
TrayMessage::Create(_tray, _tx) => {
// already handled
}
Expand Down
11 changes: 11 additions & 0 deletions core/tauri-runtime-wry/src/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ impl<T: UserEvent> TrayHandle for SystemTrayHandle<T> {
.map_err(|_| Error::FailedToSendMessage)
}

#[cfg(target_os = "macos")]
fn set_title(&self, title: &str) -> tauri_runtime::Result<()> {
self
.proxy
.send_event(Message::Tray(
self.id,
TrayMessage::UpdateTitle(title.to_owned()),
))
.map_err(|_| Error::FailedToSendMessage)
}

fn destroy(&self) -> Result<()> {
self
.proxy
Expand Down
13 changes: 13 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct SystemTray {
pub icon_as_template: bool,
#[cfg(target_os = "macos")]
pub menu_on_left_click: bool,
#[cfg(target_os = "macos")]
pub title: Option<String>,
pub on_event: Option<Box<TrayEventHandler>>,
}

Expand Down Expand Up @@ -81,6 +83,8 @@ impl Clone for SystemTray {
icon_as_template: self.icon_as_template,
#[cfg(target_os = "macos")]
menu_on_left_click: self.menu_on_left_click,
#[cfg(target_os = "macos")]
title: self.title.clone(),
}
}
}
Expand All @@ -96,6 +100,8 @@ impl Default for SystemTray {
icon_as_template: false,
#[cfg(target_os = "macos")]
menu_on_left_click: false,
#[cfg(target_os = "macos")]
title: None,
on_event: None,
}
}
Expand Down Expand Up @@ -142,6 +148,13 @@ impl SystemTray {
self
}

#[cfg(target_os = "macos")]
#[must_use]
pub fn with_title(mut self, title: &str) -> Self {
self.title = Some(title.to_owned());
self
}

/// Sets the menu to show when the system tray is right clicked.
#[must_use]
pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-runtime/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ pub trait TrayHandle: fmt::Debug + Clone + Send + Sync {
fn update_item(&self, id: u16, update: MenuUpdate) -> crate::Result<()>;
#[cfg(target_os = "macos")]
fn set_icon_as_template(&self, is_template: bool) -> crate::Result<()>;
#[cfg(target_os = "macos")]
fn set_title(&self, title: &str) -> crate::Result<()>;
fn destroy(&self) -> crate::Result<()>;
}

Expand Down
6 changes: 5 additions & 1 deletion core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,8 @@ pub struct SystemTrayConfig {
alias = "menu-on-left-click"
)]
pub menu_on_left_click: bool,
/// Title for MacOS tray
pub title: Option<String>,
}

fn default_tray_menu_on_left_click() -> bool {
Expand Down Expand Up @@ -3297,12 +3299,14 @@ mod build {
let icon_as_template = self.icon_as_template;
let menu_on_left_click = self.menu_on_left_click;
let icon_path = path_buf_lit(&self.icon_path);
let title = opt_lit(self.title.as_ref());
literal_struct!(
tokens,
SystemTrayConfig,
icon_path,
icon_as_template,
menu_on_left_click
menu_on_left_click,
title
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions core/tauri/src/app/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct SystemTray {
menu_on_left_click_set: bool,
#[cfg(target_os = "macos")]
icon_as_template_set: bool,
#[cfg(target_os = "macos")]
title: Option<String>,
}

impl fmt::Debug for SystemTray {
Expand Down Expand Up @@ -94,6 +96,8 @@ impl Default for SystemTray {
icon_as_template_set: false,
#[cfg(target_os = "macos")]
menu_on_left_click_set: false,
#[cfg(target_os = "macos")]
title: None,
}
}
}
Expand Down Expand Up @@ -228,6 +232,31 @@ impl SystemTray {
self
}

/// Sets the menu title`
///
/// # Examples
///
/// ```
/// use tauri::SystemTray;
///
/// tauri::Builder::default()
/// .setup(|app| {
/// let mut tray_builder = SystemTray::new();
/// #[cfg(target_os = "macos")]
/// {
/// tray_builder = tray_builder.with_title("My App");
/// }
/// let tray_handle = tray_builder.build(app)?;
/// Ok(())
/// });
/// ```
#[cfg(target_os = "macos")]
#[must_use]
pub fn with_title(mut self, title: &str) -> Self {
self.title = Some(title.to_owned());
self
}

/// Sets the event listener for this system tray.
///
/// # Examples
Expand Down Expand Up @@ -342,6 +371,14 @@ impl SystemTray {
.as_ref()
.map_or(false, |t| t.menu_on_left_click);
}
if self.title.is_none() {
self.title = manager
.config()
.tauri
.system_tray
.as_ref()
.and_then(|t| t.title.clone())
}
}

let tray_id = self.id.clone();
Expand Down Expand Up @@ -564,6 +601,12 @@ impl<R: Runtime> SystemTrayHandle<R> {
.map_err(Into::into)
}

/// Adds the title to the tray menu
#[cfg(target_os = "macos")]
pub fn set_title(&self, title: &str) -> crate::Result<()> {
self.inner.set_title(title).map_err(Into::into)
}

/// Destroys this system tray.
pub fn destroy(&self) -> crate::Result<()> {
self.inner.destroy().map_err(Into::into)
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ impl TrayHandle for MockTrayHandler {
Ok(())
}

#[cfg(target_os = "macos")]
fn set_title(&self, title: &str) -> tauri_runtime::Result<()> {
Ok(())
}

fn destroy(&self) -> Result<()> {
Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions examples/api/src-tauri/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ fn create_tray(app: &tauri::App) -> tauri::Result<()> {
.add_item(CustomMenuItem::new("new", "New window"))
.add_item(CustomMenuItem::new("icon_1", "Tray Icon 1"))
.add_item(CustomMenuItem::new("icon_2", "Tray Icon 2"))
.add_item(CustomMenuItem::new("set_title", "Set Title (MacOS)"))
.add_item(CustomMenuItem::new("switch_menu", "Switch Menu"))
.add_item(CustomMenuItem::new("exit_app", "Quit"))
.add_item(CustomMenuItem::new("destroy", "Destroy"));

let tray_menu2 = SystemTrayMenu::new()
.add_item(CustomMenuItem::new("toggle", "Toggle"))
.add_item(CustomMenuItem::new("new", "New window"))
Expand Down Expand Up @@ -118,6 +120,10 @@ fn create_tray(app: &tauri::App) -> tauri::Result<()> {
.build()
.unwrap();
}
"set_title" => {
#[cfg(target_os = "macos")]
tray_handle.set_title("Tauri").unwrap();
}
"icon_1" => {
#[cfg(target_os = "macos")]
tray_handle.set_icon_as_template(true).unwrap();
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,13 @@
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
"default": true,
"type": "boolean"
},
"title": {
"description": "Title for MacOS tray",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
Expand Down

0 comments on commit 120c2ac

Please sign in to comment.