implement tooltip icon

This commit is contained in:
yggverse 2024-12-05 16:41:38 +02:00
parent da4fab30e1
commit 8ec4b467ce

View File

@ -2,12 +2,13 @@ pub mod item;
use item::{value::Value, Item}; use item::{value::Value, Item};
use gtk::{ use gtk::{
gdk::Cursor,
gio::{ gio::{
prelude::{Cast, CastNone}, prelude::{Cast, CastNone},
ListStore, ListStore,
}, },
prelude::{BoxExt, ListItemExt, WidgetExt}, prelude::{BoxExt, ListItemExt, WidgetExt},
Align, Box, DropDown, Label, ListItem, SignalListItemFactory, Align, Box, DropDown, Image, Label, ListItem, Orientation, SignalListItemFactory,
}; };
pub struct List { pub struct List {
@ -37,13 +38,26 @@ impl List {
child.append(&Label::builder().halign(Align::Start).build()); child.append(&Label::builder().halign(Align::Start).build());
// Subtitle // Subtitle
child.append( let subtitle = Box::builder()
&Label::builder() .orientation(Orientation::Horizontal)
.halign(Align::Start) .css_classes(["caption", "dim-label"])
.css_classes(["caption", "dim-label"]) .halign(Align::Start)
.build();
subtitle.append(
&Image::builder()
.cursor(&Cursor::from_name("help", None).unwrap())
.icon_name("dialog-information")
.margin_end(4)
.pixel_size(11)
.build(), .build(),
); );
subtitle.append(&Label::new(None));
// Subtitle
child.append(&subtitle);
// Done // Done
this.downcast_ref::<ListItem>() this.downcast_ref::<ListItem>()
.unwrap() .unwrap()
@ -56,20 +70,29 @@ impl List {
let item = list_item.item().and_downcast::<Item>().unwrap(); let item = list_item.item().and_downcast::<Item>().unwrap();
let child = list_item.child().and_downcast::<Box>().unwrap(); let child = list_item.child().and_downcast::<Box>().unwrap();
// Update `title` (expected as the first child) // Update `title`
let title = child.first_child().and_downcast::<Label>().unwrap(); let title = child.first_child().and_downcast::<Label>().unwrap();
title.set_label(&item.title()); title.set_label(&item.title());
title.set_css_classes(if item.is_active() { &["success"] } else { &[] }); title.set_css_classes(if item.is_active() { &["success"] } else { &[] });
// Update `subtitle` (expected as the last child) // Update `subtitle`
let subtitle = child.last_child().and_downcast::<Label>().unwrap(); let subtitle = child.last_child().and_downcast::<Box>().unwrap();
subtitle.set_label(&item.subtitle()); subtitle
subtitle.set_tooltip_markup(Some(&item.tooltip())); .last_child()
.and_downcast::<Label>()
.unwrap()
.set_label(&item.subtitle());
// Update `tooltip`
let tooltip = subtitle.first_child().and_downcast::<Image>().unwrap();
tooltip.set_visible(!item.tooltip().is_empty());
tooltip.set_tooltip_markup(Some(&item.tooltip()));
}); });
// Init main `DropDown` // Init main widget
let dropdown = DropDown::builder() let dropdown = DropDown::builder()
.model(&list_store) .model(&list_store)
.factory(&factory) .factory(&factory)