From 78d3fb64e58454b03d22f7ac0ccc5ba51c560b3d Mon Sep 17 00:00:00 2001 From: ochibani <11yzyv86j@relay.firefox.com> Date: Thu, 29 Sep 2022 16:03:42 +0200 Subject: [PATCH] display home icons in folder chooser --- src/config_management.rs | 23 +++++++++++++++++++++++ src/main.rs | 8 ++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/config_management.rs b/src/config_management.rs index 0b32764..0d91bc4 100755 --- a/src/config_management.rs +++ b/src/config_management.rs @@ -1,8 +1,10 @@ extern crate glib; extern crate ini; +extern crate dirs; use glib::get_user_data_dir; use ini::Ini; +use dirs::home_dir; use std::ops::Add; use std::path::{Path, PathBuf}; @@ -91,3 +93,24 @@ pub fn set(selection: &str, key: &str, value: &str) -> bool { pub fn set_bool(selection: &str, key: &str, value: bool) -> bool { set(&selection, &key, if value { "1" } else { "0" }) } + +pub fn folder_icon(folder_chooser_name: Option<&str>) -> &str { + let home_folder = dirs::home_dir().unwrap(); + if folder_chooser_name == home_folder.as_path().file_name().unwrap().to_str(){ + "user-home" + } + else { + match folder_chooser_name { + Some("/") => "drive-harddisk", + Some("Desktop") => "user-desktop", + Some("Documents") => "folder-documents", + Some("Downloads") => "folder-download", + Some("Music") => "folder-music", + Some("Pictures") => "folder-pictures", + Some("Public") => "folder-publicshare", + Some("Templates") => "folder-templates", + Some("Videos") => "folder-videos", + _ => "folder", + } + } +} diff --git a/src/main.rs b/src/main.rs index e8f4d4d..ed4d8fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,7 @@ pub fn build_ui(application: &Application) { let delay_spin: SpinButton = builder.object("delay").unwrap(); let filename_entry: Entry = builder.object("filename").unwrap(); let folder_chooser_button: Button = builder.object("folderchooser").unwrap(); + let folder_chooser_image: Image = builder.object("folderchooserimage").unwrap(); let folder_chooser_label: Label = builder.object("folderchooserlabel").unwrap(); let follow_mouse_switch: CheckButton = builder.object("followmouseswitch").unwrap(); let format_chooser_combobox: ComboBoxText = builder.object("comboboxtext1").unwrap(); @@ -250,14 +251,17 @@ pub fn build_ui(application: &Application) { let mut folder_chooser = Some(gio::File::for_uri(&config_management::get("default", "folder"))).unwrap(); let mut folder_chooser_name = folder_chooser.basename().unwrap(); folder_chooser_label.set_label(&folder_chooser_name.to_string_lossy()); + let mut folder_chooser_icon = config_management::folder_icon(folder_chooser_name.to_str()); + folder_chooser_image.set_icon_name(Some(folder_chooser_icon)); // show file chooser dialog folder_chooser_button.connect_clicked(glib::clone!(@strong folder_chooser_native => move |_| { - folder_chooser_native.connect_response(glib::clone!(@strong folder_chooser_native, @strong folder_chooser_label => move |_, response| { + folder_chooser_native.connect_response(glib::clone!(@strong folder_chooser_native, @strong folder_chooser_label, @strong folder_chooser_image => move |_, response| { if response == gtk::ResponseType::Accept { let folder_chooser = folder_chooser_native.file().unwrap(); let folder_chooser_name = folder_chooser.basename().unwrap(); folder_chooser_label.set_label(&folder_chooser_name.to_string_lossy()); - // TODO: change folder icon + let mut folder_chooser_icon = config_management::folder_icon(folder_chooser_name.to_str()); + folder_chooser_image.set_icon_name(Some(folder_chooser_icon)); }; folder_chooser_native.destroy(); }));