Refactor state & config structs, various cleanup

This commit is contained in:
Luke Street 2023-08-09 21:53:04 -04:00
parent 94924047b7
commit 91d11c83d6
13 changed files with 736 additions and 876 deletions

View File

@ -9,258 +9,73 @@ use std::{
time::Duration, time::Duration,
}; };
use egui::{Color32, FontFamily, FontId, TextStyle};
use globset::{Glob, GlobSet, GlobSetBuilder}; use globset::{Glob, GlobSet, GlobSetBuilder};
use notify::{RecursiveMode, Watcher}; use notify::{RecursiveMode, Watcher};
use time::{OffsetDateTime, UtcOffset}; use time::UtcOffset;
use crate::{ use crate::{
config::{build_globset, load_project_config, ProjectUnit, ProjectUnitNode, CONFIG_FILENAMES}, config::{build_globset, load_project_config, ProjectUnit, ProjectUnitNode, CONFIG_FILENAMES},
jobs::{ jobs::{
check_update::{start_check_update, CheckUpdateResult}, check_update::start_check_update, objdiff::start_build, Job, JobQueue, JobResult, JobStatus,
objdiff::{start_build, BuildStatus, ObjDiffResult},
Job, JobQueue, JobResult, JobStatus,
}, },
views::{ views::{
appearance::{appearance_window, DEFAULT_COLOR_ROTATION}, appearance::{appearance_window, Appearance},
config::{config_ui, project_window}, config::{config_ui, project_window, ConfigViewState},
data_diff::data_diff_ui, data_diff::data_diff_ui,
demangle::demangle_window, demangle::{demangle_window, DemangleViewState},
function_diff::function_diff_ui, function_diff::function_diff_ui,
jobs::jobs_ui, jobs::jobs_ui,
symbol_diff::symbol_diff_ui, symbol_diff::{symbol_diff_ui, DiffViewState, View},
}, },
}; };
#[allow(clippy::enum_variant_names)] #[derive(Default)]
#[derive(Default, Eq, PartialEq)]
pub enum View {
#[default]
SymbolDiff,
FunctionDiff,
DataDiff,
}
#[derive(Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum DiffKind {
#[default]
SplitObj,
WholeBinary,
}
#[derive(Default, Clone)]
pub struct DiffConfig {
// TODO
// pub stripped_symbols: Vec<String>,
// pub mapped_symbols: HashMap<String, String>,
}
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct ViewConfig {
pub ui_font: FontId,
pub code_font: FontId,
pub diff_colors: Vec<Color32>,
pub reverse_fn_order: bool,
pub theme: eframe::Theme,
#[serde(skip)]
pub text_color: Color32, // GRAY
#[serde(skip)]
pub emphasized_text_color: Color32, // LIGHT_GRAY
#[serde(skip)]
pub deemphasized_text_color: Color32, // DARK_GRAY
#[serde(skip)]
pub highlight_color: Color32, // WHITE
#[serde(skip)]
pub replace_color: Color32, // LIGHT_BLUE
#[serde(skip)]
pub insert_color: Color32, // GREEN
#[serde(skip)]
pub delete_color: Color32, // RED
}
impl Default for ViewConfig {
fn default() -> Self {
Self {
ui_font: FontId { size: 12.0, family: FontFamily::Proportional },
code_font: FontId { size: 14.0, family: FontFamily::Monospace },
diff_colors: DEFAULT_COLOR_ROTATION.to_vec(),
reverse_fn_order: false,
theme: eframe::Theme::Dark,
text_color: Color32::GRAY,
emphasized_text_color: Color32::LIGHT_GRAY,
deemphasized_text_color: Color32::DARK_GRAY,
highlight_color: Color32::WHITE,
replace_color: Color32::LIGHT_BLUE,
insert_color: Color32::GREEN,
delete_color: Color32::from_rgb(200, 40, 41),
}
}
}
pub struct SymbolReference {
pub symbol_name: String,
pub section_name: String,
}
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct ViewState { pub struct ViewState {
#[serde(skip)]
pub jobs: JobQueue, pub jobs: JobQueue,
#[serde(skip)] pub show_appearance_config: bool,
pub build: Option<Box<ObjDiffResult>>, pub demangle_state: DemangleViewState,
#[serde(skip)]
pub highlighted_symbol: Option<String>,
#[serde(skip)]
pub selected_symbol: Option<SymbolReference>,
#[serde(skip)]
pub current_view: View,
#[serde(skip)]
pub show_view_config: bool,
#[serde(skip)]
pub show_project_config: bool,
#[serde(skip)]
pub show_demangle: bool, pub show_demangle: bool,
#[serde(skip)] pub diff_state: DiffViewState,
pub demangle_text: String, pub config_state: ConfigViewState,
#[serde(skip)] pub show_project_config: bool,
pub watch_pattern_text: String,
#[serde(skip)]
pub diff_config: DiffConfig,
#[serde(skip)]
pub search: String,
#[serde(skip)]
pub utc_offset: UtcOffset,
#[serde(skip)]
pub check_update: Option<Box<CheckUpdateResult>>,
// Config
pub diff_kind: DiffKind,
pub view_config: ViewConfig,
} }
impl Default for ViewState { #[derive(Default, Clone, serde::Deserialize, serde::Serialize)]
fn default() -> Self {
Self {
jobs: Default::default(),
build: None,
highlighted_symbol: None,
selected_symbol: None,
current_view: Default::default(),
show_view_config: false,
show_project_config: false,
show_demangle: false,
demangle_text: String::new(),
watch_pattern_text: String::new(),
diff_config: Default::default(),
search: Default::default(),
utc_offset: UtcOffset::UTC,
check_update: None,
diff_kind: Default::default(),
view_config: Default::default(),
}
}
}
#[derive(Clone, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct AppConfig { pub struct AppConfig {
pub custom_make: Option<String>, pub custom_make: Option<String>,
// WSL2 settings
#[serde(skip)]
pub available_wsl_distros: Option<Vec<String>>,
pub selected_wsl_distro: Option<String>, pub selected_wsl_distro: Option<String>,
// Split obj
pub project_dir: Option<PathBuf>, pub project_dir: Option<PathBuf>,
pub target_obj_dir: Option<PathBuf>, pub target_obj_dir: Option<PathBuf>,
pub base_obj_dir: Option<PathBuf>, pub base_obj_dir: Option<PathBuf>,
pub obj_path: Option<String>, pub obj_path: Option<String>,
pub build_target: bool, pub build_target: bool,
// Whole binary
pub left_obj: Option<PathBuf>,
pub right_obj: Option<PathBuf>,
#[serde(skip)]
pub watcher_change: bool,
pub watcher_enabled: bool, pub watcher_enabled: bool,
#[serde(skip)]
pub queue_update_check: bool,
pub auto_update_check: bool, pub auto_update_check: bool,
// Project config
#[serde(skip)]
pub config_change: bool,
#[serde(skip)]
pub watch_patterns: Vec<Glob>, pub watch_patterns: Vec<Glob>,
#[serde(skip)]
pub load_error: Option<String>,
#[serde(skip)] #[serde(skip)]
pub units: Vec<ProjectUnit>, pub units: Vec<ProjectUnit>,
#[serde(skip)] #[serde(skip)]
pub unit_nodes: Vec<ProjectUnitNode>, pub unit_nodes: Vec<ProjectUnitNode>,
#[serde(skip)] #[serde(skip)]
pub config_window_open: bool, pub watcher_change: bool,
#[serde(skip)]
pub config_change: bool,
} }
impl Default for AppConfig { #[derive(Default)]
fn default() -> Self {
Self {
custom_make: None,
available_wsl_distros: None,
selected_wsl_distro: None,
project_dir: None,
target_obj_dir: None,
base_obj_dir: None,
obj_path: None,
build_target: false,
left_obj: None,
right_obj: None,
config_change: false,
watcher_change: false,
watcher_enabled: true,
queue_update_check: false,
auto_update_check: false,
watch_patterns: vec![],
load_error: None,
units: vec![],
unit_nodes: vec![],
config_window_open: false,
}
}
}
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct App { pub struct App {
appearance: Appearance,
view_state: ViewState, view_state: ViewState,
#[serde(skip)]
config: Arc<RwLock<AppConfig>>, config: Arc<RwLock<AppConfig>>,
#[serde(skip)]
modified: Arc<AtomicBool>, modified: Arc<AtomicBool>,
#[serde(skip)]
config_modified: Arc<AtomicBool>, config_modified: Arc<AtomicBool>,
#[serde(skip)]
watcher: Option<notify::RecommendedWatcher>, watcher: Option<notify::RecommendedWatcher>,
#[serde(skip)]
relaunch_path: Rc<Mutex<Option<PathBuf>>>, relaunch_path: Rc<Mutex<Option<PathBuf>>>,
#[serde(skip)]
should_relaunch: bool, should_relaunch: bool,
} }
impl Default for App { const APPEARANCE_KEY: &str = "appearance";
fn default() -> Self {
Self {
view_state: ViewState::default(),
config: Arc::new(Default::default()),
modified: Arc::new(Default::default()),
config_modified: Arc::new(Default::default()),
watcher: None,
relaunch_path: Default::default(),
should_relaunch: false,
}
}
}
const CONFIG_KEY: &str = "app_config"; const CONFIG_KEY: &str = "app_config";
impl App { impl App {
@ -275,25 +90,24 @@ impl App {
// Load previous app state (if any). // Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work. // Note that you must enable the `persistence` feature for this to work.
let mut app = Self::default();
if let Some(storage) = cc.storage { if let Some(storage) = cc.storage {
let mut app: App = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default(); if let Some(appearance) = eframe::get_value::<Appearance>(storage, APPEARANCE_KEY) {
let mut config: AppConfig = eframe::get_value(storage, CONFIG_KEY).unwrap_or_default(); app.appearance = appearance;
if config.project_dir.is_some() { }
config.config_change = true; if let Some(mut config) = eframe::get_value::<AppConfig>(storage, CONFIG_KEY) {
config.watcher_change = true; if config.project_dir.is_some() {
app.modified.store(true, Ordering::Relaxed); config.config_change = true;
config.watcher_change = true;
app.modified.store(true, Ordering::Relaxed);
}
app.view_state.config_state.queue_update_check = config.auto_update_check;
app.config = Arc::new(RwLock::new(config));
} }
config.queue_update_check = config.auto_update_check;
app.config = Arc::new(RwLock::new(config));
app.view_state.utc_offset = utc_offset;
app.relaunch_path = relaunch_path;
app
} else {
let mut app = Self::default();
app.view_state.utc_offset = utc_offset;
app.relaunch_path = relaunch_path;
app
} }
app.appearance.utc_offset = utc_offset;
app.relaunch_path = relaunch_path;
app
} }
} }
@ -306,52 +120,24 @@ impl eframe::App for App {
return; return;
} }
let Self { config, view_state, .. } = self; let Self { config, appearance, view_state, .. } = self;
ctx.set_style(appearance.apply(ctx.style().as_ref()));
{ let ViewState {
let config = &mut view_state.view_config; jobs,
let mut style = (*ctx.style()).clone(); show_appearance_config,
style.text_styles.insert(TextStyle::Body, FontId { demangle_state,
size: (config.ui_font.size * 0.75).floor(), show_demangle,
family: config.ui_font.family.clone(), diff_state,
}); config_state,
style.text_styles.insert(TextStyle::Body, config.ui_font.clone()); show_project_config,
style.text_styles.insert(TextStyle::Button, config.ui_font.clone()); } = view_state;
style.text_styles.insert(TextStyle::Heading, FontId {
size: (config.ui_font.size * 1.5).floor(),
family: config.ui_font.family.clone(),
});
style.text_styles.insert(TextStyle::Monospace, config.code_font.clone());
match config.theme {
eframe::Theme::Dark => {
style.visuals = egui::Visuals::dark();
config.text_color = Color32::GRAY;
config.emphasized_text_color = Color32::LIGHT_GRAY;
config.deemphasized_text_color = Color32::DARK_GRAY;
config.highlight_color = Color32::WHITE;
config.replace_color = Color32::LIGHT_BLUE;
config.insert_color = Color32::GREEN;
config.delete_color = Color32::from_rgb(200, 40, 41);
}
eframe::Theme::Light => {
style.visuals = egui::Visuals::light();
config.text_color = Color32::GRAY;
config.emphasized_text_color = Color32::DARK_GRAY;
config.deemphasized_text_color = Color32::LIGHT_GRAY;
config.highlight_color = Color32::BLACK;
config.replace_color = Color32::DARK_BLUE;
config.insert_color = Color32::DARK_GREEN;
config.delete_color = Color32::from_rgb(200, 40, 41);
}
}
ctx.set_style(style);
}
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| { egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
egui::menu::bar(ui, |ui| { egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| { ui.menu_button("File", |ui| {
if ui.button("Appearance…").clicked() { if ui.button("Appearance…").clicked() {
view_state.show_view_config = !view_state.show_view_config; *show_appearance_config = !*show_appearance_config;
} }
if ui.button("Quit").clicked() { if ui.button("Quit").clicked() {
frame.close(); frame.close();
@ -359,58 +145,46 @@ impl eframe::App for App {
}); });
ui.menu_button("Tools", |ui| { ui.menu_button("Tools", |ui| {
if ui.button("Demangle…").clicked() { if ui.button("Demangle…").clicked() {
view_state.show_demangle = !view_state.show_demangle; *show_demangle = !*show_demangle;
} }
}); });
}); });
}); });
if view_state.current_view == View::FunctionDiff if diff_state.current_view == View::FunctionDiff
&& matches!(&view_state.build, Some(b) if b.first_status.success && b.second_status.success) && matches!(&diff_state.build, Some(b) if b.first_status.success && b.second_status.success)
{ {
// egui::SidePanel::left("side_panel").show(ctx, |ui| {
// if ui.button("Back").clicked() {
// view_state.current_view = View::SymbolDiff;
// }
// ui.separator();
// jobs_ui(ui, view_state);
// });
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
if function_diff_ui(ui, view_state) { if function_diff_ui(ui, jobs, diff_state, appearance) {
view_state jobs.push(start_build(config.clone()));
.jobs
.push(start_build(config.clone(), view_state.diff_config.clone()));
} }
}); });
} else if view_state.current_view == View::DataDiff } else if diff_state.current_view == View::DataDiff
&& matches!(&view_state.build, Some(b) if b.first_status.success && b.second_status.success) && matches!(&diff_state.build, Some(b) if b.first_status.success && b.second_status.success)
{ {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
if data_diff_ui(ui, view_state) { if data_diff_ui(ui, jobs, diff_state, appearance) {
view_state jobs.push(start_build(config.clone()));
.jobs
.push(start_build(config.clone(), view_state.diff_config.clone()));
} }
}); });
} else { } else {
egui::SidePanel::left("side_panel").show(ctx, |ui| { egui::SidePanel::left("side_panel").show(ctx, |ui| {
config_ui(ui, config, view_state); config_ui(ui, config, jobs, show_project_config, config_state, appearance);
jobs_ui(ui, view_state); jobs_ui(ui, jobs, appearance);
}); });
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
symbol_diff_ui(ui, view_state); symbol_diff_ui(ui, diff_state, appearance);
}); });
} }
project_window(ctx, config, view_state); project_window(ctx, config, show_project_config, config_state, appearance);
appearance_window(ctx, view_state); appearance_window(ctx, show_appearance_config, appearance);
demangle_window(ctx, view_state); demangle_window(ctx, show_demangle, demangle_state, appearance);
// Windows + request_repaint_after breaks dialogs: // Windows + request_repaint_after breaks dialogs:
// https://github.com/emilk/egui/issues/2003 // https://github.com/emilk/egui/issues/2003
if cfg!(windows) || view_state.jobs.any_running() { if cfg!(windows) || jobs.any_running() {
ctx.request_repaint(); ctx.request_repaint();
} else { } else {
ctx.request_repaint_after(Duration::from_millis(100)); ctx.request_repaint_after(Duration::from_millis(100));
@ -422,11 +196,13 @@ impl eframe::App for App {
if let Ok(config) = self.config.read() { if let Ok(config) = self.config.read() {
eframe::set_value(storage, CONFIG_KEY, &*config); eframe::set_value(storage, CONFIG_KEY, &*config);
} }
eframe::set_value(storage, eframe::APP_KEY, self); eframe::set_value(storage, APPEARANCE_KEY, &self.appearance);
} }
fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &eframe::Frame) { fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &eframe::Frame) {
for (job, result) in self.view_state.jobs.iter_finished() { let ViewState { jobs, diff_state, config_state, .. } = &mut self.view_state;
for (job, result) in jobs.iter_finished() {
match result { match result {
Ok(result) => { Ok(result) => {
log::info!("Job {} finished", job.id); log::info!("Job {} finished", job.id);
@ -437,19 +213,10 @@ impl eframe::App for App {
} }
} }
JobResult::ObjDiff(state) => { JobResult::ObjDiff(state) => {
self.view_state.build = Some(state); diff_state.build = Some(state);
}
JobResult::BinDiff(state) => {
self.view_state.build = Some(Box::new(ObjDiffResult {
first_status: BuildStatus { success: true, log: "".to_string() },
second_status: BuildStatus { success: true, log: "".to_string() },
first_obj: Some(state.first_obj),
second_obj: Some(state.second_obj),
time: OffsetDateTime::now_utc(),
}));
} }
JobResult::CheckUpdate(state) => { JobResult::CheckUpdate(state) => {
self.view_state.check_update = Some(state); config_state.check_update = Some(state);
} }
JobResult::Update(state) => { JobResult::Update(state) => {
if let Ok(mut guard) = self.relaunch_path.lock() { if let Ok(mut guard) = self.relaunch_path.lock() {
@ -483,7 +250,7 @@ impl eframe::App for App {
} }
} }
} }
self.view_state.jobs.clear_finished(); jobs.clear_finished();
if let Ok(mut config) = self.config.write() { if let Ok(mut config) = self.config.write() {
let config = &mut *config; let config = &mut *config;
@ -494,9 +261,12 @@ impl eframe::App for App {
if config.config_change { if config.config_change {
config.config_change = false; config.config_change = false;
if let Err(e) = load_project_config(config) { match load_project_config(config) {
log::error!("Failed to load project config: {e}"); Ok(()) => config_state.load_error = None,
config.load_error = Some(format!("{e}")); Err(e) => {
log::error!("Failed to load project config: {e}");
config_state.load_error = Some(format!("{e}"));
}
} }
} }
@ -526,16 +296,14 @@ impl eframe::App for App {
if config.obj_path.is_some() if config.obj_path.is_some()
&& self.modified.swap(false, Ordering::Relaxed) && self.modified.swap(false, Ordering::Relaxed)
&& !self.view_state.jobs.is_running(Job::ObjDiff) && !jobs.is_running(Job::ObjDiff)
{ {
self.view_state jobs.push(start_build(self.config.clone()));
.jobs
.push(start_build(self.config.clone(), self.view_state.diff_config.clone()));
} }
if config.queue_update_check { if config_state.queue_update_check {
self.view_state.jobs.push(start_check_update()); jobs.push(start_check_update());
config.queue_update_check = false; config_state.queue_update_check = false;
} }
} }
} }

View File

@ -3,7 +3,6 @@ use std::{collections::BTreeMap, mem::take};
use anyhow::Result; use anyhow::Result;
use crate::{ use crate::{
app::DiffConfig,
editops::{editops_find, LevEditType}, editops::{editops_find, LevEditType},
obj::{ obj::{
mips, ppc, ObjArchitecture, ObjDataDiff, ObjDataDiffKind, ObjInfo, ObjInsArg, mips, ppc, ObjArchitecture, ObjDataDiff, ObjDataDiffKind, ObjInfo, ObjInsArg,
@ -373,7 +372,7 @@ fn find_section_and_symbol(obj: &ObjInfo, name: &str) -> Option<(usize, usize)>
None None
} }
pub fn diff_objs(left: &mut ObjInfo, right: &mut ObjInfo, _diff_config: &DiffConfig) -> Result<()> { pub fn diff_objs(left: &mut ObjInfo, right: &mut ObjInfo) -> Result<()> {
for left_section in &mut left.sections { for left_section in &mut left.sections {
if left_section.kind == ObjSectionKind::Code { if left_section.kind == ObjSectionKind::Code {
for left_symbol in &mut left_section.symbols { for left_symbol in &mut left_section.symbols {

View File

@ -1,44 +0,0 @@
use std::sync::{mpsc::Receiver, Arc, RwLock};
use anyhow::{Error, Result};
use crate::{
app::{AppConfig, DiffConfig},
diff::diff_objs,
jobs::{start_job, update_status, Job, JobResult, JobState, Status},
obj::{elf, ObjInfo},
};
pub struct BinDiffResult {
pub first_obj: ObjInfo,
pub second_obj: ObjInfo,
}
fn run_build(
status: &Status,
cancel: Receiver<()>,
config: Arc<RwLock<AppConfig>>,
) -> Result<Box<BinDiffResult>> {
let config = config.read().map_err(|_| Error::msg("Failed to lock app config"))?.clone();
let target_path =
config.left_obj.as_ref().ok_or_else(|| Error::msg("Missing target obj path"))?;
let base_path = config.right_obj.as_ref().ok_or_else(|| Error::msg("Missing base obj path"))?;
update_status(status, "Loading target obj".to_string(), 0, 3, &cancel)?;
let mut left_obj = elf::read(target_path)?;
update_status(status, "Loading base obj".to_string(), 1, 3, &cancel)?;
let mut right_obj = elf::read(base_path)?;
update_status(status, "Performing diff".to_string(), 2, 3, &cancel)?;
diff_objs(&mut left_obj, &mut right_obj, &DiffConfig::default() /* TODO */)?;
update_status(status, "Complete".to_string(), 3, 3, &cancel)?;
Ok(Box::new(BinDiffResult { first_obj: left_obj, second_obj: right_obj }))
}
pub fn start_bindiff(config: Arc<RwLock<AppConfig>>) -> JobState {
start_job("Binary diff", Job::BinDiff, move |status, cancel| {
run_build(status, cancel, config).map(JobResult::BinDiff)
})
}

View File

@ -9,12 +9,8 @@ use std::{
use anyhow::Result; use anyhow::Result;
use crate::jobs::{ use crate::jobs::{check_update::CheckUpdateResult, objdiff::ObjDiffResult, update::UpdateResult};
bindiff::BinDiffResult, check_update::CheckUpdateResult, objdiff::ObjDiffResult,
update::UpdateResult,
};
pub mod bindiff;
pub mod check_update; pub mod check_update;
pub mod objdiff; pub mod objdiff;
pub mod update; pub mod update;
@ -22,7 +18,6 @@ pub mod update;
#[derive(Debug, Eq, PartialEq, Copy, Clone)] #[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Job { pub enum Job {
ObjDiff, ObjDiff,
BinDiff,
CheckUpdate, CheckUpdate,
Update, Update,
} }
@ -105,7 +100,6 @@ pub struct JobStatus {
pub enum JobResult { pub enum JobResult {
None, None,
ObjDiff(Box<ObjDiffResult>), ObjDiff(Box<ObjDiffResult>),
BinDiff(Box<BinDiffResult>),
CheckUpdate(Box<CheckUpdateResult>), CheckUpdate(Box<CheckUpdateResult>),
Update(Box<UpdateResult>), Update(Box<UpdateResult>),
} }

View File

@ -9,7 +9,7 @@ use anyhow::{Context, Error, Result};
use time::OffsetDateTime; use time::OffsetDateTime;
use crate::{ use crate::{
app::{AppConfig, DiffConfig}, app::AppConfig,
diff::diff_objs, diff::diff_objs,
jobs::{start_job, update_status, Job, JobResult, JobState, Status}, jobs::{start_job, update_status, Job, JobResult, JobState, Status},
obj::{elf, ObjInfo}, obj::{elf, ObjInfo},
@ -79,7 +79,6 @@ fn run_build(
status: &Status, status: &Status,
cancel: Receiver<()>, cancel: Receiver<()>,
config: Arc<RwLock<AppConfig>>, config: Arc<RwLock<AppConfig>>,
diff_config: DiffConfig,
) -> Result<Box<ObjDiffResult>> { ) -> Result<Box<ObjDiffResult>> {
let config = config.read().map_err(|_| Error::msg("Failed to lock app config"))?.clone(); let config = config.read().map_err(|_| Error::msg("Failed to lock app config"))?.clone();
let obj_path = config.obj_path.as_ref().ok_or_else(|| Error::msg("Missing obj path"))?; let obj_path = config.obj_path.as_ref().ok_or_else(|| Error::msg("Missing obj path"))?;
@ -129,15 +128,15 @@ fn run_build(
if let (Some(first_obj), Some(second_obj)) = (&mut first_obj, &mut second_obj) { if let (Some(first_obj), Some(second_obj)) = (&mut first_obj, &mut second_obj) {
update_status(status, "Performing diff".to_string(), 4, total, &cancel)?; update_status(status, "Performing diff".to_string(), 4, total, &cancel)?;
diff_objs(first_obj, second_obj, &diff_config)?; diff_objs(first_obj, second_obj)?;
} }
update_status(status, "Complete".to_string(), total, total, &cancel)?; update_status(status, "Complete".to_string(), total, total, &cancel)?;
Ok(Box::new(ObjDiffResult { first_status, second_status, first_obj, second_obj, time })) Ok(Box::new(ObjDiffResult { first_status, second_status, first_obj, second_obj, time }))
} }
pub fn start_build(config: Arc<RwLock<AppConfig>>, diff_config: DiffConfig) -> JobState { pub fn start_build(config: Arc<RwLock<AppConfig>>) -> JobState {
start_job("Object diff", Job::ObjDiff, move |status, cancel| { start_job("Object diff", Job::ObjDiff, move |status, cancel| {
run_build(status, cancel, config, diff_config).map(JobResult::ObjDiff) run_build(status, cancel, config).map(JobResult::ObjDiff)
}) })
} }

View File

@ -1,6 +1,95 @@
use egui::Color32; use egui::{Color32, FontFamily, FontId, TextStyle};
use time::UtcOffset;
use crate::app::ViewState; #[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct Appearance {
pub ui_font: FontId,
pub code_font: FontId,
pub diff_colors: Vec<Color32>,
pub reverse_fn_order: bool,
pub theme: eframe::Theme,
// Applied by theme
#[serde(skip)]
pub text_color: Color32, // GRAY
#[serde(skip)]
pub emphasized_text_color: Color32, // LIGHT_GRAY
#[serde(skip)]
pub deemphasized_text_color: Color32, // DARK_GRAY
#[serde(skip)]
pub highlight_color: Color32, // WHITE
#[serde(skip)]
pub replace_color: Color32, // LIGHT_BLUE
#[serde(skip)]
pub insert_color: Color32, // GREEN
#[serde(skip)]
pub delete_color: Color32, // RED
// Global
#[serde(skip)]
pub utc_offset: UtcOffset,
}
impl Default for Appearance {
fn default() -> Self {
Self {
ui_font: FontId { size: 12.0, family: FontFamily::Proportional },
code_font: FontId { size: 14.0, family: FontFamily::Monospace },
diff_colors: DEFAULT_COLOR_ROTATION.to_vec(),
reverse_fn_order: false,
theme: eframe::Theme::Dark,
text_color: Color32::GRAY,
emphasized_text_color: Color32::LIGHT_GRAY,
deemphasized_text_color: Color32::DARK_GRAY,
highlight_color: Color32::WHITE,
replace_color: Color32::LIGHT_BLUE,
insert_color: Color32::GREEN,
delete_color: Color32::from_rgb(200, 40, 41),
utc_offset: UtcOffset::UTC,
}
}
}
impl Appearance {
pub fn apply(&mut self, style: &egui::Style) -> egui::Style {
let mut style = style.clone();
style.text_styles.insert(TextStyle::Body, FontId {
size: (self.ui_font.size * 0.75).floor(),
family: self.ui_font.family.clone(),
});
style.text_styles.insert(TextStyle::Body, self.ui_font.clone());
style.text_styles.insert(TextStyle::Button, self.ui_font.clone());
style.text_styles.insert(TextStyle::Heading, FontId {
size: (self.ui_font.size * 1.5).floor(),
family: self.ui_font.family.clone(),
});
style.text_styles.insert(TextStyle::Monospace, self.code_font.clone());
match self.theme {
eframe::Theme::Dark => {
style.visuals = egui::Visuals::dark();
self.text_color = Color32::GRAY;
self.emphasized_text_color = Color32::LIGHT_GRAY;
self.deemphasized_text_color = Color32::DARK_GRAY;
self.highlight_color = Color32::WHITE;
self.replace_color = Color32::LIGHT_BLUE;
self.insert_color = Color32::GREEN;
self.delete_color = Color32::from_rgb(200, 40, 41);
}
eframe::Theme::Light => {
style.visuals = egui::Visuals::light();
self.text_color = Color32::GRAY;
self.emphasized_text_color = Color32::DARK_GRAY;
self.deemphasized_text_color = Color32::LIGHT_GRAY;
self.highlight_color = Color32::BLACK;
self.replace_color = Color32::DARK_BLUE;
self.insert_color = Color32::DARK_GREEN;
self.delete_color = Color32::from_rgb(200, 40, 41);
}
}
style
}
}
pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [ pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [
Color32::from_rgb(255, 0, 255), Color32::from_rgb(255, 0, 255),
@ -14,31 +103,27 @@ pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [
Color32::from_rgb(213, 138, 138), Color32::from_rgb(213, 138, 138),
]; ];
pub fn appearance_window(ctx: &egui::Context, view_state: &mut ViewState) { pub fn appearance_window(ctx: &egui::Context, show: &mut bool, appearance: &mut Appearance) {
egui::Window::new("Appearance").open(&mut view_state.show_view_config).show(ctx, |ui| { egui::Window::new("Appearance").open(show).show(ctx, |ui| {
egui::ComboBox::from_label("Theme") egui::ComboBox::from_label("Theme")
.selected_text(format!("{:?}", view_state.view_config.theme)) .selected_text(format!("{:?}", appearance.theme))
.show_ui(ui, |ui| { .show_ui(ui, |ui| {
ui.selectable_value(&mut view_state.view_config.theme, eframe::Theme::Dark, "Dark"); ui.selectable_value(&mut appearance.theme, eframe::Theme::Dark, "Dark");
ui.selectable_value( ui.selectable_value(&mut appearance.theme, eframe::Theme::Light, "Light");
&mut view_state.view_config.theme,
eframe::Theme::Light,
"Light",
);
}); });
ui.label("UI font:"); ui.label("UI font:");
egui::introspection::font_id_ui(ui, &mut view_state.view_config.ui_font); egui::introspection::font_id_ui(ui, &mut appearance.ui_font);
ui.separator(); ui.separator();
ui.label("Code font:"); ui.label("Code font:");
egui::introspection::font_id_ui(ui, &mut view_state.view_config.code_font); egui::introspection::font_id_ui(ui, &mut appearance.code_font);
ui.separator(); ui.separator();
ui.label("Diff colors:"); ui.label("Diff colors:");
if ui.button("Reset").clicked() { if ui.button("Reset").clicked() {
view_state.view_config.diff_colors = DEFAULT_COLOR_ROTATION.to_vec(); appearance.diff_colors = DEFAULT_COLOR_ROTATION.to_vec();
} }
let mut remove_at: Option<usize> = None; let mut remove_at: Option<usize> = None;
let num_colors = view_state.view_config.diff_colors.len(); let num_colors = appearance.diff_colors.len();
for (idx, color) in view_state.view_config.diff_colors.iter_mut().enumerate() { for (idx, color) in appearance.diff_colors.iter_mut().enumerate() {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.color_edit_button_srgba(color); ui.color_edit_button_srgba(color);
if num_colors > 1 && ui.small_button("-").clicked() { if num_colors > 1 && ui.small_button("-").clicked() {
@ -47,10 +132,10 @@ pub fn appearance_window(ctx: &egui::Context, view_state: &mut ViewState) {
}); });
} }
if let Some(idx) = remove_at { if let Some(idx) = remove_at {
view_state.view_config.diff_colors.remove(idx); appearance.diff_colors.remove(idx);
} }
if ui.small_button("+").clicked() { if ui.small_button("+").clicked() {
view_state.view_config.diff_colors.push(Color32::BLACK); appearance.diff_colors.push(Color32::BLACK);
} }
}); });
} }

View File

@ -1,7 +1,7 @@
#[cfg(windows)] #[cfg(windows)]
use std::string::FromUtf16Error; use std::string::FromUtf16Error;
use std::{ use std::{
path::PathBuf, path::{PathBuf, MAIN_SEPARATOR},
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
@ -16,12 +16,23 @@ use globset::Glob;
use self_update::cargo_crate_version; use self_update::cargo_crate_version;
use crate::{ use crate::{
app::{AppConfig, DiffKind, ViewConfig, ViewState}, app::AppConfig,
config::{ProjectUnit, ProjectUnitNode}, config::{ProjectUnit, ProjectUnitNode},
jobs::{bindiff::start_bindiff, objdiff::start_build, update::start_update}, jobs::{check_update::CheckUpdateResult, objdiff::start_build, update::start_update, JobQueue},
update::RELEASE_URL, update::RELEASE_URL,
views::appearance::Appearance,
}; };
#[derive(Default)]
pub struct ConfigViewState {
pub check_update: Option<Box<CheckUpdateResult>>,
pub watch_pattern_text: String,
pub queue_update_check: bool,
pub load_error: Option<String>,
#[cfg(windows)]
pub available_wsl_distros: Option<Vec<String>>,
}
const DEFAULT_WATCH_PATTERNS: &[&str] = &[ const DEFAULT_WATCH_PATTERNS: &[&str] = &[
"*.c", "*.cp", "*.cpp", "*.cxx", "*.h", "*.hp", "*.hpp", "*.hxx", "*.s", "*.S", "*.asm", "*.c", "*.cp", "*.cpp", "*.cxx", "*.h", "*.hp", "*.hpp", "*.hxx", "*.s", "*.S", "*.asm",
"*.inc", "*.py", "*.yml", "*.txt", "*.json", "*.inc", "*.py", "*.yml", "*.txt", "*.json",
@ -60,17 +71,20 @@ fn fetch_wsl2_distros() -> Vec<String> {
.unwrap_or_default() .unwrap_or_default()
} }
pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state: &mut ViewState) { pub fn config_ui(
ui: &mut egui::Ui,
config: &Arc<RwLock<AppConfig>>,
jobs: &mut JobQueue,
show_config_window: &mut bool,
state: &mut ConfigViewState,
appearance: &Appearance,
) {
let mut config_guard = config.write().unwrap(); let mut config_guard = config.write().unwrap();
let AppConfig { let AppConfig {
available_wsl_distros,
selected_wsl_distro, selected_wsl_distro,
target_obj_dir, target_obj_dir,
base_obj_dir, base_obj_dir,
obj_path, obj_path,
left_obj,
right_obj,
queue_update_check,
auto_update_check, auto_update_check,
units, units,
unit_nodes, unit_nodes,
@ -80,7 +94,7 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state:
ui.heading("Updates"); ui.heading("Updates");
ui.checkbox(auto_update_check, "Check for updates on startup"); ui.checkbox(auto_update_check, "Check for updates on startup");
if ui.button("Check now").clicked() { if ui.button("Check now").clicked() {
*queue_update_check = true; state.queue_update_check = true;
} }
ui.label(format!("Current version: {}", cargo_crate_version!())).on_hover_ui_at_pointer(|ui| { ui.label(format!("Current version: {}", cargo_crate_version!())).on_hover_ui_at_pointer(|ui| {
ui.label(formatcp!("Git branch: {}", env!("VERGEN_GIT_BRANCH"))); ui.label(formatcp!("Git branch: {}", env!("VERGEN_GIT_BRANCH")));
@ -88,10 +102,10 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state:
ui.label(formatcp!("Build target: {}", env!("VERGEN_CARGO_TARGET_TRIPLE"))); ui.label(formatcp!("Build target: {}", env!("VERGEN_CARGO_TARGET_TRIPLE")));
ui.label(formatcp!("Debug: {}", env!("VERGEN_CARGO_DEBUG"))); ui.label(formatcp!("Debug: {}", env!("VERGEN_CARGO_DEBUG")));
}); });
if let Some(state) = &view_state.check_update { if let Some(state) = &state.check_update {
ui.label(format!("Latest version: {}", state.latest_release.version)); ui.label(format!("Latest version: {}", state.latest_release.version));
if state.update_available { if state.update_available {
ui.colored_label(view_state.view_config.insert_color, "Update available"); ui.colored_label(appearance.insert_color, "Update available");
ui.horizontal(|ui| { ui.horizontal(|ui| {
if state.found_binary if state.found_binary
&& ui && ui
@ -101,7 +115,7 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state:
) )
.clicked() .clicked()
{ {
view_state.jobs.push(start_update()); jobs.push(start_update());
} }
if ui if ui
.button("Manual") .button("Manual")
@ -121,14 +135,14 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state:
#[cfg(windows)] #[cfg(windows)]
{ {
ui.heading("Build"); ui.heading("Build");
if available_wsl_distros.is_none() { if state.available_wsl_distros.is_none() {
*available_wsl_distros = Some(fetch_wsl2_distros()); state.available_wsl_distros = Some(fetch_wsl2_distros());
} }
egui::ComboBox::from_label("Run in WSL2") egui::ComboBox::from_label("Run in WSL2")
.selected_text(selected_wsl_distro.as_ref().unwrap_or(&"None".to_string())) .selected_text(selected_wsl_distro.as_ref().unwrap_or(&"None".to_string()))
.show_ui(ui, |ui| { .show_ui(ui, |ui| {
ui.selectable_value(selected_wsl_distro, None, "None"); ui.selectable_value(selected_wsl_distro, None, "None");
for distro in available_wsl_distros.as_ref().unwrap() { for distro in state.available_wsl_distros.as_ref().unwrap() {
ui.selectable_value(selected_wsl_distro, Some(distro.clone()), distro); ui.selectable_value(selected_wsl_distro, Some(distro.clone()), distro);
} }
}); });
@ -136,94 +150,63 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state:
} }
#[cfg(not(windows))] #[cfg(not(windows))]
{ {
let _ = available_wsl_distros;
let _ = selected_wsl_distro; let _ = selected_wsl_distro;
} }
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.heading("Project"); ui.heading("Project");
if ui.button(RichText::new("Settings")).clicked() { if ui.button(RichText::new("Settings")).clicked() {
view_state.show_project_config = true; *show_config_window = true;
} }
}); });
if view_state.diff_kind == DiffKind::SplitObj { if let (Some(base_dir), Some(target_dir)) = (base_obj_dir, target_obj_dir) {
if let (Some(base_dir), Some(target_dir)) = (base_obj_dir, target_obj_dir) { let mut new_build_obj = obj_path.clone();
let mut new_build_obj = obj_path.clone(); if units.is_empty() {
if units.is_empty() { if ui.button("Select obj").clicked() {
if ui.button("Select obj").clicked() { if let Some(path) = rfd::FileDialog::new()
if let Some(path) = rfd::FileDialog::new() .set_directory(&target_dir)
.set_directory(&target_dir) .add_filter("Object file", &["o", "elf"])
.add_filter("Object file", &["o", "elf"]) .pick_file()
.pick_file() {
{ if let Ok(obj_path) = path.strip_prefix(&base_dir) {
if let Ok(obj_path) = path.strip_prefix(&base_dir) { new_build_obj = Some(obj_path.display().to_string());
new_build_obj = Some(obj_path.display().to_string()); } else if let Ok(obj_path) = path.strip_prefix(&target_dir) {
} else if let Ok(obj_path) = path.strip_prefix(&target_dir) { new_build_obj = Some(obj_path.display().to_string());
new_build_obj = Some(obj_path.display().to_string());
}
} }
} }
if let Some(obj) = obj_path { }
ui.label(&*obj); if let Some(obj) = obj_path {
ui.label(&*obj);
}
} else {
CollapsingHeader::new(RichText::new("Objects").font(FontId {
size: appearance.ui_font.size,
family: appearance.code_font.family.clone(),
}))
.default_open(true)
.show(ui, |ui| {
for node in unit_nodes {
display_node(ui, &mut new_build_obj, node, appearance);
} }
} else { });
CollapsingHeader::new(RichText::new("Objects").font(FontId {
size: view_state.view_config.ui_font.size,
family: view_state.view_config.code_font.family.clone(),
}))
.default_open(true)
.show(ui, |ui| {
for node in unit_nodes {
display_node(ui, &mut new_build_obj, node, &view_state.view_config);
}
});
}
let mut build = false;
if new_build_obj != *obj_path {
*obj_path = new_build_obj;
// TODO apply reverse_fn_order
build = true;
}
if obj_path.is_some() && ui.button("Build").clicked() {
build = true;
}
if build {
view_state.jobs.push(start_build(config.clone(), view_state.diff_config.clone()));
}
}
} else if view_state.diff_kind == DiffKind::WholeBinary {
if ui.button("Select left obj").clicked() {
if let Some(path) =
rfd::FileDialog::new().add_filter("Object file", &["o", "elf"]).pick_file()
{
*left_obj = Some(path);
}
}
if let Some(obj) = left_obj {
ui.label(obj.to_string_lossy());
} }
if ui.button("Select right obj").clicked() { let mut build = false;
if let Some(path) = if new_build_obj != *obj_path {
rfd::FileDialog::new().add_filter("Object file", &["o", "elf"]).pick_file() *obj_path = new_build_obj;
{ // TODO apply reverse_fn_order
*right_obj = Some(path); build = true;
}
} }
if let Some(obj) = right_obj { if obj_path.is_some() && ui.button("Build").clicked() {
ui.label(obj.to_string_lossy()); build = true;
} }
if build {
if let (Some(_), Some(_)) = (left_obj, right_obj) { jobs.push(start_build(config.clone()));
if ui.button("Build").clicked() {
view_state.jobs.push(start_bindiff(config.clone()));
}
} }
} }
// ui.checkbox(&mut view_state.view_config.reverse_fn_order, "Reverse function order (deferred)"); // ui.checkbox(&mut view_config.reverse_fn_order, "Reverse function order (deferred)");
ui.separator(); ui.separator();
} }
@ -232,15 +215,15 @@ fn display_unit(
obj_path: &mut Option<String>, obj_path: &mut Option<String>,
name: &str, name: &str,
unit: &ProjectUnit, unit: &ProjectUnit,
view_config: &ViewConfig, appearance: &Appearance,
) { ) {
let path_string = unit.path.to_string_lossy().to_string(); let path_string = unit.path.to_string_lossy().to_string();
let selected = matches!(obj_path, Some(path) if path == &path_string); let selected = matches!(obj_path, Some(path) if path == &path_string);
if SelectableLabel::new( if SelectableLabel::new(
selected, selected,
RichText::new(name).font(FontId { RichText::new(name).font(FontId {
size: view_config.ui_font.size, size: appearance.ui_font.size,
family: view_config.code_font.family.clone(), family: appearance.code_font.family.clone(),
}), }),
) )
.ui(ui) .ui(ui)
@ -254,21 +237,21 @@ fn display_node(
ui: &mut egui::Ui, ui: &mut egui::Ui,
obj_path: &mut Option<String>, obj_path: &mut Option<String>,
node: &ProjectUnitNode, node: &ProjectUnitNode,
view_config: &ViewConfig, appearance: &Appearance,
) { ) {
match node { match node {
ProjectUnitNode::File(name, unit) => { ProjectUnitNode::File(name, unit) => {
display_unit(ui, obj_path, name, unit, view_config); display_unit(ui, obj_path, name, unit, appearance);
} }
ProjectUnitNode::Dir(name, children) => { ProjectUnitNode::Dir(name, children) => {
CollapsingHeader::new(RichText::new(name).font(FontId { CollapsingHeader::new(RichText::new(name).font(FontId {
size: view_config.ui_font.size, size: appearance.ui_font.size,
family: view_config.code_font.family.clone(), family: appearance.code_font.family.clone(),
})) }))
.default_open(false) .default_open(false)
.show(ui, |ui| { .show(ui, |ui| {
for node in children { for node in children {
display_node(ui, obj_path, node, view_config); display_node(ui, obj_path, node, appearance);
} }
}); });
} }
@ -277,18 +260,76 @@ fn display_node(
const HELP_ICON: &str = ""; const HELP_ICON: &str = "";
fn subheading(ui: &mut egui::Ui, text: &str, view_config: &ViewConfig) { fn subheading(ui: &mut egui::Ui, text: &str, appearance: &Appearance) {
ui.label( ui.label(
RichText::new(text).size(view_config.ui_font.size).color(view_config.emphasized_text_color), RichText::new(text).size(appearance.ui_font.size).color(appearance.emphasized_text_color),
); );
} }
fn format_path(path: &Option<PathBuf>, appearance: &Appearance) -> RichText {
let mut color = appearance.replace_color;
let text = if let Some(dir) = path {
if let Some(rel) = dirs::home_dir().and_then(|home| dir.strip_prefix(&home).ok()) {
format!("~{}{}", MAIN_SEPARATOR, rel.display())
} else {
format!("{}", dir.display())
}
} else {
color = appearance.delete_color;
"[none]".to_string()
};
RichText::new(text).color(color).family(FontFamily::Monospace)
}
fn pick_folder_ui(
ui: &mut egui::Ui,
dir: &mut Option<PathBuf>,
label: &str,
tooltip: impl FnOnce(&mut egui::Ui),
clicked: impl FnOnce(&mut Option<PathBuf>),
appearance: &Appearance,
) {
ui.horizontal(|ui| {
subheading(ui, label, appearance);
ui.link(HELP_ICON).on_hover_ui(tooltip);
if ui.button("Select").clicked() {
clicked(dir);
}
});
ui.label(format_path(dir, appearance));
}
pub fn project_window( pub fn project_window(
ctx: &egui::Context, ctx: &egui::Context,
config: &Arc<RwLock<AppConfig>>, config: &Arc<RwLock<AppConfig>>,
view_state: &mut ViewState, show: &mut bool,
state: &mut ConfigViewState,
appearance: &Appearance,
) { ) {
let mut config_guard = config.write().unwrap(); let mut config_guard = config.write().unwrap();
egui::Window::new("Project").open(show).show(ctx, |ui| {
split_obj_config_ui(ui, &mut config_guard, state, appearance);
});
if let Some(error) = &state.load_error {
let mut open = true;
egui::Window::new("Error").open(&mut open).show(ctx, |ui| {
ui.label("Failed to load project config:");
ui.colored_label(appearance.delete_color, error);
});
if !open {
state.load_error = None;
}
}
}
fn split_obj_config_ui(
ui: &mut egui::Ui,
config: &mut AppConfig,
state: &mut ConfigViewState,
appearance: &Appearance,
) {
let AppConfig { let AppConfig {
custom_make, custom_make,
project_dir, project_dir,
@ -300,239 +341,204 @@ pub fn project_window(
watcher_change, watcher_change,
watcher_enabled, watcher_enabled,
watch_patterns, watch_patterns,
load_error,
.. ..
} = &mut *config_guard; } = config;
egui::Window::new("Project").open(&mut view_state.show_project_config).show(ctx, |ui| { let text_format = TextFormat::simple(appearance.ui_font.clone(), appearance.text_color);
let text_format = TextFormat::simple( let code_format = TextFormat::simple(
view_state.view_config.ui_font.clone(), FontId { size: appearance.ui_font.size, family: appearance.code_font.family.clone() },
view_state.view_config.text_color, appearance.emphasized_text_color,
); );
let code_format = TextFormat::simple(
FontId {
size: view_state.view_config.ui_font.size,
family: view_state.view_config.code_font.family.clone(),
},
view_state.view_config.emphasized_text_color,
);
fn pick_folder_ui( pick_folder_ui(
ui: &mut egui::Ui, ui,
dir: &mut Option<PathBuf>, project_dir,
label: &str, "Project directory",
tooltip: impl FnOnce(&mut egui::Ui), |ui| {
clicked: impl FnOnce(&mut Option<PathBuf>), let mut job = LayoutJob::default();
view_config: &ViewConfig, job.append("The root project directory.\n\n", 0.0, text_format.clone());
) { job.append(
ui.horizontal(|ui| { "If a configuration file exists, it will be loaded automatically.",
subheading(ui, label, view_config); 0.0,
ui.link(HELP_ICON).on_hover_ui(tooltip); text_format.clone(),
if ui.button("Select").clicked() {
clicked(dir);
}
});
if let Some(dir) = dir {
if let Some(home) = dirs::home_dir() {
if let Ok(rel) = dir.strip_prefix(&home) {
ui.label(RichText::new(format!("~/{}", rel.display())).color(view_config.replace_color).family(FontFamily::Monospace));
return;
}
}
ui.label(RichText::new(format!("{}", dir.display())).color(view_config.replace_color).family(FontFamily::Monospace));
} else {
ui.label(RichText::new("[none]").color(view_config.delete_color).family(FontFamily::Monospace));
}
}
if view_state.diff_kind == DiffKind::SplitObj {
pick_folder_ui(
ui,
project_dir,
"Project directory",
|ui| {
let mut job = LayoutJob::default();
job.append(
"The root project directory.\n\n",
0.0,
text_format.clone()
);
job.append(
"If a configuration file exists, it will be loaded automatically.",
0.0,
text_format.clone(),
);
ui.label(job);
},
|project_dir| {
if let Some(path) = rfd::FileDialog::new().pick_folder() {
*project_dir = Some(path);
*config_change = true;
*watcher_change = true;
*target_obj_dir = None;
*base_obj_dir = None;
*obj_path = None;
}
},
&view_state.view_config,
); );
ui.separator(); ui.label(job);
},
ui.horizontal(|ui| { |project_dir| {
subheading(ui, "Custom make program", &view_state.view_config); if let Some(path) = rfd::FileDialog::new().pick_folder() {
ui.link(HELP_ICON).on_hover_ui(|ui| { *project_dir = Some(path);
let mut job = LayoutJob::default(); *config_change = true;
job.append("By default, objdiff will build with ", 0.0, text_format.clone());
job.append("make", 0.0, code_format.clone());
job.append(
".\nIf the project uses a different build system (e.g. ",
0.0,
text_format.clone(),
);
job.append("ninja", 0.0, code_format.clone());
job.append(
"), specify it here.\nThe program must be in your ",
0.0,
text_format.clone(),
);
job.append("PATH", 0.0, code_format.clone());
job.append(".", 0.0, text_format.clone());
ui.label(job);
});
});
let mut custom_make_str = custom_make.clone().unwrap_or_default();
if ui.text_edit_singleline(&mut custom_make_str).changed() {
if custom_make_str.is_empty() {
*custom_make = None;
} else {
*custom_make = Some(custom_make_str);
}
}
ui.separator();
if let Some(project_dir) = project_dir {
pick_folder_ui(
ui,
target_obj_dir,
"Target build directory",
|ui| {
let mut job = LayoutJob::default();
job.append(
"This contains the \"target\" or \"expected\" objects, which are the intended result of the match.\n\n",
0.0,
text_format.clone(),
);
job.append(
"These are usually created by the project's build system or assembled.",
0.0,
text_format.clone(),
);
ui.label(job);
},
|target_obj_dir| {
if let Some(path) =
rfd::FileDialog::new().set_directory(&project_dir).pick_folder()
{
*target_obj_dir = Some(path);
*obj_path = None;
}
},
&view_state.view_config,
);
ui.checkbox(build_target, "Build target objects").on_hover_ui(|ui| {
let mut job = LayoutJob::default();
job.append("Tells the build system to produce the target object.\n", 0.0, text_format.clone());
job.append("For example, this would call ", 0.0, text_format.clone());
job.append("make path/to/target.o", 0.0, code_format.clone());
job.append(".\n\n", 0.0, text_format.clone());
job.append("This is useful if the target objects are not already built\n", 0.0, text_format.clone());
job.append("or if they can change based on project configuration,\n", 0.0, text_format.clone());
job.append("but requires that the build system is configured correctly.", 0.0, text_format.clone());
ui.label(job);
});
ui.separator();
pick_folder_ui(
ui,
base_obj_dir,
"Base build directory",
|ui| {
let mut job = LayoutJob::default();
job.append(
"This contains the objects built from your decompiled code.",
0.0,
text_format.clone(),
);
ui.label(job);
},
|base_obj_dir| {
if let Some(path) =
rfd::FileDialog::new().set_directory(&project_dir).pick_folder()
{
*base_obj_dir = Some(path);
*obj_path = None;
}
},
&view_state.view_config,
);
ui.separator();
}
subheading(ui, "Watch settings", &view_state.view_config);
let response = ui.checkbox(watcher_enabled, "Rebuild on changes").on_hover_ui(|ui| {
let mut job = LayoutJob::default();
job.append("Automatically re-run the build & diff when files change.", 0.0, text_format.clone());
ui.label(job);
});
if response.changed() {
*watcher_change = true; *watcher_change = true;
}; *target_obj_dir = None;
*base_obj_dir = None;
ui.horizontal(|ui| { *obj_path = None;
ui.label(RichText::new("File Patterns").color(view_state.view_config.text_color));
if ui.button("Reset").clicked() {
*watch_patterns = DEFAULT_WATCH_PATTERNS.iter().map(|s| Glob::new(s).unwrap()).collect();
*watcher_change = true;
}
});
let mut remove_at: Option<usize> = None;
for (idx, glob) in watch_patterns.iter().enumerate() {
ui.horizontal(|ui| {
ui.label(RichText::new(format!("{}", glob))
.color(view_state.view_config.text_color)
.family(FontFamily::Monospace));
if ui.small_button("-").clicked() {
remove_at = Some(idx);
}
});
} }
if let Some(idx) = remove_at { },
watch_patterns.remove(idx); appearance,
*watcher_change = true; );
} ui.separator();
ui.horizontal(|ui| {
egui::TextEdit::singleline(&mut view_state.watch_pattern_text)
.desired_width(100.0)
.show(ui);
if ui.small_button("+").clicked() {
if let Ok(glob) = Glob::new(&view_state.watch_pattern_text) {
watch_patterns.push(glob);
*watcher_change = true;
view_state.watch_pattern_text.clear();
}
}
});
}
});
if let Some(error) = &load_error { ui.horizontal(|ui| {
let mut open = true; subheading(ui, "Custom make program", appearance);
egui::Window::new("Error").open(&mut open).show(ctx, |ui| { ui.link(HELP_ICON).on_hover_ui(|ui| {
ui.label("Failed to load project config:"); let mut job = LayoutJob::default();
ui.colored_label(view_state.view_config.delete_color, error); job.append("By default, objdiff will build with ", 0.0, text_format.clone());
job.append("make", 0.0, code_format.clone());
job.append(
".\nIf the project uses a different build system (e.g. ",
0.0,
text_format.clone(),
);
job.append("ninja", 0.0, code_format.clone());
job.append(
"), specify it here.\nThe program must be in your ",
0.0,
text_format.clone(),
);
job.append("PATH", 0.0, code_format.clone());
job.append(".", 0.0, text_format.clone());
ui.label(job);
}); });
if !open { });
*load_error = None; let mut custom_make_str = custom_make.clone().unwrap_or_default();
if ui.text_edit_singleline(&mut custom_make_str).changed() {
if custom_make_str.is_empty() {
*custom_make = None;
} else {
*custom_make = Some(custom_make_str);
} }
} }
ui.separator();
if let Some(project_dir) = project_dir {
pick_folder_ui(
ui,
target_obj_dir,
"Target build directory",
|ui| {
let mut job = LayoutJob::default();
job.append(
"This contains the \"target\" or \"expected\" objects, which are the intended result of the match.\n\n",
0.0,
text_format.clone(),
);
job.append(
"These are usually created by the project's build system or assembled.",
0.0,
text_format.clone(),
);
ui.label(job);
},
|target_obj_dir| {
if let Some(path) = rfd::FileDialog::new().set_directory(&project_dir).pick_folder()
{
*target_obj_dir = Some(path);
*obj_path = None;
}
},
appearance,
);
ui.checkbox(build_target, "Build target objects").on_hover_ui(|ui| {
let mut job = LayoutJob::default();
job.append(
"Tells the build system to produce the target object.\n",
0.0,
text_format.clone(),
);
job.append("For example, this would call ", 0.0, text_format.clone());
job.append("make path/to/target.o", 0.0, code_format.clone());
job.append(".\n\n", 0.0, text_format.clone());
job.append(
"This is useful if the target objects are not already built\n",
0.0,
text_format.clone(),
);
job.append(
"or if they can change based on project configuration,\n",
0.0,
text_format.clone(),
);
job.append(
"but requires that the build system is configured correctly.",
0.0,
text_format.clone(),
);
ui.label(job);
});
ui.separator();
pick_folder_ui(
ui,
base_obj_dir,
"Base build directory",
|ui| {
let mut job = LayoutJob::default();
job.append(
"This contains the objects built from your decompiled code.",
0.0,
text_format.clone(),
);
ui.label(job);
},
|base_obj_dir| {
if let Some(path) = rfd::FileDialog::new().set_directory(&project_dir).pick_folder()
{
*base_obj_dir = Some(path);
*obj_path = None;
}
},
appearance,
);
ui.separator();
}
subheading(ui, "Watch settings", appearance);
let response = ui.checkbox(watcher_enabled, "Rebuild on changes").on_hover_ui(|ui| {
let mut job = LayoutJob::default();
job.append(
"Automatically re-run the build & diff when files change.",
0.0,
text_format.clone(),
);
ui.label(job);
});
if response.changed() {
*watcher_change = true;
};
ui.horizontal(|ui| {
ui.label(RichText::new("File Patterns").color(appearance.text_color));
if ui.button("Reset").clicked() {
*watch_patterns =
DEFAULT_WATCH_PATTERNS.iter().map(|s| Glob::new(s).unwrap()).collect();
*watcher_change = true;
}
});
let mut remove_at: Option<usize> = None;
for (idx, glob) in watch_patterns.iter().enumerate() {
ui.horizontal(|ui| {
ui.label(
RichText::new(format!("{}", glob))
.color(appearance.text_color)
.family(FontFamily::Monospace),
);
if ui.small_button("-").clicked() {
remove_at = Some(idx);
}
});
}
if let Some(idx) = remove_at {
watch_patterns.remove(idx);
*watcher_change = true;
}
ui.horizontal(|ui| {
egui::TextEdit::singleline(&mut state.watch_pattern_text).desired_width(100.0).show(ui);
if ui.small_button("+").clicked() {
if let Ok(glob) = Glob::new(&state.watch_pattern_text) {
watch_patterns.push(glob);
*watcher_change = true;
state.watch_pattern_text.clear();
}
}
});
} }

View File

@ -5,10 +5,13 @@ use egui_extras::{Column, TableBuilder};
use time::format_description; use time::format_description;
use crate::{ use crate::{
app::{SymbolReference, View, ViewConfig, ViewState}, jobs::{Job, JobQueue},
jobs::Job,
obj::{ObjDataDiff, ObjDataDiffKind, ObjInfo, ObjSection}, obj::{ObjDataDiff, ObjDataDiffKind, ObjInfo, ObjSection},
views::write_text, views::{
appearance::Appearance,
symbol_diff::{DiffViewState, SymbolReference, View},
write_text,
},
}; };
const BYTES_PER_ROW: usize = 16; const BYTES_PER_ROW: usize = 16;
@ -17,29 +20,29 @@ fn find_section<'a>(obj: &'a ObjInfo, selected_symbol: &SymbolReference) -> Opti
obj.sections.iter().find(|section| section.name == selected_symbol.section_name) obj.sections.iter().find(|section| section.name == selected_symbol.section_name)
} }
fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], config: &ViewConfig) { fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], appearance: &Appearance) {
if diffs.iter().any(|d| d.kind != ObjDataDiffKind::None) { if diffs.iter().any(|d| d.kind != ObjDataDiffKind::None) {
ui.painter().rect_filled(ui.available_rect_before_wrap(), 0.0, ui.visuals().faint_bg_color); ui.painter().rect_filled(ui.available_rect_before_wrap(), 0.0, ui.visuals().faint_bg_color);
} }
let mut job = LayoutJob::default(); let mut job = LayoutJob::default();
write_text( write_text(
format!("{address:08X}: ").as_str(), format!("{address:08X}: ").as_str(),
config.text_color, appearance.text_color,
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
let mut cur_addr = 0usize; let mut cur_addr = 0usize;
for diff in diffs { for diff in diffs {
let base_color = match diff.kind { let base_color = match diff.kind {
ObjDataDiffKind::None => config.text_color, ObjDataDiffKind::None => appearance.text_color,
ObjDataDiffKind::Replace => config.replace_color, ObjDataDiffKind::Replace => appearance.replace_color,
ObjDataDiffKind::Delete => config.delete_color, ObjDataDiffKind::Delete => appearance.delete_color,
ObjDataDiffKind::Insert => config.insert_color, ObjDataDiffKind::Insert => appearance.insert_color,
}; };
if diff.data.is_empty() { if diff.data.is_empty() {
let mut str = " ".repeat(diff.len); let mut str = " ".repeat(diff.len);
str.push_str(" ".repeat(diff.len / 8).as_str()); str.push_str(" ".repeat(diff.len / 8).as_str());
write_text(str.as_str(), base_color, &mut job, config.code_font.clone()); write_text(str.as_str(), base_color, &mut job, appearance.code_font.clone());
cur_addr += diff.len; cur_addr += diff.len;
} else { } else {
let mut text = String::new(); let mut text = String::new();
@ -50,7 +53,7 @@ fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], config:
text.push(' '); text.push(' ');
} }
} }
write_text(text.as_str(), base_color, &mut job, config.code_font.clone()); write_text(text.as_str(), base_color, &mut job, appearance.code_font.clone());
} }
} }
if cur_addr < BYTES_PER_ROW { if cur_addr < BYTES_PER_ROW {
@ -58,22 +61,22 @@ fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], config:
let mut str = " ".to_string(); let mut str = " ".to_string();
str.push_str(" ".repeat(n).as_str()); str.push_str(" ".repeat(n).as_str());
str.push_str(" ".repeat(n / 8).as_str()); str.push_str(" ".repeat(n / 8).as_str());
write_text(str.as_str(), config.text_color, &mut job, config.code_font.clone()); write_text(str.as_str(), appearance.text_color, &mut job, appearance.code_font.clone());
} }
write_text(" ", config.text_color, &mut job, config.code_font.clone()); write_text(" ", appearance.text_color, &mut job, appearance.code_font.clone());
for diff in diffs { for diff in diffs {
let base_color = match diff.kind { let base_color = match diff.kind {
ObjDataDiffKind::None => config.text_color, ObjDataDiffKind::None => appearance.text_color,
ObjDataDiffKind::Replace => config.replace_color, ObjDataDiffKind::Replace => appearance.replace_color,
ObjDataDiffKind::Delete => config.delete_color, ObjDataDiffKind::Delete => appearance.delete_color,
ObjDataDiffKind::Insert => config.insert_color, ObjDataDiffKind::Insert => appearance.insert_color,
}; };
if diff.data.is_empty() { if diff.data.is_empty() {
write_text( write_text(
" ".repeat(diff.len).as_str(), " ".repeat(diff.len).as_str(),
base_color, base_color,
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
} else { } else {
let mut text = String::new(); let mut text = String::new();
@ -85,7 +88,7 @@ fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], config:
text.push('.'); text.push('.');
} }
} }
write_text(text.as_str(), base_color, &mut job, config.code_font.clone()); write_text(text.as_str(), base_color, &mut job, appearance.code_font.clone());
} }
} }
ui.add(Label::new(job).sense(Sense::click())); ui.add(Label::new(job).sense(Sense::click()));
@ -133,7 +136,7 @@ fn data_table_ui(
left_obj: &ObjInfo, left_obj: &ObjInfo,
right_obj: &ObjInfo, right_obj: &ObjInfo,
selected_symbol: &SymbolReference, selected_symbol: &SymbolReference,
config: &ViewConfig, config: &Appearance,
) -> Option<()> { ) -> Option<()> {
let left_section = find_section(left_obj, selected_symbol)?; let left_section = find_section(left_obj, selected_symbol)?;
let right_section = find_section(right_obj, selected_symbol)?; let right_section = find_section(right_obj, selected_symbol)?;
@ -161,10 +164,14 @@ fn data_table_ui(
Some(()) Some(())
} }
pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool { pub fn data_diff_ui(
ui: &mut egui::Ui,
jobs: &JobQueue,
state: &mut DiffViewState,
appearance: &Appearance,
) -> bool {
let mut rebuild = false; let mut rebuild = false;
let (Some(result), Some(selected_symbol)) = (&view_state.build, &view_state.selected_symbol) let (Some(result), Some(selected_symbol)) = (&state.build, &state.selected_symbol) else {
else {
return rebuild; return rebuild;
}; };
@ -183,16 +190,13 @@ pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.set_width(column_width); ui.set_width(column_width);
if ui.button("Back").clicked() { if ui.button("Back").clicked() {
view_state.current_view = View::SymbolDiff; state.current_view = View::SymbolDiff;
} }
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.colored_label( ui.colored_label(appearance.highlight_color, &selected_symbol.symbol_name);
view_state.view_config.highlight_color,
&selected_symbol.symbol_name,
);
ui.label("Diff target:"); ui.label("Diff target:");
}); });
}, },
@ -212,8 +216,8 @@ pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
if view_state.jobs.is_running(Job::ObjDiff) { if jobs.is_running(Job::ObjDiff) {
ui.colored_label(view_state.view_config.replace_color, "Building…"); ui.colored_label(appearance.replace_color, "Building…");
} else { } else {
ui.label("Last built:"); ui.label("Last built:");
let format = let format =
@ -221,7 +225,7 @@ pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.label( ui.label(
result result
.time .time
.to_offset(view_state.utc_offset) .to_offset(appearance.utc_offset)
.format(&format) .format(&format)
.unwrap(), .unwrap(),
); );
@ -251,7 +255,7 @@ pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
.resizable(false) .resizable(false)
.auto_shrink([false, false]) .auto_shrink([false, false])
.min_scrolled_height(available_height); .min_scrolled_height(available_height);
data_table_ui(table, left_obj, right_obj, selected_symbol, &view_state.view_config); data_table_ui(table, left_obj, right_obj, selected_symbol, appearance);
} }
rebuild rebuild

View File

@ -1,17 +1,25 @@
use egui::TextStyle; use egui::TextStyle;
use crate::app::ViewState; use crate::views::appearance::Appearance;
pub fn demangle_window(ctx: &egui::Context, view_state: &mut ViewState) { #[derive(Default)]
egui::Window::new("Demangle").open(&mut view_state.show_demangle).show(ctx, |ui| { pub struct DemangleViewState {
ui.text_edit_singleline(&mut view_state.demangle_text); pub text: String,
}
pub fn demangle_window(
ctx: &egui::Context,
show: &mut bool,
state: &mut DemangleViewState,
appearance: &Appearance,
) {
egui::Window::new("Demangle").open(show).show(ctx, |ui| {
ui.text_edit_singleline(&mut state.text);
ui.add_space(10.0); ui.add_space(10.0);
if let Some(demangled) = if let Some(demangled) = cwdemangle::demangle(&state.text, &Default::default()) {
cwdemangle::demangle(&view_state.demangle_text, &Default::default())
{
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(TextStyle::Monospace); ui.style_mut().override_text_style = Some(TextStyle::Monospace);
ui.colored_label(view_state.view_config.replace_color, &demangled); ui.colored_label(appearance.replace_color, &demangled);
}); });
if ui.button("Copy").clicked() { if ui.button("Copy").clicked() {
ui.output_mut(|output| output.copied_text = demangled); ui.output_mut(|output| output.copied_text = demangled);
@ -19,7 +27,7 @@ pub fn demangle_window(ctx: &egui::Context, view_state: &mut ViewState) {
} else { } else {
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(TextStyle::Monospace); ui.style_mut().override_text_style = Some(TextStyle::Monospace);
ui.colored_label(view_state.view_config.replace_color, "[invalid]"); ui.colored_label(appearance.replace_color, "[invalid]");
}); });
} }
}); });

View File

@ -8,13 +8,16 @@ use ppc750cl::Argument;
use time::format_description; use time::format_description;
use crate::{ use crate::{
app::{SymbolReference, View, ViewConfig, ViewState}, jobs::{Job, JobQueue},
jobs::Job,
obj::{ obj::{
ObjInfo, ObjIns, ObjInsArg, ObjInsArgDiff, ObjInsDiff, ObjInsDiffKind, ObjReloc, ObjInfo, ObjIns, ObjInsArg, ObjInsArgDiff, ObjInsDiff, ObjInsDiffKind, ObjReloc,
ObjRelocKind, ObjSymbol, ObjRelocKind, ObjSymbol,
}, },
views::{symbol_diff::match_color_for_symbol, write_text}, views::{
appearance::Appearance,
symbol_diff::{match_color_for_symbol, DiffViewState, SymbolReference, View},
write_text,
},
}; };
fn write_reloc_name( fn write_reloc_name(
@ -22,10 +25,10 @@ fn write_reloc_name(
color: Color32, color: Color32,
job: &mut LayoutJob, job: &mut LayoutJob,
font_id: FontId, font_id: FontId,
config: &ViewConfig, appearance: &Appearance,
) { ) {
let name = reloc.target.demangled_name.as_ref().unwrap_or(&reloc.target.name); let name = reloc.target.demangled_name.as_ref().unwrap_or(&reloc.target.name);
write_text(name, config.emphasized_text_color, job, font_id.clone()); write_text(name, appearance.emphasized_text_color, job, font_id.clone());
match reloc.target.addend.cmp(&0i64) { match reloc.target.addend.cmp(&0i64) {
Ordering::Greater => { Ordering::Greater => {
write_text(&format!("+{:#X}", reloc.target.addend), color, job, font_id) write_text(&format!("+{:#X}", reloc.target.addend), color, job, font_id)
@ -42,52 +45,52 @@ fn write_reloc(
color: Color32, color: Color32,
job: &mut LayoutJob, job: &mut LayoutJob,
font_id: FontId, font_id: FontId,
config: &ViewConfig, appearance: &Appearance,
) { ) {
match reloc.kind { match reloc.kind {
ObjRelocKind::PpcAddr16Lo => { ObjRelocKind::PpcAddr16Lo => {
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text("@l", color, job, font_id); write_text("@l", color, job, font_id);
} }
ObjRelocKind::PpcAddr16Hi => { ObjRelocKind::PpcAddr16Hi => {
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text("@h", color, job, font_id); write_text("@h", color, job, font_id);
} }
ObjRelocKind::PpcAddr16Ha => { ObjRelocKind::PpcAddr16Ha => {
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text("@ha", color, job, font_id); write_text("@ha", color, job, font_id);
} }
ObjRelocKind::PpcEmbSda21 => { ObjRelocKind::PpcEmbSda21 => {
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text("@sda21", color, job, font_id); write_text("@sda21", color, job, font_id);
} }
ObjRelocKind::MipsHi16 => { ObjRelocKind::MipsHi16 => {
write_text("%hi(", color, job, font_id.clone()); write_text("%hi(", color, job, font_id.clone());
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text(")", color, job, font_id); write_text(")", color, job, font_id);
} }
ObjRelocKind::MipsLo16 => { ObjRelocKind::MipsLo16 => {
write_text("%lo(", color, job, font_id.clone()); write_text("%lo(", color, job, font_id.clone());
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text(")", color, job, font_id); write_text(")", color, job, font_id);
} }
ObjRelocKind::MipsGot16 => { ObjRelocKind::MipsGot16 => {
write_text("%got(", color, job, font_id.clone()); write_text("%got(", color, job, font_id.clone());
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text(")", color, job, font_id); write_text(")", color, job, font_id);
} }
ObjRelocKind::MipsCall16 => { ObjRelocKind::MipsCall16 => {
write_text("%call16(", color, job, font_id.clone()); write_text("%call16(", color, job, font_id.clone());
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text(")", color, job, font_id); write_text(")", color, job, font_id);
} }
ObjRelocKind::MipsGpRel16 => { ObjRelocKind::MipsGpRel16 => {
write_text("%gp_rel(", color, job, font_id.clone()); write_text("%gp_rel(", color, job, font_id.clone());
write_reloc_name(reloc, color, job, font_id.clone(), config); write_reloc_name(reloc, color, job, font_id.clone(), appearance);
write_text(")", color, job, font_id); write_text(")", color, job, font_id);
} }
ObjRelocKind::PpcRel24 | ObjRelocKind::PpcRel14 | ObjRelocKind::Mips26 => { ObjRelocKind::PpcRel24 | ObjRelocKind::PpcRel14 | ObjRelocKind::Mips26 => {
write_reloc_name(reloc, color, job, font_id, config); write_reloc_name(reloc, color, job, font_id, appearance);
} }
ObjRelocKind::Absolute | ObjRelocKind::MipsGpRel32 => { ObjRelocKind::Absolute | ObjRelocKind::MipsGpRel32 => {
write_text("[INVALID]", color, job, font_id); write_text("[INVALID]", color, job, font_id);
@ -101,51 +104,51 @@ fn write_ins(
args: &[Option<ObjInsArgDiff>], args: &[Option<ObjInsArgDiff>],
base_addr: u32, base_addr: u32,
job: &mut LayoutJob, job: &mut LayoutJob,
config: &ViewConfig, appearance: &Appearance,
) { ) {
let base_color = match diff_kind { let base_color = match diff_kind {
ObjInsDiffKind::None | ObjInsDiffKind::OpMismatch | ObjInsDiffKind::ArgMismatch => { ObjInsDiffKind::None | ObjInsDiffKind::OpMismatch | ObjInsDiffKind::ArgMismatch => {
config.text_color appearance.text_color
} }
ObjInsDiffKind::Replace => config.replace_color, ObjInsDiffKind::Replace => appearance.replace_color,
ObjInsDiffKind::Delete => config.delete_color, ObjInsDiffKind::Delete => appearance.delete_color,
ObjInsDiffKind::Insert => config.insert_color, ObjInsDiffKind::Insert => appearance.insert_color,
}; };
write_text( write_text(
&format!("{:<11}", ins.mnemonic), &format!("{:<11}", ins.mnemonic),
match diff_kind { match diff_kind {
ObjInsDiffKind::OpMismatch => config.replace_color, ObjInsDiffKind::OpMismatch => appearance.replace_color,
_ => base_color, _ => base_color,
}, },
job, job,
config.code_font.clone(), appearance.code_font.clone(),
); );
let mut writing_offset = false; let mut writing_offset = false;
for (i, arg) in ins.args.iter().enumerate() { for (i, arg) in ins.args.iter().enumerate() {
if i == 0 { if i == 0 {
write_text(" ", base_color, job, config.code_font.clone()); write_text(" ", base_color, job, appearance.code_font.clone());
} }
if i > 0 && !writing_offset { if i > 0 && !writing_offset {
write_text(", ", base_color, job, config.code_font.clone()); write_text(", ", base_color, job, appearance.code_font.clone());
} }
let color = if let Some(diff) = args.get(i).and_then(|a| a.as_ref()) { let color = if let Some(diff) = args.get(i).and_then(|a| a.as_ref()) {
config.diff_colors[diff.idx % config.diff_colors.len()] appearance.diff_colors[diff.idx % appearance.diff_colors.len()]
} else { } else {
base_color base_color
}; };
match arg { match arg {
ObjInsArg::PpcArg(arg) => match arg { ObjInsArg::PpcArg(arg) => match arg {
Argument::Offset(val) => { Argument::Offset(val) => {
write_text(&format!("{val}"), color, job, config.code_font.clone()); write_text(&format!("{val}"), color, job, appearance.code_font.clone());
write_text("(", base_color, job, config.code_font.clone()); write_text("(", base_color, job, appearance.code_font.clone());
writing_offset = true; writing_offset = true;
continue; continue;
} }
Argument::Uimm(_) | Argument::Simm(_) => { Argument::Uimm(_) | Argument::Simm(_) => {
write_text(&format!("{arg}"), color, job, config.code_font.clone()); write_text(&format!("{arg}"), color, job, appearance.code_font.clone());
} }
_ => { _ => {
write_text(&format!("{arg}"), color, job, config.code_font.clone()); write_text(&format!("{arg}"), color, job, appearance.code_font.clone());
} }
}, },
ObjInsArg::Reloc => { ObjInsArg::Reloc => {
@ -153,8 +156,8 @@ fn write_ins(
ins.reloc.as_ref().unwrap(), ins.reloc.as_ref().unwrap(),
base_color, base_color,
job, job,
config.code_font.clone(), appearance.code_font.clone(),
config, appearance,
); );
} }
ObjInsArg::RelocWithBase => { ObjInsArg::RelocWithBase => {
@ -162,10 +165,10 @@ fn write_ins(
ins.reloc.as_ref().unwrap(), ins.reloc.as_ref().unwrap(),
base_color, base_color,
job, job,
config.code_font.clone(), appearance.code_font.clone(),
config, appearance,
); );
write_text("(", base_color, job, config.code_font.clone()); write_text("(", base_color, job, appearance.code_font.clone());
writing_offset = true; writing_offset = true;
continue; continue;
} }
@ -174,7 +177,7 @@ fn write_ins(
str.strip_prefix('$').unwrap_or(str), str.strip_prefix('$').unwrap_or(str),
color, color,
job, job,
config.code_font.clone(), appearance.code_font.clone(),
); );
} }
ObjInsArg::MipsArgWithBase(str) => { ObjInsArg::MipsArgWithBase(str) => {
@ -182,25 +185,25 @@ fn write_ins(
str.strip_prefix('$').unwrap_or(str), str.strip_prefix('$').unwrap_or(str),
color, color,
job, job,
config.code_font.clone(), appearance.code_font.clone(),
); );
write_text("(", base_color, job, config.code_font.clone()); write_text("(", base_color, job, appearance.code_font.clone());
writing_offset = true; writing_offset = true;
continue; continue;
} }
ObjInsArg::BranchOffset(offset) => { ObjInsArg::BranchOffset(offset) => {
let addr = offset + ins.address as i32 - base_addr as i32; let addr = offset + ins.address as i32 - base_addr as i32;
write_text(&format!("{addr:x}"), color, job, config.code_font.clone()); write_text(&format!("{addr:x}"), color, job, appearance.code_font.clone());
} }
} }
if writing_offset { if writing_offset {
write_text(")", base_color, job, config.code_font.clone()); write_text(")", base_color, job, appearance.code_font.clone());
writing_offset = false; writing_offset = false;
} }
} }
} }
fn ins_hover_ui(ui: &mut egui::Ui, ins: &ObjIns, config: &ViewConfig) { fn ins_hover_ui(ui: &mut egui::Ui, ins: &ObjIns, appearance: &Appearance) {
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
@ -226,16 +229,19 @@ fn ins_hover_ui(ui: &mut egui::Ui, ins: &ObjIns, config: &ViewConfig) {
if let Some(reloc) = &ins.reloc { if let Some(reloc) = &ins.reloc {
ui.label(format!("Relocation type: {:?}", reloc.kind)); ui.label(format!("Relocation type: {:?}", reloc.kind));
ui.colored_label(config.highlight_color, format!("Name: {}", reloc.target.name)); ui.colored_label(appearance.highlight_color, format!("Name: {}", reloc.target.name));
if let Some(section) = &reloc.target_section { if let Some(section) = &reloc.target_section {
ui.colored_label(config.highlight_color, format!("Section: {section}")); ui.colored_label(appearance.highlight_color, format!("Section: {section}"));
ui.colored_label( ui.colored_label(
config.highlight_color, appearance.highlight_color,
format!("Address: {:x}", reloc.target.address), format!("Address: {:x}", reloc.target.address),
); );
ui.colored_label(config.highlight_color, format!("Size: {:x}", reloc.target.size)); ui.colored_label(
appearance.highlight_color,
format!("Size: {:x}", reloc.target.size),
);
} else { } else {
ui.colored_label(config.highlight_color, "Extern".to_string()); ui.colored_label(appearance.highlight_color, "Extern".to_string());
} }
} }
}); });
@ -306,7 +312,12 @@ fn find_symbol<'a>(obj: &'a ObjInfo, selected_symbol: &SymbolReference) -> Optio
}) })
} }
fn asm_row_ui(ui: &mut egui::Ui, ins_diff: &ObjInsDiff, symbol: &ObjSymbol, config: &ViewConfig) { fn asm_row_ui(
ui: &mut egui::Ui,
ins_diff: &ObjInsDiff,
symbol: &ObjSymbol,
appearance: &Appearance,
) {
if ins_diff.kind != ObjInsDiffKind::None { if ins_diff.kind != ObjInsDiffKind::None {
ui.painter().rect_filled(ui.available_rect_before_wrap(), 0.0, ui.visuals().faint_bg_color); ui.painter().rect_filled(ui.available_rect_before_wrap(), 0.0, ui.visuals().faint_bg_color);
} }
@ -318,45 +329,50 @@ fn asm_row_ui(ui: &mut egui::Ui, ins_diff: &ObjInsDiff, symbol: &ObjSymbol, conf
let base_color = match ins_diff.kind { let base_color = match ins_diff.kind {
ObjInsDiffKind::None | ObjInsDiffKind::OpMismatch | ObjInsDiffKind::ArgMismatch => { ObjInsDiffKind::None | ObjInsDiffKind::OpMismatch | ObjInsDiffKind::ArgMismatch => {
config.text_color appearance.text_color
} }
ObjInsDiffKind::Replace => config.replace_color, ObjInsDiffKind::Replace => appearance.replace_color,
ObjInsDiffKind::Delete => config.delete_color, ObjInsDiffKind::Delete => appearance.delete_color,
ObjInsDiffKind::Insert => config.insert_color, ObjInsDiffKind::Insert => appearance.insert_color,
}; };
let mut pad = 6; let mut pad = 6;
if let Some(line) = ins.line { if let Some(line) = ins.line {
let line_str = format!("{line} "); let line_str = format!("{line} ");
write_text(&line_str, config.deemphasized_text_color, &mut job, config.code_font.clone()); write_text(
&line_str,
appearance.deemphasized_text_color,
&mut job,
appearance.code_font.clone(),
);
pad = 12 - line_str.len(); pad = 12 - line_str.len();
} }
write_text( write_text(
&format!("{:<1$}", format!("{:x}: ", ins.address - symbol.address as u32), pad), &format!("{:<1$}", format!("{:x}: ", ins.address - symbol.address as u32), pad),
base_color, base_color,
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
if let Some(branch) = &ins_diff.branch_from { if let Some(branch) = &ins_diff.branch_from {
write_text( write_text(
"~> ", "~> ",
config.diff_colors[branch.branch_idx % config.diff_colors.len()], appearance.diff_colors[branch.branch_idx % appearance.diff_colors.len()],
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
} else { } else {
write_text(" ", base_color, &mut job, config.code_font.clone()); write_text(" ", base_color, &mut job, appearance.code_font.clone());
} }
write_ins(ins, &ins_diff.kind, &ins_diff.arg_diff, symbol.address as u32, &mut job, config); write_ins(ins, &ins_diff.kind, &ins_diff.arg_diff, symbol.address as u32, &mut job, appearance);
if let Some(branch) = &ins_diff.branch_to { if let Some(branch) = &ins_diff.branch_to {
write_text( write_text(
" ~>", " ~>",
config.diff_colors[branch.branch_idx % config.diff_colors.len()], appearance.diff_colors[branch.branch_idx % appearance.diff_colors.len()],
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
} }
ui.add(Label::new(job).sense(Sense::click())) ui.add(Label::new(job).sense(Sense::click()))
.on_hover_ui_at_pointer(|ui| ins_hover_ui(ui, ins, config)) .on_hover_ui_at_pointer(|ui| ins_hover_ui(ui, ins, appearance))
.context_menu(|ui| ins_context_menu(ui, ins)); .context_menu(|ui| ins_context_menu(ui, ins));
} }
@ -365,21 +381,21 @@ fn asm_table_ui(
left_obj: &ObjInfo, left_obj: &ObjInfo,
right_obj: &ObjInfo, right_obj: &ObjInfo,
selected_symbol: &SymbolReference, selected_symbol: &SymbolReference,
config: &ViewConfig, appearance: &Appearance,
) -> Option<()> { ) -> Option<()> {
let left_symbol = find_symbol(left_obj, selected_symbol); let left_symbol = find_symbol(left_obj, selected_symbol);
let right_symbol = find_symbol(right_obj, selected_symbol); let right_symbol = find_symbol(right_obj, selected_symbol);
let instructions_len = left_symbol.or(right_symbol).map(|s| s.instructions.len())?; let instructions_len = left_symbol.or(right_symbol).map(|s| s.instructions.len())?;
table.body(|body| { table.body(|body| {
body.rows(config.code_font.size, instructions_len, |row_index, mut row| { body.rows(appearance.code_font.size, instructions_len, |row_index, mut row| {
row.col(|ui| { row.col(|ui| {
if let Some(symbol) = left_symbol { if let Some(symbol) = left_symbol {
asm_row_ui(ui, &symbol.instructions[row_index], symbol, config); asm_row_ui(ui, &symbol.instructions[row_index], symbol, appearance);
} }
}); });
row.col(|ui| { row.col(|ui| {
if let Some(symbol) = right_symbol { if let Some(symbol) = right_symbol {
asm_row_ui(ui, &symbol.instructions[row_index], symbol, config); asm_row_ui(ui, &symbol.instructions[row_index], symbol, appearance);
} }
}); });
}); });
@ -387,10 +403,14 @@ fn asm_table_ui(
Some(()) Some(())
} }
pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool { pub fn function_diff_ui(
ui: &mut egui::Ui,
jobs: &JobQueue,
state: &mut DiffViewState,
appearance: &Appearance,
) -> bool {
let mut rebuild = false; let mut rebuild = false;
let (Some(result), Some(selected_symbol)) = (&view_state.build, &view_state.selected_symbol) let (Some(result), Some(selected_symbol)) = (&state.build, &state.selected_symbol) else {
else {
return rebuild; return rebuild;
}; };
@ -409,15 +429,15 @@ pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.set_width(column_width); ui.set_width(column_width);
if ui.button("Back").clicked() { if ui.button("Back").clicked() {
view_state.current_view = View::SymbolDiff; state.current_view = View::SymbolDiff;
} }
let demangled = demangle(&selected_symbol.symbol_name, &Default::default()); let demangled = demangle(&selected_symbol.symbol_name, &Default::default());
let name = demangled.as_deref().unwrap_or(&selected_symbol.symbol_name); let name = demangled.as_deref().unwrap_or(&selected_symbol.symbol_name);
let mut job = LayoutJob::simple( let mut job = LayoutJob::simple(
name.to_string(), name.to_string(),
view_state.view_config.code_font.clone(), appearance.code_font.clone(),
view_state.view_config.highlight_color, appearance.highlight_color,
column_width, column_width,
); );
job.wrap.break_anywhere = true; job.wrap.break_anywhere = true;
@ -445,8 +465,8 @@ pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
if view_state.jobs.is_running(Job::ObjDiff) { if jobs.is_running(Job::ObjDiff) {
ui.colored_label(view_state.view_config.replace_color, "Building…"); ui.colored_label(appearance.replace_color, "Building…");
} else { } else {
ui.label("Last built:"); ui.label("Last built:");
let format = let format =
@ -454,7 +474,7 @@ pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.label( ui.label(
result result
.time .time
.to_offset(view_state.utc_offset) .to_offset(appearance.utc_offset)
.format(&format) .format(&format)
.unwrap(), .unwrap(),
); );
@ -471,7 +491,7 @@ pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
.and_then(|symbol| symbol.match_percent) .and_then(|symbol| symbol.match_percent)
{ {
ui.colored_label( ui.colored_label(
match_color_for_symbol(match_percent, &view_state.view_config), match_color_for_symbol(match_percent, appearance),
&format!("{match_percent:.0}%"), &format!("{match_percent:.0}%"),
); );
} else { } else {
@ -495,7 +515,7 @@ pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
.resizable(false) .resizable(false)
.auto_shrink([false, false]) .auto_shrink([false, false])
.min_scrolled_height(available_height); .min_scrolled_height(available_height);
asm_table_ui(table, left_obj, right_obj, selected_symbol, &view_state.view_config); asm_table_ui(table, left_obj, right_obj, selected_symbol, appearance);
} }
rebuild rebuild
} }

View File

@ -1,12 +1,12 @@
use egui::{ProgressBar, Widget}; use egui::{ProgressBar, Widget};
use crate::app::ViewState; use crate::{jobs::JobQueue, views::appearance::Appearance};
pub fn jobs_ui(ui: &mut egui::Ui, view_state: &mut ViewState) { pub fn jobs_ui(ui: &mut egui::Ui, jobs: &mut JobQueue, appearance: &Appearance) {
ui.label("Jobs"); ui.label("Jobs");
let mut remove_job: Option<usize> = None; let mut remove_job: Option<usize> = None;
for job in view_state.jobs.iter_mut() { for job in jobs.iter_mut() {
let Ok(status) = job.status.read() else { let Ok(status) = job.status.read() else {
continue; continue;
}; };
@ -33,7 +33,7 @@ pub fn jobs_ui(ui: &mut egui::Ui, view_state: &mut ViewState) {
if let Some(err) = &status.error { if let Some(err) = &status.error {
let err_string = err.to_string(); let err_string = err.to_string();
ui.colored_label( ui.colored_label(
view_state.view_config.delete_color, appearance.delete_color,
if err_string.len() > STATUS_LENGTH - 10 { if err_string.len() > STATUS_LENGTH - 10 {
format!("Error: {}", &err_string[0..STATUS_LENGTH - 10]) format!("Error: {}", &err_string[0..STATUS_LENGTH - 10])
} else { } else {
@ -51,6 +51,6 @@ pub fn jobs_ui(ui: &mut egui::Ui, view_state: &mut ViewState) {
} }
if let Some(idx) = remove_job { if let Some(idx) = remove_job {
view_state.jobs.remove(idx); jobs.remove(idx);
} }
} }

View File

@ -8,6 +8,7 @@ pub(crate) mod function_diff;
pub(crate) mod jobs; pub(crate) mod jobs;
pub(crate) mod symbol_diff; pub(crate) mod symbol_diff;
#[inline]
fn write_text(str: &str, color: Color32, job: &mut LayoutJob, font_id: FontId) { fn write_text(str: &str, color: Color32, job: &mut LayoutJob, font_id: FontId) {
job.append(str, 0.0, TextFormat::simple(font_id, color)); job.append(str, 0.0, TextFormat::simple(font_id, color));
} }

View File

@ -5,19 +5,41 @@ use egui::{
use egui_extras::{Size, StripBuilder}; use egui_extras::{Size, StripBuilder};
use crate::{ use crate::{
app::{SymbolReference, View, ViewConfig, ViewState}, jobs::objdiff::{BuildStatus, ObjDiffResult},
jobs::objdiff::BuildStatus,
obj::{ObjInfo, ObjSection, ObjSectionKind, ObjSymbol, ObjSymbolFlags}, obj::{ObjInfo, ObjSection, ObjSectionKind, ObjSymbol, ObjSymbolFlags},
views::write_text, views::{appearance::Appearance, write_text},
}; };
pub fn match_color_for_symbol(match_percent: f32, config: &ViewConfig) -> Color32 { pub struct SymbolReference {
pub symbol_name: String,
pub section_name: String,
}
#[allow(clippy::enum_variant_names)]
#[derive(Default, Eq, PartialEq)]
pub enum View {
#[default]
SymbolDiff,
FunctionDiff,
DataDiff,
}
#[derive(Default)]
pub struct DiffViewState {
pub build: Option<Box<ObjDiffResult>>,
pub current_view: View,
pub highlighted_symbol: Option<String>,
pub selected_symbol: Option<SymbolReference>,
pub search: String,
}
pub fn match_color_for_symbol(match_percent: f32, appearance: &Appearance) -> Color32 {
if match_percent == 100.0 { if match_percent == 100.0 {
config.insert_color appearance.insert_color
} else if match_percent >= 50.0 { } else if match_percent >= 50.0 {
config.replace_color appearance.replace_color
} else { } else {
config.delete_color appearance.delete_color
} }
} }
@ -39,17 +61,20 @@ fn symbol_context_menu_ui(ui: &mut Ui, symbol: &ObjSymbol) {
}); });
} }
fn symbol_hover_ui(ui: &mut Ui, symbol: &ObjSymbol, config: &ViewConfig) { fn symbol_hover_ui(ui: &mut Ui, symbol: &ObjSymbol, appearance: &Appearance) {
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.colored_label(config.highlight_color, format!("Name: {}", symbol.name)); ui.colored_label(appearance.highlight_color, format!("Name: {}", symbol.name));
ui.colored_label(config.highlight_color, format!("Address: {:x}", symbol.address)); ui.colored_label(appearance.highlight_color, format!("Address: {:x}", symbol.address));
if symbol.size_known { if symbol.size_known {
ui.colored_label(config.highlight_color, format!("Size: {:x}", symbol.size)); ui.colored_label(appearance.highlight_color, format!("Size: {:x}", symbol.size));
} else { } else {
ui.colored_label(config.highlight_color, format!("Size: {:x} (assumed)", symbol.size)); ui.colored_label(
appearance.highlight_color,
format!("Size: {:x} (assumed)", symbol.size),
);
} }
}); });
} }
@ -61,7 +86,7 @@ fn symbol_ui(
highlighted_symbol: &mut Option<String>, highlighted_symbol: &mut Option<String>,
selected_symbol: &mut Option<SymbolReference>, selected_symbol: &mut Option<SymbolReference>,
current_view: &mut View, current_view: &mut View,
config: &ViewConfig, appearance: &Appearance,
) { ) {
let mut job = LayoutJob::default(); let mut job = LayoutJob::default();
let name: &str = let name: &str =
@ -70,38 +95,38 @@ fn symbol_ui(
if let Some(sym) = highlighted_symbol { if let Some(sym) = highlighted_symbol {
selected = sym == &symbol.name; selected = sym == &symbol.name;
} }
write_text("[", config.text_color, &mut job, config.code_font.clone()); write_text("[", appearance.text_color, &mut job, appearance.code_font.clone());
if symbol.flags.0.contains(ObjSymbolFlags::Common) { if symbol.flags.0.contains(ObjSymbolFlags::Common) {
write_text( write_text(
"c", "c",
config.replace_color, /* Color32::from_rgb(0, 255, 255) */ appearance.replace_color, /* Color32::from_rgb(0, 255, 255) */
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
} else if symbol.flags.0.contains(ObjSymbolFlags::Global) { } else if symbol.flags.0.contains(ObjSymbolFlags::Global) {
write_text("g", config.insert_color, &mut job, config.code_font.clone()); write_text("g", appearance.insert_color, &mut job, appearance.code_font.clone());
} else if symbol.flags.0.contains(ObjSymbolFlags::Local) { } else if symbol.flags.0.contains(ObjSymbolFlags::Local) {
write_text("l", config.text_color, &mut job, config.code_font.clone()); write_text("l", appearance.text_color, &mut job, appearance.code_font.clone());
} }
if symbol.flags.0.contains(ObjSymbolFlags::Weak) { if symbol.flags.0.contains(ObjSymbolFlags::Weak) {
write_text("w", config.text_color, &mut job, config.code_font.clone()); write_text("w", appearance.text_color, &mut job, appearance.code_font.clone());
} }
write_text("] ", config.text_color, &mut job, config.code_font.clone()); write_text("] ", appearance.text_color, &mut job, appearance.code_font.clone());
if let Some(match_percent) = symbol.match_percent { if let Some(match_percent) = symbol.match_percent {
write_text("(", config.text_color, &mut job, config.code_font.clone()); write_text("(", appearance.text_color, &mut job, appearance.code_font.clone());
write_text( write_text(
&format!("{match_percent:.0}%"), &format!("{match_percent:.0}%"),
match_color_for_symbol(match_percent, config), match_color_for_symbol(match_percent, appearance),
&mut job, &mut job,
config.code_font.clone(), appearance.code_font.clone(),
); );
write_text(") ", config.text_color, &mut job, config.code_font.clone()); write_text(") ", appearance.text_color, &mut job, appearance.code_font.clone());
} }
write_text(name, config.highlight_color, &mut job, config.code_font.clone()); write_text(name, appearance.highlight_color, &mut job, appearance.code_font.clone());
let response = SelectableLabel::new(selected, job) let response = SelectableLabel::new(selected, job)
.ui(ui) .ui(ui)
.context_menu(|ui| symbol_context_menu_ui(ui, symbol)) .context_menu(|ui| symbol_context_menu_ui(ui, symbol))
.on_hover_ui_at_pointer(|ui| symbol_hover_ui(ui, symbol, config)); .on_hover_ui_at_pointer(|ui| symbol_hover_ui(ui, symbol, appearance));
if response.clicked() { if response.clicked() {
if let Some(section) = section { if let Some(section) = section {
if section.kind == ObjSectionKind::Code { if section.kind == ObjSectionKind::Code {
@ -141,7 +166,7 @@ fn symbol_list_ui(
selected_symbol: &mut Option<SymbolReference>, selected_symbol: &mut Option<SymbolReference>,
current_view: &mut View, current_view: &mut View,
lower_search: &str, lower_search: &str,
config: &ViewConfig, appearance: &Appearance,
) { ) {
ScrollArea::both().auto_shrink([false, false]).show(ui, |ui| { ScrollArea::both().auto_shrink([false, false]).show(ui, |ui| {
ui.scope(|ui| { ui.scope(|ui| {
@ -158,7 +183,7 @@ fn symbol_list_ui(
highlighted_symbol, highlighted_symbol,
selected_symbol, selected_symbol,
current_view, current_view,
config, appearance,
); );
} }
}); });
@ -168,7 +193,7 @@ fn symbol_list_ui(
CollapsingHeader::new(format!("{} ({:x})", section.name, section.size)) CollapsingHeader::new(format!("{} ({:x})", section.name, section.size))
.default_open(true) .default_open(true)
.show(ui, |ui| { .show(ui, |ui| {
if section.kind == ObjSectionKind::Code && config.reverse_fn_order { if section.kind == ObjSectionKind::Code && appearance.reverse_fn_order {
for symbol in section.symbols.iter().rev() { for symbol in section.symbols.iter().rev() {
if !symbol_matches_search(symbol, lower_search) { if !symbol_matches_search(symbol, lower_search) {
continue; continue;
@ -180,7 +205,7 @@ fn symbol_list_ui(
highlighted_symbol, highlighted_symbol,
selected_symbol, selected_symbol,
current_view, current_view,
config, appearance,
); );
} }
} else { } else {
@ -195,7 +220,7 @@ fn symbol_list_ui(
highlighted_symbol, highlighted_symbol,
selected_symbol, selected_symbol,
current_view, current_view,
config, appearance,
); );
} }
} }
@ -205,25 +230,20 @@ fn symbol_list_ui(
}); });
} }
fn build_log_ui(ui: &mut Ui, status: &BuildStatus, config: &ViewConfig) { fn build_log_ui(ui: &mut Ui, status: &BuildStatus, appearance: &Appearance) {
ScrollArea::both().auto_shrink([false, false]).show(ui, |ui| { ScrollArea::both().auto_shrink([false, false]).show(ui, |ui| {
ui.scope(|ui| { ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.colored_label(config.replace_color, &status.log); ui.colored_label(appearance.replace_color, &status.log);
}); });
}); });
} }
pub fn symbol_diff_ui(ui: &mut Ui, view_state: &mut ViewState) { pub fn symbol_diff_ui(ui: &mut Ui, state: &mut DiffViewState, appearance: &Appearance) {
let (Some(result), highlighted_symbol, selected_symbol, current_view, search) = ( let DiffViewState { build, current_view, highlighted_symbol, selected_symbol, search } = state;
&view_state.build, let Some(result) = build else {
&mut view_state.highlighted_symbol,
&mut view_state.selected_symbol,
&mut view_state.current_view,
&mut view_state.search,
) else {
return; return;
}; };
@ -297,11 +317,11 @@ pub fn symbol_diff_ui(ui: &mut Ui, view_state: &mut ViewState) {
selected_symbol, selected_symbol,
current_view, current_view,
&lower_search, &lower_search,
&view_state.view_config, appearance,
); );
} }
} else { } else {
build_log_ui(ui, &result.first_status, &view_state.view_config); build_log_ui(ui, &result.first_status, appearance);
} }
}); });
}); });
@ -316,11 +336,11 @@ pub fn symbol_diff_ui(ui: &mut Ui, view_state: &mut ViewState) {
selected_symbol, selected_symbol,
current_view, current_view,
&lower_search, &lower_search,
&view_state.view_config, appearance,
); );
} }
} else { } else {
build_log_ui(ui, &result.second_status, &view_state.view_config); build_log_ui(ui, &result.second_status, appearance);
} }
}); });
}); });