fix: button styling

This commit is contained in:
Ashley Wulber 2025-02-18 11:34:27 -05:00
parent 5ef18de61f
commit 69ae35010f
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
4 changed files with 57 additions and 33 deletions

View file

@ -12,11 +12,12 @@ use cosmic::iced::widget::column;
use cosmic::iced::Length; use cosmic::iced::Length;
use cosmic::iced_core::Size; use cosmic::iced_core::Size;
use cosmic::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler}; use cosmic::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
use cosmic::widget::button;
use cosmic::widget::icon::{from_name, Handle}; use cosmic::widget::icon::{from_name, Handle};
use cosmic::widget::menu::KeyBind; use cosmic::widget::menu::KeyBind;
use cosmic::widget::{button, text};
use cosmic::widget::{ use cosmic::widget::{
container, container,
menu::menu_button,
menu::{self, action::MenuAction}, menu::{self, action::MenuAction},
nav_bar, responsive, nav_bar, responsive,
}; };
@ -329,12 +330,18 @@ impl cosmic::Application for App {
), ),
(menu::root("hi 4"), { (menu::root("hi 4"), {
let mut root_items = vec![menu::Tree::with_children( let mut root_items = vec![menu::Tree::with_children(
menu::root("hi 41 extra root"), menu_button(vec![text("hi 41 extra root").into()])
vec![menu::Tree::with_children(menu::root("hi 41 extra 2"), { .on_press(Message::Hi),
let mut root_items = vec![menu::Tree::with_children(
menu::root("hi 41 extra root"),
vec![menu::Tree::with_children( vec![menu::Tree::with_children(
menu::root("hi 41 extra 2"), menu_button(vec![text("hi 41 extra 2").into()])
.on_press(Message::Hi),
{
let mut root_items = vec![menu::Tree::with_children(
menu_button(vec![text("hi 41 extra root").into()])
.on_press(Message::Hi),
vec![menu::Tree::with_children(
menu_button(vec![text("hi 41 extra 2").into()])
.on_press(Message::Hi),
menu::items( menu::items(
&self.keybinds, &self.keybinds,
vec![menu::Item::Button( vec![menu::Item::Button(
@ -357,7 +364,8 @@ impl cosmic::Application for App {
); );
root_items.append(&mut items); root_items.append(&mut items);
root_items root_items
})], },
)],
)]; )];
let mut items = menu::items( let mut items = menu::items(
&self.keybinds, &self.keybinds,

View file

@ -441,7 +441,14 @@ impl Core {
trees trees
.into_iter() .into_iter()
.map(|mt| { .map(|mt| {
menu::Tree::<_>::with_children(mt.0.width(Length::Fill), mt.1) menu::Tree::<_>::with_children(
mt.0.width(Length::Fill)
.height(Length::Fixed(36.0))
.padding([4, 16])
.class(crate::theme::Button::MenuItem)
.force_enabled(true),
mt.1,
)
}) })
.collect(), .collect(),
)]), )]),

View file

@ -51,7 +51,6 @@ pub enum SurfaceMessage {
}, },
} }
#[cfg(feature = "wayland")]
impl std::fmt::Debug for SurfaceMessage { impl std::fmt::Debug for SurfaceMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {

View file

@ -55,6 +55,7 @@ pub struct Button<'a, Message> {
selected: bool, selected: bool,
style: crate::theme::Button, style: crate::theme::Button,
variant: Variant<Message>, variant: Variant<Message>,
force_enabled: bool,
} }
impl<'a, Message> Button<'a, Message> { impl<'a, Message> Button<'a, Message> {
@ -77,6 +78,7 @@ impl<'a, Message> Button<'a, Message> {
selected: false, selected: false,
style: crate::theme::Button::default(), style: crate::theme::Button::default(),
variant: Variant::Normal, variant: Variant::Normal,
force_enabled: false,
} }
} }
@ -90,6 +92,7 @@ impl<'a, Message> Button<'a, Message> {
name: None, name: None,
#[cfg(feature = "a11y")] #[cfg(feature = "a11y")]
description: None, description: None,
force_enabled: false,
#[cfg(feature = "a11y")] #[cfg(feature = "a11y")]
label: None, label: None,
content: content.into(), content: content.into(),
@ -163,6 +166,12 @@ impl<'a, Message> Button<'a, Message> {
self self
} }
/// Sets the the [`Button`] to enabled whether or not it has handlers for on press.
pub fn force_enabled(mut self, enabled: bool) -> Self {
self.force_enabled = enabled;
self
}
/// Sets the widget to a selected state. /// Sets the widget to a selected state.
/// ///
/// Displays a selection indicator on image buttons. /// Displays a selection indicator on image buttons.
@ -348,7 +357,8 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
let mut headerbar_alpha = None; let mut headerbar_alpha = None;
let is_enabled = self.on_press.is_some() || self.on_press_down.is_some(); let is_enabled =
self.on_press.is_some() || self.on_press_down.is_some() || self.force_enabled;
let is_mouse_over = cursor.position().is_some_and(|p| bounds.contains(p)); let is_mouse_over = cursor.position().is_some_and(|p| bounds.contains(p));
let state = tree.state.downcast_ref::<State>(); let state = tree.state.downcast_ref::<State>();