fix(menu): use icons and fix misaligned button labels

This commit is contained in:
Michael Aaron Murphy 2024-05-30 13:08:07 +02:00 committed by Michael Murphy
parent 3a8cbec6ae
commit bd48ad0fb5

View file

@ -141,21 +141,19 @@ where
} }
} }
/// This macro creates a button for a MenuTree. pub fn menu_button<'a, Message: 'a>(
#[macro_export] children: Vec<crate::Element<'a, Message>>,
macro_rules! menu_button { ) -> crate::widget::Button<'a, Message> {
($($x:expr),+ $(,)?) => ( widget::button(
widget::button( widget::Row::with_children(children)
widget::Row::with_children(vec![$($x.into()),+])
.align_items(Alignment::Center) .align_items(Alignment::Center)
.height(Length::Fill) .height(Length::Fill)
.width(Length::Fill), .width(Length::Fill),
) )
.height(Length::Fixed(36.0)) .height(Length::Fixed(36.0))
.padding([4, 16]) .padding([4, 16])
.width(Length::Fill) .width(Length::Fill)
.style(theme::Button::MenuItem) .style(theme::Button::MenuItem)
);
} }
/// Represents a menu item that performs an action when selected or a separator between menu items. /// Represents a menu item that performs an action when selected or a separator between menu items.
@ -243,11 +241,11 @@ where
match item { match item {
MenuItem::Button(label, action) => { MenuItem::Button(label, action) => {
let key = find_key(&action, key_binds); let key = find_key(&action, key_binds);
let menu_button = menu_button!( let menu_button = menu_button(vec![
widget::text(label), widget::text(label).into(),
widget::horizontal_space(Length::Fill), widget::horizontal_space(Length::Fill).into(),
widget::text(key), widget::text(key).into(),
) ])
.on_press(action.message()); .on_press(action.message());
trees.push(MenuTree::<Message, Renderer>::new(menu_button)); trees.push(MenuTree::<Message, Renderer>::new(menu_button));
@ -255,29 +253,36 @@ where
MenuItem::CheckBox(label, value, action) => { MenuItem::CheckBox(label, value, action) => {
let key = find_key(&action, key_binds); let key = find_key(&action, key_binds);
trees.push(MenuTree::new( trees.push(MenuTree::new(
menu_button!( menu_button(vec![
if value { if value {
// TODO: add a object-select-symbolic icon, `Message: 'static` is required when using an icon widget. widget::icon::from_name("object-select-symbolic")
widget::container(widget::text("")) .size(16)
.icon()
.width(Length::Fixed(16.0))
.into()
} else { } else {
widget::container(widget::Space::with_width(Length::Fixed(16.0))) widget::Space::with_width(Length::Fixed(17.0)).into()
}, },
widget::Space::with_width(Length::Fixed(8.0)), widget::Space::with_width(Length::Fixed(8.0)).into(),
widget::text(label), widget::text(label)
widget::horizontal_space(Length::Fill), .horizontal_alignment(iced::alignment::Horizontal::Left)
widget::text(key) .into(),
) widget::horizontal_space(Length::Fill).into(),
widget::text(key).into(),
])
.on_press(action.message()), .on_press(action.message()),
)); ));
} }
MenuItem::Folder(label, children) => { MenuItem::Folder(label, children) => {
trees.push(MenuTree::<Message, Renderer>::with_children( trees.push(MenuTree::<Message, Renderer>::with_children(
menu_button!( menu_button(vec![
widget::text(label), widget::text(label).into(),
widget::horizontal_space(Length::Fill), widget::horizontal_space(Length::Fill).into(),
// TODO: add a pan-end-symbolic icon, `Message: 'static` is required when using an icon widget. widget::icon::from_name("pan-end-symbolic")
widget::text(""), .size(16)
), .icon()
.into(),
]),
menu_items(key_binds, children), menu_items(key_binds, children),
)); ));
} }