Skip to content

Commit

Permalink
Merge pull request #40 from AmionSky/ksni-separator
Browse files Browse the repository at this point in the history
 Add menu item separator for ksni
  • Loading branch information
olback authored Oct 18, 2023
2 parents 04ee1d0 + ba0d4ae commit e3bd6c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ksni = ["dep:ksni"]
libappindicator = ["dep:libappindicator", "dep:gtk"]

[dependencies]
ksni = { version = "0.1.3", optional = true }
ksni = { version = "0.2.0", optional = true }
libappindicator = { version = "0.7", optional = true } # Tray icon
gtk = { version = "0.15", optional = true }

Expand Down
49 changes: 29 additions & 20 deletions src/api/linux_ksni/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use crate::{TIError, IconSource};
use ksni::{menu::StandardItem, Handle, Icon};
use std::sync::Arc;

struct TrayItem {
label: String,
action: Option<Arc<dyn Fn() + Send + Sync + 'static>>
enum TrayItem {
Label(String),
MenuItem {
label: String,
action: Arc<dyn Fn() + Send + Sync + 'static>
},
Separator
}

struct Tray {
Expand Down Expand Up @@ -47,25 +51,25 @@ impl ksni::Tray for Tray {
}

fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
self.actions.iter().map(|item| {
let action = item.action.clone();
if let Some(action) = action {
self.actions.iter().map(|item| match item {
TrayItem::Label(label) => StandardItem {
label: label.clone(),
enabled: false,
..Default::default()
}
.into(),
TrayItem::MenuItem { label, action } => {
let action = action.clone();
StandardItem {
label: item.label.clone(),
label: label.clone(),
activate: Box::new(move |_| {
action();
}),
..Default::default()
}
.into()
} else {
StandardItem {
label: item.label.clone(),
enabled: false,
..Default::default()
}
.into()
}
TrayItem::Separator => ksni::MenuItem::Separator,
}).collect()
}
}
Expand Down Expand Up @@ -96,10 +100,7 @@ impl TrayItemLinux {

pub fn add_label(&mut self, label: &str) -> Result<(), TIError> {
self.tray.update(move |tray| {
tray.actions.push(TrayItem {
label: label.to_string(),
action: None
});
tray.actions.push(TrayItem::Label(label.to_string()));
});

Ok(())
Expand All @@ -114,11 +115,19 @@ impl TrayItemLinux {
});

self.tray.update(move |tray| {
tray.actions.push(TrayItem {
tray.actions.push(TrayItem::MenuItem {
label: label.to_string(),
action: Some(action.clone())
action: action.clone()
});
});
Ok(())
}

pub fn add_separator(&mut self) -> Result<(), TIError> {
self.tray.update(move |tray| {
tray.actions.push(TrayItem::Separator);
});

Ok(())
}
}

0 comments on commit e3bd6c1

Please sign in to comment.