extern crate alloc;
use alloc::vec::Vec;
use core::hash::Hash;
use azul_css::AzString;
use crate::{
callbacks::{CoreCallback, CoreCallbackType},
refany::RefAny,
resources::ImageRef,
window::{ContextMenuMouseButton, OptionVirtualKeyCodeCombo},
};
#[derive(Debug, Default, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C)]
pub struct Menu {
pub items: MenuItemVec,
pub position: MenuPopupPosition,
pub context_mouse_btn: ContextMenuMouseButton,
}
impl_option!(
Menu,
OptionMenu,
copy = false,
[Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord]
);
impl Menu {
pub fn create(items: MenuItemVec) -> Self {
Self {
items,
position: MenuPopupPosition::AutoCursor,
context_mouse_btn: ContextMenuMouseButton::Right,
}
}
pub fn with_position(mut self, position: MenuPopupPosition) -> Self {
self.position = position;
self
}
}
impl Menu {
pub fn swap_with_default(&mut self) -> Self {
let mut new = Self::default();
core::mem::swap(&mut new, self);
new
}
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C)]
pub enum MenuPopupPosition {
BottomLeftOfCursor,
BottomRightOfCursor,
TopLeftOfCursor,
TopRightOfCursor,
BottomOfHitRect,
LeftOfHitRect,
TopOfHitRect,
RightOfHitRect,
AutoCursor,
AutoHitRect,
}
impl Default for MenuPopupPosition {
fn default() -> Self {
Self::AutoCursor
}
}
impl Menu {
pub fn get_hash(&self) -> u64 {
use highway::{HighwayHash, HighwayHasher, Key};
let mut hasher = HighwayHasher::new(Key([0; 4]));
self.hash(&mut hasher);
hasher.finalize64()
}
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C)]
pub enum MenuItemState {
Normal,
Greyed,
Disabled,
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C, u8)]
pub enum MenuItem {
String(StringMenuItem),
Separator,
BreakLine,
}
impl_option!(
MenuItem,
OptionMenuItem,
copy = false,
[Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord]
);
impl_vec!(MenuItem, MenuItemVec, MenuItemVecDestructor, MenuItemVecDestructorType, MenuItemVecSlice, OptionMenuItem);
impl_vec_clone!(MenuItem, MenuItemVec, MenuItemVecDestructor);
impl_vec_debug!(MenuItem, MenuItemVec);
impl_vec_partialeq!(MenuItem, MenuItemVec);
impl_vec_partialord!(MenuItem, MenuItemVec);
impl_vec_hash!(MenuItem, MenuItemVec);
impl_vec_eq!(MenuItem, MenuItemVec);
impl_vec_ord!(MenuItem, MenuItemVec);
#[derive(Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C)]
pub struct StringMenuItem {
pub label: AzString,
pub accelerator: OptionVirtualKeyCodeCombo,
pub callback: OptionCoreMenuCallback,
pub menu_item_state: MenuItemState,
pub icon: OptionMenuItemIcon,
pub children: MenuItemVec,
}
impl StringMenuItem {
pub const fn create(label: AzString) -> Self {
StringMenuItem {
label,
accelerator: OptionVirtualKeyCodeCombo::None,
callback: OptionCoreMenuCallback::None,
menu_item_state: MenuItemState::Normal,
icon: OptionMenuItemIcon::None,
children: MenuItemVec::from_const_slice(&[]),
}
}
pub fn swap_with_default(&mut self) -> Self {
let mut default = Self {
label: AzString::from_const_str(""),
accelerator: None.into(),
callback: None.into(),
menu_item_state: MenuItemState::Normal,
icon: None.into(),
children: Vec::new().into(),
};
core::mem::swap(&mut default, self);
default
}
pub fn with_children(mut self, children: MenuItemVec) -> Self {
self.children = children;
self
}
pub fn with_child(mut self, child: MenuItem) -> Self {
let mut children = self.children.into_library_owned_vec();
children.push(child);
self.children = children.into();
self
}
pub fn with_callback<I: Into<CoreCallback>>(mut self, data: RefAny, callback: I) -> Self {
self.callback = Some(CoreMenuCallback {
refany: data,
callback: callback.into(),
})
.into();
self
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C, u8)]
pub enum MenuItemIcon {
Checkbox(bool),
Image(ImageRef),
}
impl_option!(
MenuItemIcon,
OptionMenuItemIcon,
copy = false,
[Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord]
);
#[derive(Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord)]
#[repr(C)]
pub struct CoreMenuCallback {
pub refany: RefAny,
pub callback: CoreCallback,
}
impl_option!(
CoreMenuCallback,
OptionCoreMenuCallback,
copy = false,
[Debug, Clone, PartialEq, PartialOrd, Hash, Eq, Ord]
);