use egui::Ui;
use crate::Icons;
use crate::components::{Component, IconComponent};
use crate::{compact_menu, menu_button, menu_item, menu_toggle, menu_tool_button};
pub struct MenuItem<'a> {
icon: &'a str,
label: &'a str,
shortcut: Option<&'a str>,
danger: bool,
enabled: bool,
}
impl<'a> MenuItem<'a> {
pub fn new(icon: &'a str, label: &'a str) -> Self {
Self {
icon,
label,
shortcut: None,
danger: false,
enabled: true,
}
}
pub fn shortcut(mut self, shortcut: &'a str) -> Self {
self.shortcut = Some(shortcut);
self
}
pub fn danger(mut self, danger: bool) -> Self {
self.danger = danger;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
}
impl IconComponent for MenuItem<'_> {
type Output = bool;
fn show(self, ui: &mut Ui, icons: &mut Icons) -> bool {
ui.add_enabled_ui(self.enabled, |ui| {
menu_item(ui, icons, self.icon, self.label, self.shortcut, self.danger)
})
.inner
}
}
pub struct MenuToggle<'a> {
label: &'a str,
on: bool,
enabled: bool,
}
impl<'a> MenuToggle<'a> {
pub fn new(label: &'a str, on: bool) -> Self {
Self {
label,
on,
enabled: true,
}
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
}
impl Component for MenuToggle<'_> {
type Output = bool;
fn show(self, ui: &mut Ui) -> bool {
menu_toggle(ui, self.label, self.on, self.enabled)
}
}
pub struct Menu<'a> {
label: &'a str,
width: f32,
}
impl<'a> Menu<'a> {
pub fn new(label: &'a str) -> Self {
Self { label, width: 180.0 }
}
pub fn width(mut self, width: f32) -> Self {
self.width = width;
self
}
pub fn show(self, ui: &mut Ui, add: impl FnOnce(&mut Ui)) {
ui.menu_button(self.label, |ui| {
compact_menu(ui);
ui.set_min_width(self.width);
add(ui);
});
}
}
pub struct MenuButton<'a> {
label: &'a str,
}
impl<'a> MenuButton<'a> {
pub fn new(label: &'a str) -> Self {
Self { label }
}
}
impl Component for MenuButton<'_> {
type Output = bool;
fn show(self, ui: &mut Ui) -> bool {
menu_button(ui, self.label)
}
}
pub struct MenuIconButton<'a> {
icon: &'a str,
tooltip: &'a str,
width: f32,
}
impl<'a> MenuIconButton<'a> {
pub fn new(icon: &'a str, tooltip: &'a str) -> Self {
Self {
icon,
tooltip,
width: 180.0,
}
}
pub fn width(mut self, width: f32) -> Self {
self.width = width;
self
}
pub fn show(self, ui: &mut Ui, icons: &mut Icons, add: impl FnOnce(&mut Ui, &mut Icons)) {
menu_tool_button(ui, icons, self.icon, self.tooltip, self.width, add);
}
}