Skip to content

Commit 91d11c8

Browse files
committed
Refactor state & config structs, various cleanup
1 parent 9492404 commit 91d11c8

File tree

13 files changed

+727
-867
lines changed

13 files changed

+727
-867
lines changed

src/app.rs

Lines changed: 81 additions & 313 deletions
Large diffs are not rendered by default.

src/diff.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{collections::BTreeMap, mem::take};
33
use anyhow::Result;
44

55
use crate::{
6-
app::DiffConfig,
76
editops::{editops_find, LevEditType},
87
obj::{
98
mips, ppc, ObjArchitecture, ObjDataDiff, ObjDataDiffKind, ObjInfo, ObjInsArg,
@@ -373,7 +372,7 @@ fn find_section_and_symbol(obj: &ObjInfo, name: &str) -> Option<(usize, usize)>
373372
None
374373
}
375374

376-
pub fn diff_objs(left: &mut ObjInfo, right: &mut ObjInfo, _diff_config: &DiffConfig) -> Result<()> {
375+
pub fn diff_objs(left: &mut ObjInfo, right: &mut ObjInfo) -> Result<()> {
377376
for left_section in &mut left.sections {
378377
if left_section.kind == ObjSectionKind::Code {
379378
for left_symbol in &mut left_section.symbols {

src/jobs/bindiff.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/jobs/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@ use std::{
99

1010
use anyhow::Result;
1111

12-
use crate::jobs::{
13-
bindiff::BinDiffResult, check_update::CheckUpdateResult, objdiff::ObjDiffResult,
14-
update::UpdateResult,
15-
};
12+
use crate::jobs::{check_update::CheckUpdateResult, objdiff::ObjDiffResult, update::UpdateResult};
1613

17-
pub mod bindiff;
1814
pub mod check_update;
1915
pub mod objdiff;
2016
pub mod update;
2117

2218
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
2319
pub enum Job {
2420
ObjDiff,
25-
BinDiff,
2621
CheckUpdate,
2722
Update,
2823
}
@@ -105,7 +100,6 @@ pub struct JobStatus {
105100
pub enum JobResult {
106101
None,
107102
ObjDiff(Box<ObjDiffResult>),
108-
BinDiff(Box<BinDiffResult>),
109103
CheckUpdate(Box<CheckUpdateResult>),
110104
Update(Box<UpdateResult>),
111105
}

src/jobs/objdiff.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use anyhow::{Context, Error, Result};
99
use time::OffsetDateTime;
1010

1111
use crate::{
12-
app::{AppConfig, DiffConfig},
12+
app::AppConfig,
1313
diff::diff_objs,
1414
jobs::{start_job, update_status, Job, JobResult, JobState, Status},
1515
obj::{elf, ObjInfo},
@@ -79,7 +79,6 @@ fn run_build(
7979
status: &Status,
8080
cancel: Receiver<()>,
8181
config: Arc<RwLock<AppConfig>>,
82-
diff_config: DiffConfig,
8382
) -> Result<Box<ObjDiffResult>> {
8483
let config = config.read().map_err(|_| Error::msg("Failed to lock app config"))?.clone();
8584
let obj_path = config.obj_path.as_ref().ok_or_else(|| Error::msg("Missing obj path"))?;
@@ -129,15 +128,15 @@ fn run_build(
129128

130129
if let (Some(first_obj), Some(second_obj)) = (&mut first_obj, &mut second_obj) {
131130
update_status(status, "Performing diff".to_string(), 4, total, &cancel)?;
132-
diff_objs(first_obj, second_obj, &diff_config)?;
131+
diff_objs(first_obj, second_obj)?;
133132
}
134133

135134
update_status(status, "Complete".to_string(), total, total, &cancel)?;
136135
Ok(Box::new(ObjDiffResult { first_status, second_status, first_obj, second_obj, time }))
137136
}
138137

139-
pub fn start_build(config: Arc<RwLock<AppConfig>>, diff_config: DiffConfig) -> JobState {
138+
pub fn start_build(config: Arc<RwLock<AppConfig>>) -> JobState {
140139
start_job("Object diff", Job::ObjDiff, move |status, cancel| {
141-
run_build(status, cancel, config, diff_config).map(JobResult::ObjDiff)
140+
run_build(status, cancel, config).map(JobResult::ObjDiff)
142141
})
143142
}

src/views/appearance.rs

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,95 @@
1-
use egui::Color32;
1+
use egui::{Color32, FontFamily, FontId, TextStyle};
2+
use time::UtcOffset;
23

3-
use crate::app::ViewState;
4+
#[derive(serde::Deserialize, serde::Serialize)]
5+
#[serde(default)]
6+
pub struct Appearance {
7+
pub ui_font: FontId,
8+
pub code_font: FontId,
9+
pub diff_colors: Vec<Color32>,
10+
pub reverse_fn_order: bool,
11+
pub theme: eframe::Theme,
12+
13+
// Applied by theme
14+
#[serde(skip)]
15+
pub text_color: Color32, // GRAY
16+
#[serde(skip)]
17+
pub emphasized_text_color: Color32, // LIGHT_GRAY
18+
#[serde(skip)]
19+
pub deemphasized_text_color: Color32, // DARK_GRAY
20+
#[serde(skip)]
21+
pub highlight_color: Color32, // WHITE
22+
#[serde(skip)]
23+
pub replace_color: Color32, // LIGHT_BLUE
24+
#[serde(skip)]
25+
pub insert_color: Color32, // GREEN
26+
#[serde(skip)]
27+
pub delete_color: Color32, // RED
28+
29+
// Global
30+
#[serde(skip)]
31+
pub utc_offset: UtcOffset,
32+
}
33+
34+
impl Default for Appearance {
35+
fn default() -> Self {
36+
Self {
37+
ui_font: FontId { size: 12.0, family: FontFamily::Proportional },
38+
code_font: FontId { size: 14.0, family: FontFamily::Monospace },
39+
diff_colors: DEFAULT_COLOR_ROTATION.to_vec(),
40+
reverse_fn_order: false,
41+
theme: eframe::Theme::Dark,
42+
text_color: Color32::GRAY,
43+
emphasized_text_color: Color32::LIGHT_GRAY,
44+
deemphasized_text_color: Color32::DARK_GRAY,
45+
highlight_color: Color32::WHITE,
46+
replace_color: Color32::LIGHT_BLUE,
47+
insert_color: Color32::GREEN,
48+
delete_color: Color32::from_rgb(200, 40, 41),
49+
utc_offset: UtcOffset::UTC,
50+
}
51+
}
52+
}
53+
54+
impl Appearance {
55+
pub fn apply(&mut self, style: &egui::Style) -> egui::Style {
56+
let mut style = style.clone();
57+
style.text_styles.insert(TextStyle::Body, FontId {
58+
size: (self.ui_font.size * 0.75).floor(),
59+
family: self.ui_font.family.clone(),
60+
});
61+
style.text_styles.insert(TextStyle::Body, self.ui_font.clone());
62+
style.text_styles.insert(TextStyle::Button, self.ui_font.clone());
63+
style.text_styles.insert(TextStyle::Heading, FontId {
64+
size: (self.ui_font.size * 1.5).floor(),
65+
family: self.ui_font.family.clone(),
66+
});
67+
style.text_styles.insert(TextStyle::Monospace, self.code_font.clone());
68+
match self.theme {
69+
eframe::Theme::Dark => {
70+
style.visuals = egui::Visuals::dark();
71+
self.text_color = Color32::GRAY;
72+
self.emphasized_text_color = Color32::LIGHT_GRAY;
73+
self.deemphasized_text_color = Color32::DARK_GRAY;
74+
self.highlight_color = Color32::WHITE;
75+
self.replace_color = Color32::LIGHT_BLUE;
76+
self.insert_color = Color32::GREEN;
77+
self.delete_color = Color32::from_rgb(200, 40, 41);
78+
}
79+
eframe::Theme::Light => {
80+
style.visuals = egui::Visuals::light();
81+
self.text_color = Color32::GRAY;
82+
self.emphasized_text_color = Color32::DARK_GRAY;
83+
self.deemphasized_text_color = Color32::LIGHT_GRAY;
84+
self.highlight_color = Color32::BLACK;
85+
self.replace_color = Color32::DARK_BLUE;
86+
self.insert_color = Color32::DARK_GREEN;
87+
self.delete_color = Color32::from_rgb(200, 40, 41);
88+
}
89+
}
90+
style
91+
}
92+
}
493

594
pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [
695
Color32::from_rgb(255, 0, 255),
@@ -14,31 +103,27 @@ pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [
14103
Color32::from_rgb(213, 138, 138),
15104
];
16105

17-
pub fn appearance_window(ctx: &egui::Context, view_state: &mut ViewState) {
18-
egui::Window::new("Appearance").open(&mut view_state.show_view_config).show(ctx, |ui| {
106+
pub fn appearance_window(ctx: &egui::Context, show: &mut bool, appearance: &mut Appearance) {
107+
egui::Window::new("Appearance").open(show).show(ctx, |ui| {
19108
egui::ComboBox::from_label("Theme")
20-
.selected_text(format!("{:?}", view_state.view_config.theme))
109+
.selected_text(format!("{:?}", appearance.theme))
21110
.show_ui(ui, |ui| {
22-
ui.selectable_value(&mut view_state.view_config.theme, eframe::Theme::Dark, "Dark");
23-
ui.selectable_value(
24-
&mut view_state.view_config.theme,
25-
eframe::Theme::Light,
26-
"Light",
27-
);
111+
ui.selectable_value(&mut appearance.theme, eframe::Theme::Dark, "Dark");
112+
ui.selectable_value(&mut appearance.theme, eframe::Theme::Light, "Light");
28113
});
29114
ui.label("UI font:");
30-
egui::introspection::font_id_ui(ui, &mut view_state.view_config.ui_font);
115+
egui::introspection::font_id_ui(ui, &mut appearance.ui_font);
31116
ui.separator();
32117
ui.label("Code font:");
33-
egui::introspection::font_id_ui(ui, &mut view_state.view_config.code_font);
118+
egui::introspection::font_id_ui(ui, &mut appearance.code_font);
34119
ui.separator();
35120
ui.label("Diff colors:");
36121
if ui.button("Reset").clicked() {
37-
view_state.view_config.diff_colors = DEFAULT_COLOR_ROTATION.to_vec();
122+
appearance.diff_colors = DEFAULT_COLOR_ROTATION.to_vec();
38123
}
39124
let mut remove_at: Option<usize> = None;
40-
let num_colors = view_state.view_config.diff_colors.len();
41-
for (idx, color) in view_state.view_config.diff_colors.iter_mut().enumerate() {
125+
let num_colors = appearance.diff_colors.len();
126+
for (idx, color) in appearance.diff_colors.iter_mut().enumerate() {
42127
ui.horizontal(|ui| {
43128
ui.color_edit_button_srgba(color);
44129
if num_colors > 1 && ui.small_button("-").clicked() {
@@ -47,10 +132,10 @@ pub fn appearance_window(ctx: &egui::Context, view_state: &mut ViewState) {
47132
});
48133
}
49134
if let Some(idx) = remove_at {
50-
view_state.view_config.diff_colors.remove(idx);
135+
appearance.diff_colors.remove(idx);
51136
}
52137
if ui.small_button("+").clicked() {
53-
view_state.view_config.diff_colors.push(Color32::BLACK);
138+
appearance.diff_colors.push(Color32::BLACK);
54139
}
55140
});
56141
}

0 commit comments

Comments
 (0)