Skip to content

Commit

Permalink
fonts: implement roberto's typography for notedeck
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <jb55@jb55.com>
  • Loading branch information
jb55 committed Feb 16, 2024
1 parent 5ecc437 commit b0d0e45
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 34 deletions.
44 changes: 24 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::abbrev;
use crate::error::Error;
use crate::fonts::setup_gossip_fonts;
use crate::fonts::{setup_gossip_fonts, NamedFontFamily};
use crate::frame_history::FrameHistory;
use crate::images::fetch_img;
use crate::notecache::NoteCache;
Expand All @@ -12,6 +12,7 @@ use egui::containers::scroll_area::ScrollBarVisibility;
use egui::widgets::Spinner;
use egui::{
Color32, Context, Frame, Hyperlink, Image, Label, Margin, RichText, Style, TextureHandle,
Visuals,
};

use enostr::{ClientMessage, Filter, Pubkey, RelayEvent, RelayMessage};
Expand All @@ -30,6 +31,7 @@ use tracing::{debug, error, info, warn};
use enostr::RelayPool;

const PURPLE: Color32 = Color32::from_rgb(0xCC, 0x43, 0xC5);
const DARK_BG: Color32 = egui::Color32::from_rgb(40, 44, 52);

Check warning on line 34 in src/app.rs

View workflow job for this annotation

GitHub Actions / Check

constant `DARK_BG` is never used

Check failure on line 34 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

constant `DARK_BG` is never used

Check warning on line 34 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

constant `DARK_BG` is never used

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
enum UrlKey<'a> {
Expand Down Expand Up @@ -329,7 +331,6 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
#[cfg(feature = "profiling")]
setup_profiling();

setup_gossip_fonts(ctx);
damus.pool = RelayPool::new();
relay_setup(&mut damus.pool, ctx);
damus.state = DamusState::Initialized;
Expand Down Expand Up @@ -407,6 +408,10 @@ fn process_message(damus: &mut Damus, relay: &str, msg: &RelayMessage) {
}

