Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/linux-ksni.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"muda": minor
---

Add a `linux-ksni` feature with a GTK-free compatibility menu model for Linux StatusNotifierItem tray integrations.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ include = ["README.md", "src/**/*.rs", "Cargo.toml", "LICENSE-APACHE", "LICENSE-
default = ["libxdo", "gtk"]
libxdo = ["dep:libxdo"]
gtk = ["dep:gtk"]
linux-ksni = ["dep:arc-swap", "dep:png"]
common-controls-v6 = []
serde = ["dep:serde", "dpi/serde", "keyboard-types/serde"]

[dependencies]
arc-swap = { version = "1.7.1", optional = true }
crossbeam-channel = "0.5"
keyboard-types = "0.7"
once_cell = "1"
Expand All @@ -46,6 +48,7 @@ features = [
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
gtk = { version = "0.18", optional = true }
libxdo = { version = "0.6.0", optional = true }
png = { version = "0.18", optional = true }

[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.6.1"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Menu Utilities library for Desktop Applications.
- `libxdo`: Enables linking to `libxdo` on Linux or FreeBSD which is used for the predefined `Copy`, `Cut`, `Paste` and `SelectAll` menu item.
- `serde`: Enables de/serializing the dpi types.
- `gtk`: Enables the `gtk` crate dependency on Linux or FreeBSD. This is required for `muda` to function properly on Linux or FreeBSD.
- `linux-ksni`: Enables a GTK-free Linux menu compatibility model for StatusNotifierItem tray integrations.

## Dependencies (Linux Only)

Expand Down
75 changes: 64 additions & 11 deletions src/items/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

use std::{cell::RefCell, mem, rc::Rc};

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
use std::sync::Arc;

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
use arc_swap::ArcSwap;

use crate::{
accelerator::{Accelerator, KeyAccelerator},
sealed::IsMenuItemBase,
Expand All @@ -20,6 +26,8 @@ use crate::{
pub struct CheckMenuItem {
pub(crate) id: Rc<MenuId>,
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
pub(crate) compat: Arc<ArcSwap<crate::CompatMenuItem>>,
}

impl IsMenuItemBase for CheckMenuItem {}
Expand All @@ -38,6 +46,19 @@ impl IsMenuItem for CheckMenuItem {
}

impl CheckMenuItem {
#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
pub(crate) fn compat_menu_item(
item: &crate::platform_impl::MenuChild,
) -> crate::CompatMenuItem {
crate::CompatCheckmarkItem {
id: item.id().0.clone(),
label: super::strip_mnemonic(item.text()),
enabled: item.is_enabled(),
checked: item.is_checked(),
}
.into()
}

/// Create a new check menu item.
///
/// - `text` could optionally contain an `&` before a character to assign this character as the mnemonic
Expand All @@ -55,9 +76,14 @@ impl CheckMenuItem {
accelerator.map(KeyAccelerator::from),
None,
);
#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
let compat = item.compat_child();

Self {
id: Rc::new(item.id().clone()),
inner: Rc::new(RefCell::new(item)),
#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
compat,
}
}

Expand All @@ -73,15 +99,21 @@ impl CheckMenuItem {
accelerator: Option<Accelerator>,
) -> Self {
let id = id.into();
let item = crate::platform_impl::MenuChild::new_check(
text.as_ref(),
enabled,
checked,
accelerator.map(KeyAccelerator::from),
Some(id),
);
#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
let compat = item.compat_child();

Self {
id: Rc::new(id.clone()),
inner: Rc::new(RefCell::new(crate::platform_impl::MenuChild::new_check(
text.as_ref(),
enabled,
checked,
accelerator.map(KeyAccelerator::from),
Some(id),
))),
id: Rc::new(item.id().clone()),
inner: Rc::new(RefCell::new(item)),
#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
compat,
}
}

Expand All @@ -99,7 +131,14 @@ impl CheckMenuItem {
/// an `&` before a character to assign this character as the mnemonic
/// for this check menu item. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn set_text<S: AsRef<str>>(&self, text: S) {
self.inner.borrow_mut().set_text(text.as_ref())
let mut inner = self.inner.borrow_mut();
inner.set_text(text.as_ref());

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
{
self.compat.store(Arc::new(Self::compat_menu_item(&inner)));
crate::send_menu_update();
}
}

/// Get whether this check menu item is enabled or not.
Expand All @@ -109,7 +148,14 @@ impl CheckMenuItem {

/// Enable or disable this check menu item.
pub fn set_enabled(&self, enabled: bool) {
self.inner.borrow_mut().set_enabled(enabled)
let mut inner = self.inner.borrow_mut();
inner.set_enabled(enabled);

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
{
self.compat.store(Arc::new(Self::compat_menu_item(&inner)));
crate::send_menu_update();
}
}

/// Set this check menu item accelerator.
Expand All @@ -135,7 +181,14 @@ impl CheckMenuItem {

/// Check or Uncheck this check menu item.
pub fn set_checked(&self, checked: bool) {
self.inner.borrow_mut().set_checked(checked)
let mut inner = self.inner.borrow_mut();
inner.set_checked(checked);

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
{
self.compat.store(Arc::new(Self::compat_menu_item(&inner)));
crate::send_menu_update();
}
}

/// Convert this menu item into its menu ID.
Expand Down
66 changes: 66 additions & 0 deletions src/items/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::sync::Arc;

use arc_swap::ArcSwap;

#[derive(Debug, Clone)]
pub struct CompatStandardItem {
pub id: String,
pub label: String,
pub enabled: bool,
pub icon: Option<Vec<u8>>,
}

#[derive(Debug, Clone)]
pub struct CompatCheckmarkItem {
pub id: String,
pub label: String,
pub enabled: bool,
pub checked: bool,
}

#[derive(Debug, Clone)]
pub struct CompatSubMenuItem {
pub label: String,
pub enabled: bool,
pub submenu: Vec<Arc<ArcSwap<CompatMenuItem>>>,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum CompatMenuItem {
Standard(CompatStandardItem),
Checkmark(CompatCheckmarkItem),
SubMenu(CompatSubMenuItem),
Separator,
}

impl Default for CompatMenuItem {
fn default() -> Self {
CompatMenuItem::Separator
}
}

impl From<CompatStandardItem> for CompatMenuItem {
fn from(item: CompatStandardItem) -> Self {
CompatMenuItem::Standard(item)
}
}

impl From<CompatCheckmarkItem> for CompatMenuItem {
fn from(item: CompatCheckmarkItem) -> Self {
CompatMenuItem::Checkmark(item)
}
}

impl From<CompatSubMenuItem> for CompatMenuItem {
fn from(item: CompatSubMenuItem) -> Self {
CompatMenuItem::SubMenu(item)
}
}

pub fn strip_mnemonic(text: impl AsRef<str>) -> String {
text.as_ref()
.replace("&&", "[~~]")
.replace('&', "")
.replace("[~~]", "&")
}
Loading