Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaust committed Nov 10, 2024
1 parent 1b8707f commit c5f9cd1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
45 changes: 24 additions & 21 deletions examples/tao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use tao::event_loop::{ControlFlow, EventLoopBuilder};
use tray_icon::{
menu::{AboutMetadata, Menu, MenuEvent, MenuItem, CheckMenuItem, IconMenuItem, PredefinedMenuItem, Submenu},
menu::{
AboutMetadata, CheckMenuItem, IconMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem,
Submenu,
},
TrayIconBuilder, TrayIconEvent,
};

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

let tray_menu = Menu::new();

let icon_i = IconMenuItem::new("Icon", true, Some(menu_icon(std::path::Path::new(path))), None);
let icon_i = IconMenuItem::new(
"Icon",
true,
Some(load_menu_icon(std::path::Path::new(path))),
None,
);
let check_i = CheckMenuItem::new("Check", true, false, None);
let subitem_i = MenuItem::new("Subitem", true, None);
let submenu_i = Submenu::new("Submenu", true);
Expand Down Expand Up @@ -52,7 +60,7 @@ fn main() {
std::thread::sleep(std::time::Duration::from_millis(16));

if let tao::event::Event::NewEvents(tao::event::StartCause::Init) = event {
let icon = load_icon(std::path::Path::new(path));
let icon = load_try_icon(std::path::Path::new(path));

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

fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
fn load_icon(path: &std::path::Path) -> (Vec<u8>, u32, u32) {
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
}

fn load_try_icon(path: &std::path::Path) -> tray_icon::Icon {
let (icon_rgba, icon_width, icon_height) = load_icon(path);
tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}

fn menu_icon(path: &std::path::Path) -> muda::Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
fn load_menu_icon(path: &std::path::Path) -> muda::Icon {
let (icon_rgba, icon_width, icon_height) = load_icon(path);
muda::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}
26 changes: 14 additions & 12 deletions src/platform_impl/linux/menu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use muda::{AboutDialog, PredefinedMenuItemType};
use muda::{AboutDialog, PredefinedMenuItemKind};

use super::tray::Tray;

Expand All @@ -8,7 +8,7 @@ pub struct StandardItem {
label: String,
enabled: bool,
icon: Option<Vec<u8>>,
predefined_menu_item_type: Option<PredefinedMenuItemType>,
predefined_menu_item_kind: Option<PredefinedMenuItemKind>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -61,7 +61,7 @@ impl From<muda::MenuItemKind> for MenuItem {
label: menu_item.text().replace('&', ""),
enabled: menu_item.is_enabled(),
icon: None,
predefined_menu_item_type: None,
predefined_menu_item_kind: None,
}
.into(),
muda::MenuItemKind::Submenu(submenu) => SubMenuItem {
Expand All @@ -71,22 +71,22 @@ impl From<muda::MenuItemKind> for MenuItem {
}
.into(),
muda::MenuItemKind::Predefined(predefined_menu_item) => {
match predefined_menu_item.predefined_item_type() {
Some(PredefinedMenuItemType::Separator) => MenuItem::Separator,
Some(predefined_menu_item_type) => StandardItem {
match predefined_menu_item.predefined_item_kind() {
Some(PredefinedMenuItemKind::Separator) => MenuItem::Separator,
Some(predefined_menu_item_kind) => StandardItem {
id: predefined_menu_item.id().0.clone(),
label: predefined_menu_item.text().replace('&', ""),
enabled: true,
icon: None,
predefined_menu_item_type: Some(predefined_menu_item_type),
predefined_menu_item_kind: Some(predefined_menu_item_kind),
}
.into(),
_ => StandardItem {
id: predefined_menu_item.id().0.clone(),
label: predefined_menu_item.text().replace('&', ""),
enabled: true,
icon: None,
predefined_menu_item_type: None,
predefined_menu_item_kind: None,
}
.into(),
}
Expand All @@ -102,8 +102,10 @@ impl From<muda::MenuItemKind> for MenuItem {
id: icon_menu_item.id().0.clone(),
label: icon_menu_item.text().replace('&', ""),
enabled: icon_menu_item.is_enabled(),
icon: icon_menu_item.icon().map(|icon| icon.to_png()),
predefined_menu_item_type: None,
icon: icon_menu_item
.icon()
.map(|icon| icon.to_pixbuf().save_to_bufferv("png", &[]).unwrap()),
predefined_menu_item_kind: None,
}
.into(),
}
Expand All @@ -115,8 +117,8 @@ impl From<MenuItem> for ksni::MenuItem<Tray> {
match item {
MenuItem::Standard(menu_item) => {
let id = menu_item.id;
match menu_item.predefined_menu_item_type {
Some(PredefinedMenuItemType::About(Some(metadata))) => {
match menu_item.predefined_menu_item_kind {
Some(PredefinedMenuItemKind::About(Some(metadata))) => {
let about_dialog = AboutDialog::new(metadata);
ksni::menu::StandardItem {
label: menu_item.label,
Expand Down
13 changes: 1 addition & 12 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub struct TrayIcon {
impl TrayIcon {
pub fn new(id: TrayIconId, attrs: TrayIconAttributes) -> crate::Result<Self> {
let icon = attrs.icon.map(|icon| icon.inner.into());

let title = title_or_pkg_name(attrs.title.unwrap_or_default());

let title = attrs.title.unwrap_or_default();
let tooltip = attrs.tooltip.unwrap_or_default();

let menu = attrs
Expand Down Expand Up @@ -77,7 +75,6 @@ impl TrayIcon {
.map(AsRef::as_ref)
.unwrap_or_default()
.to_string();
let title = title_or_pkg_name(title);

self.tray_handle.update(|tray| {
tray.set_title(title);
Expand All @@ -100,11 +97,3 @@ impl TrayIcon {
None
}
}

fn title_or_pkg_name(title: String) -> String {
if !title.is_empty() {
title
} else {
env!("CARGO_PKG_NAME").into()
}
}

0 comments on commit c5f9cd1

Please sign in to comment.