items are hoverable

This commit is contained in:
Chris Cochrun 2025-01-17 07:27:02 -06:00
parent a792bc459f
commit 9753c6f366

View file

@ -25,6 +25,8 @@ pub(crate) struct Library {
presentation_library: Model<Presentation>, presentation_library: Model<Presentation>,
library_open: Option<LibraryKind>, library_open: Option<LibraryKind>,
library_hovered: Option<LibraryKind>, library_hovered: Option<LibraryKind>,
selected_item: Option<(LibraryKind, i32)>,
hovered_item: Option<(LibraryKind, i32)>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -34,6 +36,8 @@ pub(crate) enum Message {
OpenItem, OpenItem,
HoverLibrary(Option<LibraryKind>), HoverLibrary(Option<LibraryKind>),
OpenLibrary(Option<LibraryKind>), OpenLibrary(Option<LibraryKind>),
HoverItem(Option<(LibraryKind, i32)>),
SelectItem(Option<(LibraryKind, i32)>),
None, None,
} }
@ -50,6 +54,8 @@ impl Library {
.await, .await,
library_open: None, library_open: None,
library_hovered: None, library_hovered: None,
selected_item: None,
hovered_item: None,
} }
} }
@ -67,6 +73,14 @@ impl Library {
self.library_open = library_kind; self.library_open = library_kind;
Task::none() Task::none()
} }
Message::HoverItem(item) => {
self.hovered_item = item;
Task::none()
}
Message::SelectItem(item) => {
self.hovered_item = item;
Task::none()
}
} }
} }
@ -156,24 +170,29 @@ impl Library {
}) })
.on_enter(Message::HoverLibrary(Some(model.kind))) .on_enter(Message::HoverLibrary(Some(model.kind)))
.on_exit(Message::HoverLibrary(None)); .on_exit(Message::HoverLibrary(None));
let lib_container = if self.library_open == Some(model.kind) { let lib_container =
let items = scrollable( if self.library_open == Some(model.kind) {
column({ let items = scrollable(
model.items.iter().map(|item| { column({
let text = model.items.iter().enumerate().map(
Container::new(responsive(|size| { |(index, item)| {
text::heading(elide_text( let text = Container::new(
item.title(), responsive(|size| {
size.width, text::heading(elide_text(
)) item.title(),
.center() size.width,
.wrapping(textm::Wrapping::None) ))
.into() .center()
})) .wrapping(
.center_y(25) textm::Wrapping::None,
.center_x(Length::Fill); )
let icon = icon::from_name({ .into()
match model.kind { }),
)
.center_y(25)
.center_x(Length::Fill);
let icon = icon::from_name({
match model.kind {
LibraryKind::Song => { LibraryKind::Song => {
"folder-music-symbolic" "folder-music-symbolic"
} }
@ -187,47 +206,85 @@ impl Library {
"x-office-presentation-symbolic" "x-office-presentation-symbolic"
} }
} }
}); });
Container::new( mouse_area(
rowm![ Container::new(
horizontal_space().width(0), rowm![
icon, horizontal_space()
text .width(0),
] icon,
.spacing(10) text
.align_y(Vertical::Center), ]
.spacing(10)
.align_y(Vertical::Center),
)
.padding(5)
.width(Length::Fill)
.style(move |t| {
container::Style::default()
.background(Background::Color(
if let Some((
library,
hovered,
)) = self.hovered_item
{
if model.kind == library
&& hovered
== index as i32
{
t.cosmic()
.button
.hover
.into()
} else {
t.cosmic()
.button
.base
.into()
}
} else {
t.cosmic()
.button
.base
.into()
},
))
.border(
Border::default().rounded(
t.cosmic()
.corner_radii
.radius_l,
),
)
}),
)
.on_enter(Message::HoverItem(Some((
model.kind,
index as i32,
))))
.on_exit(Message::HoverItem(None))
.on_press(Message::SelectItem(Some(
(model.kind, index as i32),
)))
.into()
},
) )
.padding(5)
.width(Length::Fill)
.style(|t| {
container::Style::default()
.background(Background::Color(
t.cosmic().button.base.into(),
))
.border(Border::default().rounded(
t.cosmic().corner_radii.radius_l,
))
})
.into()
}) })
}) .spacing(2)
.spacing(2) .width(Length::Fill),
.width(Length::Fill), );
); Container::new(items).padding(5).style(|t| {
Container::new(items).padding(5).style(|t| { container::Style::default()
container::Style::default() .background(Background::Color(
.background(Background::Color( t.cosmic().primary.base.into(),
t.cosmic().primary.base.into(), ))
)) .border(Border::default().rounded(
.border(
Border::default().rounded(
t.cosmic().corner_radii.radius_m, t.cosmic().corner_radii.radius_m,
), ))
) })
}) } else {
} else { Container::new(Space::new(0, 0))
Container::new(Space::new(0, 0)) };
};
column![button, lib_container].into() column![button, lib_container].into()
} }
} }