Skip to content

Commit

Permalink
Merge pull request #53 from encounter/less-state
Browse files Browse the repository at this point in the history
Less HexView state, update egui to 0.27
  • Loading branch information
ethteck authored Sep 3, 2024
2 parents 0c77f23 + 30d62dc commit 5690789
Show file tree
Hide file tree
Showing 9 changed files with 991 additions and 778 deletions.
1,256 changes: 731 additions & 525 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ anyhow = "1.0.81"
argh = "0.1.12"
dirs = "5.0.1"
dtoa = "1.0.9"
eframe = { version = "0.26.2", features = ["persistence"] }
eframe = { version = "0.27.2", features = ["persistence"] }
egui-modal = "0.3.5"
egui-phosphor = "0.4.0"
egui-phosphor = "0.5.0"
encoding_rs = "0.8.33"
bdiff_hex_view = { version = "0.8.5", path="../hex_view" }
iset = "0.2.2"
Expand Down
73 changes: 47 additions & 26 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl BdiffApp {
};

for file in config.files.iter() {
match ret.open_file(&file.path, &cc.egui_ctx) {
match ret.open_file(&file.path) {
Ok(fv) => {
if let Some(map) = file.map.as_ref() {
fv.mt.load_file(map);
Expand All @@ -122,12 +122,12 @@ impl BdiffApp {
ret
}

pub fn open_file(&mut self, path: &Path, ctx: &Context) -> Result<&mut FileView, Error> {
pub fn open_file(&mut self, path: &Path) -> Result<&mut FileView, Error> {
let file = BinFile::from_path(path)?;
self.config.files.push(path.into());
self.config.changed = true;

let fv = FileView::new(ctx, file, self.next_hv_id);
let fv = FileView::new(file, self.next_hv_id);
self.file_views.push(fv);
self.next_hv_id += 1;

Expand Down Expand Up @@ -191,56 +191,71 @@ impl BdiffApp {
for fv in self.file_views.iter_mut() {
// Keys
if ctx.input(|i| i.key_pressed(egui::Key::Home)) {
fv.hv.set_cur_pos(0);
fv.hv.set_cur_pos(&fv.file.data, 0);
}
if ctx.input(|i| i.key_pressed(egui::Key::End))
&& fv.file.data.len() >= fv.hv.bytes_per_screen()
&& fv.file.data.len() >= fv.hv.bytes_per_screen(&fv.file.data)
{
fv.hv
.set_cur_pos(fv.file.data.len() - fv.hv.bytes_per_screen())
fv.hv.set_cur_pos(
&fv.file.data,
fv.file.data.len() - fv.hv.bytes_per_screen(&fv.file.data),
)
}
if ctx.input(|i| i.key_pressed(egui::Key::PageUp)) {
fv.hv.adjust_cur_pos(-(fv.hv.bytes_per_screen() as isize))
fv.hv.adjust_cur_pos(
&fv.file.data,
-(fv.hv.bytes_per_screen(&fv.file.data) as isize),
)
}
if ctx.input(|i| i.key_pressed(egui::Key::PageDown)) {
fv.hv.adjust_cur_pos(fv.hv.bytes_per_screen() as isize)
fv.hv.adjust_cur_pos(
&fv.file.data,
fv.hv.bytes_per_screen(&fv.file.data) as isize,
)
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowLeft)) {
fv.hv.adjust_cur_pos(-1)
fv.hv.adjust_cur_pos(&fv.file.data, -1)
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowRight)) {
fv.hv.adjust_cur_pos(1)
fv.hv.adjust_cur_pos(&fv.file.data, 1)
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowUp)) {
fv.hv.adjust_cur_pos(-(fv.bytes_per_row as isize))
fv.hv
.adjust_cur_pos(&fv.file.data, -(fv.bytes_per_row as isize))
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowDown)) {
fv.hv.adjust_cur_pos(fv.bytes_per_row as isize)
fv.hv
.adjust_cur_pos(&fv.file.data, fv.bytes_per_row as isize)
}
if ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
let last_byte = fv.cur_pos + fv.hv.bytes_per_screen();
let last_byte = fv.cur_pos + fv.hv.bytes_per_screen(&fv.file.data);

if self.diff_state.enabled {
if last_byte < fv.file.data.len() {
match self.diff_state.get_next_diff(last_byte) {
Some(next_diff) => {
// Move to the next diff
let new_pos = next_diff - (next_diff % fv.bytes_per_row);
fv.hv.set_cur_pos(new_pos);
fv.hv.set_cur_pos(&fv.file.data, new_pos);
}
None => {
// Move to the end of the file
if fv.file.data.len() >= fv.hv.bytes_per_screen() {
if fv.file.data.len() >= fv.hv.bytes_per_screen(&fv.file.data) {
fv.hv.set_cur_pos(
fv.file.data.len() - fv.hv.bytes_per_screen(),
&fv.file.data,
fv.file.data.len()
- fv.hv.bytes_per_screen(&fv.file.data),
);
}
}
}
}
} else {
// Move one screen down
fv.hv.adjust_cur_pos(fv.hv.bytes_per_screen() as isize)
fv.hv.adjust_cur_pos(
&fv.file.data,
fv.hv.bytes_per_screen(&fv.file.data) as isize,
)
}
}

Expand All @@ -264,8 +279,10 @@ impl BdiffApp {
self.scroll_overflow -= (scroll_amt * scroll_threshold) as f32;
}
}
fv.hv
.adjust_cur_pos(-scroll_amt * lines_per_scroll * fv.bytes_per_row as isize)
fv.hv.adjust_cur_pos(
&fv.file.data,
-scroll_amt * lines_per_scroll * fv.bytes_per_row as isize,
)
}
}

Expand Down Expand Up @@ -458,12 +475,16 @@ impl eframe::App for BdiffApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let mut style: egui::Style = (*ctx.style()).clone();
style.visuals.popup_shadow = Shadow {
extrusion: 0.0,
offset: Default::default(),
blur: 0.0,
color: egui::Color32::TRANSPARENT,
spread: 0.0,
};
style.visuals.window_shadow = Shadow {
extrusion: 0.0,
offset: Default::default(),
blur: 0.0,
color: egui::Color32::TRANSPARENT,
spread: 0.0,
};
style.visuals.menu_rounding = Rounding::default();
style.visuals.window_rounding = Rounding::default();
Expand Down Expand Up @@ -514,7 +535,7 @@ impl eframe::App for BdiffApp {
// Open dropped files
if ctx.input(|i| !i.raw.dropped_files.is_empty()) {
for file in ctx.input(|i| i.raw.dropped_files.clone()) {
let _ = self.open_file(&file.path.unwrap(), ctx);
let _ = self.open_file(&file.path.unwrap());
}
}

Expand All @@ -524,7 +545,7 @@ impl eframe::App for BdiffApp {

for fv in self.file_views.iter() {
if self.last_selected_hv.is_some() && fv.id == self.last_selected_hv.unwrap() {
let selected_bytes = fv.hv.get_selected_bytes();
let selected_bytes = fv.hv.get_selected_bytes(&fv.file.data);

let selected_bytes: String = match fv.selection.side {
HexViewSelectionSide::Hex => selected_bytes
Expand Down Expand Up @@ -553,7 +574,7 @@ impl eframe::App for BdiffApp {
ui.menu_button("File", |ui| {
if ui.button("Open").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() {
let _ = self.open_file(&path, ctx);
let _ = self.open_file(&path);
}

ui.close_menu();
Expand Down Expand Up @@ -770,7 +791,7 @@ impl BdiffApp {
match pos {
Some(pos) => {
for fv in self.file_views.iter_mut() {
fv.hv.set_cur_pos(pos);
fv.hv.set_cur_pos(&fv.file.data, pos);
}
goto_modal.close();
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/data_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl DataViewer {
&mut self,
ui: &mut egui::Ui,
hv_id: usize,
selected_bytes: Vec<u8>,
selected_bytes: &[u8],
endianness: Endianness,
) {
if !self.show {
Expand Down Expand Up @@ -150,7 +150,7 @@ impl DataViewer {
fn display_data_types(
&mut self,
ui: &mut egui::Ui,
selected_bytes: Vec<u8>,
selected_bytes: &[u8],
endianness: Endianness,
) {
let mut float_buffer = dtoa::Buffer::new();
Expand Down
29 changes: 16 additions & 13 deletions app/src/file_view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Error;
use bdiff_hex_view::{CursorState, HexView, HexViewSelection, HexViewSelectionState};
use eframe::egui::FontId;
use eframe::{
egui::{self, Context, Id},
egui::{self, Id},
epaint::Color32,
};

Expand Down Expand Up @@ -35,12 +36,11 @@ pub struct FileView {
}

impl FileView {
pub fn new(ctx: &Context, file: BinFile, id: usize) -> Self {
pub fn new(file: BinFile, id: usize) -> Self {
let min_rows = 10;
let max_rows = 25;
let default_bytes_per_row = 0x10;
let num_rows = (file.data.len() / default_bytes_per_row).clamp(min_rows, max_rows) as u32;
let file_bytes = file.data.clone();

Self {
id,
Expand All @@ -53,7 +53,7 @@ impl FileView {
cursor_pos: None,
show_selection_info: true,
show_cursor_info: true,
hv: HexView::new(&ctx, &file_bytes, id),
hv: HexView::new(id),
sv: StringViewer::default(),
dv: DataViewer::default(),
mt: MapTool::default(),
Expand Down Expand Up @@ -165,13 +165,16 @@ impl FileView {
egui::Layout::left_to_right(eframe::emath::Align::Min),
|ui: &mut egui::Ui| {
ui.vertical(|ui| {
self.hv.show(
ui,
cursor_state,
can_selection_change,
font_size,
settings.byte_grouping.into(),
);
ui.group(|ui| {
self.hv.show(
ui,
&self.file.data,
cursor_state,
can_selection_change,
FontId::monospace(font_size),
settings.byte_grouping.into(),
);
});

if self.show_selection_info {
let selection_text = match self.selection.state {
Expand Down Expand Up @@ -246,13 +249,13 @@ impl FileView {
self.dv.display(
ui,
self.id,
self.hv.get_selected_bytes(),
self.hv.get_selected_bytes(&self.file.data),
self.file.endianness,
);
self.sv.display(
ui,
self.id,
self.hv.get_selected_bytes(),
self.hv.get_selected_bytes(&self.file.data),
self.file.endianness,
);
self.mt.display(ui);
Expand Down
2 changes: 1 addition & 1 deletion app/src/map_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn collect_data(path: PathBuf) -> IntervalMap<usize, MapFileEntry> {

let mut mf: mapfile_parser::MapFile = mapfile_parser::MapFile::new();

mf.read_map_file(path);
mf.read_map_file(&path);

for segment in &mf.segments_list {
for file in &segment.files_list {
Expand Down
4 changes: 2 additions & 2 deletions app/src/string_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl StringViewer {
&mut self,
ui: &mut egui::Ui,
hv_id: usize,
selected_bytes: Vec<u8>,
selected_bytes: &[u8],
endianness: Endianness,
) {
if !self.show {
Expand Down Expand Up @@ -59,7 +59,7 @@ impl StringViewer {
if self.utf8 {
ui.add(egui::Label::new(egui::RichText::new("UTF-8").monospace()));
ui.text_edit_singleline(
&mut String::from_utf8(selected_bytes.clone()).unwrap_or_default(),
&mut String::from_utf8_lossy(selected_bytes).into_owned(),
);
ui.end_row();
}
Expand Down
2 changes: 1 addition & 1 deletion hex_view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ readme = "README.md"
publish = false

[dependencies]
egui = { version = "0.26.2", features = ["persistence"] }
egui = { version = "0.27.2", features = ["persistence"] }
serde = "1.0"
serde_json = "1.0"
Loading

0 comments on commit 5690789

Please sign in to comment.