2022-05-22 20:59:09 +12:00
|
|
|
use gdk4::{Key, ModifierType};
|
|
|
|
use gtk4::prelude::*;
|
|
|
|
use gtk4::GestureClick;
|
2021-11-25 20:36:49 +13:00
|
|
|
|
2021-12-20 02:45:10 +13:00
|
|
|
use crate::help_functions::*;
|
2021-12-24 21:18:55 +13:00
|
|
|
use crate::notebook_enums::NotebookUpperEnum;
|
2021-12-20 02:45:10 +13:00
|
|
|
|
2021-11-25 20:36:49 +13:00
|
|
|
// TODO add option to open files and folders from context menu activated by pressing ONCE with right mouse button
|
|
|
|
|
2023-10-05 19:06:47 +13:00
|
|
|
pub fn opening_enter_function_ported_upper_directories(
|
|
|
|
event_controller: >k4::EventControllerKey,
|
|
|
|
_key_value: Key,
|
|
|
|
key_code: u32,
|
|
|
|
_modifier_type: ModifierType,
|
|
|
|
) -> glib::Propagation {
|
2022-05-22 20:59:09 +12:00
|
|
|
let tree_view = event_controller.widget().downcast::<gtk4::TreeView>().unwrap();
|
2023-10-11 07:54:41 +13:00
|
|
|
|
|
|
|
if cfg!(debug_assertions) {
|
2022-12-21 20:44:26 +13:00
|
|
|
println!("key_code {key_code}");
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
match get_notebook_upper_enum_from_tree_view(&tree_view) {
|
|
|
|
NotebookUpperEnum::IncludedDirectories => {
|
|
|
|
handle_tree_keypress_upper_directories(
|
|
|
|
&tree_view,
|
|
|
|
key_code,
|
|
|
|
ColumnsIncludedDirectory::Path as i32,
|
|
|
|
Some(ColumnsIncludedDirectory::ReferenceButton as i32),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
NotebookUpperEnum::ExcludedDirectories => {
|
|
|
|
handle_tree_keypress_upper_directories(&tree_view, key_code, ColumnsExcludedDirectory::Path as i32, None);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 07:43:06 +13:00
|
|
|
// false // True catches signal, and don't send it to function, e.g. up button is caught and don't move selection
|
2023-10-05 19:06:47 +13:00
|
|
|
glib::Propagation::Proceed
|
2021-11-29 23:38:38 +13:00
|
|
|
}
|
2021-12-18 07:44:29 +13:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn opening_middle_mouse_function(gesture_click: &GestureClick, _number_of_clicks: i32, _b: f64, _c: f64) {
|
|
|
|
let tree_view = gesture_click.widget().downcast::<gtk4::TreeView>().unwrap();
|
2021-12-19 04:48:30 +13:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
let nt_object = get_notebook_object_from_tree_view(&tree_view);
|
|
|
|
if let Some(column_header) = nt_object.column_header {
|
|
|
|
if gesture_click.current_button() == 2 {
|
|
|
|
reverse_selection(&tree_view, column_header, nt_object.column_selection);
|
2022-01-11 07:30:35 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn opening_double_click_function_directories(gesture_click: &GestureClick, number_of_clicks: i32, _b: f64, _c: f64) {
|
|
|
|
let tree_view = gesture_click.widget().downcast::<gtk4::TreeView>().unwrap();
|
|
|
|
|
|
|
|
if number_of_clicks == 2 && (gesture_click.current_button() == 1 || gesture_click.current_button() == 3) {
|
|
|
|
match get_notebook_upper_enum_from_tree_view(&tree_view) {
|
2021-12-24 21:18:55 +13:00
|
|
|
NotebookUpperEnum::IncludedDirectories => {
|
2022-05-22 20:59:09 +12:00
|
|
|
common_open_function_upper_directories(&tree_view, ColumnsIncludedDirectory::Path as i32);
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
|
|
|
NotebookUpperEnum::ExcludedDirectories => {
|
2022-05-22 20:59:09 +12:00
|
|
|
common_open_function_upper_directories(&tree_view, ColumnsExcludedDirectory::Path as i32);
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 19:06:47 +13:00
|
|
|
pub fn opening_enter_function_ported(event_controller: >k4::EventControllerKey, _key: Key, key_code: u32, _modifier_type: ModifierType) -> glib::Propagation {
|
2022-05-22 20:59:09 +12:00
|
|
|
let tree_view = event_controller.widget().downcast::<gtk4::TreeView>().unwrap();
|
2023-10-11 07:54:41 +13:00
|
|
|
if cfg!(debug_assertions) {
|
2022-12-21 20:44:26 +13:00
|
|
|
println!("key_code {key_code}");
|
2022-05-22 20:59:09 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
let nt_object = get_notebook_object_from_tree_view(&tree_view);
|
|
|
|
handle_tree_keypress(
|
|
|
|
&tree_view,
|
|
|
|
key_code,
|
|
|
|
nt_object.column_name,
|
|
|
|
nt_object.column_path,
|
|
|
|
nt_object.column_selection,
|
|
|
|
nt_object.column_header,
|
|
|
|
);
|
2023-10-05 19:06:47 +13:00
|
|
|
glib::Propagation::Proceed // True catches signal, and don't send it to function, e.g. up button is caught and don't move selection
|
2022-05-22 20:59:09 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn opening_double_click_function(gesture_click: &GestureClick, number_of_clicks: i32, _b: f64, _c: f64) {
|
|
|
|
let tree_view = gesture_click.widget().downcast::<gtk4::TreeView>().unwrap();
|
|
|
|
|
|
|
|
let nt_object = get_notebook_object_from_tree_view(&tree_view);
|
|
|
|
if number_of_clicks == 2 {
|
|
|
|
if gesture_click.current_button() == 1 {
|
2023-01-29 06:54:02 +13:00
|
|
|
common_open_function(&tree_view, nt_object.column_name, nt_object.column_path, &OpenMode::PathAndName);
|
2022-05-22 20:59:09 +12:00
|
|
|
} else if gesture_click.current_button() == 3 {
|
2023-01-29 06:54:02 +13:00
|
|
|
common_open_function(&tree_view, nt_object.column_name, nt_object.column_path, &OpenMode::OnlyPath);
|
2022-05-22 20:59:09 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2021-11-25 20:36:49 +13:00
|
|
|
enum OpenMode {
|
|
|
|
OnlyPath,
|
|
|
|
PathAndName,
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
fn common_mark_function(tree_view: >k4::TreeView, column_selection: i32, column_header: Option<i32>) {
|
2021-11-25 20:36:49 +13:00
|
|
|
let selection = tree_view.selection();
|
|
|
|
let (selected_rows, tree_model) = selection.selected_rows();
|
|
|
|
|
|
|
|
let model = get_list_store(tree_view);
|
|
|
|
|
|
|
|
for tree_path in selected_rows.iter().rev() {
|
2022-05-22 20:59:09 +12:00
|
|
|
if let Some(column_header) = column_header {
|
|
|
|
if model.get::<bool>(&model.iter(tree_path).unwrap(), column_header) {
|
2021-12-24 21:18:55 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-05-22 20:59:09 +12:00
|
|
|
let value = !tree_model.get::<bool>(&tree_model.iter(tree_path).unwrap(), column_selection);
|
2021-12-24 21:18:55 +13:00
|
|
|
model.set_value(&tree_model.iter(tree_path).unwrap(), column_selection as u32, &value.to_value());
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
fn common_open_function(tree_view: >k4::TreeView, column_name: i32, column_path: i32, opening_mode: &OpenMode) {
|
2021-11-25 20:36:49 +13:00
|
|
|
let selection = tree_view.selection();
|
|
|
|
let (selected_rows, tree_model) = selection.selected_rows();
|
|
|
|
|
|
|
|
for tree_path in selected_rows.iter().rev() {
|
2022-05-22 20:59:09 +12:00
|
|
|
let name = tree_model.get::<String>(&tree_model.iter(tree_path).unwrap(), column_name);
|
|
|
|
let path = tree_model.get::<String>(&tree_model.iter(tree_path).unwrap(), column_path);
|
2021-11-25 20:36:49 +13:00
|
|
|
|
2021-12-19 11:45:37 +13:00
|
|
|
let end_path = match opening_mode {
|
|
|
|
OpenMode::OnlyPath => path,
|
2021-12-22 06:23:17 +13:00
|
|
|
OpenMode::PathAndName => get_full_name_from_path_name(&path, &name),
|
2021-12-19 11:45:37 +13:00
|
|
|
};
|
2021-11-25 20:36:49 +13:00
|
|
|
|
2022-06-18 20:59:46 +12:00
|
|
|
if let Err(e) = open::that(&end_path) {
|
2022-12-21 20:44:26 +13:00
|
|
|
println!("Failed to open file {end_path}, reason {e}");
|
2022-06-18 20:59:46 +12:00
|
|
|
};
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 03:52:55 +12:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
fn reverse_selection(tree_view: >k4::TreeView, column_header: i32, column_selection: i32) {
|
2022-01-11 07:30:35 +13:00
|
|
|
let (selected_rows, model) = tree_view.selection().selected_rows();
|
2022-05-22 20:59:09 +12:00
|
|
|
let model = model.downcast::<gtk4::ListStore>().unwrap();
|
2022-01-11 07:30:35 +13:00
|
|
|
|
|
|
|
if selected_rows.len() != 1 {
|
|
|
|
return; // Multiple selection is not supported because it is a lot of harder to do it properly
|
|
|
|
}
|
|
|
|
let tree_path = selected_rows[0].clone();
|
|
|
|
let current_iter = model.iter(&tree_path).unwrap();
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
if model.get::<bool>(¤t_iter, column_header) {
|
2022-01-11 07:30:35 +13:00
|
|
|
return; // Selecting header is not supported(this is available by using reference)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will revert selection of current selected item, but I don't think that this is needed
|
2022-05-22 20:59:09 +12:00
|
|
|
// let current_value = model.get::<bool>(¤t_iter, column_selection);
|
2022-01-11 07:30:35 +13:00
|
|
|
// model.set_value(¤t_iter, column_selection as u32, &(!current_value).to_value());
|
|
|
|
|
2022-02-02 05:08:41 +13:00
|
|
|
let to_upper_iter = current_iter;
|
2022-01-11 07:30:35 +13:00
|
|
|
loop {
|
|
|
|
if !model.iter_previous(&to_upper_iter) {
|
|
|
|
break;
|
|
|
|
}
|
2022-05-22 20:59:09 +12:00
|
|
|
if model.get::<bool>(&to_upper_iter, column_header) {
|
2022-01-11 07:30:35 +13:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
let current_value = model.get::<bool>(&to_upper_iter, column_selection);
|
2022-01-11 07:30:35 +13:00
|
|
|
model.set_value(&to_upper_iter, column_selection as u32, &(!current_value).to_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
let to_lower_iter = current_iter;
|
|
|
|
loop {
|
|
|
|
if !model.iter_next(&to_lower_iter) {
|
|
|
|
break;
|
|
|
|
}
|
2022-05-22 20:59:09 +12:00
|
|
|
if model.get::<bool>(&to_lower_iter, column_header) {
|
2022-01-11 07:30:35 +13:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
let current_value = model.get::<bool>(&to_lower_iter, column_selection);
|
2022-01-11 07:30:35 +13:00
|
|
|
model.set_value(&to_lower_iter, column_selection as u32, &(!current_value).to_value());
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 20:36:49 +13:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
fn common_open_function_upper_directories(tree_view: >k4::TreeView, column_full_path: i32) {
|
2021-12-24 21:18:55 +13:00
|
|
|
let selection = tree_view.selection();
|
|
|
|
let (selected_rows, tree_model) = selection.selected_rows();
|
|
|
|
|
|
|
|
for tree_path in selected_rows.iter().rev() {
|
2022-05-22 20:59:09 +12:00
|
|
|
let full_path = tree_model.get::<String>(&tree_model.iter(tree_path).unwrap(), column_full_path);
|
2021-12-24 21:18:55 +13:00
|
|
|
|
2022-06-18 20:59:46 +12:00
|
|
|
if let Err(e) = open::that(&full_path) {
|
2022-12-21 20:44:26 +13:00
|
|
|
println!("Failed to open file {full_path}, reason {e}");
|
2022-06-18 20:59:46 +12:00
|
|
|
};
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
fn handle_tree_keypress_upper_directories(tree_view: >k4::TreeView, key_code: u32, full_path_column: i32, mark_column: Option<i32>) {
|
2021-12-24 21:18:55 +13:00
|
|
|
match key_code {
|
|
|
|
KEY_ENTER => {
|
|
|
|
common_open_function_upper_directories(tree_view, full_path_column);
|
|
|
|
}
|
|
|
|
KEY_SPACE => {
|
|
|
|
if let Some(mark_column) = mark_column {
|
|
|
|
common_mark_function(tree_view, mark_column, None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
fn handle_tree_keypress(tree_view: >k4::TreeView, key_code: u32, name_column: i32, path_column: i32, mark_column: i32, column_header: Option<i32>) {
|
2021-11-29 23:38:38 +13:00
|
|
|
match key_code {
|
|
|
|
KEY_ENTER => {
|
2023-01-29 06:54:02 +13:00
|
|
|
common_open_function(tree_view, name_column, path_column, &OpenMode::PathAndName);
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
2021-11-29 23:38:38 +13:00
|
|
|
KEY_SPACE => {
|
2022-05-22 20:59:09 +12:00
|
|
|
common_mark_function(tree_view, mark_column, column_header);
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn select_function_duplicates(_tree_selection: >k4::TreeSelection, tree_model: >k4::TreeModel, tree_path: >k4::TreePath, _is_path_currently_selected: bool) -> bool {
|
|
|
|
!tree_model.get::<bool>(&tree_model.iter(tree_path).unwrap(), ColumnsDuplicates::IsHeader as i32)
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn select_function_same_music(_tree_selection: >k4::TreeSelection, tree_model: >k4::TreeModel, tree_path: >k4::TreePath, _is_path_currently_selected: bool) -> bool {
|
|
|
|
!tree_model.get::<bool>(&tree_model.iter(tree_path).unwrap(), ColumnsSameMusic::IsHeader as i32)
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn select_function_similar_images(_tree_selection: >k4::TreeSelection, tree_model: >k4::TreeModel, tree_path: >k4::TreePath, _is_path_currently_selected: bool) -> bool {
|
|
|
|
!tree_model.get::<bool>(&tree_model.iter(tree_path).unwrap(), ColumnsSimilarImages::IsHeader as i32)
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn select_function_similar_videos(_tree_selection: >k4::TreeSelection, tree_model: >k4::TreeModel, tree_path: >k4::TreePath, _is_path_currently_selected: bool) -> bool {
|
|
|
|
!tree_model.get::<bool>(&tree_model.iter(tree_path).unwrap(), ColumnsSimilarVideos::IsHeader as i32)
|
2021-11-25 20:36:49 +13:00
|
|
|
}
|
2022-06-01 03:52:55 +12:00
|
|
|
|
2022-05-22 20:59:09 +12:00
|
|
|
pub fn select_function_always_true(_tree_selection: >k4::TreeSelection, _tree_model: >k4::TreeModel, _tree_path: >k4::TreePath, _is_path_currently_selected: bool) -> bool {
|
2021-12-24 21:18:55 +13:00
|
|
|
true
|
|
|
|
}
|