fn render_damus(damus: &mut Damus, ctx: &Context) {
ctx.style_mut(|style| {
set_app_style(&mut style.visuals);
});

if is_mobile(ctx) {
render_damus_mobile(ctx, damus);
} else {
Expand Down Expand Up @@ -436,6 +441,8 @@ impl Damus {
//}
//

setup_gossip_fonts(&cc.egui_ctx);

cc.egui_ctx
.set_pixels_per_point(cc.egui_ctx.pixels_per_point() + 0.2);

Expand Down Expand Up @@ -531,7 +538,9 @@ fn ui_abbreviate_name(ui: &mut egui::Ui, name: &str, len: usize) {
ui.strong(&name[..closest]);
ui.strong("...");
} else {
ui.strong(name);
ui.add(Label::new(
RichText::new(name).family(NamedFontFamily::Medium.as_family()),
));
}
}

Expand Down Expand Up @@ -683,7 +692,7 @@ fn render_note_contents(
}

BlockType::Text => {
ui.weak(block.as_str());
ui.label(block.as_str());
}

_ => {
Expand All @@ -708,15 +717,12 @@ fn render_reltime(ui: &mut egui::Ui, note_cache: &mut NoteCache) {
#[cfg(feature = "profiling")]
puffin::profile_function!();

ui.add(Label::new(
RichText::new("⋅")
.size(10.0)
.color(ui.visuals().weak_text_color()),
));
let color = Color32::from_rgb(0x8A, 0x8A, 0x8A);
ui.add(Label::new(RichText::new("⋅").size(10.0).color(color)));
ui.add(Label::new(
RichText::new(note_cache.reltime_str())
.size(10.0)
.color(ui.visuals().weak_text_color()),
.color(color),
));
}

Expand Down Expand Up @@ -814,8 +820,6 @@ fn top_panel(ctx: &egui::Context) -> egui::TopBottomPanel {

fn render_panel<'a>(ctx: &egui::Context, app: &'a mut Damus, timeline_ind: usize) {
top_panel(ctx).show(ctx, |ui| {
set_app_style(ui);

ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
ui.visuals_mut().button_frame = false;
egui::widgets::global_dark_light_mode_switch(ui);
Expand Down Expand Up @@ -854,13 +858,15 @@ fn render_panel<'a>(ctx: &egui::Context, app: &'a mut Damus, timeline_ind: usize
});
}

fn set_app_style(ui: &mut egui::Ui) {
ui.visuals_mut().hyperlink_color = PURPLE;
if ui.visuals().dark_mode {
ui.visuals_mut().override_text_color = Some(egui::Color32::from_rgb(250, 250, 250));
ui.visuals_mut().panel_fill = egui::Color32::from_rgb(30, 30, 30);
fn set_app_style(visuals: &mut Visuals) {
visuals.hyperlink_color = PURPLE;
if visuals.dark_mode {
visuals.override_text_color = Some(egui::Color32::from_rgb(250, 250, 250));
visuals.panel_fill = egui::Color32::from_rgb(31, 31, 31);
//visuals.override_text_color = Some(egui::Color32::from_rgb(170, 177, 190));
//visuals.panel_fill = egui::Color32::from_rgb(40, 44, 52);
} else {
ui.visuals_mut().override_text_color = Some(egui::Color32::BLACK);
visuals.override_text_color = Some(egui::Color32::BLACK);
};
}

Expand Down Expand Up @@ -905,7 +911,6 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) {
if app.n_panels == 1 {
let panel_width = ctx.screen_rect().width();
main_panel(&ctx.style()).show(ctx, |ui| {
set_app_style(ui);
timeline_panel(ui, panel_width, 0, |ui| {
//postbox(ui, app);
timeline_view(ui, app, 0);
Expand All @@ -916,7 +921,6 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) {
}

main_panel(&ctx.style()).show(ctx, |ui| {
set_app_style(ui);
egui::ScrollArea::horizontal()
.auto_shrink([false; 2])
.show(ui, |ui| {
Expand Down
85 changes: 71 additions & 14 deletions src/fonts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
use egui::{FontData, FontDefinitions, FontFamily, FontTweak};
use egui::{FontData, FontDefinitions, FontTweak};
use std::collections::BTreeMap;
use tracing::debug;

pub enum NamedFontFamily {
Medium,
}

impl NamedFontFamily {
pub fn as_str(self) -> &'static str {
match self {
//Self::Bold => "bold",
Self::Medium => "medium",
}
}

pub fn as_family(self) -> egui::FontFamily {
egui::FontFamily::Name(self.as_str().into())
}
}

pub fn setup_fonts(ctx: &egui::Context) {
let mut fonts = FontDefinitions::default();
Expand All @@ -18,7 +36,7 @@ pub fn setup_fonts(ctx: &egui::Context) {
// Put my font first (highest priority):
fonts
.families
.get_mut(&FontFamily::Proportional)
.get_mut(&egui::FontFamily::Proportional)
.unwrap()
.insert(0, our_font);

Expand All @@ -35,10 +53,32 @@ pub fn setup_gossip_fonts(ctx: &egui::Context) {
let mut font_data: BTreeMap<String, FontData> = BTreeMap::new();
let mut families = BTreeMap::new();

font_data.insert(
"Onest".to_owned(),
FontData::from_static(include_bytes!(
"../assets/fonts/onest/OnestRegular1602-hint.ttf"
)),
);

font_data.insert(
"OnestMedium".to_owned(),
FontData::from_static(include_bytes!(
"../assets/fonts/onest/OnestMedium1602-hint.ttf"
)),
);

font_data.insert(
"DejaVuSans".to_owned(),
FontData::from_static(include_bytes!("../assets/fonts/DejaVuSansSansEmoji.ttf")),
);
/*
font_data.insert(
"OnestBold".to_owned(),
FontData::from_static(include_bytes!(
"../assets/fonts/onest/OnestBold1602-hint.ttf"
)),
);
font_data.insert(
"DejaVuSansBold".to_owned(),
FontData::from_static(include_bytes!(
Expand All @@ -47,9 +87,16 @@ pub fn setup_gossip_fonts(ctx: &egui::Context) {
);
font_data.insert(
"NotoSansCJK".to_owned(),
FontData::from_static(include_bytes!("../assets/fonts/NotoSansCJK-Regular.ttc")),
"DejaVuSans".to_owned(),
FontData::from_static(include_bytes!("../assets/fonts/DejaVuSansSansEmoji.ttf")),
);
font_data.insert(
"DejaVuSansBold".to_owned(),
FontData::from_static(include_bytes!(
"../assets/fonts/DejaVuSans-Bold-SansEmoji.ttf"
)),
);
*/

font_data.insert(
"Inconsolata".to_owned(),
Expand All @@ -63,9 +110,14 @@ pub fn setup_gossip_fonts(ctx: &egui::Context) {
),
);

font_data.insert(
"NotoSansCJK".to_owned(),
FontData::from_static(include_bytes!("../assets/fonts/NotoSansCJK-Regular.ttc")),
);

// Some good looking emojis. Use as first priority:
font_data.insert(
"NotoEmoji-Regular".to_owned(),
"NotoEmoji".to_owned(),
FontData::from_static(include_bytes!("../assets/fonts/NotoEmoji-Regular.ttf")).tweak(
FontTweak {
scale: 1.1, // make them a touch larger
Expand All @@ -76,23 +128,28 @@ pub fn setup_gossip_fonts(ctx: &egui::Context) {
),
);

let mut proportional = vec!["DejaVuSans".to_owned(), "NotoEmoji-Regular".to_owned()];
//if cfg!(feature = "lang-cjk") {
proportional.push("NotoSansCJK".to_owned());
//}
let mut proportional = vec![

Check warning on line 131 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Check

variable does not need to be mutable

Check failure on line 131 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Clippy

variable does not need to be mutable

Check warning on line 131 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable
"Onest".to_owned(),
"DejaVuSans".to_owned(),
"NotoEmoji".to_owned(),
"NotoSansCJK".to_owned(),
];

families.insert(FontFamily::Proportional, proportional);
families.insert(egui::FontFamily::Proportional, proportional);

families.insert(
FontFamily::Monospace,
vec!["Inconsolata".to_owned(), "NotoEmoji-Regular".to_owned()],
egui::FontFamily::Monospace,
vec!["Inconsolata".to_owned(), "NotoEmoji".to_owned()],
);

families.insert(
FontFamily::Name("Bold".into()),
vec!["DejaVuSansBold".to_owned()],
egui::FontFamily::Name(NamedFontFamily::Medium.as_str().into()),
//egui::FontFamily::Name("bold".into()),
vec!["OnestMedium".to_owned(), "NotoEmoji".to_owned()],
);

debug!("fonts: {:?}", families);

let defs = FontDefinitions {
font_data,
families,
Expand Down

0 comments on commit b0d0e45

Please sign in to comment.