Font size setting

This commit is contained in:
Jeremy Soller 2023-11-03 15:58:26 -06:00
parent 6c0e104314
commit fd42a47684
5 changed files with 76 additions and 40 deletions

33
Cargo.lock generated
View file

@ -792,7 +792,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-config" name = "cosmic-config"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"atomicwrites", "atomicwrites",
"cosmic-config-derive", "cosmic-config-derive",
@ -806,7 +806,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-config-derive" name = "cosmic-config-derive"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
@ -873,7 +873,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-theme" name = "cosmic-theme"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"almost", "almost",
"cosmic-config", "cosmic-config",
@ -1994,7 +1994,7 @@ dependencies = [
[[package]] [[package]]
name = "iced" name = "iced"
version = "0.10.0" version = "0.10.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"iced_accessibility", "iced_accessibility",
"iced_core", "iced_core",
@ -2009,7 +2009,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_accessibility" name = "iced_accessibility"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"accesskit", "accesskit",
"accesskit_winit", "accesskit_winit",
@ -2018,12 +2018,13 @@ dependencies = [
[[package]] [[package]]
name = "iced_core" name = "iced_core"
version = "0.10.0" version = "0.10.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"instant", "instant",
"log", "log",
"palette", "palette",
"serde",
"thiserror", "thiserror",
"twox-hash", "twox-hash",
] ]
@ -2031,7 +2032,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_futures" name = "iced_futures"
version = "0.7.0" version = "0.7.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"futures", "futures",
"iced_core", "iced_core",
@ -2043,7 +2044,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_graphics" name = "iced_graphics"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"bytemuck", "bytemuck",
@ -2061,7 +2062,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_renderer" name = "iced_renderer"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"iced_graphics", "iced_graphics",
"iced_tiny_skia", "iced_tiny_skia",
@ -2074,7 +2075,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_runtime" name = "iced_runtime"
version = "0.1.1" version = "0.1.1"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"iced_core", "iced_core",
"iced_futures", "iced_futures",
@ -2084,7 +2085,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_style" name = "iced_style"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"iced_core", "iced_core",
"once_cell", "once_cell",
@ -2094,7 +2095,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_tiny_skia" name = "iced_tiny_skia"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"bytemuck", "bytemuck",
"cosmic-text 0.9.0", "cosmic-text 0.9.0",
@ -2112,7 +2113,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_wgpu" name = "iced_wgpu"
version = "0.11.1" version = "0.11.1"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"bytemuck", "bytemuck",
@ -2134,7 +2135,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_widget" name = "iced_widget"
version = "0.1.3" version = "0.1.3"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"iced_renderer", "iced_renderer",
"iced_runtime", "iced_runtime",
@ -2148,7 +2149,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_winit" name = "iced_winit"
version = "0.10.0" version = "0.10.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"iced_graphics", "iced_graphics",
"iced_runtime", "iced_runtime",
@ -2405,7 +2406,7 @@ checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
[[package]] [[package]]
name = "libcosmic" name = "libcosmic"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic?branch=menu#ef6c21b647480e8bfea80e3dc628079de90f51c3" source = "git+https://github.com/pop-os/libcosmic?branch=menu#a9818966c368c2f1f8e7b401f172a12770a66ff5"
dependencies = [ dependencies = [
"apply", "apply",
"cosmic-config", "cosmic-config",

View file

@ -1,12 +1,9 @@
use cosmic::iced::keyboard::{KeyCode, Modifiers}; use cosmic::iced::keyboard::{KeyCode, Modifiers};
use cosmic_text::Metrics;
use std::{collections::HashMap, fmt}; use std::{collections::HashMap, fmt};
use crate::{ContextPage, Message}; use crate::{ContextPage, Message};
const DEFAULT_FONT_SIZE: f32 = 14.0;
const DEFAULT_SYNTAX_THEME_DARK: &'static str = "base16-eighties.dark";
const DEFAULT_SYNTAX_THEME_LIGHT: &'static str = "base16-ocean.light";
// Makes key binding definitions simpler // Makes key binding definitions simpler
const CTRL: Modifiers = Modifiers::CTRL; const CTRL: Modifiers = Modifiers::CTRL;
const ALT: Modifiers = Modifiers::ALT; const ALT: Modifiers = Modifiers::ALT;
@ -72,9 +69,9 @@ impl fmt::Display for KeyBind {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Config { pub struct Config {
pub font_size: f32, pub font_size: u16,
pub syntax_theme_dark: String, pub syntax_theme_dark: String,
pub syntax_theme_light: String, pub syntax_theme_light: String,
pub vim_bindings: bool, pub vim_bindings: bool,
@ -86,18 +83,20 @@ impl Config {
//TODO: load from cosmic-config //TODO: load from cosmic-config
pub fn load() -> Self { pub fn load() -> Self {
Self { Self {
font_size: DEFAULT_FONT_SIZE, font_size: 14,
syntax_theme_dark: DEFAULT_SYNTAX_THEME_DARK.to_string(), syntax_theme_dark: "base16-eighties.dark".to_string(),
syntax_theme_light: DEFAULT_SYNTAX_THEME_LIGHT.to_string(), syntax_theme_light: "base16-ocean.light".to_string(),
vim_bindings: false, vim_bindings: false,
word_wrap: false, word_wrap: false,
keybinds: KeyBind::load(), keybinds: KeyBind::load(),
} }
} }
// Calculate line height from font size // Calculate metrics from font size
pub fn line_height(&self) -> f32 { pub fn metrics(&self) -> Metrics {
(self.font_size * 1.4).ceil() let font_size = self.font_size as f32;
let line_height = (font_size * 1.4).ceil();
Metrics::new(font_size, line_height)
} }
// Get current syntax theme based on dark mode // Get current syntax theme based on dark mode

View file

@ -66,6 +66,7 @@ pub enum Message {
Cut, Cut,
Copy, Copy,
DefaultFont(usize), DefaultFont(usize),
DefaultFontSize(usize),
KeyBind(KeyBind), KeyBind(KeyBind),
NewFile, NewFile,
NewWindow, NewWindow,
@ -107,6 +108,8 @@ pub struct App {
tab_model: segmented_button::SingleSelectModel, tab_model: segmented_button::SingleSelectModel,
config: Config, config: Config,
font_names: Vec<String>, font_names: Vec<String>,
font_size_names: Vec<String>,
font_sizes: Vec<u16>,
theme_names: Vec<String>, theme_names: Vec<String>,
context_page: ContextPage, context_page: ContextPage,
} }
@ -235,6 +238,11 @@ impl App {
} }
} }
fn save_config(&mut self) {
//TODO: save config (work lost due to drive failure)
self.update_config();
}
fn update_nav_bar_active(&mut self) { fn update_nav_bar_active(&mut self) {
let tab_path_opt = match self.active_tab() { let tab_path_opt = match self.active_tab() {
Some(tab) => tab.path_opt.clone(), Some(tab) => tab.path_opt.clone(),
@ -346,6 +354,13 @@ impl cosmic::Application for App {
font_names font_names
}; };
let mut font_size_names = Vec::new();
let mut font_sizes = Vec::new();
for font_size in 4..=32 {
font_size_names.push(format!("{}px", font_size));
font_sizes.push(font_size);
}
let mut theme_names = Vec::with_capacity(SYNTAX_SYSTEM.theme_set.themes.len()); let mut theme_names = Vec::with_capacity(SYNTAX_SYSTEM.theme_set.themes.len());
for (theme_name, _theme) in SYNTAX_SYSTEM.theme_set.themes.iter() { for (theme_name, _theme) in SYNTAX_SYSTEM.theme_set.themes.iter() {
theme_names.push(theme_name.to_string()); theme_names.push(theme_name.to_string());
@ -357,6 +372,8 @@ impl cosmic::Application for App {
tab_model: segmented_button::Model::builder().build(), tab_model: segmented_button::Model::builder().build(),
config: Config::load(), config: Config::load(),
font_names, font_names,
font_size_names,
font_sizes,
theme_names, theme_names,
context_page: ContextPage::Settings, context_page: ContextPage::Settings,
}; };
@ -494,6 +511,17 @@ impl cosmic::Application for App {
} }
} }
} }
Message::DefaultFontSize(index) => {
match self.font_sizes.get(index) {
Some(font_size) => {
self.config.font_size = *font_size;
self.save_config();
}
None => {
log::warn!("failed to find font with index {}", index);
}
}
}
Message::KeyBind(key_bind) => { Message::KeyBind(key_bind) => {
for (config_key_bind, config_message) in self.config.keybinds.iter() { for (config_key_bind, config_message) in self.config.keybinds.iter() {
if config_key_bind == &key_bind { if config_key_bind == &key_bind {
@ -595,7 +623,7 @@ impl cosmic::Application for App {
} else { } else {
self.config.syntax_theme_light = theme_name.to_string(); self.config.syntax_theme_light = theme_name.to_string();
} }
self.update_config(); self.save_config();
} }
None => { None => {
log::warn!("failed to find syntax theme with index {}", index); log::warn!("failed to find syntax theme with index {}", index);
@ -643,11 +671,11 @@ impl cosmic::Application for App {
} }
Message::ToggleWordWrap => { Message::ToggleWordWrap => {
self.config.word_wrap = !self.config.word_wrap; self.config.word_wrap = !self.config.word_wrap;
self.update_config(); self.save_config();
} }
Message::VimBindings(vim_bindings) => { Message::VimBindings(vim_bindings) => {
self.config.vim_bindings = vim_bindings; self.config.vim_bindings = vim_bindings;
self.update_config(); self.save_config();
} }
} }
@ -721,6 +749,10 @@ impl cosmic::Application for App {
.iter() .iter()
.position(|font_name| font_name == current_font_name) .position(|font_name| font_name == current_font_name)
}; };
let font_size_selected = self
.font_sizes
.iter()
.position(|font_size| font_size == &self.config.font_size);
widget::settings::view_column(vec![ widget::settings::view_column(vec![
widget::settings::view_section(fl!("appearance")) widget::settings::view_section(fl!("appearance"))
.add(widget::settings::item::builder(fl!("theme")).control( .add(widget::settings::item::builder(fl!("theme")).control(
@ -737,7 +769,9 @@ impl cosmic::Application for App {
) )
.add( .add(
widget::settings::item::builder(fl!("default-font-size")).control( widget::settings::item::builder(fl!("default-font-size")).control(
widget::dropdown(&["TODO"], Some(0), |_index| Message::Todo), widget::dropdown(&self.font_size_names, font_size_selected, |index| {
Message::DefaultFontSize(index)
}),
), ),
) )
.into(), .into(),
@ -776,7 +810,7 @@ impl cosmic::Application for App {
match self.active_tab() { match self.active_tab() {
Some(tab) => { Some(tab) => {
tab_column = tab_column.push(text_box(&tab.editor).padding(8)); tab_column = tab_column.push(text_box(&tab.editor, self.config.metrics()).padding(8));
let status = match tab.editor.lock().unwrap().mode() { let status = match tab.editor.lock().unwrap().mode() {
ViMode::Passthrough => { ViMode::Passthrough => {
//TODO: status line //TODO: status line

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use cosmic_text::{Attrs, Buffer, Edit, Metrics, SyntaxEditor, ViEditor, Wrap}; use cosmic_text::{Attrs, Buffer, Edit, SyntaxEditor, ViEditor, Wrap};
use std::{fs, path::PathBuf, sync::Mutex}; use std::{fs, path::PathBuf, sync::Mutex};
use crate::{fl, Config, FONT_SYSTEM, SYNTAX_SYSTEM}; use crate::{fl, Config, FONT_SYSTEM, SYNTAX_SYSTEM};
@ -19,7 +19,7 @@ impl Tab {
let editor = SyntaxEditor::new( let editor = SyntaxEditor::new(
Buffer::new( Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(), &mut FONT_SYSTEM.lock().unwrap(),
Metrics::new(config.font_size, config.line_height()), config.metrics(),
), ),
&SYNTAX_SYSTEM, &SYNTAX_SYSTEM,
config.syntax_theme(cosmic::theme::is_dark()), config.syntax_theme(cosmic::theme::is_dark()),

View file

@ -59,13 +59,15 @@ impl StyleSheet for Theme {
pub struct TextBox<'a> { pub struct TextBox<'a> {
editor: &'a Mutex<ViEditor<'static>>, editor: &'a Mutex<ViEditor<'static>>,
metrics: Metrics,
padding: Padding, padding: Padding,
} }
impl<'a> TextBox<'a> { impl<'a> TextBox<'a> {
pub fn new(editor: &'a Mutex<ViEditor<'static>>) -> Self { pub fn new(editor: &'a Mutex<ViEditor<'static>>, metrics: Metrics) -> Self {
Self { Self {
editor, editor,
metrics,
padding: Padding::new(0.0), padding: Padding::new(0.0),
} }
} }
@ -76,8 +78,8 @@ impl<'a> TextBox<'a> {
} }
} }
pub fn text_box<'a>(editor: &'a Mutex<ViEditor<'static>>) -> TextBox<'a> { pub fn text_box<'a>(editor: &'a Mutex<ViEditor<'static>>, metrics: Metrics) -> TextBox<'a> {
TextBox::new(editor) TextBox::new(editor, metrics)
} }
//TODO: improve performance //TODO: improve performance
@ -264,8 +266,7 @@ where
// Set metrics and size // Set metrics and size
editor.buffer_mut().set_metrics_and_size( editor.buffer_mut().set_metrics_and_size(
//TODO: get from config self.metrics.scale(scale_factor as f32),
Metrics::new(14.0, 20.0).scale(scale_factor as f32),
image_w as f32, image_w as f32,
image_h as f32, image_h as f32,
); );
@ -336,6 +337,7 @@ where
layout.position() + [self.padding.left as f32, self.padding.top as f32].into(), layout.position() + [self.padding.left as f32, self.padding.top as f32].into(),
Size::new(view_w as f32, view_h as f32), Size::new(view_w as f32, view_h as f32),
), ),
[0.0; 4],
); );
let duration = instant.elapsed(); let duration = instant.elapsed();