From fdc9c4905b719e162a44167caa418635b2e88777 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 3 Nov 2023 19:00:41 -0600 Subject: [PATCH] Store font name in config --- Cargo.lock | 4 ++-- src/config.rs | 4 +++- src/main.rs | 33 ++++++++++++++++++++++++--------- src/tab.rs | 12 ++++++++++-- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 97c2b71..18f3cfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5359,9 +5359,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.5.18" +version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176b6138793677221d420fd2f0aeeced263f197688b36484660da767bca2fa32" +checksum = "829846f3e3db426d4cee4510841b71a8e58aa2a76b1132579487ae430ccd9c7b" dependencies = [ "memchr", ] diff --git a/src/config.rs b/src/config.rs index fd083e9..487e48e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -106,6 +106,7 @@ impl fmt::Display for KeyBind { #[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)] pub struct Config { + pub font_name: String, pub font_size: u16, pub syntax_theme_dark: String, pub syntax_theme_light: String, @@ -117,6 +118,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { + font_name: "Fira Mono".to_string(), font_size: 14, syntax_theme_dark: "gruvbox-dark".to_string(), syntax_theme_light: "gruvbox-light".to_string(), @@ -130,7 +132,7 @@ impl Default for Config { impl Config { // Calculate metrics from font size pub fn metrics(&self) -> Metrics { - let font_size = self.font_size as f32; + let font_size = self.font_size.max(1) as f32; let line_height = (font_size * 1.4).ceil(); Metrics::new(font_size, line_height) } diff --git a/src/main.rs b/src/main.rs index 48fb1a1..4882956 100644 --- a/src/main.rs +++ b/src/main.rs @@ -366,6 +366,12 @@ impl cosmic::Application for App { } }; + // Update font name from config + { + let mut font_system = FONT_SYSTEM.lock().unwrap(); + font_system.db_mut().set_monospace_family(&config.font_name); + } + let font_names = { let mut font_names = Vec::new(); let font_system = FONT_SYSTEM.lock().unwrap(); @@ -533,17 +539,26 @@ impl cosmic::Application for App { Message::DefaultFont(index) => { match self.font_names.get(index) { Some(font_name) => { - let mut font_system = FONT_SYSTEM.lock().unwrap(); - font_system.db_mut().set_monospace_family(font_name); - // This does a complete reset of shaping data! - let entities: Vec<_> = self.tab_model.iter().collect(); - for entity in entities { - if let Some(tab) = self.tab_model.data_mut::(entity) { - let mut editor = tab.editor.lock().unwrap(); - for line in editor.buffer_mut().lines.iter_mut() { - line.reset(); + if font_name != &self.config.font_name { + // Update font name from config + { + let mut font_system = FONT_SYSTEM.lock().unwrap(); + font_system.db_mut().set_monospace_family(font_name); + } + + // This does a complete reset of shaping data! + let entities: Vec<_> = self.tab_model.iter().collect(); + for entity in entities { + if let Some(tab) = self.tab_model.data_mut::(entity) { + let mut editor = tab.editor.lock().unwrap(); + for line in editor.buffer_mut().lines.iter_mut() { + line.reset(); + } } } + + self.config.font_name = font_name.to_string(); + self.save_config(); } } None => { diff --git a/src/tab.rs b/src/tab.rs index bc0d3a6..2e4f790 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-3.0-only -use cosmic_text::{Attrs, Buffer, Edit, SyntaxEditor, ViEditor, Wrap}; +use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap}; use std::{fs, path::PathBuf, sync::Mutex}; use crate::{fl, Config, FONT_SYSTEM, SYNTAX_SYSTEM}; @@ -16,8 +16,16 @@ impl Tab { //TODO: do not repeat, used in App::init let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace); + let mut buffer = Buffer::new_empty(config.metrics()); + buffer.set_text( + &mut FONT_SYSTEM.lock().unwrap(), + "", + attrs, + Shaping::Advanced, + ); + let editor = SyntaxEditor::new( - Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), config.metrics()), + buffer, &SYNTAX_SYSTEM, config.syntax_theme(cosmic::theme::is_dark()), )