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]]
name = "cosmic-config"
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 = [
"atomicwrites",
"cosmic-config-derive",
@ -806,7 +806,7 @@ dependencies = [
[[package]]
name = "cosmic-config-derive"
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 = [
"quote",
"syn 1.0.109",
@ -873,7 +873,7 @@ dependencies = [
[[package]]
name = "cosmic-theme"
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 = [
"almost",
"cosmic-config",
@ -1994,7 +1994,7 @@ dependencies = [
[[package]]
name = "iced"
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 = [
"iced_accessibility",
"iced_core",
@ -2009,7 +2009,7 @@ dependencies = [
[[package]]
name = "iced_accessibility"
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 = [
"accesskit",
"accesskit_winit",
@ -2018,12 +2018,13 @@ dependencies = [
[[package]]
name = "iced_core"
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 = [
"bitflags 1.3.2",
"instant",
"log",
"palette",
"serde",
"thiserror",
"twox-hash",
]
@ -2031,7 +2032,7 @@ dependencies = [
[[package]]
name = "iced_futures"
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 = [
"futures",
"iced_core",
@ -2043,7 +2044,7 @@ dependencies = [
[[package]]
name = "iced_graphics"
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 = [
"bitflags 1.3.2",
"bytemuck",
@ -2061,7 +2062,7 @@ dependencies = [
[[package]]
name = "iced_renderer"
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 = [
"iced_graphics",
"iced_tiny_skia",
@ -2074,7 +2075,7 @@ dependencies = [
[[package]]
name = "iced_runtime"
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 = [
"iced_core",
"iced_futures",
@ -2084,7 +2085,7 @@ dependencies = [
[[package]]
name = "iced_style"
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 = [
"iced_core",
"once_cell",
@ -2094,7 +2095,7 @@ dependencies = [
[[package]]
name = "iced_tiny_skia"
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 = [
"bytemuck",
"cosmic-text 0.9.0",
@ -2112,7 +2113,7 @@ dependencies = [
[[package]]
name = "iced_wgpu"
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 = [
"bitflags 1.3.2",
"bytemuck",
@ -2134,7 +2135,7 @@ dependencies = [
[[package]]
name = "iced_widget"
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 = [
"iced_renderer",
"iced_runtime",
@ -2148,7 +2149,7 @@ dependencies = [
[[package]]
name = "iced_winit"
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 = [
"iced_graphics",
"iced_runtime",
@ -2405,7 +2406,7 @@ checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
[[package]]
name = "libcosmic"
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 = [
"apply",
"cosmic-config",

View file

@ -1,12 +1,9 @@
use cosmic::iced::keyboard::{KeyCode, Modifiers};
use cosmic_text::Metrics;
use std::{collections::HashMap, fmt};
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
const CTRL: Modifiers = Modifiers::CTRL;
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 font_size: f32,
pub font_size: u16,
pub syntax_theme_dark: String,
pub syntax_theme_light: String,
pub vim_bindings: bool,
@ -86,18 +83,20 @@ impl Config {
//TODO: load from cosmic-config
pub fn load() -> Self {
Self {
font_size: DEFAULT_FONT_SIZE,
syntax_theme_dark: DEFAULT_SYNTAX_THEME_DARK.to_string(),
syntax_theme_light: DEFAULT_SYNTAX_THEME_LIGHT.to_string(),
font_size: 14,
syntax_theme_dark: "base16-eighties.dark".to_string(),
syntax_theme_light: "base16-ocean.light".to_string(),
vim_bindings: false,
word_wrap: false,
keybinds: KeyBind::load(),
}
}
// Calculate line height from font size
pub fn line_height(&self) -> f32 {
(self.font_size * 1.4).ceil()
// Calculate metrics from font size
pub fn metrics(&self) -> Metrics {
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

View file

@ -66,6 +66,7 @@ pub enum Message {
Cut,
Copy,
DefaultFont(usize),
DefaultFontSize(usize),
KeyBind(KeyBind),
NewFile,
NewWindow,
@ -107,6 +108,8 @@ pub struct App {
tab_model: segmented_button::SingleSelectModel,
config: Config,
font_names: Vec<String>,
font_size_names: Vec<String>,
font_sizes: Vec<u16>,
theme_names: Vec<String>,
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) {
let tab_path_opt = match self.active_tab() {
Some(tab) => tab.path_opt.clone(),
@ -346,6 +354,13 @@ impl cosmic::Application for App {
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());
for (theme_name, _theme) in SYNTAX_SYSTEM.theme_set.themes.iter() {
theme_names.push(theme_name.to_string());
@ -357,6 +372,8 @@ impl cosmic::Application for App {
tab_model: segmented_button::Model::builder().build(),
config: Config::load(),
font_names,
font_size_names,
font_sizes,
theme_names,
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) => {
for (config_key_bind, config_message) in self.config.keybinds.iter() {
if config_key_bind == &key_bind {
@ -595,7 +623,7 @@ impl cosmic::Application for App {
} else {
self.config.syntax_theme_light = theme_name.to_string();
}
self.update_config();
self.save_config();
}
None => {
log::warn!("failed to find syntax theme with index {}", index);
@ -643,11 +671,11 @@ impl cosmic::Application for App {
}
Message::ToggleWordWrap => {
self.config.word_wrap = !self.config.word_wrap;
self.update_config();
self.save_config();
}
Message::VimBindings(vim_bindings) => {
self.config.vim_bindings = vim_bindings;
self.update_config();
self.save_config();
}
}
@ -721,6 +749,10 @@ impl cosmic::Application for App {
.iter()
.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_section(fl!("appearance"))
.add(widget::settings::item::builder(fl!("theme")).control(
@ -737,7 +769,9 @@ impl cosmic::Application for App {
)
.add(
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(),
@ -776,7 +810,7 @@ impl cosmic::Application for App {
match self.active_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() {
ViMode::Passthrough => {
//TODO: status line

View file

@ -1,6 +1,6 @@
// 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 crate::{fl, Config, FONT_SYSTEM, SYNTAX_SYSTEM};
@ -19,7 +19,7 @@ impl Tab {
let editor = SyntaxEditor::new(
Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(),
Metrics::new(config.font_size, config.line_height()),
config.metrics(),
),
&SYNTAX_SYSTEM,
config.syntax_theme(cosmic::theme::is_dark()),

View file

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