From b01c08bdbe9bb6b5e2ed98a77b8d0867df0f3d59 Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Sun, 26 May 2024 14:32:16 +0200 Subject: [PATCH] Use EntryCompletion instead of custom Popovers --- src/ui/autosuggestion_element.rs | 32 ---------- src/ui/autosuggestion_popover.rs | 104 ------------------------------- src/ui/mod.rs | 2 - src/ui/sheet_edit_dialog.rs | 40 ------------ 4 files changed, 178 deletions(-) delete mode 100644 src/ui/autosuggestion_element.rs delete mode 100644 src/ui/autosuggestion_popover.rs diff --git a/src/ui/autosuggestion_element.rs b/src/ui/autosuggestion_element.rs deleted file mode 100644 index de97bff..0000000 --- a/src/ui/autosuggestion_element.rs +++ /dev/null @@ -1,32 +0,0 @@ -use gtk::prelude::*; -use relm4::prelude::*; - -pub struct AutosuggestionElementModel { - pub label: String, -} - -#[relm4::factory(pub)] -impl FactoryComponent for AutosuggestionElementModel { - type Init = String; - type ParentWidget = gtk::ListBox; - type CommandOutput = (); - type Input = (); - type Output = (); - - view! { - #[root] - gtk::ListBoxRow { - gtk::Label { - set_label: &self.label, - set_halign: gtk::Align::Start, - set_margin_all: 0, - } - } - } - - fn init_model(label: Self::Init, _index: &DynamicIndex, _sender: FactorySender) -> Self { - AutosuggestionElementModel { label } - } - - fn update(&mut self, msg: Self::Input, _sender: FactorySender) {} -} diff --git a/src/ui/autosuggestion_popover.rs b/src/ui/autosuggestion_popover.rs deleted file mode 100644 index c23d280..0000000 --- a/src/ui/autosuggestion_popover.rs +++ /dev/null @@ -1,104 +0,0 @@ -use gtk::prelude::*; - -use relm4::component::{AsyncComponent, AsyncComponentParts}; -use relm4::factory::FactoryVecDeque; -use relm4::gtk; -use relm4::{AsyncComponentSender, RelmListBoxExt}; - -use super::autosuggestion_element::AutosuggestionElementModel; - -pub struct AutosuggestionPopoverModel { - hidden: bool, - suggestions: Vec, - suggestion_rows: FactoryVecDeque, -} - -#[derive(Debug)] -pub enum AutosuggestionPopoverInput { - Show, - Hide, - SetSuggestions(Vec), - SuggestionIndexSelected(i32), - None, -} - -#[derive(Debug)] -pub enum AutosuggestionPopoverOutput { - SuggestionSelected(String), -} - -#[relm4::component(pub, async)] -impl AsyncComponent for AutosuggestionPopoverModel { - type Init = (); - type Input = AutosuggestionPopoverInput; - type Output = AutosuggestionPopoverOutput; - type CommandOutput = (); - - view! { - gtk::Popover { - #[watch] - set_visible: !model.hidden, - set_has_arrow: false, - model.suggestion_rows.widget() -> &relm4::gtk::ListBox { - set_hexpand: true, - // set_orientation: gtk::Orientation::Vertical, - // set_spacing: 5, - connect_row_activated[sender] => move |list_box, row| { - let index = list_box.index_of_child(row).unwrap(); - sender.input(AutosuggestionPopoverInput::SuggestionIndexSelected(index)); - }, - }, - } - } - - async fn init( - _params: Self::Init, - root: Self::Root, - sender: AsyncComponentSender, - ) -> AsyncComponentParts { - let suggestion_rows = FactoryVecDeque::builder() - .launch(gtk::ListBox::default()) - .forward(sender.input_sender(), |_| AutosuggestionPopoverInput::None); - - let model = AutosuggestionPopoverModel { - hidden: true, - suggestions: Vec::new(), - suggestion_rows, - }; - let widgets = view_output!(); - - AsyncComponentParts { model, widgets } - } - - // TODO: init_loading_widgets - - async fn update( - &mut self, - msg: Self::Input, - _sender: AsyncComponentSender, - _root: &Self::Root, - ) { - match msg { - AutosuggestionPopoverInput::Show => self.hidden = false, - AutosuggestionPopoverInput::Hide => self.hidden = true, - AutosuggestionPopoverInput::SetSuggestions(suggestions) => { - self.suggestion_rows.guard().clear(); - for suggestion in suggestions.iter() { - self.suggestion_rows - .guard() - .push_back(suggestion.to_string()); - } - self.suggestions = suggestions; - } - AutosuggestionPopoverInput::None => {} - AutosuggestionPopoverInput::SuggestionIndexSelected(index) => { - let suggestion = self.suggestions.get(index as usize).unwrap(); - _sender - .output(AutosuggestionPopoverOutput::SuggestionSelected( - suggestion.to_string(), - )) - .unwrap(); - } - } - } -} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 0c07866..2b1b37e 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,6 +1,4 @@ pub mod app; -pub mod autosuggestion_element; -pub mod autosuggestion_popover; pub mod mcdu; pub mod sheet_edit_dialog; pub mod sheet_listing; diff --git a/src/ui/sheet_edit_dialog.rs b/src/ui/sheet_edit_dialog.rs index 57c7a10..3b461b4 100644 --- a/src/ui/sheet_edit_dialog.rs +++ b/src/ui/sheet_edit_dialog.rs @@ -15,8 +15,6 @@ use relm4_components::alert::{Alert, AlertMsg, AlertSettings}; use crate::{database::Database, sheet::Sheet, sheet_dao}; -use super::autosuggestion_popover::{AutosuggestionPopoverInput, AutosuggestionPopoverModel}; - pub struct SheetEditDialogModel { database: Arc, hidden: bool, @@ -27,7 +25,6 @@ pub struct SheetEditDialogModel { is_book: bool, book_sheets: Vec<(String, String, i64)>, alert_empty_fields: Connector, - autosuggestion_popover: AsyncController, } pub struct SheetEditDialogInit { @@ -39,8 +36,6 @@ pub struct SheetEditDialogInit { pub enum SheetEditDialogInput { Accept, Cancel, - ComposerTextChanged(String), - ComposerSuggestionSelected(String), } #[derive(Debug)] @@ -86,7 +81,6 @@ impl AsyncComponent for SheetEditDialogModel { set_buffer: &model.composer_entry_buffer, set_completion: Some(&model.composer_entry_completion), set_hexpand: true, - connect_changed[sender] => move |entry| sender.input(SheetEditDialogInput::ComposerTextChanged(entry.text().to_string())), }, }, gtk::Box { @@ -116,7 +110,6 @@ impl AsyncComponent for SheetEditDialogModel { connect_clicked[sender] => move |_| sender.input(SheetEditDialogInput::Accept) }, }, - // model.autosuggestion_popover.widget(), } } } @@ -181,12 +174,6 @@ impl AsyncComponent for SheetEditDialogModel { // whether the completions should be presented in a popup window composer_entry_completion.set_popup_completion(true); - let autosuggestion_popover = AutosuggestionPopoverModel::builder() - .launch(()) - .forward(sender.input_sender(), |output| match output { - crate::ui::autosuggestion_popover::AutosuggestionPopoverOutput::SuggestionSelected(suggestion) => SheetEditDialogInput::ComposerSuggestionSelected(suggestion), - }); - let model = SheetEditDialogModel { database: params.database, hidden: false, @@ -205,7 +192,6 @@ impl AsyncComponent for SheetEditDialogModel { cancel_label: None, option_label: None, }), - autosuggestion_popover, }; let widgets = view_output!(); @@ -265,32 +251,6 @@ impl AsyncComponent for SheetEditDialogModel { self.hidden = true; self.sheet = None; } - SheetEditDialogInput::ComposerTextChanged(composer_name) => { - // let suggestions = vec![ - // "Hello".into(), - // "World".into(), - // "Seattle".into(), - // "Salzburg".into(), - // ]; - - // self.autosuggestion_popover - // .emit(AutosuggestionPopoverInput::SetSuggestions(suggestions)); - // self.autosuggestion_popover - // .emit(AutosuggestionPopoverInput::Show); - // self.model.filtered_suggestions = self - // .model - // .suggestions - // .iter() - // .filter(|suggestion| suggestion.starts_with(&text)) - // .cloned() - // .collect(); - // self.update_suggestions(); - } - SheetEditDialogInput::ComposerSuggestionSelected(composer_name) => { - self.composer_entry_buffer.set_text(composer_name); - self.autosuggestion_popover - .emit(AutosuggestionPopoverInput::Hide); - } } } }