Skip to content

Commit

Permalink
feat(macos): add SystemTray::set_tooltip, ref #409 (#410)
Browse files Browse the repository at this point in the history
* feat: add set_tool_tip to tray

* add changes file

* note: make clear that this is wip

* fix comments

* tooltip example

* prepare for future windows implementation

* fmt

* fix build

* fix build again

Co-authored-by: amrbashir <amr.bashir2015@gmail.com>
  • Loading branch information
pevers and amrbashir committed Aug 2, 2022
1 parent ed23378 commit 14e2656
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/set-tool-tip-tray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Implement `SystemTrayBuilder::with_tooltip` and `SystemTray::set_tooltip` on macOS.
1 change: 1 addition & 0 deletions examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn main() {

#[cfg(not(target_os = "linux"))]
let system_tray = SystemTrayBuilder::new(icon, Some(tray_menu))
.with_tooltip("tao - windowing creation library")
.build(&event_loop)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait SystemTrayBuilderExtLinux {
#[cfg(feature = "tray")]
impl SystemTrayBuilderExtLinux for SystemTrayBuilder {
fn with_temp_icon_dir<P: AsRef<Path>>(mut self, p: P) -> Self {
self.0.temp_icon_dir = Some(p.as_ref().to_path_buf());
self.platform_tray_builder.temp_icon_dir = Some(p.as_ref().to_path_buf());
self
}
}
6 changes: 3 additions & 3 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
menu::CustomMenuItem,
monitor::MonitorHandle,
platform_impl::{get_aux_state_mut, Parent},
window::{Icon, Window, WindowBuilder},
window::{Window, WindowBuilder},
};

#[cfg(feature = "tray")]
Expand Down Expand Up @@ -519,12 +519,12 @@ pub trait SystemTrayBuilderExtMacOS {
#[cfg(feature = "tray")]
impl SystemTrayBuilderExtMacOS for SystemTrayBuilder {
fn with_icon_as_template(mut self, is_template: bool) -> Self {
self.0.system_tray.icon_is_template = is_template;
self.platform_tray_builder.system_tray.icon_is_template = is_template;
self
}

fn with_menu_on_left_click(mut self, enable: bool) -> Self {
self.0.system_tray.menu_on_left_click = enable;
self.platform_tray_builder.system_tray.menu_on_left_click = enable;
self
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/platform_impl/linux/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl SystemTrayBuilder {
pub fn build<T: 'static>(
self,
window_target: &EventLoopWindowTarget<T>,
_tooltip: Option<String>,
) -> Result<RootSystemTray, OsError> {
let mut app_indicator = AppIndicator::new("tao application", "");

Expand Down Expand Up @@ -88,6 +89,8 @@ impl SystemTray {
self.path = icon_path;
}

pub fn set_tooltip(&self, _tooltip: &str) {}

pub fn set_menu(&mut self, tray_menu: &Menu) {
let mut menu =
tray_menu
Expand Down
15 changes: 14 additions & 1 deletion src/platform_impl/macos/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use cocoa::{
NSStatusBar, NSStatusItem, NSWindow,
},
base::{id, nil, NO, YES},
foundation::{NSAutoreleasePool, NSData, NSPoint, NSSize},
foundation::{NSAutoreleasePool, NSData, NSPoint, NSSize, NSString},
};
use objc::{
declare::ClassDecl,
Expand Down Expand Up @@ -58,6 +58,7 @@ impl SystemTrayBuilder {
pub fn build<T: 'static>(
self,
_window_target: &EventLoopWindowTarget<T>,
tooltip: Option<String>,
) -> Result<RootSystemTray, OsError> {
unsafe {
// use our existing status bar
Expand Down Expand Up @@ -90,6 +91,11 @@ impl SystemTrayBuilder {
(*tray_target).set_ivar("menu", menu.menu);
let () = msg_send![menu.menu, setDelegate: tray_target];
}

// attach tool_tip if provided
if let Some(tooltip) = tooltip {
self.system_tray.set_tooltip(&tooltip);
}
}

Ok(RootSystemTray(self.system_tray))
Expand Down Expand Up @@ -123,6 +129,13 @@ impl SystemTray {
}
}

pub fn set_tooltip(&self, tooltip: &str) {
unsafe {
let tooltip = NSString::alloc(nil).init_str(tooltip);
let _: () = msg_send![self.ns_status_bar.button(), setToolTip: tooltip];
}
}

fn create_button_with_icon(&self) {
const ICON_WIDTH: f64 = 18.0;
const ICON_HEIGHT: f64 = 18.0;
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/windows/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl SystemTrayBuilder {
pub fn build<T: 'static>(
self,
window_target: &EventLoopWindowTarget<T>,
_tooltip: Option<String>,
) -> Result<RootSystemTray, RootOsError> {
let hmenu: Option<HMENU> = self.tray_menu.map(|m| m.hmenu());

Expand Down Expand Up @@ -179,6 +180,10 @@ impl SystemTray {
}
}

pub fn set_tooltip(&self, _tooltip: &str) {
//
}

pub fn set_menu(&mut self, tray_menu: &Menu) {
unsafe {
// send the new menu to the subclass proc where we will update there
Expand Down
41 changes: 35 additions & 6 deletions src/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,32 @@ use crate::{
pub use crate::icon::{BadIcon, Icon};

/// Object that allows you to build SystemTray instance.
pub struct SystemTrayBuilder(pub(crate) SystemTrayBuilderPlatform);
pub struct SystemTrayBuilder {
pub(crate) platform_tray_builder: SystemTrayBuilderPlatform,
tooltip: Option<String>,
}

impl SystemTrayBuilder {
/// Creates a new SystemTray for platforms where this is appropriate.
pub fn new(icon: Icon, tray_menu: Option<ContextMenu>) -> Self {
Self(SystemTrayBuilderPlatform::new(
icon,
tray_menu.map(|m| m.0.menu_platform),
))
Self {
platform_tray_builder: SystemTrayBuilderPlatform::new(
icon,
tray_menu.map(|m| m.0.menu_platform),
),
tooltip: None,
}
}

/// Adds a tooltip for this tray icon.
///
/// ## Platform-specific:
///
/// - **Windows:** Not implemented.
/// - **Linux:** Unsupported
pub fn with_tooltip(mut self, tooltip: &str) -> Self {
self.tooltip = Some(tooltip.to_string());
self
}

/// Builds the SystemTray.
Expand All @@ -58,7 +75,9 @@ impl SystemTrayBuilder {
self,
window_target: &EventLoopWindowTarget<T>,
) -> Result<SystemTray, OsError> {
self.0.build(window_target)
self
.platform_tray_builder
.build(window_target, self.tooltip)
}
}

Expand All @@ -82,4 +101,14 @@ impl SystemTray {
pub fn set_menu(&mut self, tray_menu: &ContextMenu) {
self.0.set_menu(&tray_menu.0.menu_platform)
}

/// Sets the tooltip for this tray icon.
///
/// ## Platform-specific:
///
/// - **Windows:** Not implemented.
/// - **Linux:** Unsupported
pub fn set_tooltip(&mut self, tooltip: &str) {
self.0.set_tooltip(tooltip);
}
}

0 comments on commit 14e2656

Please sign in to comment.