Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Periodically check for and handle pending GTK events while performing long operations. #625

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions czkawka_gui/src/connect_things/connect_button_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ pub fn empty_folder_remover(
let mut messages: String = "".to_string();

// Must be deleted from end to start, because when deleting entries, TreePath(and also TreeIter) will points to invalid data
for tree_path in selected_rows.iter().rev() {
for (counter, tree_path) in selected_rows.iter().rev().enumerate() {
handle_gtk_pending_event_counter(counter);
let iter = model.iter(tree_path).unwrap();

let name = model.value(&iter, column_file_name).get::<String>().unwrap();
Expand Down Expand Up @@ -408,7 +409,8 @@ pub fn basic_remove(
}

// Must be deleted from end to start, because when deleting entries, TreePath(and also TreeIter) will points to invalid data
for tree_path in selected_rows.iter().rev() {
for (counter, tree_path) in selected_rows.iter().rev().enumerate() {
handle_gtk_pending_event_counter(counter);
let iter = model.iter(tree_path).unwrap();

let name = model.value(&iter, column_file_name).get::<String>().unwrap();
Expand Down Expand Up @@ -504,10 +506,13 @@ pub fn tree_remove(
}

// Delete duplicated entries, and remove real files
let mut counter = 0_usize;
for (path, mut vec_file_name) in map_with_path_to_delete {
vec_file_name.sort();
vec_file_name.dedup();
for file_name in vec_file_name {
handle_gtk_pending_event_counter(counter);
counter += 1;
if !use_trash {
if let Err(e) = fs::remove_file(get_full_name_from_path_name(&path, &file_name)) {
messages += flg!(
Expand Down
6 changes: 4 additions & 2 deletions czkawka_gui/src/connect_things/connect_button_hardlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ pub fn hardlink_symlink(
}
if hardlinking {
for symhardlink_data in vec_symhardlink_data {
for file_to_hardlink in symhardlink_data.files_to_symhardlink {
for (counter, file_to_hardlink) in symhardlink_data.files_to_symhardlink.into_iter().enumerate() {
handle_gtk_pending_event_counter(counter);
if let Err(e) = make_hard_link(&PathBuf::from(&symhardlink_data.original_data), &PathBuf::from(&file_to_hardlink)) {
add_text_to_text_view(text_view_errors, format!("{} {}, reason {}", flg!("hardlink_failed"), file_to_hardlink, e).as_str());
continue;
Expand All @@ -203,7 +204,8 @@ pub fn hardlink_symlink(
}
} else {
for symhardlink_data in vec_symhardlink_data {
for file_to_symlink in symhardlink_data.files_to_symhardlink {
for (counter, file_to_symlink) in symhardlink_data.files_to_symhardlink.into_iter().enumerate() {
handle_gtk_pending_event_counter(counter);
if let Err(e) = fs::remove_file(&file_to_symlink) {
add_text_to_text_view(
text_view_errors,
Expand Down
3 changes: 2 additions & 1 deletion czkawka_gui/src/connect_things/connect_button_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ fn move_files_common(
let mut moved_files: u32 = 0;

// Save to variable paths of files, and remove it when not removing all occurrences.
'next_result: for tree_path in selected_rows.iter().rev() {
'next_result: for (counter, tree_path) in selected_rows.iter().rev().enumerate() {
handle_gtk_pending_event_counter(counter);
let iter = model.iter(tree_path).unwrap();

let file_name = model.value(&iter, column_file_name).get::<String>().unwrap();
Expand Down
17 changes: 17 additions & 0 deletions czkawka_gui/src/help_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub const KEY_SPACE: u32 = 65;
// pub const KEY_HOME: u32 = 115;
// pub const KEY_END: u32 = 110;

pub const CHECK_GTK_EVENTS_INTERVAL: usize = 100;

#[derive(Eq, PartialEq)]
pub enum PopoverTypes {
All,
Expand Down Expand Up @@ -776,6 +778,21 @@ pub fn get_custom_label_from_button_with_image(button: &gtk::Bin) -> gtk::Label
panic!("Button doesn't have proper custom label child");
}

pub fn handle_gtk_pending_event() -> bool {
let have_pending = gtk::events_pending();
if have_pending {
gtk::main_iteration();
}
have_pending
}

pub fn handle_gtk_pending_event_counter(counter: usize) -> bool {
if counter > 0 && (counter % CHECK_GTK_EVENTS_INTERVAL) == 0 {
return handle_gtk_pending_event();
}
false
}

// GTK 4
// pub fn get_custom_label_from_button_with_image<P: IsA<gtk4::Widget>>(button: &P) -> gtk4::Label {
// let internal_box = button.first_child().unwrap().downcast::<gtk4::Box>().unwrap();
Expand Down