Skip to content

Commit

Permalink
Improvements to font selection UI
Browse files Browse the repository at this point in the history
  • Loading branch information
encounter committed Nov 28, 2023
1 parent 346be6e commit c8caedc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 51 deletions.
126 changes: 77 additions & 49 deletions src/views/appearance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use egui::{Color32, FontFamily, FontId, TextStyle, Widget};
use egui::{text::LayoutJob, Color32, FontFamily, FontId, TextStyle, Widget};
use time::UtcOffset;

use crate::fonts::load_font_if_needed;
Expand Down Expand Up @@ -47,11 +47,14 @@ pub struct FontState {
// loaded_families: HashMap<String, LoadedFontFamily>,
}

const DEFAULT_UI_FONT: FontId = FontId { size: 12.0, family: FontFamily::Proportional };
const DEFAULT_CODE_FONT: FontId = FontId { size: 14.0, family: FontFamily::Monospace };

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 },
ui_font: DEFAULT_UI_FONT,
code_font: DEFAULT_CODE_FONT,
diff_colors: DEFAULT_COLOR_ROTATION.to_vec(),
theme: eframe::Theme::Dark,
text_color: Color32::GRAY,
Expand Down Expand Up @@ -126,7 +129,7 @@ impl Appearance {
ctx,
&self.fonts.source,
&next_ui_font,
FontFamily::Proportional,
DEFAULT_UI_FONT.family,
&mut self.fonts.definitions,
) {
Ok(()) => self.ui_font = next_ui_font,
Expand All @@ -140,7 +143,7 @@ impl Appearance {
ctx,
&self.fonts.source,
&next_code_font,
FontFamily::Monospace,
DEFAULT_CODE_FONT.family,
&mut self.fonts.definitions,
) {
Ok(()) => self.code_font = next_code_font,
Expand All @@ -157,28 +160,28 @@ impl Appearance {
ctx,
&self.fonts.source,
&self.ui_font,
FontFamily::Proportional,
DEFAULT_UI_FONT.family,
&mut self.fonts.definitions,
) {
Ok(_) => {}
Err(e) => {
log::error!("Failed to load font: {}", e);
// Revert to default
self.ui_font = FontId { size: 12.0, family: FontFamily::Proportional };
self.ui_font = DEFAULT_UI_FONT;
}
}
match load_font_if_needed(
ctx,
&self.fonts.source,
&self.code_font,
FontFamily::Monospace,
DEFAULT_CODE_FONT.family,
&mut self.fonts.definitions,
) {
Ok(_) => {}
Err(e) => {
log::error!("Failed to load font: {}", e);
// Revert to default
self.code_font = FontId { size: 14.0, family: FontFamily::Monospace };
self.code_font = DEFAULT_CODE_FONT;
}
}
}
Expand All @@ -196,34 +199,63 @@ pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [
Color32::from_rgb(213, 138, 138),
];

fn font_id_ui(ui: &mut egui::Ui, mut font_id: FontId, font_state: &FontState) -> Option<FontId> {
let mut changed =
egui::Slider::new(&mut font_id.size, 4.0..=40.0).max_decimals(1).ui(ui).changed();
let family = &mut font_id.family;
changed |= egui::ComboBox::from_label("Font family")
.selected_text(family.to_string())
.show_ui(ui, |ui| {
let mut result = false;
result |= ui
.selectable_value(family, FontFamily::Proportional, "Proportional (built-in)")
.changed();
result |= ui
.selectable_value(family, FontFamily::Monospace, "Monospace (built-in)")
.changed();
for family_name in &font_state.family_names {
fn font_id_ui(
ui: &mut egui::Ui,
label: &str,
mut font_id: FontId,
default: FontId,
appearance: &Appearance,
) -> Option<FontId> {
ui.push_id(label, |ui| {
let mut font_size = font_id.size;
let label_job = LayoutJob::simple(
font_id.family.to_string(),
font_id.clone(),
appearance.text_color,
0.0,
);
let mut changed = ui
.horizontal(|ui| {
ui.label(label);
let mut changed = egui::Slider::new(&mut font_id.size, 4.0..=40.0)
.max_decimals(1)
.ui(ui)
.changed();
if ui.button("Reset").clicked() {
font_id = default;
changed = true;
}
changed
})
.inner;
let family = &mut font_id.family;
changed |= egui::ComboBox::from_label("Font family")
.selected_text(label_job)
.width(font_size * 20.0)
.show_ui(ui, |ui| {
let mut result = false;
result |= ui
.selectable_value(
family,
FontFamily::Name(Arc::from(family_name.as_str())),
family_name,
)
.selectable_value(family, FontFamily::Proportional, "Proportional (built-in)")
.changed();
}
result
})
.inner
.unwrap_or(false);
changed.then_some(font_id)
result |= ui
.selectable_value(family, FontFamily::Monospace, "Monospace (built-in)")
.changed();
for family_name in &appearance.fonts.family_names {
result |= ui
.selectable_value(
family,
FontFamily::Name(Arc::from(family_name.as_str())),
family_name,
)
.changed();
}
result
})
.inner
.unwrap_or(false);
changed.then_some(font_id)
})
.inner
}

pub fn appearance_window(ctx: &egui::Context, show: &mut bool, appearance: &mut Appearance) {
Expand All @@ -234,21 +266,17 @@ pub fn appearance_window(ctx: &egui::Context, show: &mut bool, appearance: &mut
ui.selectable_value(&mut appearance.theme, eframe::Theme::Dark, "Dark");
ui.selectable_value(&mut appearance.theme, eframe::Theme::Light, "Light");
});
ui.horizontal(|ui| {
ui.label("UI font:");
ui.push_id("ui_font", |ui| {
appearance.next_ui_font =
font_id_ui(ui, appearance.ui_font.clone(), &appearance.fonts);
});
});
ui.separator();
ui.horizontal(|ui| {
ui.label("Code font:");
ui.push_id("code_font", |ui| {
appearance.next_code_font =
font_id_ui(ui, appearance.code_font.clone(), &appearance.fonts);
});
});
appearance.next_ui_font =
font_id_ui(ui, "UI font:", appearance.ui_font.clone(), DEFAULT_UI_FONT, appearance);
ui.separator();
appearance.next_code_font = font_id_ui(
ui,
"Code font:",
appearance.code_font.clone(),
DEFAULT_CODE_FONT,
appearance,
);
ui.separator();
ui.label("Diff colors:");
if ui.button("Reset").clicked() {
Expand Down
3 changes: 1 addition & 2 deletions src/views/function_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::{
};

use cwdemangle::demangle;
use eframe::emath::Align;
use egui::{text::LayoutJob, Color32, Label, Layout, RichText, Sense, TextFormat, Vec2};
use egui::{text::LayoutJob, Align, Color32, Label, Layout, RichText, Sense, TextFormat, Vec2};
use egui_extras::{Column, TableBuilder, TableRow};
use ppc750cl::Argument;
use time::format_description;
Expand Down

0 comments on commit c8caedc

Please sign in to comment.