Skip to content

Commit

Permalink
FileChooserNative
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin committed May 21, 2023
1 parent 4087d0b commit 18df74a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 101 deletions.
51 changes: 17 additions & 34 deletions src/gui_connection/connect_add_files_button.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::path::PathBuf;

use crate::add_files_folders::add_files_to_check;
use crate::fls;
use gtk4::prelude::*;
use gtk4::ResponseType;

use crate::add_files_folders::add_files_to_check;

use crate::gui_data_things::gui_data::GuiData;
use crate::help_function::{get_list_store_from_tree_view, get_selected_folders_files_in_dialog};
use crate::update_records::{update_records, UpdateMode};
Expand All @@ -17,39 +15,24 @@ pub fn connect_add_files_button(gui_data: &GuiData) {

let label_files_folders = gui_data.upper_buttons.label_files_folders.clone();

let window_main = gui_data.window_main.clone();
button_add_files.connect_clicked(move |_| {
let chooser = gtk4::FileChooserDialog::builder()
.title(fls!("dialog_name_files_to_include"))
.action(gtk4::FileChooserAction::Open)
.transient_for(&window_main)
.modal(true)
.build();
chooser.add_button(&fls!("dialog_button_ok"), ResponseType::Ok);
chooser.add_button(&fls!("dialog_button_cancel"), ResponseType::Cancel);

chooser.set_select_multiple(true);
chooser.show();

let tree_view_results = tree_view_results.clone();
let label_files_folders = label_files_folders.clone();
let shared_result_entries = shared_result_entries.clone();
let rules = rules.clone();
let file_chooser_dialog_add_files = gui_data.upper_buttons.file_chooser_dialog_add_files.clone();

chooser.connect_response(move |dialog, response_type| {
if response_type == ResponseType::Ok {
let files: Vec<PathBuf> = get_selected_folders_files_in_dialog(dialog);

let list_store = get_list_store_from_tree_view(&tree_view_results);
{
let mut result_entries = shared_result_entries.borrow_mut();
add_files_to_check(files, &list_store, &mut result_entries);
}
file_chooser_dialog_add_files.connect_response(move |file_chooser_dialog_add_files, response| {
let shared_result_entries = shared_result_entries.clone();
if response == ResponseType::Accept {
let files_to_check = get_selected_folders_files_in_dialog(file_chooser_dialog_add_files);

update_records(&tree_view_results, &shared_result_entries, &rules, &UpdateMode::FileAdded, &label_files_folders);
let list_store = get_list_store_from_tree_view(&tree_view_results);
{
let mut result_entries = shared_result_entries.borrow_mut();
add_files_to_check(files_to_check, &list_store, &mut result_entries);
}

dialog.close();
});
update_records(&tree_view_results, &shared_result_entries, &rules, &UpdateMode::FileAdded, &label_files_folders);
}
});

button_add_files.connect_clicked(move |_| {
file_chooser_dialog_add_files.show();
});
}
161 changes: 98 additions & 63 deletions src/gui_connection/connect_add_folders_button.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;

use glib::signal::Inhibit;
use gtk4::prelude::*;
use gtk4::{Orientation, ResponseType};
use gtk4::{Orientation, ResponseType, Widget, Window};

use crate::add_files_folders::add_folders_to_check;
use crate::fls;
use crate::gui_data_things::gui_data::GuiData;
use crate::help_function::{get_all_boxes_from_widget, get_list_store_from_tree_view, get_selected_folders_files_in_dialog};
use crate::help_function::{get_list_store_from_tree_view, get_selected_folders_files_in_dialog, ResultEntries};
use crate::rule::rules::Rules;
use crate::update_records::{update_records, UpdateMode};

pub fn connect_add_folders_button(gui_data: &GuiData) {
Expand All @@ -20,69 +23,101 @@ pub fn connect_add_folders_button(gui_data: &GuiData) {

let window_main = gui_data.window_main.clone();

let file_chooser_dialog_add_folders = gui_data.upper_buttons.file_chooser_dialog_add_folders.clone();

let shared_result_entries = shared_result_entries;
let label_files_folders = label_files_folders;
let tree_view_results = tree_view_results;
let rules = rules;

file_chooser_dialog_add_folders.connect_response(move |file_chooser_dialog_add_folders, response| {
let shared_result_entries = shared_result_entries.clone();
if response == ResponseType::Accept {
let folders_to_check = get_selected_folders_files_in_dialog(file_chooser_dialog_add_folders);
if !folders_to_check.is_empty() {
create_scan_inside_ignore_files_dialog(
folders_to_check,
tree_view_results.clone(),
shared_result_entries,
rules.clone(),
label_files_folders.clone(),
&window_main,
);
}
}
});

button_add_folders.connect_clicked(move |_| {
let chooser = gtk4::FileChooserDialog::builder()
.title(fls!("dialog_name_files_to_include"))
.action(gtk4::FileChooserAction::SelectFolder)
.transient_for(&window_main)
.modal(true)
.build();
chooser.add_button(&fls!("dialog_button_ok"), ResponseType::Ok);
chooser.add_button(&fls!("dialog_button_cancel"), ResponseType::Cancel);

chooser.set_select_multiple(true);
{
// Adds recursive button to FileDialog
let box_pack = gtk4::Box::new(Orientation::Horizontal, 0);

let switch_scan_inside = gtk4::Switch::new();
box_pack.append(&switch_scan_inside);

let label_scan_inside = gtk4::Label::builder().label(fls!("dialog_scan_inside")).margin_end(5).build();
box_pack.append(&label_scan_inside);

let switch_ignore_folders = gtk4::Switch::new();
switch_ignore_folders.set_active(true);
box_pack.append(&switch_ignore_folders);

let label_ignore_folders = gtk4::Label::builder().label(fls!("dialog_ignore_folders")).margin_end(5).build();
box_pack.append(&label_ignore_folders);

let internal_box = get_all_boxes_from_widget(&chooser)[0].clone();
internal_box.append(&box_pack);

switch_ignore_folders.set_sensitive(false);
let sif = switch_ignore_folders.clone(); // TODO GTK 4
switch_scan_inside.connect_state_set(move |_, b| {
sif.set_sensitive(b);
Inhibit(false)
});

chooser.set_title(Some(&fls!("dialog_name_folders_to_include")));
chooser.show();
file_chooser_dialog_add_folders.show();
});
}

fn create_scan_inside_ignore_files_dialog(
folders_to_check: Vec<PathBuf>,
tree_view_results: gtk4::TreeView,
shared_result_entries: Rc<RefCell<ResultEntries>>,
rules: Rc<RefCell<Rules>>,
label_files_folders: gtk4::Label,
window_main: &Window,
) {
let dialog = gtk4::Dialog::builder().transient_for(window_main).modal(true).build();
dialog.add_button(&fls!("dialog_button_ok"), ResponseType::Ok);
dialog.add_button(&fls!("dialog_button_cancel"), ResponseType::Cancel);

dialog.show();

let dialog_box = dialog.child().unwrap().downcast::<gtk4::Box>().unwrap();
dialog_box.set_margin_top(10);
dialog_box.set_margin_bottom(10);
dialog_box.set_margin_start(10);
dialog_box.set_margin_end(10);

let box_general = gtk4::Box::builder()
.orientation(Orientation::Vertical)
.margin_end(10)
.margin_top(10)
.margin_start(10)
.margin_bottom(10)
.build();

let box_first = gtk4::Box::new(Orientation::Horizontal, 10);
let switch_scan_inside = gtk4::Switch::new();
let label_scan_inside = gtk4::Label::builder().label(fls!("dialog_scan_inside")).margin_end(5).build();
box_first.append(&switch_scan_inside);
box_first.append(&label_scan_inside);

let box_second = gtk4::Box::new(Orientation::Horizontal, 10);
let switch_ignore_folders = gtk4::Switch::new();
switch_ignore_folders.set_active(true);
let label_ignore_folders = gtk4::Label::builder().label(fls!("dialog_ignore_folders")).margin_end(5).build();
box_second.append(&switch_ignore_folders);
box_second.append(&label_ignore_folders);

box_general.append(&box_first);
box_general.append(&box_second);

box_general.insert_after(&dialog_box, None::<&Widget>);

switch_ignore_folders.set_sensitive(false);
let sif = switch_ignore_folders.clone();
switch_scan_inside.connect_state_set(move |_, b| {
sif.set_sensitive(b);
Inhibit(false)
});

dialog.connect_response(move |dialog, response| {
if response == ResponseType::Ok {
let folders_to_check = folders_to_check.clone();
let shared_result_entries = shared_result_entries.clone();
let label_files_folders = label_files_folders.clone();
let tree_view_results = tree_view_results.clone();
let rules = rules.clone();

chooser.connect_response(move |chooser, response| {
if response == ResponseType::Ok {
let list_store = get_list_store_from_tree_view(&tree_view_results);

let folders_to_check: Vec<PathBuf> = get_selected_folders_files_in_dialog(chooser);

let ignore_folders = switch_ignore_folders.is_active();
let check_folders_inside = switch_scan_inside.is_active();
{
let mut result_entries = shared_result_entries.borrow_mut();
add_folders_to_check(folders_to_check, &list_store, &mut result_entries, check_folders_inside, ignore_folders);
}
update_records(&tree_view_results, &shared_result_entries, &rules, &UpdateMode::FileAdded, &label_files_folders);
}

chooser.close();
});
};
let list_store = get_list_store_from_tree_view(&tree_view_results);
let _ignore_folders = switch_ignore_folders.is_active();
let _check_folders_inside = switch_scan_inside.is_active();
{
let mut result_entries = shared_result_entries.borrow_mut();
add_folders_to_check(folders_to_check, &list_store, &mut result_entries, true, true);
}
update_records(&tree_view_results, &shared_result_entries, &rules, &UpdateMode::FileAdded, &label_files_folders);
}
dialog.close();
});
}
2 changes: 1 addition & 1 deletion src/gui_connection/connect_start_renaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn connect_start_renaming(gui_data: &GuiData) {

let label_name = fls!(
"renaming_question",
generate_translation_hashmap(vec![("capture_number", number_of_renamed_files.to_string())])
generate_translation_hashmap(vec![("number_of_renamed_files", number_of_renamed_files.to_string())])
);
let question_label = Label::new(Some(label_name.as_str()));

Expand Down
2 changes: 1 addition & 1 deletion src/gui_data_things/gui_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl GuiData {
window_main.show();
window_main.set_title(Some("Szyszka"));

let upper_buttons = GuiUpperButtons::create_from_builder(&builder_window_main);
let upper_buttons = GuiUpperButtons::create_from_builder(&builder_window_main, &window_main);
let results = GuiResults::create_from_builder(&builder_window_main);
let rules_bottom_panel = GuiRulesBottomPanel::create_from_builder(&builder_window_main);
let popover_select = GuiPopoverSelect::create_from_builder(&builder_popover);
Expand Down
23 changes: 22 additions & 1 deletion src/gui_data_things/gui_data_upper_buttons.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use gtk4::prelude::*;
use gtk4::Window;

use crate::fls;
use crate::help_function::{get_custom_label_from_widget, set_icon_of_button};
Expand All @@ -19,10 +20,13 @@ pub struct GuiUpperButtons {
pub label_files_folders: gtk4::Label,
pub button_results_one_up: gtk4::Button,
pub button_results_one_down: gtk4::Button,

pub file_chooser_dialog_add_files: gtk4::FileChooserNative,
pub file_chooser_dialog_add_folders: gtk4::FileChooserNative,
}

impl GuiUpperButtons {
pub fn create_from_builder(builder: &gtk4::Builder) -> Self {
pub fn create_from_builder(builder: &gtk4::Builder, window_main: &Window) -> Self {
let button_setting: gtk4::Button = builder.object("button_setting").unwrap();
let button_start_rename: gtk4::Button = builder.object("button_start_rename").unwrap();
let button_remove_selection: gtk4::Button = builder.object("button_remove_selection").unwrap();
Expand All @@ -38,6 +42,19 @@ impl GuiUpperButtons {
set_icon_of_button(&button_results_one_up, SZY_ICON_UP);
set_icon_of_button(&button_results_one_down, SZY_ICON_DOWN);

let file_chooser_dialog_add_files = gtk4::FileChooserNative::builder()
.action(gtk4::FileChooserAction::Open)
.transient_for(window_main)
.select_multiple(true)
.modal(true)
.build();
let file_chooser_dialog_add_folders = gtk4::FileChooserNative::builder()
.action(gtk4::FileChooserAction::SelectFolder)
.transient_for(window_main)
.select_multiple(true)
.modal(true)
.build();

Self {
button_setting,
button_start_rename,
Expand All @@ -49,6 +66,8 @@ impl GuiUpperButtons {
label_files_folders,
button_results_one_up,
button_results_one_down,
file_chooser_dialog_add_files,
file_chooser_dialog_add_folders,
}
}
pub fn update_language(&self) {
Expand All @@ -61,5 +80,7 @@ impl GuiUpperButtons {
get_custom_label_from_widget(&self.button_results_one_up).set_label(&fls!("upper_results_one_up_button"));
get_custom_label_from_widget(&self.button_results_one_down).set_label(&fls!("upper_results_one_down_button"));
self.menu_button_select_popup.set_label(&fls!("upper_select_popup_button"));
self.file_chooser_dialog_add_files.set_title(&fls!("dialog_name_files_to_include"));
self.file_chooser_dialog_add_folders.set_title(&fls!("dialog_name_folders_to_include"));
}
}
2 changes: 1 addition & 1 deletion src/help_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ pub fn get_all_children<P: IsA<Widget>>(wid: &P) -> Vec<Widget> {
vector
}

pub fn get_selected_folders_files_in_dialog(dialog: &FileChooserDialog) -> Vec<PathBuf> {
pub fn get_selected_folders_files_in_dialog(dialog: &FileChooserNative) -> Vec<PathBuf> {
let mut files: Vec<PathBuf> = Vec::new();
let g_files = dialog.files();
for index in 0..g_files.n_items() {
Expand Down

0 comments on commit 18df74a

Please sign in to comment.