Skip to content

Commit

Permalink
Add dark/light theme toggle (light theme WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
encounter committed Jul 15, 2023
1 parent c7a326b commit b02e32f
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "objdiff"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
rust-version = "1.65"
authors = ["Luke Street <[email protected]>"]
Expand Down
65 changes: 62 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ pub struct ViewConfig {
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 {
Expand All @@ -77,6 +92,14 @@ impl Default for ViewConfig {
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),
}
}
}
Expand Down Expand Up @@ -249,7 +272,7 @@ impl eframe::App for App {
let Self { config, view_state, .. } = self;

{
let config = &view_state.view_config;
let config = &mut view_state.view_config;
let mut style = (*ctx.style()).clone();
style.text_styles.insert(TextStyle::Body, FontId {
size: (config.ui_font.size * 0.75).floor(),
Expand All @@ -262,6 +285,28 @@ impl eframe::App for App {
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);
}

Expand Down Expand Up @@ -323,6 +368,20 @@ impl eframe::App for App {
}

egui::Window::new("Config").open(&mut view_state.show_config).show(ctx, |ui| {
egui::ComboBox::from_label("Theme")
.selected_text(format!("{:?}", view_state.view_config.theme))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut view_state.view_config.theme,
eframe::Theme::Dark,
"Dark",
);
ui.selectable_value(
&mut view_state.view_config.theme,
eframe::Theme::Light,
"Light",
);
});
ui.label("UI font:");
egui::introspection::font_id_ui(ui, &mut view_state.view_config.ui_font);
ui.separator();
Expand Down Expand Up @@ -359,15 +418,15 @@ impl eframe::App for App {
{
ui.scope(|ui| {
ui.style_mut().override_text_style = Some(TextStyle::Monospace);
ui.colored_label(Color32::LIGHT_BLUE, &demangled);
ui.colored_label(view_state.view_config.replace_color, &demangled);
});
if ui.button("Copy").clicked() {
ui.output_mut(|output| output.copied_text = demangled);
}
} else {
ui.scope(|ui| {
ui.style_mut().override_text_style = Some(TextStyle::Monospace);
ui.colored_label(Color32::LIGHT_RED, "[invalid]");
ui.colored_label(view_state.view_config.replace_color, "[invalid]");
});
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ pub fn diff_objs(left: &mut ObjInfo, right: &mut ObjInfo, _diff_config: &DiffCon
}
}
} else {
let Some(right_section) = right.sections.iter_mut().find(|s| s.name == left_section.name) else {
let Some(right_section) =
right.sections.iter_mut().find(|s| s.name == left_section.name)
else {
continue;
};
if left_section.kind == ObjSectionKind::Data {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn main() {

let exec_path: Rc<Mutex<Option<PathBuf>>> = Rc::new(Mutex::new(None));
let exec_path_clone = exec_path.clone();
let mut native_options = eframe::NativeOptions::default();
let mut native_options =
eframe::NativeOptions { follow_system_theme: false, ..Default::default() };
match load_icon() {
Ok(data) => {
native_options.icon_data = Some(data);
Expand Down
4 changes: 2 additions & 2 deletions src/views/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};
#[cfg(windows)]
use anyhow::{Context, Result};
use const_format::formatcp;
use egui::{output::OpenUrl, Color32};
use egui::output::OpenUrl;
use self_update::cargo_crate_version;

use crate::{
Expand Down Expand Up @@ -79,7 +79,7 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc<RwLock<AppConfig>>, view_state:
if let Some(state) = &view_state.check_update {
ui.label(format!("Latest version: {}", state.latest_release.version));
if state.update_available {
ui.colored_label(Color32::LIGHT_GREEN, "Update available");
ui.colored_label(view_state.view_config.insert_color, "Update available");
ui.horizontal(|ui| {
if state.found_binary
&& ui
Expand Down
34 changes: 19 additions & 15 deletions src/views/data_diff.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{cmp::min, default::Default, mem::take};

use egui::{text::LayoutJob, Align, Color32, Label, Layout, Sense, Vec2};
use egui::{text::LayoutJob, Align, Label, Layout, Sense, Vec2};
use egui_extras::{Column, TableBuilder};
use time::format_description;

use crate::{
app::{SymbolReference, View, ViewConfig, ViewState},
jobs::Job,
obj::{ObjDataDiff, ObjDataDiffKind, ObjInfo, ObjSection},
views::{write_text, COLOR_RED},
views::write_text,
};

const BYTES_PER_ROW: usize = 16;
Expand All @@ -24,17 +24,17 @@ fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], config:
let mut job = LayoutJob::default();
write_text(
format!("{address:08X}: ").as_str(),
Color32::GRAY,
config.text_color,
&mut job,
config.code_font.clone(),
);
let mut cur_addr = 0usize;
for diff in diffs {
let base_color = match diff.kind {
ObjDataDiffKind::None => Color32::GRAY,
ObjDataDiffKind::Replace => Color32::LIGHT_BLUE,
ObjDataDiffKind::Delete => COLOR_RED,
ObjDataDiffKind::Insert => Color32::GREEN,
ObjDataDiffKind::None => config.text_color,
ObjDataDiffKind::Replace => config.replace_color,
ObjDataDiffKind::Delete => config.delete_color,
ObjDataDiffKind::Insert => config.insert_color,
};
if diff.data.is_empty() {
let mut str = " ".repeat(diff.len);
Expand All @@ -58,15 +58,15 @@ fn data_row_ui(ui: &mut egui::Ui, address: usize, diffs: &[ObjDataDiff], config:
let mut str = " ".to_string();
str.push_str(" ".repeat(n).as_str());
str.push_str(" ".repeat(n / 8).as_str());
write_text(str.as_str(), Color32::GRAY, &mut job, config.code_font.clone());
write_text(str.as_str(), config.text_color, &mut job, config.code_font.clone());
}
write_text(" ", Color32::GRAY, &mut job, config.code_font.clone());
write_text(" ", config.text_color, &mut job, config.code_font.clone());
for diff in diffs {
let base_color = match diff.kind {
ObjDataDiffKind::None => Color32::GRAY,
ObjDataDiffKind::Replace => Color32::LIGHT_BLUE,
ObjDataDiffKind::Delete => COLOR_RED,
ObjDataDiffKind::Insert => Color32::GREEN,
ObjDataDiffKind::None => config.text_color,
ObjDataDiffKind::Replace => config.replace_color,
ObjDataDiffKind::Delete => config.delete_color,
ObjDataDiffKind::Insert => config.insert_color,
};
if diff.data.is_empty() {
write_text(
Expand Down Expand Up @@ -163,7 +163,8 @@ fn data_table_ui(

pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
let mut rebuild = false;
let (Some(result), Some(selected_symbol)) = (&view_state.build, &view_state.selected_symbol) else {
let (Some(result), Some(selected_symbol)) = (&view_state.build, &view_state.selected_symbol)
else {
return rebuild;
};

Expand All @@ -188,7 +189,10 @@ pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool {
ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
ui.style_mut().wrap = Some(false);
ui.colored_label(Color32::WHITE, &selected_symbol.symbol_name);
ui.colored_label(
view_state.view_config.highlight_color,
&selected_symbol.symbol_name,
);
ui.label("Diff target:");
});
},
Expand Down
Loading

0 comments on commit b02e32f

Please sign in to comment.