implement tooltip for input counters

This commit is contained in:
yggverse 2025-01-31 20:51:13 +02:00
parent afc33c1a03
commit d1c05b6469
4 changed files with 25 additions and 16 deletions

View file

@ -1,4 +1,5 @@
use gtk::{prelude::WidgetExt, Label};
use plurify::Plurify;
pub struct Counter {
pub label: Label,
@ -34,6 +35,11 @@ impl Counter {
// Toggle visibility on chars left provided
self.label.set_visible(!is_empty);
self.label.set_tooltip_text(Some(&format!(
"{value} {} left",
(value as usize).plurify(&["byte", "bytes", "bytes"])
)));
}
None => self.label.set_visible(false),
}

View file

@ -57,7 +57,8 @@ impl Titan for gtk::Box {
form.buffer().connect_changed({
let control = control.clone();
move |this| control.update(Some(this.char_count()))
let form = form.clone();
move |this| control.update(this.char_count(), form.text().len())
});
// Return activated `Self`

View file

@ -44,12 +44,9 @@ impl Control {
}
// Actions
pub fn update(&self, char_count: Option<i32>) {
pub fn update(&self, chars_count: i32, bytes_total: usize) {
// Update children components
self.counter.update(char_count);
self.send.set_sensitive(match char_count {
Some(total) => total > 0,
None => false,
});
self.counter.update(chars_count, bytes_total);
self.send.set_sensitive(bytes_total > 0);
}
}

View file

@ -1,8 +1,9 @@
use gtk::{prelude::WidgetExt, Label};
use plurify::Plurify;
pub trait Counter {
fn counter() -> Self;
fn update(&self, char_count: Option<i32>);
fn update(&self, char_count: i32, bytes_total: usize);
}
impl Counter for Label {
@ -14,13 +15,17 @@ impl Counter for Label {
// Actions
fn update(&self, char_count: Option<i32>) {
match char_count {
Some(value) => {
self.set_label(&value.to_string());
self.set_visible(value > 0);
}
None => self.set_visible(false),
}
fn update(&self, chars_count: i32, bytes_total: usize) {
self.set_visible(if bytes_total > 0 {
self.set_label(&bytes_total.to_string());
self.set_tooltip_markup(Some(&format!(
"{bytes_total} {} <sup>/ {chars_count} {}</sup>",
(bytes_total).plurify(&["byte", "bytes", "bytes"]),
(chars_count as usize).plurify(&["char", "chars", "chars"]),
)));
true
} else {
false
})
}
}