From f2c1c01b969db60589c2a0b78ef6ab2506721eb3 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Thu, 19 Jan 2023 17:08:34 +1300 Subject: [PATCH] Support dark mode highlighting --- src/tags.rs | 29 ++++------------------------- src/ui/feed.rs | 2 +- src/ui/mod.rs | 2 +- src/ui/style.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/tags.rs b/src/tags.rs index faf301d4..95cc8d60 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -1,8 +1,5 @@ use crate::db::DbRelay; -use eframe::epaint::{ - text::{LayoutJob, TextFormat}, - Color32, FontFamily, FontId, -}; +use eframe::epaint::text::LayoutJob; use memoize::memoize; use nostr_types::{Id, PublicKey, PublicKeyHex, Tag}; @@ -92,14 +89,14 @@ pub async fn add_event_to_tags(existing_tags: &mut Vec, added: Id, marker: } } -enum HighlightType { +pub(crate) enum HighlightType { Nothing, PublicKey, Event, } #[memoize] -pub fn textarea_highlighter(text: String) -> LayoutJob { +pub fn textarea_highlighter(text: String, dark_mode: bool) -> LayoutJob { let mut job = LayoutJob::default(); let ids = notes_from_text(&text); @@ -135,25 +132,7 @@ pub fn textarea_highlighter(text: String) -> LayoutJob { job.append( chunk, 0.0, - match highlight { - 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() - }, - }, + crate::ui::style::highlight_text_format(highlight, dark_mode), ); curr = index; diff --git a/src/ui/feed.rs b/src/ui/feed.rs index f23ba343..ef75efcf 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -186,7 +186,7 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram // Text area 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; ui.fonts().layout_job(layout_job) }; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index fc181fd2..02eee995 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -3,7 +3,7 @@ mod help; mod people; mod relays; mod settings; -mod style; +pub(crate) mod style; mod widgets; mod you; diff --git a/src/ui/style.rs b/src/ui/style.rs index 8db25ed1..adfd738f 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -1,8 +1,10 @@ +use crate::tags::HighlightType; use eframe::{egui, epaint}; use egui::style::{Selection, Visuals, Widgets}; use egui::{ Color32, FontData, FontDefinitions, FontFamily, FontId, FontTweak, Rounding, Stroke, TextStyle, }; +use epaint::text::TextFormat; use epaint::Shadow; 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 { let mut text_styles: BTreeMap = BTreeMap::new();