Skip to content

Commit

Permalink
Add copy
Browse files Browse the repository at this point in the history
  • Loading branch information
AeEn123 committed Jan 13, 2025
1 parent 1425b36 commit 8dca448
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 21 deletions.
6 changes: 6 additions & 0 deletions locales/en-GB.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ button-search = Search <Ctrl+F>
button-swap = Swap assets <F4>
button-copy-logs = Copy log to clipboard
button-export-logs = Export log to file
button-copy = Copy <Ctrl+D>
# Confirmations
confirmation-generic-confirmation-title = Confirmation
Expand Down Expand Up @@ -63,6 +64,7 @@ behavior = Behavior
check-for-updates = Check for updates
automatically-install-updates = Automatically install updates
use-alias = Export your renamed filenames
use-topbar-buttons = Enable toolbar
# Descriptions
Expand All @@ -73,6 +75,8 @@ use-alias-description = Instead of exporting the raw file name for the asset, ch
swap-choose-file = Double click a file to swap
swap-with = Double click a file to swap with "{ $asset }"
logs-description = The logs show how the program is performing, if any errors happen, they will show up here
copy-choose-file = Double click a file to copy
overwrite-with = Double click a file to overwrite with "{ $asset }"
# Statuses
Expand All @@ -83,6 +87,8 @@ extracting-files = Extracting files ({ $item }/{ $total })
filtering-files = Filtering files ({ $item }/{ $total })
all-extracted = All files extracted
stage = Stage { $stage }/{ $max }: { $status }
swapped = Swapped { $item_a } with { $item_b }
copied = Overwritten { $item_b } with { $item_a }
# Error Statuses
failed-deleting-file = ERROR: Failed to delete ({ $item }/{ $total })
Expand Down
72 changes: 51 additions & 21 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,33 @@ struct TabViewer<'a> {
swapping_asset_a: &'a mut Option<String>,
locale: &'a mut FluentBundle<Arc<FluentResource>>,
asset_context_menu_open: &'a mut Option<usize>,
copying: &'a mut bool,
}

