From f8ed3d6a279a6defb8e289431d37c371e434c6ba Mon Sep 17 00:00:00 2001 From: salihgerdan <8333520+salihgerdan@users.noreply.github.com> Date: Fri, 10 Mar 2023 22:09:05 +0300 Subject: [PATCH] Open directories from the command line --- src/gui.rs | 19 ++++++++++++------- src/gui/treemap_widget/imp.rs | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 710b0de..cf1a117 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,16 +1,17 @@ use crate::config; -use gtk::{prelude::*, ResponseType}; +use gtk::{gio::ApplicationFlags, glib, prelude::*, ResponseType}; use std::rc::Rc; mod treemap_widget; use treemap_widget::TreeMapWidget; pub fn initiate_ui() { - let application = gtk::Application::new(Some(config::APP_NAME), Default::default()); - application.connect_activate(build_ui); + let application = gtk::Application::new(Some(config::APP_NAME), ApplicationFlags::HANDLES_OPEN); + application.connect_open(build_ui); + application.connect_activate(|app| build_ui(app, &[], "")); application.run(); } -fn build_ui(application: >k::Application) { +fn build_ui(application: >k::Application, arg_dirs: &[gtk::gio::File], _: &str) { let window = gtk::ApplicationWindow::new(application); window.set_title(Some(config::APP_TITLE)); @@ -34,21 +35,25 @@ fn build_ui(application: >k::Application) { Some("Cancel"), ); - file_chooser.connect_response(move |d: >k::FileChooserNative, response: ResponseType| { + file_chooser.connect_response(glib::clone!(@weak treemap_widget => move |d: >k::FileChooserNative, response: ResponseType| { if response == ResponseType::Accept { let directory = d.file().expect("Couldn't get directory"); let path = directory.path().expect("Couldn't get path"); - treemap_widget.start_scan(&path.display().to_string()); + treemap_widget.start_scan(path.to_str().unwrap()); println!("{}", path.display()); } d.hide(); - }); + })); let open_button = gtk::Button::new(); open_button.set_icon_name("document-open"); headerbar.pack_start(&open_button); open_button.connect_clicked(move |_| file_chooser.show()); + if let Some(dir) = arg_dirs.get(0) { + treemap_widget.start_scan(dir.path().expect("Couldn't get path").to_str().unwrap()); + } + window.show(); } diff --git a/src/gui/treemap_widget/imp.rs b/src/gui/treemap_widget/imp.rs index 7972619..a8362bd 100644 --- a/src/gui/treemap_widget/imp.rs +++ b/src/gui/treemap_widget/imp.rs @@ -148,7 +148,7 @@ impl ObjectImpl for TreeMapWidget { fn constructed(&self) { self.parent_constructed(); - self.scan_complete_flag.replace(false); + self.scan_complete_flag.replace(true); let obj = self.obj(); obj.set_width_request(100); obj.set_height_request(100);