Support dark mode highlighting

This commit is contained in:
Mike Dilger 2023-01-19 17:08:34 +13:00
parent 9642c6a4da
commit f2c1c01b96
4 changed files with 51 additions and 27 deletions

View File

@ -1,8 +1,5 @@
use crate::db::DbRelay; use crate::db::DbRelay;
use eframe::epaint::{ use eframe::epaint::text::LayoutJob;
text::{LayoutJob, TextFormat},
Color32, FontFamily, FontId,
};
use memoize::memoize; use memoize::memoize;
use nostr_types::{Id, PublicKey, PublicKeyHex, Tag}; use nostr_types::{Id, PublicKey, PublicKeyHex, Tag};
@ -92,14 +89,14 @@ pub async fn add_event_to_tags(existing_tags: &mut Vec<Tag>, added: Id, marker:
} }
} }
enum HighlightType { pub(crate) enum HighlightType {
Nothing, Nothing,
PublicKey, PublicKey,
Event, Event,
} }
#[memoize] #[memoize]
pub fn textarea_highlighter(text: String) -> LayoutJob { pub fn textarea_highlighter(text: String, dark_mode: bool) -> LayoutJob {
let mut job = LayoutJob::default(); let mut job = LayoutJob::default();
let ids = notes_from_text(&text); let ids = notes_from_text(&text);
@ -135,25 +132,7 @@ pub fn textarea_highlighter(text: String) -> LayoutJob {
job.append( job.append(
chunk, chunk,
0.0, 0.0,
match highlight { crate::ui::style::highlight_text_format(highlight, dark_mode),
HighlightType::Nothing => TextFormat {
font_id: FontId::new(12.0, FontFamily::Proportional),
color: Color32::BLACK,
..Default::default()
},
HighlightType::PublicKey => TextFormat {
font_id: FontId::new(12.0, FontFamily::Monospace),
background: Color32::LIGHT_GRAY,
color: Color32::DARK_GREEN,
..Default::default()
},
HighlightType::Event => TextFormat {
font_id: FontId::new(12.0, FontFamily::Monospace),
background: Color32::LIGHT_GRAY,
color: Color32::DARK_RED,
..Default::default()
},
},
); );
curr = index; curr = index;

View File

@ -186,7 +186,7 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
// Text area // Text area
let mut layouter = |ui: &Ui, text: &str, wrap_width: f32| { let mut layouter = |ui: &Ui, text: &str, wrap_width: f32| {
let mut layout_job = textarea_highlighter(text.to_owned()); let mut layout_job = textarea_highlighter(text.to_owned(), ui.visuals().dark_mode);
layout_job.wrap.max_width = wrap_width; layout_job.wrap.max_width = wrap_width;
ui.fonts().layout_job(layout_job) ui.fonts().layout_job(layout_job)
}; };

View File

@ -3,7 +3,7 @@ mod help;
mod people; mod people;
mod relays; mod relays;
mod settings; mod settings;
mod style; pub(crate) mod style;
mod widgets; mod widgets;
mod you; mod you;

View File

@ -1,8 +1,10 @@
use crate::tags::HighlightType;
use eframe::{egui, epaint}; use eframe::{egui, epaint};
use egui::style::{Selection, Visuals, Widgets}; use egui::style::{Selection, Visuals, Widgets};
use egui::{ use egui::{
Color32, FontData, FontDefinitions, FontFamily, FontId, FontTweak, Rounding, Stroke, TextStyle, Color32, FontData, FontDefinitions, FontFamily, FontId, FontTweak, Rounding, Stroke, TextStyle,
}; };
use epaint::text::TextFormat;
use epaint::Shadow; use epaint::Shadow;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -80,6 +82,49 @@ pub(super) fn light_mode_visuals() -> Visuals {
} }
} }
pub(crate) fn highlight_text_format(highlight_type: HighlightType, dark_mode: bool) -> TextFormat {
let main = if dark_mode {
Color32::WHITE
} else {
Color32::BLACK
};
let grey = if dark_mode {
Color32::DARK_GRAY
} else {
Color32::LIGHT_GRAY
};
let green = if dark_mode {
Color32::LIGHT_GREEN
} else {
Color32::DARK_GREEN
};
let red = if dark_mode {
Color32::LIGHT_RED
} else {
Color32::DARK_RED
};
match highlight_type {
HighlightType::Nothing => TextFormat {
font_id: FontId::new(12.0, FontFamily::Proportional),
color: main,
..Default::default()
},
HighlightType::PublicKey => TextFormat {
font_id: FontId::new(12.0, FontFamily::Monospace),
background: grey,
color: green,
..Default::default()
},
HighlightType::Event => TextFormat {
font_id: FontId::new(12.0, FontFamily::Monospace),
background: grey,
color: red,
..Default::default()
},
}
}
pub(super) fn text_styles() -> BTreeMap<TextStyle, FontId> { pub(super) fn text_styles() -> BTreeMap<TextStyle, FontId> {
let mut text_styles: BTreeMap<TextStyle, FontId> = BTreeMap::new(); let mut text_styles: BTreeMap<TextStyle, FontId> = BTreeMap::new();