use crate::{menu::MenuId, run_main_thread, AppHandle, Manager, Runtime};
pub struct CheckMenuItem<R: Runtime> {
pub(crate) id: MenuId,
pub(crate) inner: muda::CheckMenuItem,
pub(crate) app_handle: AppHandle<R>,
}
impl<R: Runtime> Clone for CheckMenuItem<R> {
fn clone(&self) -> Self {
Self {
id: self.id.clone(),
inner: self.inner.clone(),
app_handle: self.app_handle.clone(),
}
}
}
unsafe impl<R: Runtime> Sync for CheckMenuItem<R> {}
unsafe impl<R: Runtime> Send for CheckMenuItem<R> {}
impl<R: Runtime> super::sealed::IsMenuItemBase for CheckMenuItem<R> {
fn inner(&self) -> &dyn muda::IsMenuItem {
&self.inner
}
}
impl<R: Runtime> super::IsMenuItem<R> for CheckMenuItem<R> {
fn kind(&self) -> super::MenuItemKind<R> {
super::MenuItemKind::Check(self.clone())
}
fn id(&self) -> &MenuId {
&self.id
}
}
impl<R: Runtime> CheckMenuItem<R> {
pub fn new<M: Manager<R>, S: AsRef<str>>(
manager: &M,
text: S,
enabled: bool,
checked: bool,
acccelerator: Option<S>,
) -> Self {
let item = muda::CheckMenuItem::new(
text,
enabled,
checked,
acccelerator.and_then(|s| s.as_ref().parse().ok()),
);
Self {
id: item.id().clone(),
inner: item,
app_handle: manager.app_handle().clone(),
}
}
pub fn with_id<M: Manager<R>, I: Into<MenuId>, S: AsRef<str>>(
manager: &M,
id: I,
text: S,
enabled: bool,
checked: bool,
acccelerator: Option<S>,
) -> Self {
let item = muda::CheckMenuItem::with_id(
id,
text,
enabled,
checked,
acccelerator.and_then(|s| s.as_ref().parse().ok()),
);
Self {
id: item.id().clone(),
inner: item,
app_handle: manager.app_handle().clone(),
}
}
pub fn app_handle(&self) -> &AppHandle<R> {
&self.app_handle
}
pub fn id(&self) -> &MenuId {
&self.id
}
pub fn text(&self) -> crate::Result<String> {
run_main_thread!(self, |self_: Self| self_.inner.text())
}
pub fn set_text<S: AsRef<str>>(&self, text: S) -> crate::Result<()> {
let text = text.as_ref().to_string();
run_main_thread!(self, |self_: Self| self_.inner.set_text(text))
}
pub fn is_enabled(&self) -> crate::Result<bool> {
run_main_thread!(self, |self_: Self| self_.inner.is_enabled())
}
pub fn set_enabled(&self, enabled: bool) -> crate::Result<()> {
run_main_thread!(self, |self_: Self| self_.inner.set_enabled(enabled))
}
pub fn set_accelerator<S: AsRef<str>>(&self, acccelerator: Option<S>) -> crate::Result<()> {
let accel = acccelerator.and_then(|s| s.as_ref().parse().ok());
run_main_thread!(self, |self_: Self| self_.inner.set_accelerator(accel))?.map_err(Into::into)
}
pub fn is_checked(&self) -> crate::Result<bool> {
run_main_thread!(self, |self_: Self| self_.inner.is_checked())
}
pub fn set_checked(&self, checked: bool) -> crate::Result<()> {
run_main_thread!(self, |self_: Self| self_.inner.set_checked(checked))
}
}