use std::sync::Arc;
use crate::menu::MenuItemInner;
use crate::{Manager, Runtime, menu::MenuId};
use super::MenuItem;
impl<R: Runtime> MenuItem<R> {
pub fn new<M, T, A>(
manager: &M,
text: T,
enabled: bool,
accelerator: Option<A>,
) -> crate::Result<Self>
where
M: Manager<R>,
T: AsRef<str>,
A: AsRef<str>,
{
let handle = manager.app_handle();
let app_handle = handle.clone();
let text = text.as_ref().to_owned();
let accelerator = accelerator.and_then(|s| s.as_ref().parse().ok());
let item = handle.run_on_main_thread_blocking(move || {
let item = muda::MenuItem::new(text, enabled, accelerator);
MenuItemInner::new(app_handle, item)
})?;
Ok(Self(Arc::new(item)))
}
pub fn with_id<M, I, T, A>(
manager: &M,
id: I,
text: T,
enabled: bool,
accelerator: Option<A>,
) -> crate::Result<Self>
where
M: Manager<R>,
I: Into<MenuId>,
T: AsRef<str>,
A: AsRef<str>,
{
let handle = manager.app_handle();
let app_handle = handle.clone();
let id = id.into();
let accelerator = accelerator.and_then(|s| s.as_ref().parse().ok());
let text = text.as_ref().to_owned();
let item = handle.run_on_main_thread_blocking(move || {
let item = muda::MenuItem::with_id(id.clone(), text, enabled, accelerator);
MenuItemInner::new(app_handle, item)
})?;
Ok(Self(Arc::new(item)))
}
pub fn text(&self) -> crate::Result<String> {
self.with_inner_blocking(|i| i.text())
}
pub fn set_text<S: AsRef<str>>(&self, text: S) -> crate::Result<()> {
let text = text.as_ref().to_string();
self.with_inner_blocking(|i| i.set_text(text))
}
pub fn is_enabled(&self) -> crate::Result<bool> {
self.with_inner_blocking(|i| i.is_enabled())
}
pub fn set_enabled(&self, enabled: bool) -> crate::Result<()> {
self.with_inner_blocking(move |i| i.set_enabled(enabled))
}
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
self
.with_inner_blocking(move |i| i.set_accelerator(accel))?
.map_err(Into::into)
}
}