fn double_click(dir: String, value: String, mode: String, swapping: &mut bool, swapping_asset_a: &mut Option<String>) {
if !*swapping {
let temp_dir = logic::get_temp_dir(true);
let alias = logic::get_asset_alias(&value);
let destination = format!("{}/{}", temp_dir, alias); // Join both paths
let origin = format!("{}/{}", dir, value);
let new_destination = logic::extract_file(origin, mode, destination.clone(), true);
if new_destination != "None" {
let _ = open::that(new_destination); // Open when finished
fn double_click(dir: String, value: String, mode: String, swapping: &mut bool, copying: &mut bool, swapping_asset_a: &mut Option<String>) {
if *copying {
if swapping_asset_a.is_none() {
*swapping_asset_a = Some(value);
} else {
logic::copy_assets(&dir, &swapping_asset_a.as_ref().unwrap(), &value);
}
} else {
} else if *swapping {
if swapping_asset_a.is_none() {
*swapping_asset_a = Some(value);
} else {
logic::swap_assets(&dir, &swapping_asset_a.as_ref().unwrap(), &value);
*swapping_asset_a = None;
*swapping = false
}
} else {
let temp_dir = logic::get_temp_dir(true);
let alias = logic::get_asset_alias(&value);
let destination = format!("{}/{}", temp_dir, alias); // Join both paths
let origin = format!("{}/{}", dir, value);
let new_destination = logic::extract_file(origin, mode, destination.clone(), true);
if new_destination != "None" {
let _ = open::that(new_destination); // Open when finished
}
}
}

Expand Down Expand Up @@ -130,10 +137,9 @@ fn toggle_swap(swapping: &mut bool, swapping_asset_a: &mut Option<String>, local
*swapping = !*swapping;

}

}

fn asset_buttons(ui: &mut egui::Ui, locale: &FluentBundle<Arc<FluentResource>>, searching: &mut bool, renaming: &mut bool, asset_context_menu_open: &mut Option<usize>, swapping: &mut bool, swapping_asset_a: &mut Option<String>, cache_directory: &str, tab: &str, file_list: &Vec<String>, focus_search_box: &mut bool) {
fn asset_buttons(ui: &mut egui::Ui, locale: &FluentBundle<Arc<FluentResource>>, searching: &mut bool, renaming: &mut bool, asset_context_menu_open: &mut Option<usize>, swapping: &mut bool, swapping_asset_a: &mut Option<String>, cache_directory: &str, tab: &str, file_list: &Vec<String>, focus_search_box: &mut bool, copying: &mut bool) {
if ui.button(logic::get_message(locale, "button-search", None)).clicked() {
*searching = !*searching;
*focus_search_box = true;
Expand All @@ -158,11 +164,14 @@ fn asset_buttons(ui: &mut egui::Ui, locale: &FluentBundle<Arc<FluentResource>>,
logic::refresh(cache_directory.to_owned(), tab.to_owned(), false, false);
*asset_context_menu_open = None;
}

if ui.button(logic::get_message(locale, "button-swap", None)).clicked() {
toggle_swap(swapping,swapping_asset_a, locale);
*asset_context_menu_open = None;
}
if ui.button(logic::get_message(locale, "button-copy", None)).clicked() {
toggle_swap(copying,swapping_asset_a, locale);
*asset_context_menu_open = None;
}
}

fn add_dependency_credit(dependency: [&str;2], ui: &mut egui::Ui, sponsor_message: &str) {
Expand Down Expand Up @@ -237,15 +246,23 @@ impl egui_dock::TabViewer for TabViewer<'_> {
if ui.input(|i| i.key_pressed(egui::Key::F4)) {
toggle_swap(self.swapping, self.swapping_asset_a, self.locale);
}
if ui.input(|i| i.modifiers.ctrl && i.key_pressed(egui::Key::D)) {
// Ctrl+C (Copy)
toggle_swap(self.copying, self.swapping_asset_a, self.locale);
}

// GUI logic below here

// Top UI buttons
if logic::get_config_bool("use_topbar_buttons").unwrap_or(true) {
ui.horizontal(|ui| {
asset_buttons(ui, self.locale, self.searching, self.renaming,
self.asset_context_menu_open, self.swapping,
self.swapping_asset_a, &cache_directory, &tab, &file_list, &mut focus_search_box);
ui.push_id("Topbar buttons", |ui| {
egui::ScrollArea::horizontal().show(ui, |ui| {
ui.horizontal(|ui| {
asset_buttons(ui, self.locale, self.searching, self.renaming,
self.asset_context_menu_open, self.swapping, self.swapping_asset_a,
&cache_directory, &tab, &file_list, &mut focus_search_box, &mut self.copying);
});
})
});
}

Expand Down Expand Up @@ -283,7 +300,7 @@ impl egui_dock::TabViewer for TabViewer<'_> {
if let Some(selected) = *self.selected {
// Get file name after getting the selected value
if let Some(file_name) = file_list.get(selected) {
double_click(cache_directory.clone(), file_name.to_string(), tab.to_string(), self.swapping, self.swapping_asset_a);
double_click(cache_directory.clone(), file_name.to_string(), tab.to_string(), self.swapping, self.copying, self.swapping_asset_a);
}
}
}
Expand All @@ -304,6 +321,16 @@ impl egui_dock::TabViewer for TabViewer<'_> {
}
}

if *self.copying {
if self.swapping_asset_a.as_ref().is_none() {
ui.heading(logic::get_message(self.locale, "copy-choose-file", None));
} else {
let mut args = fluent_bundle::FluentArgs::new();
args.set("asset", logic::get_asset_alias(self.swapping_asset_a.as_ref().unwrap()));
ui.heading(logic::get_message(self.locale, "overwrite-with", Some(&args)));
}
}

let file_list = if *self.searching {
let old_search_query = self.search_query.clone();

Expand Down Expand Up @@ -401,15 +428,15 @@ impl egui_dock::TabViewer for TabViewer<'_> {
if *asset == i {
response.context_menu(|ui| {
asset_buttons(ui, self.locale, self.searching, self.renaming,
self.asset_context_menu_open, self.swapping,
self.swapping_asset_a, &cache_directory, &tab, &file_list, &mut focus_search_box);
self.asset_context_menu_open, self.swapping, self.swapping_asset_a,
&cache_directory, &tab, &file_list, &mut focus_search_box, &mut self.copying);
});
}

}

if response.double_clicked() {
double_click(cache_directory.clone(), file_name.to_string(), tab.to_string(), self.swapping, self.swapping_asset_a);
double_click(cache_directory.clone(), file_name.to_string(), tab.to_string(), self.swapping, self.copying, self.swapping_asset_a);
}

// Handle keyboard scrolling
Expand Down Expand Up @@ -544,6 +571,7 @@ struct MyApp {
swapping_asset_a: Option<String>,
locale: FluentBundle<Arc<FluentResource>>,
asset_context_menu_open: Option<usize>,
copying: bool,
}

impl Default for MyApp {
Expand Down Expand Up @@ -571,6 +599,7 @@ impl Default for MyApp {
swapping_asset_a: None,
locale: logic::get_locale(None),
asset_context_menu_open: None,
copying: false,
}
}
}
Expand Down Expand Up @@ -671,6 +700,7 @@ impl eframe::App for MyApp {
current_tab: &mut self.current_tab,
locale: &mut self.locale,
asset_context_menu_open: &mut self.asset_context_menu_open,
copying: &mut self.copying,
});

{
Expand Down
39 changes: 39 additions & 0 deletions src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,46 @@ pub fn swap_assets(dir: &str, asset_a: &str, asset_b: &str) {
log::error(&format!("Error opening file '{}': {}", asset_b_path, e));
}
};
let mut args= FluentArgs::new();
args.set("item_a", asset_a);
args.set("item_b", asset_b);
update_status(get_message(&locale, "swapped", Some(&args)));
}

pub fn copy_assets(dir: &str, asset_a: &str, asset_b: &str) {
let asset_a_path = format!("{}/{}", dir, asset_a);
let asset_b_path = format!("{}/{}", dir, asset_b);
let locale = get_locale(None);

let asset_a_bytes = match fs::read(&asset_a_path) {
Ok(bytes) => {
bytes
},
Err(e) => {
let mut args= FluentArgs::new();
args.set("error", e.to_string());

update_status(get_message(&locale, "failed-opening-file", Some(&args)));
log::error(&format!("Error opening file '{}': {}", asset_a_path, e));
return
}
};

match fs::write(&asset_b_path, asset_a_bytes) {
Ok(_) => (),
Err(e) => {
let mut args= FluentArgs::new();
args.set("error", e.to_string());

update_status(get_message(&locale, "failed-opening-file", Some(&args)));
log::error(&format!("Error opening file '{}': {}", asset_b_path, e));
}
};

let mut args= FluentArgs::new();
args.set("item_a", asset_a);
args.set("item_b", asset_b);
update_status(get_message(&locale, "copied", Some(&args)));
}

pub fn filter_file_list(query: String) {
Expand Down

0 comments on commit 8dca448

Please sign in to comment.