From e1c0f968272d89191ffcdf1f591f8471ac4d8adb Mon Sep 17 00:00:00 2001 From: Gabriel Brand Date: Tue, 14 May 2024 12:39:31 +0200 Subject: csv export dialog: Port GtkDialog to AdwDialog - Use AdwToolbarView - GNOME HIG: Writing Style - GNOME HIG: Add access keys - Disable export button when no file is selected --- src/furtherance.gresource.xml | 2 +- src/gtk/csv_export_dialog.ui | 110 ++++++++++++++++++++++++++++++++++++++++++ src/gtk/dialogs.ui | 101 -------------------------------------- src/ui/window.rs | 78 +++++++++++++++--------------- 4 files changed, 149 insertions(+), 142 deletions(-) create mode 100644 src/gtk/csv_export_dialog.ui delete mode 100644 src/gtk/dialogs.ui (limited to 'src') diff --git a/src/furtherance.gresource.xml b/src/furtherance.gresource.xml index 96f7f67..5a3878a 100644 --- a/src/furtherance.gresource.xml +++ b/src/furtherance.gresource.xml @@ -9,7 +9,7 @@ gtk/tasks_group.ui gtk/tasks_page.ui gtk/task_row.ui - gtk/dialogs.ui + gtk/csv_export_dialog.ui gtk/window.ui diff --git a/src/gtk/csv_export_dialog.ui b/src/gtk/csv_export_dialog.ui new file mode 100644 index 0000000..7e37882 --- /dev/null +++ b/src/gtk/csv_export_dialog.ui @@ -0,0 +1,110 @@ + + + + Export as CSV + + + + + false + false + + + _Cancel + true + + + + + _Export + true + false + + + + + + + + 800 + 600 + true + false + fill + fill + + + vertical + 24 + 12 + 12 + 12 + 12 + + + horizontal + 12 + + + - No File Selected - + true + start + start + + + + + + folder-open-symbolic + Select File + + + + + + + Export Preferences + fill + + + Sort _By + true + + + + Start Time + Stop Time + Task Name + + + + + + + + Sort _Order + true + + + + Ascending + Descending + + + + + + + + + + + + + + + diff --git a/src/gtk/dialogs.ui b/src/gtk/dialogs.ui deleted file mode 100644 index ef141e0..0000000 --- a/src/gtk/dialogs.ui +++ /dev/null @@ -1,101 +0,0 @@ - - - - - 1 - true - Export as CSV - - - Cancel - - - - - Export - - - - - csv_export_cancelbutton - csv_export_applybutton - - - - 800 - 600 - true - false - fill - fill - - - vertical - 24 - 12 - 12 - 12 - 12 - - - horizontal - 12 - - - - no file selected - - true - start - start - - - - - - folder-open-symbolic - - - - - - - CSV export preferences - fill - - - Sort by - - - - Start time - Stop time - Task name - - - - - - - - Sort order - - - - Ascending - Descending - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/ui/window.rs b/src/ui/window.rs index 01d3052..9b49ed6 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -47,8 +47,6 @@ mod imp { pub struct FurtheranceWindow { // Template widgets #[template_child] - pub header_bar: TemplateChild, - #[template_child] pub add_task: TemplateChild, #[template_child] @@ -911,27 +909,32 @@ impl FurtheranceWindow { } pub fn open_csv_export_dialog(&self) { - let builder = gtk::Builder::from_resource("/com/lakoliu/Furtherance/gtk/dialogs.ui"); - let dialog = builder.object::("dialog_csv_export").unwrap(); + let builder = gtk::Builder::from_resource("/com/lakoliu/Furtherance/gtk/csv_export_dialog.ui"); + let dialog = builder.object::("csv_export_dialog").unwrap(); let tasksort_row = builder - .object::("csv_export_tasksort_row") + .object::("tasksort_row") .unwrap(); let sortorder_row = builder - .object::("csv_export_sortorder_row") + .object::("sortorder_row") .unwrap(); let filechooser_button = builder - .object::("csv_export_filechooser_button") + .object::("filechooser_button") .unwrap(); - let chosenfile_label = builder - .object::("csv_export_chosenfile_label") + let selected_file_label = builder + .object::("selected_file_label") + .unwrap(); + let cancel_button = builder + .object::("cancel_button") + .unwrap(); + let export_button = builder + .object::("export_button") .unwrap(); - - dialog.set_transient_for(Some(self)); let filefilter = gtk::FileFilter::new(); filefilter.add_mime_type("text/csv"); filefilter.add_pattern("*.csv"); + // TODO: Deprecated since 4.10. Use GtkFileDialog instead. let filechooser = gtk::FileChooserNative::builder() .title(&gettext("Create or choose a CSV file")) .modal(true) @@ -946,52 +949,47 @@ impl FurtheranceWindow { filechooser.set_current_name("data.csv"); filechooser_button.connect_clicked( - clone!(@weak self as window, @weak filechooser, @weak dialog => move |_| { - dialog.hide(); + clone!(@weak self as window, @weak filechooser => move |_| { filechooser.show(); }), ); filechooser.connect_response( - clone!(@weak dialog, @weak chosenfile_label => move |filechooser, response| { + clone!(@weak selected_file_label, @weak export_button => move |filechooser, response| { if response == gtk::ResponseType::Accept { if let Some(path) = filechooser.file().and_then(|file| file.path()) { - chosenfile_label.set_label(&path.to_string_lossy()); - } else { - chosenfile_label.set_label(&gettext(" - no file selected - ")); + selected_file_label.set_label(&path.to_string_lossy()); + export_button.set_sensitive(true); } } - - dialog.show(); }), ); - dialog.connect_response(clone!(@weak self as window, @weak filechooser, @weak tasksort_row, @weak sortorder_row => move |dialog, response| { - match response { - gtk::ResponseType::Apply => { - let sort = TaskSort::try_from(tasksort_row.selected()).unwrap_or_default(); - let order = SortOrder::try_from(sortorder_row.selected()).unwrap_or_default(); - - if let Some(file) = filechooser.file() { - glib::MainContext::default().spawn_local(clone!(@strong window, @strong file => async move { - if let Err(e) = FurtheranceWindow::export_csv_to_file(sort, order, &file).await { - log::error!("replace file {:?} failed, Err {}", file, e); - window.display_toast(&gettext("Exporting as CSV failed.")); - } else { - window.display_toast(&gettext("Exported as CSV successfully.")); - }; - })); - } - } - _ => {} - } - + cancel_button.connect_clicked(clone!(@weak dialog => move |_| { dialog.close(); })); + export_button.connect_clicked(clone!(@weak self as window, @weak dialog, @weak filechooser, @weak tasksort_row, @weak sortorder_row => move |_| { + let sort = TaskSort::try_from(tasksort_row.selected()).unwrap_or_default(); + let order = SortOrder::try_from(sortorder_row.selected()).unwrap_or_default(); + + if let Some(file) = filechooser.file() { + glib::MainContext::default().spawn_local(clone!(@strong window, @strong file => async move { + if let Err(e) = FurtheranceWindow::export_csv_to_file(sort, order, &file).await { + log::error!("replace file {:?} failed, Err {}", file, e); + window.display_toast(&gettext("Exporting as CSV failed.")); + } else { + window.display_toast(&gettext("Exported as CSV successfully.")); + }; + })); + + dialog.close(); + } + })); + *self.imp().filechooser.borrow_mut() = filechooser; - dialog.show() + dialog.present(self); } pub fn vertical_align(&self, align: gtk::Align) { -- cgit 1.4.1