making the button selectable?

This commit is contained in:
Chris Cochrun 2025-02-10 10:02:33 -06:00
parent e7d06b5b6c
commit 2ea2a390a4

View file

@ -7,6 +7,7 @@ use cosmic::{
},
Element, Task,
};
use tracing::debug;
use crate::core::{
content::Content,
@ -79,6 +80,7 @@ impl Library {
}
Message::SelectItem(item) => {
self.hovered_item = item;
debug!(?self.hovered_item);
Task::none()
}
}
@ -170,25 +172,21 @@ impl Library {
})
.on_enter(Message::HoverLibrary(Some(model.kind)))
.on_exit(Message::HoverLibrary(None));
let lib_container =
if self.library_open == Some(model.kind) {
let lib_container = if self.library_open == Some(model.kind) {
let items = scrollable(
column({
model.items.iter().enumerate().map(
|(index, item)| {
let text = Container::new(
responsive(|size| {
let text =
Container::new(responsive(|size| {
text::heading(elide_text(
item.title(),
size.width,
))
.center()
.wrapping(
textm::Wrapping::None,
)
.wrapping(textm::Wrapping::None)
.into()
}),
)
}))
.center_y(25)
.center_x(Length::Fill);
let icon = icon::from_name({
@ -210,8 +208,7 @@ impl Library {
mouse_area(
Container::new(
rowm![
horizontal_space()
.width(0),
horizontal_space().width(0),
icon,
text
]
@ -222,15 +219,43 @@ impl Library {
.width(Length::Fill)
.style(move |t| {
container::Style::default()
.background(Background::Color(
.background(
Background::Color(
if let Some((
library,
hovered,
)) = self.hovered_item
selected,
)) =
self.selected_item
{
if model.kind == library
if model.kind
== library
&& selected
== index
as i32
{
t.cosmic()
.accent
.selected
.into()
} else {
t.cosmic()
.button
.base
.into()
}
} else if let Some(
(
library,
hovered,
),
) =
self.hovered_item
{
if model.kind
== library
&& hovered
== index as i32
== index
as i32
{
t.cosmic()
.button
@ -248,9 +273,11 @@ impl Library {
.base
.into()
},
))
),
)
.border(
Border::default().rounded(
Border::default()
.rounded(
t.cosmic()
.corner_radii
.radius_l,
@ -263,9 +290,10 @@ impl Library {
index as i32,
))))
.on_exit(Message::HoverItem(None))
.on_press(Message::SelectItem(Some(
(model.kind, index as i32),
)))
.on_press(Message::SelectItem(Some((
model.kind,
index as i32,
))))
.into()
},
)
@ -278,9 +306,11 @@ impl Library {
.background(Background::Color(
t.cosmic().primary.base.into(),
))
.border(Border::default().rounded(
.border(
Border::default().rounded(
t.cosmic().corner_radii.radius_m,
))
),
)
})
} else {
Container::new(Space::new(0, 0))