about summary refs log tree commit diff
path: root/src/ui
diff options
context:
space:
mode:
authorJoan Lledó <jlledom@member.fsf.org>2023-09-23 13:41:53 +0200
committerJoan Lledó <jlledom@member.fsf.org>2023-09-30 13:14:51 +0200
commit39761d3597c4ce6db1709501e3601b018aaeb233 (patch)
tree0239398ca15afabb28edcd4b56e3576f9bc88d27 /src/ui
parent5adf507e2a0d17f88ab6bc4541f6f2cb12bde193 (diff)
downloadFurtherance-39761d3597c4ce6db1709501e3601b018aaeb233.tar.zst
Add autocomplete popup
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/window.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/ui/window.rs b/src/ui/window.rs
index 2270711..0f54828 100644
--- a/src/ui/window.rs
+++ b/src/ui/window.rs
@@ -115,6 +115,8 @@ glib::wrapper! {
 }
 
 impl FurtheranceWindow {
+    const MIN_PREFIX_LENGTH: i32 = 3;
+
     pub fn new(app: &Application) -> Self {
         glib::Object::builder()
             .property("application", Some(app))
@@ -182,6 +184,13 @@ impl FurtheranceWindow {
         imp.start_button.set_sensitive(false);
         imp.start_button.add_css_class("suggested-action");
         self.refresh_timer();
+
+        let task_autocomplete = gtk::EntryCompletion::new();
+        task_autocomplete.set_text_column(0);
+        task_autocomplete.set_minimum_key_length(FurtheranceWindow::MIN_PREFIX_LENGTH);
+        task_autocomplete.set_match_func(|_ac, _s, _it| { true });
+        imp.task_input.set_completion(Some(&task_autocomplete));
+
         imp.task_input.grab_focus();
 
         if settings_manager::get_bool("autosave") {
@@ -200,11 +209,18 @@ impl FurtheranceWindow {
                 let imp2 = imp::FurtheranceWindow::from_obj(&this);
                 let task_input_text = task_input.text();
                 let split_tags: Vec<&str> = task_input_text.trim().split('#').collect();
-                if split_tags[0].trim().is_empty() {
+                let task_name = split_tags[0];
+                if task_name.trim().is_empty() {
                     imp2.start_button.set_sensitive(false);
                 } else {
                     imp2.start_button.set_sensitive(true);
                 }
+
+                if task_input.text().len() >= FurtheranceWindow::MIN_PREFIX_LENGTH.try_into().unwrap() {
+                    let task_autocomplete = task_input.completion().unwrap();
+                    let model = Self::update_list_model(task_name.to_string());
+                    task_autocomplete.set_model(Some(&model));
+                }
             }));
 
         imp.start_button.connect_clicked(clone!(@weak self as this => move |button| {
@@ -528,6 +544,18 @@ impl FurtheranceWindow {
         imp.task_input.set_activates_default(true);
     }
 
+    fn update_list_model(task_name: String) -> gtk::ListStore {
+        let col_types: [glib::Type; 1] = [glib::Type::STRING];
+        let mut task_list = database::get_list_by_name(task_name).unwrap();
+        task_list.dedup_by(|a, b| a.task_name == b.task_name && a.tags == b.tags);
+        let store = gtk::ListStore::new(&col_types);
+
+        for task in task_list {
+            store.set(&store.append(), &[(0, &task.to_string())]);
+        }
+        store
+    }
+
     fn get_idle_time(&self) -> Result<u64, Box<dyn std::error::Error>> {
         let c = Connection::new_session()?;