Skip to content

Commit

Permalink
Allow custom font
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Jun 26, 2023
1 parent d756b57 commit 9ece059
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ chrono = { version = "0.4", features = ['serde'] }
fern = "0.6.1"
iced = { version = "0.9", features = ["tokio", "lazy", "advanced", "image"] }
log = "0.4.16"
once_cell = "1.18.0"
palette = "=0.7.2"
thiserror = "1.0.30"
tokio = { version = "1.0", features = ["rt", "fs", "process"] }
Expand Down
6 changes: 6 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ servers:
# Channels to join upon connecting to the server
channels:
- "##rust"

# Font settings
font:
# Specify the monospaced font family to use
# - Default is Iosevka Term and provided by this application
family: Iosevka Term

# Settings applied by default to new buffers
new_buffer:
Expand Down
8 changes: 8 additions & 0 deletions data/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ pub struct Config {
#[serde(default)]
pub palette: Palette,
pub servers: server::Map,
#[serde(default)]
pub font: Font,
/// Default settings when creating a new buffer
#[serde(default)]
pub new_buffer: buffer::Settings,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct Font {
pub family: Option<String>,
// TODO: Do we make size, etc configurable and pass to Theme?
}

impl Config {
pub fn config_dir() -> PathBuf {
let dir = environment::config_dir().join("halloy");
Expand Down
63 changes: 51 additions & 12 deletions src/font.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,60 @@
use data::Config;
use once_cell::sync::OnceCell;

use iced::font::{self, Error};
use iced::{Command, Font};
use iced::Command;

pub const MONO: Font = Font {
pub static MONO: Font = Font::new(false);
pub static MONO_BOLD: Font = Font::new(true);
pub const ICON: iced::Font = iced::Font {
monospaced: true,
..Font::with_name("Iosevka Term")
..iced::Font::with_name("bootstrap-icons")
};

pub const MONO_BOLD: Font = Font {
monospaced: true,
weight: font::Weight::Bold,
..Font::with_name("Iosevka Term")
};
#[derive(Debug, Clone)]
pub struct Font {
bold: bool,
inner: OnceCell<iced::Font>,
}

pub const ICON: Font = Font {
monospaced: true,
..Font::with_name("bootstrap-icons")
};
impl Font {
const fn new(bold: bool) -> Self {
Self {
bold,
inner: OnceCell::new(),
}
}

fn set(&self, name: String) {
let name = Box::leak(name.into_boxed_str());
let weight = if self.bold {
font::Weight::Bold
} else {
font::Weight::Normal
};

let _ = self.inner.set(iced::Font {
monospaced: true,
weight,
..iced::Font::with_name(name)
});
}
}

impl From<Font> for iced::Font {
fn from(value: Font) -> Self {
value.inner.get().copied().expect("font is set on startup")
}
}

pub fn set(config: Option<&Config>) {
let family = config
.and_then(|config| config.font.family.clone())
.unwrap_or_else(|| String::from("Iosevka Term"));

MONO.set(family.clone());
MONO_BOLD.set(family);
}

pub fn load() -> Command<Result<(), Error>> {
Command::batch(vec![
Expand Down
34 changes: 23 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ pub fn main() -> iced::Result {
data::environment::formatted_version()
);

if let Err(error) = Halloy::run(settings()) {
let config_load = Config::load();

// DANGER ZONE - font must be set using config
// before we do any iced related stuff w/ it
font::set(config_load.as_ref().ok());

if let Err(error) = Halloy::run(settings(config_load)) {
log::error!("{}", error.to_string());
Err(error)
} else {
Expand All @@ -72,15 +78,19 @@ fn window_settings() -> iced::window::Settings {
}
}

fn settings() -> iced::Settings<()> {
fn settings(
config_load: Result<Config, config::Error>,
) -> iced::Settings<Result<Config, config::Error>> {
iced::Settings {
default_font: font::MONO,
default_font: font::MONO.clone().into(),
default_text_size: theme::TEXT_SIZE,
window: iced::window::Settings {
..window_settings()
},
exit_on_close_request: false,
..Default::default()
flags: config_load,
id: None,
antialiasing: false,
}
}

Expand All @@ -92,7 +102,9 @@ struct Halloy {
}

impl Halloy {
pub fn load_from_state() -> (Halloy, Command<Message>) {
pub fn load_from_state(
config_load: Result<Config, config::Error>,
) -> (Halloy, Command<Message>) {
let load_dashboard = |config| match data::Dashboard::load() {
Ok(dashboard) => screen::Dashboard::restore(dashboard),
Err(error) => {
Expand All @@ -104,7 +116,7 @@ impl Halloy {
}
};

let (screen, config, command) = match Config::load() {
let (screen, config, command) = match config_load {
Ok(config) => {
let (screen, command) = load_dashboard(&config);

Expand Down Expand Up @@ -160,10 +172,10 @@ impl Application for Halloy {
type Executor = executor::Default;
type Message = Message;
type Theme = theme::Theme;
type Flags = ();
type Flags = Result<Config, config::Error>;

fn new(_flags: ()) -> (Halloy, Command<Self::Message>) {
let (halloy, command) = Halloy::load_from_state();
fn new(config_load: Self::Flags) -> (Halloy, Command<Self::Message>) {
let (halloy, command) = Halloy::load_from_state(config_load);

(
halloy,
Expand Down Expand Up @@ -199,7 +211,7 @@ impl Application for Halloy {
if let Some(event) = help.update(message) {
match event {
help::Event::RefreshConfiguration => {
let (halloy, command) = Halloy::load_from_state();
let (halloy, command) = Halloy::load_from_state(Config::load());
*self = halloy;

return command;
Expand All @@ -217,7 +229,7 @@ impl Application for Halloy {
if let Some(event) = welcome.update(message) {
match event {
welcome::Event::RefreshConfiguration => {
let (halloy, command) = Halloy::load_from_state();
let (halloy, command) = Halloy::load_from_state(Config::load());
*self = halloy;

return command;
Expand Down
2 changes: 1 addition & 1 deletion src/screen/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Help {
.spacing(1)
.push(icon::error().size(45))
.push(vertical_space(10))
.push(text("Error reading configuration file").font(font::MONO_BOLD))
.push(text("Error reading configuration file").font(font::MONO_BOLD.clone()))
.push(vertical_space(3))
.push(text(self.error.to_string()).style(theme::Text::Error))
.push(vertical_space(10))
Expand Down
2 changes: 1 addition & 1 deletion src/screen/welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Welcome {
.spacing(1)
.push(image(format!("{}/assets/logo.png", env!("CARGO_MANIFEST_DIR"))).width(150))
.push(vertical_space(10))
.push(text("Welcome to Halloy!").font(font::MONO_BOLD))
.push(text("Welcome to Halloy!").font(font::MONO_BOLD.clone()))
.push(vertical_space(4))
.push(text(
"No configuration file found. Please follow the steps below to proceed",
Expand Down

0 comments on commit 9ece059

Please sign in to comment.