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
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ default = ["libxdo"]
libxdo = ["dep:libxdo"]
common-controls-v6 = []
serde = ["dep:serde", "dpi/serde"]
linux-ksni = ["arc-swap"]

[dependencies]
arc-swap = { version = "1.7.1", optional = true }
crossbeam-channel = "0.5"
keyboard-types = "0.7"
once_cell = "1"
Expand Down
6 changes: 6 additions & 0 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ impl Icon {
let win_icon = PlatformIcon::from_resource(ordinal, size)?;
Ok(Icon { inner: win_icon })
}

/// Convert the icon into a GTK pixbuf object.
#[cfg(target_os = "linux")]
pub fn to_pixbuf(&self) -> gtk::gdk_pixbuf::Pixbuf {
self.inner.to_pixbuf()
}
}

/// A native Icon to be used for the menu item
Expand Down
94 changes: 79 additions & 15 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, sealed::IsMenuItemBase, IsMenuItem, MenuId, MenuItemKind};

/// A check menu item inside a [`Menu`] or [`Submenu`]
Expand All @@ -12,10 +18,12 @@ use crate::{accelerator::Accelerator, sealed::IsMenuItemBase, IsMenuItem, MenuId
///
/// [`Menu`]: crate::Menu
/// [`Submenu`]: crate::Submenu
#[derive(Clone)]
#[derive(Debug, Clone)]
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 @@ -34,6 +42,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 @@ -44,16 +65,22 @@ impl CheckMenuItem {
checked: bool,
accelerator: Option<Accelerator>,
) -> Self {
let item = crate::platform_impl::MenuChild::new_check(
let inner = crate::platform_impl::MenuChild::new_check(
text.as_ref(),
enabled,
checked,
accelerator,
None,
);

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
let compat = Self::compat_menu_item(&inner);

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

Expand All @@ -69,15 +96,22 @@ impl CheckMenuItem {
accelerator: Option<Accelerator>,
) -> Self {
let id = id.into();
let inner = crate::platform_impl::MenuChild::new_check(
text.as_ref(),
enabled,
checked,
accelerator,
Some(id.clone()),
);

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
let compat = Self::compat_menu_item(&inner);

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

Expand All @@ -95,7 +129,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)));

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
crate::send_menu_update();
}

/// Get whether this check menu item is enabled or not.
Expand All @@ -105,7 +146,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)));

#[cfg(all(feature = "linux-ksni", target_os = "linux"))]
crate::send_menu_update();
}

/// Set this check menu item accelerator.
Expand All @@ -120,7 +168,23 @@ impl CheckMenuItem {

/// Check or Uncheck this check menu item.
pub fn set_checked(&self, checked: bool) {
self.inner.borrow_mut().set_checked(checked)
#[cfg(target_os = "macos")]
{
let inner = self.inner.borrow();
inner.set_checked(checked);
}

#[cfg(not(target_os = "macos"))]
{
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
63 changes: 63 additions & 0 deletions src/items/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::sync::Arc;

use arc_swap::ArcSwap;

use crate::PredefinedMenuItemKind;

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

#[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 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
Loading