Store font name in config
This commit is contained in:
parent
e4e35af9d3
commit
fdc9c4905b
4 changed files with 39 additions and 14 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -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",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
33
src/main.rs
33
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::<Tab>(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::<Tab>(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 => {
|
||||
|
|
|
|||
12
src/tab.rs
12
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()),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue