Skip to content

Commit c5f9cd1

Browse files
committed
Apply review suggestions
1 parent 1b8707f commit c5f9cd1

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

examples/tao.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use tao::event_loop::{ControlFlow, EventLoopBuilder};
88
use tray_icon::{
9-
menu::{AboutMetadata, Menu, MenuEvent, MenuItem, CheckMenuItem, IconMenuItem, PredefinedMenuItem, Submenu},
9+
menu::{
10+
AboutMetadata, CheckMenuItem, IconMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem,
11+
Submenu,
12+
},
1013
TrayIconBuilder, TrayIconEvent,
1114
};
1215

@@ -17,7 +20,12 @@ fn main() {
1720

1821
let tray_menu = Menu::new();
1922

20-
let icon_i = IconMenuItem::new("Icon", true, Some(menu_icon(std::path::Path::new(path))), None);
23+
let icon_i = IconMenuItem::new(
24+
"Icon",
25+
true,
26+
Some(load_menu_icon(std::path::Path::new(path))),
27+
None,
28+
);
2129
let check_i = CheckMenuItem::new("Check", true, false, None);
2230
let subitem_i = MenuItem::new("Subitem", true, None);
2331
let submenu_i = Submenu::new("Submenu", true);
@@ -52,7 +60,7 @@ fn main() {
5260
std::thread::sleep(std::time::Duration::from_millis(16));
5361

5462
if let tao::event::Event::NewEvents(tao::event::StartCause::Init) = event {
55-
let icon = load_icon(std::path::Path::new(path));
63+
let icon = load_try_icon(std::path::Path::new(path));
5664

5765
// We create the icon once the event loop is actually running
5866
// to prevent issues like https://github.com/tauri-apps/tray-icon/issues/90
@@ -90,26 +98,21 @@ fn main() {
9098
})
9199
}
92100

93-
fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
94-
let (icon_rgba, icon_width, icon_height) = {
95-
let image = image::open(path)
96-
.expect("Failed to open icon path")
97-
.into_rgba8();
98-
let (width, height) = image.dimensions();
99-
let rgba = image.into_raw();
100-
(rgba, width, height)
101-
};
101+
fn load_icon(path: &std::path::Path) -> (Vec<u8>, u32, u32) {
102+
let image = image::open(path)
103+
.expect("Failed to open icon path")
104+
.into_rgba8();
105+
let (width, height) = image.dimensions();
106+
let rgba = image.into_raw();
107+
(rgba, width, height)
108+
}
109+
110+
fn load_try_icon(path: &std::path::Path) -> tray_icon::Icon {
111+
let (icon_rgba, icon_width, icon_height) = load_icon(path);
102112
tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
103113
}
104114

105-
fn menu_icon(path: &std::path::Path) -> muda::Icon {
106-
let (icon_rgba, icon_width, icon_height) = {
107-
let image = image::open(path)
108-
.expect("Failed to open icon path")
109-
.into_rgba8();
110-
let (width, height) = image.dimensions();
111-
let rgba = image.into_raw();
112-
(rgba, width, height)
113-
};
115+
fn load_menu_icon(path: &std::path::Path) -> muda::Icon {
116+
let (icon_rgba, icon_width, icon_height) = load_icon(path);
114117
muda::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
115118
}

src/platform_impl/linux/menu.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use muda::{AboutDialog, PredefinedMenuItemType};
1+
use muda::{AboutDialog, PredefinedMenuItemKind};
22

33
use super::tray::Tray;
44

@@ -8,7 +8,7 @@ pub struct StandardItem {
88
label: String,
99
enabled: bool,
1010
icon: Option<Vec<u8>>,
11-
predefined_menu_item_type: Option<PredefinedMenuItemType>,
11+
predefined_menu_item_kind: Option<PredefinedMenuItemKind>,
1212
}
1313

1414
#[derive(Debug, Clone)]
@@ -61,7 +61,7 @@ impl From<muda::MenuItemKind> for MenuItem {
6161
label: menu_item.text().replace('&', ""),
6262
enabled: menu_item.is_enabled(),
6363
icon: None,
64-
predefined_menu_item_type: None,
64+
predefined_menu_item_kind: None,
6565
}
6666
.into(),
6767
muda::MenuItemKind::Submenu(submenu) => SubMenuItem {
@@ -71,22 +71,22 @@ impl From<muda::MenuItemKind> for MenuItem {
7171
}
7272
.into(),
7373
muda::MenuItemKind::Predefined(predefined_menu_item) => {
74-
match predefined_menu_item.predefined_item_type() {
75-
Some(PredefinedMenuItemType::Separator) => MenuItem::Separator,
76-
Some(predefined_menu_item_type) => StandardItem {
74+
match predefined_menu_item.predefined_item_kind() {
75+
Some(PredefinedMenuItemKind::Separator) => MenuItem::Separator,
76+
Some(predefined_menu_item_kind) => StandardItem {
7777
id: predefined_menu_item.id().0.clone(),
7878
label: predefined_menu_item.text().replace('&', ""),
7979
enabled: true,
8080
icon: None,
81-
predefined_menu_item_type: Some(predefined_menu_item_type),
81+
predefined_menu_item_kind: Some(predefined_menu_item_kind),
8282
}
8383
.into(),
8484
_ => StandardItem {
8585
id: predefined_menu_item.id().0.clone(),
8686
label: predefined_menu_item.text().replace('&', ""),
8787
enabled: true,
8888
icon: None,
89-
predefined_menu_item_type: None,
89+
predefined_menu_item_kind: None,
9090
}
9191
.into(),
9292
}
@@ -102,8 +102,10 @@ impl From<muda::MenuItemKind> for MenuItem {
102102
id: icon_menu_item.id().0.clone(),
103103
label: icon_menu_item.text().replace('&', ""),
104104
enabled: icon_menu_item.is_enabled(),
105-
icon: icon_menu_item.icon().map(|icon| icon.to_png()),
106-
predefined_menu_item_type: None,
105+
icon: icon_menu_item
106+
.icon()
107+
.map(|icon| icon.to_pixbuf().save_to_bufferv("png", &[]).unwrap()),
108+
predefined_menu_item_kind: None,
107109
}
108110
.into(),
109111
}
@@ -115,8 +117,8 @@ impl From<MenuItem> for ksni::MenuItem<Tray> {
115117
match item {
116118
MenuItem::Standard(menu_item) => {
117119
let id = menu_item.id;
118-
match menu_item.predefined_menu_item_type {
119-
Some(PredefinedMenuItemType::About(Some(metadata))) => {
120+
match menu_item.predefined_menu_item_kind {
121+
Some(PredefinedMenuItemKind::About(Some(metadata))) => {
120122
let about_dialog = AboutDialog::new(metadata);
121123
ksni::menu::StandardItem {
122124
label: menu_item.label,

src/platform_impl/linux/mod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ pub struct TrayIcon {
1818
impl TrayIcon {
1919
pub fn new(id: TrayIconId, attrs: TrayIconAttributes) -> crate::Result<Self> {
2020
let icon = attrs.icon.map(|icon| icon.inner.into());
21-
22-
let title = title_or_pkg_name(attrs.title.unwrap_or_default());
23-
21+
let title = attrs.title.unwrap_or_default();
2422
let tooltip = attrs.tooltip.unwrap_or_default();
2523

2624
let menu = attrs
@@ -77,7 +75,6 @@ impl TrayIcon {
7775
.map(AsRef::as_ref)
7876
.unwrap_or_default()
7977
.to_string();
80-
let title = title_or_pkg_name(title);
8178

8279
self.tray_handle.update(|tray| {
8380
tray.set_title(title);
@@ -100,11 +97,3 @@ impl TrayIcon {
10097
None
10198
}
10299
}
103-
104-
fn title_or_pkg_name(title: String) -> String {
105-
if !title.is_empty() {
106-
title
107-
} else {
108-
env!("CARGO_PKG_NAME").into()
109-
}
110-
}

0 commit comments

Comments
 (0)