Certainly! I have reviewed your code and identified the issue where the settings do not apply correctly. The problem stems from the way you handle the temporary settings within the open_settings function. I'll provide a detailed explanation of the issue, the fix, and enhancements to make the UI look more advanced and technical.
Issue Explanation:
In your original open_settings function, you create local mutable copies of the settings variables:
fn open_settings(&mut self, ctx: &egui::Context) {
// Create local mutable copies of the settings
let mut min_length = self.min_length;
let mut include_ascii = self.include_ascii;
let mut include_utf16le = self.include_utf16le;
let mut include_utf16be = self.include_utf16be;
// Variable to track if "Apply" was clicked
let mut apply_changes = false;
// Open the settings window
// ...
}
However, these local variables are re-initialized on every frame, which means that any changes the user makes are lost unless they click "Apply" immediately within the same frame. This behavior causes the settings to appear as if they are not being applied.
Solution:
To fix this issue, we need to persist the temporary settings between frames while the settings window is open. One effective way to achieve this is by introducing a Settings struct and storing both the current and temporary settings in your application state.
Here are the steps to fix the issue:
-
Create a Settings Struct:
struct Settings {
min_length: usize,
include_ascii: bool,
include_utf16le: bool,
include_utf16be: bool,
}
impl Default for Settings {
fn default() -> Self {
Settings {
min_length: 4,
include_ascii: true,
include_utf16le: true,
include_utf16be: true,
}
}
}
-
Update the StringDumperApp Struct:
struct StringDumperApp {
// Existing fields...
strings: Vec<String>,
file_path: Option<PathBuf>,
search_text: String,
filtered_strings: Vec<String>,
// Settings
current_settings: Settings,
temp_settings: Option<Settings>,
// For keeping track of open windows
settings_open: bool,
about_open: bool,
}
-
Initialize current_settings and temp_settings:
impl Default for StringDumperApp {
fn default() -> Self {
Self {
strings: Vec::new(),
file_path: None,
search_text: String::new(),
filtered_strings: Vec::new(),
current_settings: Settings::default(),
temp_settings: None,
settings_open: false,
about_open: false,
}
}
}
-
Modify the open_settings Function:
fn open_settings(&mut self, ctx: &egui::Context) {
// Initialize temp_settings if None
if self.temp_settings.is_none() {
self.temp_settings = Some(self.current_settings.clone());
}
egui::Window::new("Settings")
.open(&mut self.settings_open)
.show(ctx, |ui| {
let temp_settings = self.temp_settings.as_mut().unwrap();
ui.label("Minimum String Length:");
ui.add(egui::Slider::new(&mut temp_settings.min_length, 1..=20));
ui.separator();
ui.label("String Encodings to Include:");
ui.checkbox(&mut temp_settings.include_ascii, "ASCII");
ui.checkbox(&mut temp_settings.include_utf16le, "UTF-16 Little Endian");
ui.checkbox(&mut temp_settings.include_utf16be, "UTF-16 Big Endian");
ui.separator();
if ui.button("Apply").clicked() {
self.current_settings = temp_settings.clone();
self.temp_settings = None; // Reset temp settings
self.load_strings();
self.settings_open = false; // Close settings window
}
if ui.button("Cancel").clicked() {
self.temp_settings = None; // Discard changes
self.settings_open = false; // Close settings window
}
});
}
-
Update the load_strings Function:
fn load_strings(&mut self) {
if let Some(path) = &self.file_path {
match extract_strings_from_file(
path,
self.current_settings.min_length,
self.current_settings.include_ascii,
self.current_settings.include_utf16le,
self.current_settings.include_utf16be,
) {
Ok(strings) => {
self.strings = strings;
self.filter_strings();
}
Err(e) => {
eprintln!("Error extracting strings: {}", e);
}
}
}
}
With these changes, the temporary settings persist while the settings window is open, and applying changes updates the actual settings used by the application.
Enhancing the UI:
To make the UI look more advanced and technical, we can introduce the following enhancements:
-
Add a Side Panel with File Information:
Display detailed information about the loaded file, such as file size, modification date, and a hexdump preview.
-
Use Collapsible Headers:
Organize the UI using collapsible headers to group related controls and information.
-
Implement a Dark Theme:
Switch to a dark theme, which often gives applications a more technical and modern appearance.
-
Use Tabs for Different Views:
Introduce tabs to separate different functionalities, such as "Strings View," "Hex View," and "Log."
-
Add Advanced Controls:
Include options like regular expression search, string encoding selection, and byte order marks detection.
Here's how you can implement some of these enhancements:
1. Update ui_central_panel to Include Tabs:
fn ui_central_panel(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::TopBottomPanel::top("header_panel").show_inside(ui, |ui| {
ui.vertical_centered(|ui| {
ui.heading("Advanced String Dumper");
});
});
egui::SidePanel::left("side_panel")
.resizable(true)
.default_width(200.0)
.show_inside(ui, |ui| {
ui.heading("File Information");
ui.separator();
if let Some(path) = &self.file_path {
ui.label("Loaded File:");
ui.monospace(format!("{}", path.display()));
ui.separator();
// Here, you can add more file information like size, modification date, etc.
} else {
ui.label("No file loaded.");
}
});
egui::TopBottomPanel::bottom("search_panel").show_inside(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Search:");
if ui
.add(
egui::TextEdit::singleline(&mut self.search_text)
.hint_text("Enter search term")
.desired_width(ui.available_width() - 100.0),
)
.changed()
{
self.filter_strings();
}
if ui.button("Clear").clicked() {
self.search_text.clear();
self.filter_strings();
}
});
});
egui::CentralPanel::default().show_inside(ui, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
for s in &self.filtered_strings {
ui.monospace(s);
}
});
});
});
}
2. Implement a Dark Theme:
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
// Set dark theme
ctx.set_visuals(egui::Visuals::dark());
self.ui_menu(ctx, frame);
if self.settings_open {
self.open_settings(ctx);
}
if self.about_open {
self.show_about(ctx);
}
self.ui_central_panel(ctx, frame);
}
3. Add a Hex View Tab (Example):
You can add a tabbed interface and include a hex view of the loaded file.
fn ui_central_panel(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::TopBottomPanel::top("header_panel").show_inside(ui, |ui| {
ui.vertical_centered(|ui| {
ui.heading("Advanced String Dumper");
});
});
// Tabs
egui::TopBottomPanel::top("tabs_panel").show_inside(ui, |ui| {
ui.horizontal(|ui| {
if ui.selectable_label(true, "Strings View").clicked() {
// Set active tab
}
if ui.selectable_label(false, "Hex View").clicked() {
// Set active tab
}
});
});
// Based on the active tab, display different content
egui::CentralPanel::default().show_inside(ui, |ui| {
// Display strings or hex view based on the active tab
egui::ScrollArea::vertical().show(ui, |ui| {
// ...
});
});
});
}
Complete Modified Code:
Below is the complete modified code with the fixes and enhancements:
use eframe::egui;
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::PathBuf;
struct Settings {
min_length: usize,
include_ascii: bool,
include_utf16le: bool,
include_utf16be: bool,
}
impl Default for Settings {
fn default() -> Self {
Settings {
min_length: 4,
include_ascii: true,
include_utf16le: true,
include_utf16be: true,
}
}
}
struct StringDumperApp {
// Data related to the application
strings: Vec<String>,
file_path: Option<PathBuf>,
search_text: String,
filtered_strings: Vec<String>,
// Settings
current_settings: Settings,
temp_settings: Option<Settings>,
settings_open: bool,
about_open: bool,
}
impl Default for StringDumperApp {
fn default() -> Self {
Self {
strings: Vec::new(),
file_path: None,
search_text: String::new(),
filtered_strings: Vec::new(),
current_settings: Settings::default(),
temp_settings: None,
settings_open: false,
about_open: false,
}
}
}
impl eframe::App for StringDumperApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
// Set dark theme
ctx.set_visuals(egui::Visuals::dark());
self.ui_menu(ctx, frame);
if self.settings_open {
self.open_settings(ctx);
}
if self.about_open {
self.show_about(ctx);
}
self.ui_central_panel(ctx, frame);
}
}
impl StringDumperApp {
fn ui_menu(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
use egui::menu;
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
ui.horizontal_wrapped(|ui| {
ui.visuals_mut().button_frame = false;
menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Open File").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() {
self.file_path = Some(path);
self.load_strings();
}
ui.close_menu();
}
if ui.button("Save Strings").clicked() {
if let Some(path) = rfd::FileDialog::new().save_file() {
self.save_strings(&path);
}
ui.close_menu();
}
if ui.button("Exit").clicked() {
std::process::exit(0);
}
});
ui.menu_button("Edit", |ui| {
if ui.button("Settings").clicked() {
self.settings_open = true;
self.temp_settings = Some(self.current_settings.clone());
ui.close_menu();
}
});
ui.menu_button("Help", |ui| {
if ui.button("About").clicked() {
self.about_open = true;
ui.close_menu();
}
});
});
});
});
}
fn ui_central_panel(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::TopBottomPanel::top("header_panel").show_inside(ui, |ui| {
ui.vertical_centered(|ui| {
ui.heading("Advanced String Dumper");
});
});
egui::SidePanel::left("side_panel")
.resizable(true)
.default_width(200.0)
.show_inside(ui, |ui| {
ui.heading("File Information");
ui.separator();
if let Some(path) = &self.file_path {
ui.label("Loaded File:");
ui.monospace(format!("{}", path.display()));
ui.separator();
// Here, you can add more file information like size, modification date, etc.
} else {
ui.label("No file loaded.");
}
});
egui::TopBottomPanel::bottom("search_panel").show_inside(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Search:");
if ui
.add(
egui::TextEdit::singleline(&mut self.search_text)
.hint_text("Enter search term")
.desired_width(ui.available_width() - 100.0),
)
.changed()
{
self.filter_strings();
}
if ui.button("Clear").clicked() {
self.search_text.clear();
self.filter_strings();
}
});
});
egui::CentralPanel::default().show_inside(ui, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
for s in &self.filtered_strings {
ui.monospace(s);
}
});
});
});
}
fn load_strings(&mut self) {
if let Some(path) = &self.file_path {
match extract_strings_from_file(
path,
self.current_settings.min_length,
self.current_settings.include_ascii,
self.current_settings.include_utf16le,
self.current_settings.include_utf16be,
) {
Ok(strings) => {
self.strings = strings;
self.filter_strings();
}
Err(e) => {
eprintln!("Error extracting strings: {}", e);
}
}
}
}
fn filter_strings(&mut self) {
if self.search_text.is_empty() {
self.filtered_strings = self.strings.clone();
} else {
let search_lower = self.search_text.to_lowercase();
self.filtered_strings = self
.strings
.iter()
.filter(|s| s.to_lowercase().contains(&search_lower))
.cloned()
.collect();
}
}
fn save_strings(&self, path: &std::path::Path) {
if let Ok(mut file) = File::create(path) {
for s in &self.filtered_strings {
if let Err(e) = writeln!(file, "{}", s) {
eprintln!("Error writing to file: {}", e);
break;
}
}
} else {
eprintln!("Error creating file");
}
}
fn open_settings(&mut self, ctx: &egui::Context) {
// Initialize temp_settings if None
if self.temp_settings.is_none() {
self.temp_settings = Some(self.current_settings.clone());
}
egui::Window::new("Settings")
.open(&mut self.settings_open)
.show(ctx, |ui| {
let temp_settings = self.temp_settings.as_mut().unwrap();
ui.label("Minimum String Length:");
ui.add(egui::Slider::new(&mut temp_settings.min_length, 1..=20));
ui.separator();
ui.label("String Encodings to Include:");
ui.checkbox(&mut temp_settings.include_ascii, "ASCII");
ui.checkbox(&mut temp_settings.include_utf16le, "UTF-16 Little Endian");
ui.checkbox(&mut temp_settings.include_utf16be, "UTF-16 Big Endian");
ui.separator();
ui.horizontal(|ui| {
if ui.button("Apply").clicked() {
self.current_settings = temp_settings.clone();
self.temp_settings = None; // Reset temp settings
self.load_strings();
self.settings_open = false; // Close settings window
}
if ui.button("Cancel").clicked() {
self.temp_settings = None; // Discard changes
self.settings_open = false; // Close settings window
}
});
});
}
fn show_about(&mut self, ctx: &egui::Context) {
egui::Window::new("About")
.open(&mut self.about_open)
.show(ctx, |ui| {
ui.label("Advanced String Dumper");
ui.label("Version 1.0");
ui.add_space(10.0);
ui.label("Developed using Rust and egui.");
ui.label("This application extracts strings from binary files using advanced methods.");
});
}
}
fn extract_strings_from_file(
path: &std::path::Path,
min_length: usize,
include_ascii: bool,
include_utf16le: bool,
include_utf16be: bool,
) -> io::Result<Vec<String>> {
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let strings = extract_strings(
&buffer,
min_length,
include_ascii,
include_utf16le,
include_utf16be,
);
Ok(strings)
}
fn extract_strings(
data: &[u8],
min_length: usize,
include_ascii: bool,
include_utf16le: bool,
include_utf16be: bool,
) -> Vec<String> {
let mut strings = Vec::new();
if include_ascii {
strings.extend(extract_ascii_strings(data, min_length));
}
if include_utf16le {
strings.extend(extract_unicode_strings(data, min_length, true));
}
if include_utf16be {
strings.extend(extract_unicode_strings(data, min_length, false));
}
strings
}
fn extract_ascii_strings(data: &[u8], min_length: usize) -> Vec<String> {
let mut strings = Vec::new();
let mut current_string = Vec::new();
for &byte in data {
if byte.is_ascii_graphic() || byte == b' ' {
current_string.push(byte);
} else {
if current_string.len() >= min_length {
if let Ok(s) = String::from_utf8(current_string.clone()) {
strings.push(s);
}
}
current_string.clear();
}
}
// Check if there's a string at the end
if current_string.len() >= min_length {
if let Ok(s) = String::from_utf8(current_string.clone()) {
strings.push(s);
}
}
strings
}
fn extract_unicode_strings(data: &[u8], min_length: usize, little_endian: bool) -> Vec<String> {
let mut strings = Vec::new();
let mut current_string = Vec::new();
let mut i = 0;
while i + 1 < data.len() {
let b1 = data[i];
let b2 = data[i + 1];
let code_unit = if little_endian {
u16::from_le_bytes([b1, b2])
} else {
u16::from_be_bytes([b1, b2])
};
if code_unit == 0 {
if current_string.len() >= min_length {
let s = String::from_utf16_lossy(¤t_string);
strings.push(s);
}
current_string.clear();
} else {
current_string.push(code_unit);
}
i += 2;
}
// Check if there's a string at the end
if current_string.len() >= min_length {
let s = String::from_utf16_lossy(¤t_string);
strings.push(s);
}
strings
}
fn main() {
let app = StringDumperApp::default();
let native_options = eframe::NativeOptions::default();
eframe::run_native("String Dumper", native_options, Box::new(|_cc| Ok(Box::new(app)))).unwrap();
}
Conclusion:
By persisting the temporary settings between frames and updating the UI, the issue with the settings not applying has been fixed. Additionally, the UI has been enhanced to look more advanced and technical, providing a better user experience.