draft search results widget

This commit is contained in:
yggverse 2024-12-18 07:34:10 +02:00
parent 1017a405d2
commit 98d2f15136
3 changed files with 54 additions and 0 deletions

View File

@ -1,11 +1,13 @@
mod input;
mod match_case;
mod navigation;
mod result;
mod separator;
use super::Subject;
use input::Input;
use navigation::Navigation;
use result::Result;
use gtk::{
prelude::{
@ -27,6 +29,7 @@ impl Form {
/// Create new `Self`
pub fn new(subject: &Rc<RefCell<Option<Subject>>>) -> Self {
// Init components
let result = Rc::new(Result::new());
let input = Rc::new(Input::new());
let match_case = match_case::new();
let navigation = Rc::new(Navigation::new());
@ -44,12 +47,15 @@ impl Form {
g_box.append(&navigation.g_box);
g_box.append(&match_case);
g_box.append(&separator);
g_box.append(&result.label);
// Connect events
input.entry.connect_changed({
let input = input.clone();
let match_case = match_case.clone();
let navigation = navigation.clone();
let result = result.clone();
let separator = separator.clone();
let subject = subject.clone();
move |_| {
let matches = find(
@ -57,6 +63,13 @@ impl Form {
input.entry.text().as_str(),
match_case.is_active(),
);
if !matches.is_empty() {
result.show(0, matches.len());
separator.set_visible(true);
} else {
result.hide();
separator.set_visible(false);
}
input.update(!matches.is_empty());
navigation.update(matches);
}

View File

@ -0,0 +1,40 @@
use gtk::{prelude::WidgetExt, Label};
const MARGIN: i32 = 3;
pub struct Result {
pub label: Label,
}
impl Result {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
label: Label::builder()
.margin_end(MARGIN)
.margin_start(MARGIN)
.visible(false)
.build(),
}
}
// Actions
pub fn show(&self, current: usize, total: usize) {
if total > 0 {
self.label
.set_label(&format!("{current} if {total} matches"));
self.label.remove_css_class("error");
} else {
self.label.set_label(&format!("Phrase not found"));
self.label.add_css_class("error");
}
self.label.set_visible(true);
}
pub fn hide(&self) {
self.label.set_visible(false);
}
}

View File

@ -8,5 +8,6 @@ pub fn new() -> Separator {
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.visible(false)
.build()
}