Skip to content

Commit

Permalink
Add send_menu_shutdown function and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaust committed Dec 11, 2024
1 parent 181552f commit e2667f5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 70 deletions.
61 changes: 1 addition & 60 deletions src/items/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use arc_swap::ArcSwap;

use crate::{ContextMenu, MenuItemKind, PredefinedMenuItemKind};
use crate::PredefinedMenuItemKind;

#[derive(Debug, Clone)]
pub struct CompatStandardItem {
Expand Down Expand Up @@ -55,65 +55,6 @@ impl From<CompatSubMenuItem> for CompatMenuItem {
}
}

impl From<MenuItemKind> for CompatMenuItem {
fn from(item: MenuItemKind) -> Self {
match item {
MenuItemKind::MenuItem(menu_item) => CompatStandardItem {
id: menu_item.id().0.clone(),
label: strip_accelerator(menu_item.text()),
enabled: menu_item.is_enabled(),
icon: None,
predefined_menu_item_kind: None,
}
.into(),
MenuItemKind::Submenu(submenu) => CompatSubMenuItem {
label: strip_accelerator(submenu.text()),
enabled: submenu.is_enabled(),
submenu: submenu.compat_items(),
}
.into(),
MenuItemKind::Predefined(predefined_menu_item) => {
match predefined_menu_item.predefined_item_kind() {
Some(PredefinedMenuItemKind::Separator) => CompatMenuItem::Separator,
Some(predefined_menu_item_kind) => CompatStandardItem {
id: predefined_menu_item.id().0.clone(),
label: strip_accelerator(predefined_menu_item.text()),
enabled: true,
icon: None,
predefined_menu_item_kind: Some(predefined_menu_item_kind),
}
.into(),
_ => CompatStandardItem {
id: predefined_menu_item.id().0.clone(),
label: strip_accelerator(predefined_menu_item.text()),
enabled: true,
icon: None,
predefined_menu_item_kind: None,
}
.into(),
}
}
MenuItemKind::Check(check_menu_item) => CompatCheckmarkItem {
id: check_menu_item.id().0.clone(),
label: strip_accelerator(check_menu_item.text()),
enabled: check_menu_item.is_enabled(),
checked: check_menu_item.is_checked(),
}
.into(),
MenuItemKind::Icon(icon_menu_item) => CompatStandardItem {
id: icon_menu_item.id().0.clone(),
label: strip_accelerator(icon_menu_item.text()),
enabled: icon_menu_item.is_enabled(),
icon: icon_menu_item
.icon()
.map(|icon| icon.to_pixbuf().save_to_bufferv("png", &[]).unwrap()),
predefined_menu_item_kind: None,
}
.into(),
}
}
}

pub fn strip_accelerator(text: impl AsRef<str>) -> String {
text.as_ref().replace('&', "")
}
6 changes: 0 additions & 6 deletions src/items/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,6 @@ impl IconMenuItem {
pub fn set_native_icon(&self, icon: Option<NativeIcon>) {
let mut item = self.inner.borrow_mut();
item.set_native_icon(icon);

#[cfg(feature = "ksni")]
self.compat.store(Arc::new(Self::compat_menu_item(&item)));

#[cfg(feature = "ksni")]
crate::send_menu_update();
}

/// Change this menu item icon to a native image or remove it.
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,19 @@ impl MenuEvent {
}

#[cfg(feature = "ksni")]
static MENU_UPDATE_CHANNEL: Lazy<(Sender<()>, Receiver<()>)> = Lazy::new(unbounded);
static MENU_UPDATE_CHANNEL: Lazy<(Sender<std::ops::ControlFlow<()>>, Receiver<std::ops::ControlFlow<()>>)> = Lazy::new(unbounded);

#[cfg(feature = "ksni")]
pub fn recv_menu_update() -> std::result::Result<(), crossbeam_channel::RecvError> {
pub fn recv_menu_update() -> std::result::Result<std::ops::ControlFlow<()>, crossbeam_channel::RecvError> {
MENU_UPDATE_CHANNEL.1.recv()
}

#[cfg(feature = "ksni")]
pub(crate) fn send_menu_update() {
let _ = MENU_UPDATE_CHANNEL.0.send(());
pub fn send_menu_update() {
let _ = MENU_UPDATE_CHANNEL.0.send(std::ops::ControlFlow::Continue(()));
}

#[cfg(feature = "ksni")]
pub fn send_menu_shutdown() {
let _ = MENU_UPDATE_CHANNEL.0.send(std::ops::ControlFlow::Break(()));
}

0 comments on commit e2667f5

Please sign in to comment.