feat(bar): add custom font loader

This commit is contained in:
LGUG2Z
2024-09-02 11:35:50 -07:00
parent 8e74e97706
commit fdc7706d23
3 changed files with 135 additions and 5 deletions

92
Cargo.lock generated
View File

@@ -859,6 +859,15 @@ dependencies = [
"error-code",
]
[[package]]
name = "cmake"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a"
dependencies = [
"cc",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
@@ -994,6 +1003,19 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "core-graphics"
version = "0.22.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-graphics-types",
"foreign-types 0.3.2",
"libc",
]
[[package]]
name = "core-graphics"
version = "0.23.2"
@@ -1018,6 +1040,18 @@ dependencies = [
"libc",
]
[[package]]
name = "core-text"
version = "19.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d74ada66e07c1cefa18f8abfba765b486f250de2e4a999e5727fc0dd4b4a25"
dependencies = [
"core-foundation",
"core-graphics 0.22.3",
"foreign-types 0.3.2",
"libc",
]
[[package]]
name = "cpufeatures"
version = "0.2.12"
@@ -1484,6 +1518,16 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "expat-sys"
version = "2.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658f19728920138342f68408b7cf7644d90d4784353d8ebc32e7e8663dbe45fa"
dependencies = [
"cmake",
"pkg-config",
]
[[package]]
name = "eyre"
version = "0.6.12"
@@ -1561,6 +1605,19 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "font-loader"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c49d6b4c11dca1a1dd931a34a9f397e2da91abe3de4110505f3530a80e560b52"
dependencies = [
"core-foundation",
"core-text",
"libc",
"servo-fontconfig",
"winapi",
]
[[package]]
name = "foreign-types"
version = "0.3.2"
@@ -1612,6 +1669,17 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "freetype-sys"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a37d4011c0cc628dfa766fcc195454f4b068d7afdc2adfd28861191d866e731a"
dependencies = [
"cmake",
"libc",
"pkg-config",
]
[[package]]
name = "fs-tail"
version = "0.1.4"
@@ -2388,6 +2456,7 @@ dependencies = [
"chrono",
"crossbeam-channel",
"eframe",
"font-loader",
"komorebi-client",
"netdev",
"serde_json",
@@ -4071,6 +4140,27 @@ dependencies = [
"unsafe-libyaml",
]
[[package]]
name = "servo-fontconfig"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7e3e22fe5fd73d04ebf0daa049d3efe3eae55369ce38ab16d07ddd9ac5c217c"
dependencies = [
"libc",
"servo-fontconfig-sys",
]
[[package]]
name = "servo-fontconfig-sys"
version = "5.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e36b879db9892dfa40f95da1c38a835d41634b825fbd8c4c418093d53c24b388"
dependencies = [
"expat-sys",
"freetype-sys",
"pkg-config",
]
[[package]]
name = "sha1"
version = "0.10.6"
@@ -5664,7 +5754,7 @@ dependencies = [
"calloop",
"cfg_aliases 0.1.1",
"core-foundation",
"core-graphics",
"core-graphics 0.23.2",
"cursor-icon",
"icrate",
"js-sys",

View File

@@ -16,4 +16,5 @@ serde_json = "1"
starship-battery = "0.9"
sysinfo = "0.31"
crossbeam-channel = "0.5"
windows = { workspace = true }
windows = { workspace = true }
font-loader = "0.11"

View File

@@ -32,10 +32,13 @@ use eframe::egui::ViewportBuilder;
use eframe::egui::Visuals;
use eframe::emath::Pos2;
use eframe::emath::Vec2;
use font_loader::system_fonts;
use font_loader::system_fonts::FontPropertyBuilder;
use komorebi_client::CycleDirection;
use komorebi_client::SocketMessage;
use std::io::BufReader;
use std::io::Read;
use std::ops::Deref;
use std::process::Command;
use std::sync::Arc;
use std::time::Duration;
@@ -65,7 +68,7 @@ impl From<Position> for Pos2 {
}
}
#[derive(Copy, Clone, Debug)]
#[derive(Clone, Debug)]
pub struct Config {
inner_size: Position,
position: Position,
@@ -73,6 +76,7 @@ pub struct Config {
transparent: bool,
monitor_index: usize,
monitor_work_area_offset: Option<komorebi_client::Rect>,
font_family: Option<String>,
time: Time,
date: Date,
storage: StorageConfig,
@@ -89,6 +93,7 @@ fn main() -> eframe::Result<()> {
outer_margin: Position { x: 10.0, y: 10.0 },
transparent: false,
monitor_index: 0,
font_family: Some(String::from("JetBrains Mono")),
monitor_work_area_offset: Some(komorebi_client::Rect {
left: 0,
top: 40,
@@ -217,19 +222,53 @@ struct Komobar {
network: Network,
}
fn add_custom_font(ctx: &egui::Context, name: &str) {
let mut fonts = egui::FontDefinitions::default();
let property = FontPropertyBuilder::new().family(name).build();
if let Some((font, _)) = system_fonts::get(&property) {
// Install my own font (maybe supporting non-latin characters).
// .ttf and .otf files supported.
fonts
.font_data
.insert(name.to_owned(), egui::FontData::from_owned(font));
// Put my font first (highest priority) for proportional text:
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.insert(0, name.to_owned());
// Put my font as last fallback for monospace:
fonts
.families
.entry(egui::FontFamily::Monospace)
.or_default()
.push(name.to_owned());
// Tell egui to use these fonts:
ctx.set_fonts(fonts);
}
}
impl Komobar {
fn new(
_cc: &eframe::CreationContext<'_>,
cc: &eframe::CreationContext<'_>,
rx: Receiver<komorebi_client::Notification>,
config: Arc<Config>,
) -> Self {
if let Some(font_family) = &config.font_family {
add_custom_font(&cc.egui_ctx, font_family);
}
// Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
// Restore app state using cc.storage (requires the "persistence" feature).
// Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
// for e.g. egui::PaintCallback.
Self {
config: *config,
config: config.deref().clone(),
state_receiver: rx,
selected_workspace: String::new(),
focused_window_title: String::new(),