diff --git a/Cargo.lock b/Cargo.lock index 8f6f8c9..deddf0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2400,6 +2400,7 @@ dependencies = [ "serde", "tokio", "toml", + "ttf-parser", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index 7a689ba..5b775f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,12 @@ version = "0.1.0" edition = "2024" [dependencies] -iced = { version = "0.14.0", features = ["tokio"] } +iced = { version = "0.14.0", features = ["tokio", "advanced"] } iced_aw = { path = "vendor/iced_aw", default-features = false, features = ["color_picker"] } russh = "0.65.0" russh-sftp = "3.0.1" serde = { version = "1.0.229", features = ["derive"] } tokio = { version = "1.54.1", features = ["full"] } toml = "0.8" +ttf-parser = "0.25" uuid = { version = "1.29.0", features = ["v4"] } diff --git a/assets/fonts/Inter/Inter-Variable.ttf b/assets/fonts/Inter/Inter-Variable.ttf new file mode 100644 index 0000000..047c92f Binary files /dev/null and b/assets/fonts/Inter/Inter-Variable.ttf differ diff --git a/assets/fonts/Inter/OFL.txt b/assets/fonts/Inter/OFL.txt new file mode 100644 index 0000000..21f6aff --- /dev/null +++ b/assets/fonts/Inter/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/assets/fonts/JetBrainsMono/JetBrainsMono-Variable.ttf b/assets/fonts/JetBrainsMono/JetBrainsMono-Variable.ttf new file mode 100644 index 0000000..5b7a526 Binary files /dev/null and b/assets/fonts/JetBrainsMono/JetBrainsMono-Variable.ttf differ diff --git a/assets/fonts/JetBrainsMono/JetBrainsMonoNL-Variable.ttf b/assets/fonts/JetBrainsMono/JetBrainsMonoNL-Variable.ttf new file mode 100644 index 0000000..ecac91e Binary files /dev/null and b/assets/fonts/JetBrainsMono/JetBrainsMonoNL-Variable.ttf differ diff --git a/assets/fonts/JetBrainsMono/OFL.txt b/assets/fonts/JetBrainsMono/OFL.txt new file mode 100644 index 0000000..5ceee00 --- /dev/null +++ b/assets/fonts/JetBrainsMono/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2020 The JetBrains Mono Project Authors (https://github.com/JetBrains/JetBrainsMono) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/src/main.rs b/src/main.rs index 9f9314e..f0af6f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,12 @@ use std::cmp::Ordering; -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeSet, HashMap, HashSet}; use std::path::{Path, PathBuf}; +use std::sync::OnceLock; use serde::{Deserialize, Serialize}; use iced::time::{Duration, Instant}; -use iced::widget::text::Wrapping; +use iced::widget::text::{IntoFragment, Wrapping}; use iced::widget::{Id, operation}; use iced::widget::{ button, checkbox, column, container, mouse_area, pick_list, responsive, row, rule, scrollable, @@ -27,6 +28,14 @@ const SETTINGS_H1_SIZE: f32 = 22.0; const SETTINGS_H2_SIZE: f32 = 18.0; const SETTINGS_H3_SIZE: f32 = 16.0; const SETTINGS_CAPTION_SIZE: f32 = 13.0; +/// Default font choice for every typography level. +const MONO_FONT_FAMILY: &str = "JetBrains Mono"; +const TERMINAL_FONT_SIZE: f32 = 14.0; +const SSH_FONT_SIZE: f32 = 14.0; +/// Pre-filled font sizes offered by the typography drop-down. +const FONT_SIZE_STEPS: [f32; 14] = [ + 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 18.0, 20.0, 22.0, 24.0, 28.0, 32.0, 36.0, +]; /// Tab width bounds and gap, used to fit as many tabs as the strip allows. const TAB_MAX_WIDTH: f32 = 230.0; const TAB_MIN_WIDTH: f32 = 100.0; @@ -96,7 +105,20 @@ fn boot() -> (Moorly, Task) { state.window_order.push(id); state.main_window = Some(id); state.focused_window = Some(id); - (state, task.map(|_| Message::Ignore)) + let fonts = Task::batch([ + iced::font::load(include_bytes!("../assets/fonts/Inter/Inter-Variable.ttf").as_slice()) + .map(|_| Message::Ignore), + iced::font::load( + include_bytes!("../assets/fonts/JetBrainsMono/JetBrainsMono-Variable.ttf").as_slice(), + ) + .map(|_| Message::Ignore), + iced::font::load( + include_bytes!("../assets/fonts/JetBrainsMono/JetBrainsMonoNL-Variable.ttf").as_slice(), + ) + .map(|_| Message::Ignore), + ]); + + (state, Task::batch([task.map(|_| Message::Ignore), fonts])) } fn window_title(state: &Moorly, window: window::Id) -> String { @@ -1492,6 +1514,11 @@ struct Moorly { show_new_session_button_draft: bool, show_command_palette_description: bool, show_command_palette_description_draft: bool, + typography: Typography, + typography_draft: Typography, + typography_size_inputs: [String; 6], + typography_open: bool, + typography_font_menu: Option, command_palette_enabled: bool, command_palette_enabled_draft: bool, advanced_open: bool, @@ -1690,6 +1717,7 @@ struct CustomTheme { colors: [Color; 5], custom_toasts: bool, toast_colors: [Color; 9], + typography: Typography, } #[derive(Debug, Serialize, Deserialize)] @@ -1707,6 +1735,8 @@ struct ThemeFile { #[serde(skip_serializing_if = "Option::is_none")] text: Option, #[serde(default)] + typography: TypographyConfig, + #[serde(default)] toast: ToastFile, } @@ -1742,6 +1772,7 @@ impl ThemeFile { base: &[Color; 5], custom_toasts: bool, toast_colors: &[Color; 9], + typography: &Typography, ) -> Self { let changed = |option: ColorOption| { let index = option.index(); @@ -1757,6 +1788,7 @@ impl ThemeFile { main_background: changed(ColorOption::MainBackground), sidebar_background: changed(ColorOption::SidebarBackground), text: changed(ColorOption::Text), + typography: TypographyConfig::from_typography(typography), toast: ToastFile::from_colors(custom_toasts, toast_colors), } } @@ -1829,6 +1861,336 @@ fn builtin_toast_colors(preference: theme::Preference) -> [Color; 9] { notifications::ToastColors::default_for(&theme).as_array() } +/// One customizable text style in the Appearance settings. +#[derive(Debug, Clone, PartialEq)] +struct TextStyle { + font: String, + size: f32, + bold: bool, + italic: bool, + underline: bool, + strikethrough: bool, +} + +impl TextStyle { + fn new(font: &str, size: f32) -> Self { + Self { + font: font.to_owned(), + size, + bold: false, + italic: false, + underline: false, + strikethrough: false, + } + } +} + +/// The text styles used across the settings UI and terminal sessions. +#[derive(Debug, Clone, PartialEq)] +struct Typography { + h1: TextStyle, + h2: TextStyle, + h3: TextStyle, + caption: TextStyle, + terminal: TextStyle, + ssh: TextStyle, +} + +impl Default for Typography { + fn default() -> Self { + let ui_font = default_ui_font(); + + Self { + h1: TextStyle::new(ui_font, SETTINGS_H1_SIZE), + h2: TextStyle::new(ui_font, SETTINGS_H2_SIZE), + h3: TextStyle::new(ui_font, SETTINGS_H3_SIZE), + caption: TextStyle::new(ui_font, SETTINGS_CAPTION_SIZE), + terminal: TextStyle::new(MONO_FONT_FAMILY, TERMINAL_FONT_SIZE), + ssh: TextStyle::new(MONO_FONT_FAMILY, SSH_FONT_SIZE), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum TypographyLevel { + H1, + H2, + H3, + Caption, + Terminal, + Ssh, +} + +impl TypographyLevel { + const ALL: [Self; 6] = [ + Self::H1, + Self::H2, + Self::H3, + Self::Caption, + Self::Terminal, + Self::Ssh, + ]; + + fn label(self) -> &'static str { + match self { + Self::H1 => "H1 - Title", + Self::H2 => "H2 - Sub-title", + Self::H3 => "H3 - Text", + Self::Caption => "Caption", + Self::Terminal => "Terminal", + Self::Ssh => "SSH", + } + } + + fn default_size(self) -> f32 { + match self { + Self::H1 => SETTINGS_H1_SIZE, + Self::H2 => SETTINGS_H2_SIZE, + Self::H3 => SETTINGS_H3_SIZE, + Self::Caption => SETTINGS_CAPTION_SIZE, + Self::Terminal => TERMINAL_FONT_SIZE, + Self::Ssh => SSH_FONT_SIZE, + } + } + + fn index(self) -> usize { + match self { + Self::H1 => 0, + Self::H2 => 1, + Self::H3 => 2, + Self::Caption => 3, + Self::Terminal => 4, + Self::Ssh => 5, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum TextStyleFlag { + Bold, + Italic, + Underline, + Strikethrough, +} + +impl TextStyleFlag { + const ALL: [Self; 4] = [ + Self::Bold, + Self::Italic, + Self::Underline, + Self::Strikethrough, + ]; + + fn label(self) -> &'static str { + match self { + Self::Bold => "Bold", + Self::Italic => "Italic", + Self::Underline => "Underline", + Self::Strikethrough => "Strikethrough", + } + } + + fn enabled(self, style: &TextStyle) -> bool { + match self { + Self::Bold => style.bold, + Self::Italic => style.italic, + Self::Underline => style.underline, + Self::Strikethrough => style.strikethrough, + } + } + + fn toggle(self, style: &mut TextStyle) { + match self { + Self::Bold => style.bold = !style.bold, + Self::Italic => style.italic = !style.italic, + Self::Underline => style.underline = !style.underline, + Self::Strikethrough => style.strikethrough = !style.strikethrough, + } + } +} + +impl Typography { + fn style(&self, level: TypographyLevel) -> &TextStyle { + match level { + TypographyLevel::H1 => &self.h1, + TypographyLevel::H2 => &self.h2, + TypographyLevel::H3 => &self.h3, + TypographyLevel::Caption => &self.caption, + TypographyLevel::Terminal => &self.terminal, + TypographyLevel::Ssh => &self.ssh, + } + } + + fn style_mut(&mut self, level: TypographyLevel) -> &mut TextStyle { + match level { + TypographyLevel::H1 => &mut self.h1, + TypographyLevel::H2 => &mut self.h2, + TypographyLevel::H3 => &mut self.h3, + TypographyLevel::Caption => &mut self.caption, + TypographyLevel::Terminal => &mut self.terminal, + TypographyLevel::Ssh => &mut self.ssh, + } + } +} + +static FONT_FAMILIES: OnceLock> = OnceLock::new(); + +fn default_ui_font() -> &'static str { + if cfg!(windows) { "Segoe UI" } else { "Inter" } +} + +/// Font families known to iced's font system, loaded once at first use. +fn available_font_families() -> &'static [&'static str] { + FONT_FAMILIES.get_or_init(|| { + let mut names = BTreeSet::new(); + + for bundled in ["Inter", "JetBrains Mono", "JetBrains Mono NL"] { + names.insert(bundled.to_owned()); + } + + if let Ok(mut font_system) = iced::advanced::graphics::text::font_system().write() { + let db = font_system.raw().db(); + + for face in db.faces() { + let Some((family, _)) = face.families.first() else { + continue; + }; + + // Skip symbol/emoji-only faces; their rows would render blank. + let supports_latin = db + .with_face_data(face.id, |data, index| { + ttf_parser::Face::parse(data, index).is_ok_and(|face| { + ['A', 'a', '0'] + .iter() + .all(|character| face.glyph_index(*character).is_some()) + }) + }) + .unwrap_or(false); + + if supports_latin { + names.insert(family.clone()); + } + } + } + + names + .into_iter() + .map(|name| Box::leak(name.into_boxed_str()) as &'static str) + .collect() + }) +} + +fn font_for_name(name: &str) -> Font { + let requested = if name.trim().is_empty() || name == "Default" { + default_ui_font() + } else { + name + }; + + if let Some(family) = available_font_families() + .iter() + .find(|family| **family == requested) + { + return Font::with_name(*family); + } + + // A missing theme font falls back to the platform default, then to the + // bundled UI font. + if requested != default_ui_font() + && let Some(family) = available_font_families() + .iter() + .find(|family| **family == default_ui_font()) + { + return Font::with_name(*family); + } + + if let Some(family) = available_font_families() + .iter() + .find(|family| **family == "Inter") + { + return Font::with_name(*family); + } + + Font::DEFAULT +} + +fn typography_font(style: &TextStyle) -> Font { + let mut font = font_for_name(&style.font); + + if style.bold { + font.weight = font::Weight::Bold; + } + if style.italic { + font.style = font::Style::Italic; + } + + font +} + +fn styled_text<'a>(style: &TextStyle, content: impl IntoFragment<'a>) -> Element<'a, Message> { + let span: iced::widget::text::Span<'_, (), Font> = iced::widget::text::Span::new(content) + .size(style.size) + .font(typography_font(style)) + .underline(style.underline) + .strikethrough(style.strikethrough); + + iced::widget::rich_text([span]).into() +} + +/// One entry in a typography size drop-down. +#[derive(Debug, Clone, PartialEq)] +struct SizeChoice { + size: Option, + label: String, +} + +impl std::fmt::Display for SizeChoice { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.label) + } +} + +fn size_choices(level: TypographyLevel, selected: f32) -> (Vec, SizeChoice) { + let default = level.default_size(); + let mut choices: Vec = FONT_SIZE_STEPS + .iter() + .map(|size| SizeChoice { + size: Some(*size), + label: if (*size - default).abs() < 0.01 { + format!("{size} px (Default)") + } else { + format!("{size} px") + }, + }) + .collect(); + + if let Some(choice) = choices + .iter() + .find(|choice| { + choice + .size + .is_some_and(|size| (size - selected).abs() < 0.01) + }) + .cloned() + { + return (choices, choice); + } + + let custom = SizeChoice { + size: None, + label: "".to_owned(), + }; + choices.insert(0, custom.clone()); + (choices, custom) +} + +fn typography_size_inputs(typography: &Typography) -> [String; 6] { + std::array::from_fn(|index| { + let level = TypographyLevel::ALL[index]; + format!("{}", typography.style(level).size) + }) +} + /// Everything persisted in the config file, grouped by settings tab. #[derive(Debug, Clone, Serialize, Deserialize)] struct ConfigFile { @@ -1899,6 +2261,8 @@ struct AppearanceConfig { #[serde(default = "default_true")] show_command_palette_description: bool, #[serde(default)] + typography: TypographyConfig, + #[serde(default)] toast: ToastFile, } @@ -1911,11 +2275,136 @@ impl Default for AppearanceConfig { hide_tab_close: true, show_new_session_button: true, show_command_palette_description: true, + typography: TypographyConfig::default(), toast: ToastFile::default(), } } } +#[derive(Debug, Clone, Serialize, Deserialize)] +struct TypographyConfig { + #[serde(default = "default_h1_style")] + h1: TextStyleConfig, + #[serde(default = "default_h2_style")] + h2: TextStyleConfig, + #[serde(default = "default_h3_style")] + h3: TextStyleConfig, + #[serde(default = "default_caption_style")] + caption: TextStyleConfig, + #[serde(default = "default_terminal_style")] + terminal: TextStyleConfig, + #[serde(default = "default_ssh_style")] + ssh: TextStyleConfig, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct TextStyleConfig { + #[serde(default = "default_font_family")] + font: String, + #[serde(default = "default_body_size")] + size: f32, + #[serde(default)] + bold: bool, + #[serde(default)] + italic: bool, + #[serde(default)] + underline: bool, + #[serde(default)] + strikethrough: bool, +} + +impl Default for TextStyleConfig { + fn default() -> Self { + Self::from_style(&Typography::default().h3) + } +} + +impl TextStyleConfig { + fn from_style(style: &TextStyle) -> Self { + Self { + font: style.font.clone(), + size: style.size, + bold: style.bold, + italic: style.italic, + underline: style.underline, + strikethrough: style.strikethrough, + } + } + + fn into_style(&self) -> TextStyle { + TextStyle { + font: self.font.clone(), + size: self.size, + bold: self.bold, + italic: self.italic, + underline: self.underline, + strikethrough: self.strikethrough, + } + } +} + +impl Default for TypographyConfig { + fn default() -> Self { + Self::from_typography(&Typography::default()) + } +} + +impl TypographyConfig { + fn from_typography(typography: &Typography) -> Self { + Self { + h1: TextStyleConfig::from_style(&typography.h1), + h2: TextStyleConfig::from_style(&typography.h2), + h3: TextStyleConfig::from_style(&typography.h3), + caption: TextStyleConfig::from_style(&typography.caption), + terminal: TextStyleConfig::from_style(&typography.terminal), + ssh: TextStyleConfig::from_style(&typography.ssh), + } + } + + fn into_typography(&self) -> Typography { + Typography { + h1: self.h1.into_style(), + h2: self.h2.into_style(), + h3: self.h3.into_style(), + caption: self.caption.into_style(), + terminal: self.terminal.into_style(), + ssh: self.ssh.into_style(), + } + } +} + +fn default_font_family() -> String { + default_ui_font().to_owned() +} + +fn default_body_size() -> f32 { + SETTINGS_H3_SIZE +} + +fn default_h1_style() -> TextStyleConfig { + TextStyleConfig::from_style(&Typography::default().h1) +} + +fn default_h2_style() -> TextStyleConfig { + TextStyleConfig::from_style(&Typography::default().h2) +} + +fn default_h3_style() -> TextStyleConfig { + TextStyleConfig::from_style(&Typography::default().h3) +} + +fn default_caption_style() -> TextStyleConfig { + TextStyleConfig::from_style(&Typography::default().caption) +} + +fn default_terminal_style() -> TextStyleConfig { + TextStyleConfig::from_style(&Typography::default().terminal) +} + +fn default_ssh_style() -> TextStyleConfig { + TextStyleConfig::from_style(&Typography::default().ssh) +} + #[derive(Debug, Clone, Serialize, Deserialize)] struct ShortcutConfig { #[serde(default = "default_settings_shortcut")] @@ -2073,6 +2562,7 @@ fn load_custom_themes() -> Vec { colors, custom_toasts: file.toast.enabled, toast_colors, + typography: file.typography.into_typography(), }); } @@ -2122,6 +2612,7 @@ fn write_custom_theme( colors: &[Color; 5], custom_toasts: bool, toast_colors: &[Color; 9], + typography: &Typography, ) -> Option<()> { let dir = themes_dir()?; std::fs::create_dir_all(&dir).ok()?; @@ -2134,6 +2625,7 @@ fn write_custom_theme( &base, custom_toasts, toast_colors, + typography, ); let text = toml::to_string_pretty(&file).ok()?; @@ -3391,6 +3883,9 @@ fn apply_theme_choice(state: &mut Moorly, choice: ThemeChoice) { state.colors = theme.colors; state.custom_toasts = theme.custom_toasts; state.toast_colors = theme.toast_colors; + state.typography = theme.typography.clone(); + state.typography_draft = state.typography.clone(); + state.typography_size_inputs = typography_size_inputs(&state.typography); state.theme_choice = ThemeChoice::Custom { id: theme.id.clone(), name: theme.name.clone(), @@ -3507,6 +4002,7 @@ fn resolve_theme( [Color; 5], bool, [Color; 9], + Typography, bool, ) { if let Some(id) = &appearance.custom_theme { @@ -3520,6 +4016,7 @@ fn resolve_theme( theme.colors, theme.custom_toasts, theme.toast_colors, + theme.typography.clone(), false, ); } @@ -3533,6 +4030,7 @@ fn resolve_theme( builtin_colors(preference), custom_toasts, toast_colors, + appearance.typography.into_typography(), true, ); } @@ -3546,6 +4044,7 @@ fn resolve_theme( builtin_colors(preference), custom_toasts, toast_colors, + appearance.typography.into_typography(), false, ) } @@ -3581,6 +4080,8 @@ fn apply_settings(state: &mut Moorly) { state.hide_tab_close = state.hide_tab_close_draft; state.show_new_session_button = state.show_new_session_button_draft; state.show_command_palette_description = state.show_command_palette_description_draft; + state.typography = state.typography_draft.clone(); + state.typography_size_inputs = typography_size_inputs(&state.typography); state.command_palette_enabled = state.command_palette_enabled_draft; state.saved_theme = state.theme; @@ -3617,6 +4118,8 @@ fn undo_settings(state: &mut Moorly) { state.hide_tab_close_draft = state.hide_tab_close; state.show_new_session_button_draft = state.show_new_session_button; state.show_command_palette_description_draft = state.show_command_palette_description; + state.typography_draft = state.typography.clone(); + state.typography_size_inputs = typography_size_inputs(&state.typography_draft); state.command_palette_enabled_draft = state.command_palette_enabled; state.shortcuts = state.saved_shortcuts.clone(); state.shortcut_capture = None; @@ -3679,6 +4182,7 @@ fn config_file(state: &Moorly) -> ConfigFile { hide_tab_close: state.hide_tab_close, show_new_session_button: state.show_new_session_button, show_command_palette_description: state.show_command_palette_description, + typography: TypographyConfig::from_typography(&state.typography), toast: ToastFile::from_colors(state.builtin_custom_toasts, &state.builtin_toast_colors), }, shortcuts: ShortcutConfig { @@ -3723,7 +4227,7 @@ impl Default for Moorly { fn default() -> Self { let config = load_config(); let custom_themes = load_custom_themes(); - let (theme_choice, theme, colors, custom_toasts, toast_colors, theme_missing) = + let (theme_choice, theme, colors, custom_toasts, toast_colors, typography, theme_missing) = resolve_theme(&config.appearance, &custom_themes); let (builtin_custom_toasts, builtin_toast_palette) = builtin_toast_config(&config.appearance); @@ -3732,6 +4236,7 @@ impl Default for Moorly { let hide_tab_close = config.appearance.hide_tab_close; let show_new_session_button = config.appearance.show_new_session_button; let show_command_palette_description = config.appearance.show_command_palette_description; + let typography_size_inputs = typography_size_inputs(&typography); let command_palette_enabled = config.command_palette.enabled; let shortcuts = Shortcuts { settings: Shortcut::parse(&config.shortcuts.settings), @@ -3776,6 +4281,11 @@ impl Default for Moorly { show_new_session_button_draft: show_new_session_button, show_command_palette_description, show_command_palette_description_draft: show_command_palette_description, + typography: typography.clone(), + typography_draft: typography, + typography_size_inputs, + typography_open: false, + typography_font_menu: None, command_palette_enabled, command_palette_enabled_draft: command_palette_enabled, advanced_open: false, @@ -3854,6 +4364,13 @@ enum Message { SetHideTabClose(bool), SetShowNewSessionButton(bool), SetShowCommandPaletteDescription(bool), + ToggleTypography, + ToggleTypographyFontMenu(TypographyLevel), + TypographyFontSelected(TypographyLevel, String), + TypographySizeSelected(TypographyLevel, SizeChoice), + TypographySizeInputChanged(TypographyLevel, String), + TypographySizeStep(TypographyLevel, i32), + TypographyStyleToggled(TypographyLevel, TextStyleFlag), SetCommandPaletteEnabled(bool), ToggleAdvanced, SetCustomToasts(bool), @@ -4274,6 +4791,7 @@ fn update(state: &mut Moorly, message: Message) -> Task { &state.colors, state.custom_toasts, &state.toast_colors, + &state.typography, ) .is_none() { @@ -4307,6 +4825,7 @@ fn update(state: &mut Moorly, message: Message) -> Task { &state.colors, state.custom_toasts, &state.toast_colors, + &state.typography, ) .is_some() { @@ -4316,6 +4835,7 @@ fn update(state: &mut Moorly, message: Message) -> Task { theme.colors = state.colors; theme.custom_toasts = state.custom_toasts; theme.toast_colors = state.toast_colors; + theme.typography = state.typography.clone(); } if matches!( @@ -4337,6 +4857,9 @@ fn update(state: &mut Moorly, message: Message) -> Task { state.colors = theme.colors; state.custom_toasts = theme.custom_toasts; state.toast_colors = theme.toast_colors; + state.typography = theme.typography.clone(); + state.typography_draft = state.typography.clone(); + state.typography_size_inputs = typography_size_inputs(&state.typography); if matches!( &state.saved_theme_choice, @@ -4368,6 +4891,7 @@ fn update(state: &mut Moorly, message: Message) -> Task { theme.colors == state.colors && theme.custom_toasts == state.custom_toasts && theme.toast_colors == state.toast_colors + && theme.typography == state.typography }) .unwrap_or(true); @@ -4386,6 +4910,10 @@ fn update(state: &mut Moorly, message: Message) -> Task { state.colors = theme.colors; state.custom_toasts = theme.custom_toasts; state.toast_colors = theme.toast_colors; + state.typography = theme.typography.clone(); + state.typography_draft = state.typography.clone(); + state.typography_size_inputs = + typography_size_inputs(&state.typography); } if matches!( @@ -4479,6 +5007,38 @@ fn update(state: &mut Moorly, message: Message) -> Task { Message::SetShowCommandPaletteDescription(value) => { state.show_command_palette_description_draft = value; } + Message::ToggleTypography => state.typography_open = !state.typography_open, + Message::ToggleTypographyFontMenu(level) => { + state.typography_font_menu = + (state.typography_font_menu != Some(level)).then_some(level); + } + Message::TypographyFontSelected(level, font) => { + state.typography_draft.style_mut(level).font = font; + state.typography_font_menu = None; + } + Message::TypographySizeSelected(level, choice) => { + if let Some(size) = choice.size { + state.typography_draft.style_mut(level).size = size; + state.typography_size_inputs[level.index()] = format!("{size}"); + } + } + Message::TypographySizeInputChanged(level, value) => { + state.typography_size_inputs[level.index()] = value.clone(); + + if let Ok(size) = value.trim().parse::() + && size > 0.0 + { + state.typography_draft.style_mut(level).size = size; + } + } + Message::TypographySizeStep(level, delta) => { + let size = (state.typography_draft.style(level).size + delta as f32).max(1.0); + state.typography_draft.style_mut(level).size = size; + state.typography_size_inputs[level.index()] = format!("{}", size); + } + Message::TypographyStyleToggled(level, flag) => { + flag.toggle(state.typography_draft.style_mut(level)); + } Message::SetCommandPaletteEnabled(value) => { state.command_palette_enabled_draft = value; } @@ -6194,7 +6754,7 @@ fn theme_picker(state: &Moorly) -> Element<'_, Message> { .cloned(); row![ - text("Themes").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Themes"), space::horizontal(), pick_list(options, selected, Message::SelectTheme).width(200), ] @@ -6203,14 +6763,272 @@ fn theme_picker(state: &Moorly) -> Element<'_, Message> { .into() } +/// Collapsible custom typography controls for the four text styles. +fn typography_panel(state: &Moorly) -> Element<'_, Message> { + let glyph = if state.typography_open { "▴" } else { "▾" }; + let header = button( + row![ + styled_text(&state.typography.h2, "Custom typography"), + space::horizontal(), + styled_text(&state.typography.h2, glyph), + ] + .width(Length::Fill) + .align_y(Alignment::Center), + ) + .on_press(Message::ToggleTypography) + .width(Length::Fill) + .padding([6, 0]) + .style(button::text); + + let mut panel = column![header].spacing(8).width(Length::Fill); + + if state.typography_open { + let mut rows = column![].spacing(14).width(Length::Fill); + + for level in TypographyLevel::ALL { + let style = state.typography_draft.style(level); + let (size_choices, size_selected) = size_choices(level, style.size); + let is_section = matches!(level, TypographyLevel::Terminal | TypographyLevel::Ssh); + + let heading: Element<'_, Message> = if is_section { + styled_text(&state.typography.h2, level.label()) + } else { + styled_text( + &state.typography.h3, + format!("Typography - {}", level.label()), + ) + }; + + let mut style_row = row![].spacing(6); + for flag in TextStyleFlag::ALL { + style_row = style_row.push(typography_style_button( + &state.typography.h3, + level, + flag, + flag.enabled(style), + )); + } + + let group = column![ + heading, + styled_text(&state.typography.h3, "Font family"), + font_family_picker(state, level), + styled_text(&state.typography.h3, "Font Size"), + row![ + pick_list(size_choices, Some(size_selected), move |choice| { + Message::TypographySizeSelected(level, choice) + }) + .width(Length::Fill), + typography_size_box(state, level), + ] + .spacing(8) + .align_y(Alignment::Center), + style_row, + ] + .spacing(4); + + rows = rows.push(group); + + if level != TypographyLevel::Ssh { + rows = rows.push(rule::horizontal(1)); + } + } + + panel = panel.push(rows); + } + + container(panel) + .width(Length::Fill) + .padding(8) + .style(theme::advanced_frame) + .into() +} + +fn font_family_choices(current: &str) -> Vec { + let mut choices: Vec = available_font_families() + .iter() + .map(|family| (*family).to_owned()) + .collect(); + + if !choices.iter().any(|choice| choice == current) { + choices.push(current.to_owned()); + } + + choices +} + +fn font_family_picker(state: &Moorly, level: TypographyLevel) -> Element<'_, Message> { + let current = state.typography_draft.style(level).font.clone(); + let open = state.typography_font_menu == Some(level); + + let field = button( + text(current.clone()) + .font(font_for_name(¤t)) + .size(state.typography.h3.size), + ) + .on_press(Message::ToggleTypographyFontMenu(level)) + .width(Length::Fill) + .padding([6, 10]) + .style(|theme: &iced::Theme, status| { + let palette = theme.extended_palette(); + let mut button = button::text(theme, status); + button.border = Border { + color: palette.background.strong.color, + width: 1.0, + radius: 4.0.into(), + }; + button + }); + + let mut column = column![field].spacing(4).width(Length::Fill); + + if open { + let mut choices = column![].spacing(2).width(Length::Fill); + + for family in font_family_choices(¤t) { + let selected = family == current; + let label = text(family.clone()) + .font(font_for_name(&family)) + .size(state.typography.h3.size); + + choices = choices.push( + button(label) + .on_press(Message::TypographyFontSelected(level, family)) + .width(Length::Fill) + .padding([4, 8]) + .style(move |theme, status| theme::selectable_row(theme, status, selected)), + ); + } + + let list = container( + scrollable(choices) + .height(Length::Fixed(180.0)) + .width(Length::Fill), + ) + .width(Length::Fill) + .padding(2) + .style(|theme: &iced::Theme| { + let palette = theme.extended_palette(); + + container::Style { + border: Border { + color: palette.background.strong.color, + width: 1.0, + radius: 4.0.into(), + }, + ..container::Style::default() + } + }); + + column = column.push(list); + } + + column.into() +} + +fn typography_style_button( + style: &TextStyle, + level: TypographyLevel, + flag: TextStyleFlag, + active: bool, +) -> Element<'static, Message> { + button(styled_text(style, flag.label())) + .on_press(Message::TypographyStyleToggled(level, flag)) + .padding([4, 8]) + .style(move |theme, status| { + let mut button = button::text(theme, status); + button.border = Border { + color: if active { + theme::ACTIVE_TAB + } else { + Color::WHITE + }, + width: 1.0, + radius: 5.0.into(), + }; + button + }) + .into() +} + +/// Current size with `-` and `+` buttons embedded at the sides. +fn typography_size_box(state: &Moorly, level: TypographyLevel) -> Element<'_, Message> { + let field = text_input("", &state.typography_size_inputs[level.index()]) + .on_input(move |value| Message::TypographySizeInputChanged(level, value)) + .padding(Padding { + top: 6.0, + right: 26.0, + bottom: 6.0, + left: 26.0, + }) + .style(theme::filter_field) + .width(Length::Fixed(116.0)); + + let step_style = |theme: &iced::Theme, status: button::Status| { + let mut button = button::text(theme, status); + + if status == button::Status::Hovered { + button.background = Some(theme.extended_palette().background.weak.color.into()); + } + + button + }; + + let minus = container( + button(text("-").size(12)) + .on_press(Message::TypographySizeStep(level, -1)) + .width(Length::Fill) + .height(Length::Fill) + .padding(0) + .style(step_style), + ) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::Start) + .align_y(Alignment::Center) + .padding(Padding::ZERO.left(0.0)) + .max_width(26.0); + + let plus = container( + button(text("+").size(12)) + .on_press(Message::TypographySizeStep(level, 1)) + .width(Length::Fill) + .height(Length::Fill) + .padding(0) + .style(step_style), + ) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::End) + .align_y(Alignment::Center) + .padding(Padding::ZERO.right(0.0)) + .max_width(26.0); + + let left_divider = container(container(rule::vertical(1)).height(Length::Fixed(18.0))) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::Start) + .align_y(Alignment::Center) + .padding(Padding::ZERO.left(26.0)); + + let right_divider = container(container(rule::vertical(1)).height(Length::Fixed(18.0))) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::End) + .align_y(Alignment::Center) + .padding(Padding::ZERO.right(26.0)); + + stack![field, minus, plus, left_divider, right_divider].into() +} + /// Collapsible area for future advanced appearance settings. fn advanced_panel(state: &Moorly) -> Element<'_, Message> { let glyph = if state.advanced_open { "▴" } else { "▾" }; let header = button( row![ - text("Advanced customization").size(SETTINGS_H2_SIZE), + styled_text(&state.typography.h2, "Advanced customization"), space::horizontal(), - text(glyph).size(SETTINGS_H2_SIZE), + styled_text(&state.typography.h2, glyph), ] .width(Length::Fill) .align_y(Alignment::Center), @@ -6232,7 +7050,7 @@ fn advanced_panel(state: &Moorly) -> Element<'_, Message> { rows = rows.push(rule::horizontal(1)); rows = rows.push( row![ - text("Custom toaster colors").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Custom toaster colors"), space::horizontal(), toggler(state.custom_toasts).on_toggle(Message::SetCustomToasts), ] @@ -6286,6 +7104,7 @@ fn advanced_panel(state: &Moorly) -> Element<'_, Message> { /// One theme color option: label on the left, clickable swatch on the right. fn color_row(state: &Moorly, option: ColorOption) -> Element<'_, Message> { color_swatch_row( + &state.typography.h3, option.label(), state.colors[option.index()], state.open_color_picker == Some(option), @@ -6297,6 +7116,7 @@ fn color_row(state: &Moorly, option: ColorOption) -> Element<'_, Message> { /// One custom-toast color option, using the same swatch control as the theme. fn toast_color_row(state: &Moorly, option: ToastColorOption) -> Element<'_, Message> { color_swatch_row( + &state.typography.h3, option.label(), state.toast_colors[option.index()], state.open_toast_color_picker == Some(option), @@ -6306,6 +7126,7 @@ fn toast_color_row(state: &Moorly, option: ToastColorOption) -> Element<'_, Mess } fn color_swatch_row<'a>( + style: &'a TextStyle, label: &'static str, color: Color, open: bool, @@ -6332,35 +7153,36 @@ fn color_swatch_row<'a>( let picker = iced_aw::color_picker(open, color, button, Message::CancelColorPicker, on_submit); - row![ - text(label).size(SETTINGS_H3_SIZE), - space::horizontal(), - picker - ] - .align_y(Alignment::Center) - .into() + row![styled_text(style, label), space::horizontal(), picker] + .align_y(Alignment::Center) + .into() } /// Shortcut bindings list. fn shortcuts_panel(state: &Moorly) -> Element<'_, Message> { let mut panel = column![ shortcut_row( + &state.typography.h3, ShortcutAction::Settings, state.shortcuts.get(ShortcutAction::Settings), ), shortcut_row( + &state.typography.h3, ShortcutAction::Quit, state.shortcuts.get(ShortcutAction::Quit) ), shortcut_row( + &state.typography.h3, ShortcutAction::CommandPalette, state.shortcuts.get(ShortcutAction::CommandPalette), ), shortcut_row( + &state.typography.h3, ShortcutAction::QuickTerminal, state.shortcuts.get(ShortcutAction::QuickTerminal), ), shortcut_row( + &state.typography.h3, ShortcutAction::QuickSsh, state.shortcuts.get(ShortcutAction::QuickSsh), ), @@ -6378,14 +7200,18 @@ fn shortcuts_panel(state: &Moorly) -> Element<'_, Message> { } /// One read-only binding row with buttons to clear or rebind it. -fn shortcut_row(action: ShortcutAction, shortcut: Option<&Shortcut>) -> Element<'static, Message> { +fn shortcut_row( + style: &TextStyle, + action: ShortcutAction, + shortcut: Option<&Shortcut>, +) -> Element<'static, Message> { let value = shortcut.map(ToString::to_string).unwrap_or_default(); // No `on_input`: the field only displays the binding. let field = text_input("None", &value).width(180).padding(6); row![ - container(text(action.label()).size(SETTINGS_H3_SIZE)).width(120), + container(styled_text(style, action.label())).width(120), field, button("Default") .on_press(Message::DefaultShortcut(action)) @@ -7067,7 +7893,7 @@ fn trash_dialog_view(state: &Moorly) -> Element<'_, Message> { .style(theme::settings_frame); container( - column![text("Trash").size(SETTINGS_H2_SIZE), header, listing] + column![styled_text(&state.typography.h2, "Trash"), header, listing] .spacing(10) .width(Length::Fill), ) @@ -7706,7 +8532,7 @@ fn start_page(state: &Moorly, window: window::Id) -> Element<'_, Message> { fn command_palette_panel(state: &Moorly) -> Element<'_, Message> { let mut rows = column![ row![ - text("Enable command palette").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Enable command palette"), space::horizontal(), toggler(state.command_palette_enabled_draft) .on_toggle(Message::SetCommandPaletteEnabled), @@ -7715,7 +8541,7 @@ fn command_palette_panel(state: &Moorly) -> Element<'_, Message> { rule::horizontal(1), row![ container(text("Command").size(13)).width(180), - text("Description").size(SETTINGS_CAPTION_SIZE), + styled_text(&state.typography.caption, "Description"), ] .spacing(12), rule::horizontal(1), @@ -7724,6 +8550,7 @@ fn command_palette_panel(state: &Moorly) -> Element<'_, Message> { for command in Command::ALL { rows = rows.push(command_palette_row( + &state.typography.caption, command.label(state), command.description(state), )); @@ -7742,6 +8569,7 @@ fn command_palette_panel(state: &Moorly) -> Element<'_, Message> { ), ] { rows = rows.push(command_palette_row( + &state.typography.caption, label.to_owned(), description.to_owned(), )); @@ -7750,10 +8578,14 @@ fn command_palette_panel(state: &Moorly) -> Element<'_, Message> { rows.into() } -fn command_palette_row(command: String, description: String) -> Element<'static, Message> { +fn command_palette_row( + style: &TextStyle, + command: String, + description: String, +) -> Element<'static, Message> { row![ container(text(command).size(13)).width(180), - text(description).size(SETTINGS_CAPTION_SIZE), + styled_text(style, description), ] .spacing(12) .into() @@ -7761,7 +8593,7 @@ fn command_palette_row(command: String, description: String) -> Element<'static, fn general_panel(state: &Moorly) -> Element<'_, Message> { row![ - text("Fade In/Fade out animation").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Fade In/Fade out animation"), space::horizontal(), toggler(state.fade_settings_draft).on_toggle(Message::SetFadeSettings), ] @@ -7772,7 +8604,7 @@ fn general_panel(state: &Moorly) -> Element<'_, Message> { /// Startup settings that are edited as a draft until saved. fn startup_panel(state: &Moorly) -> Element<'_, Message> { row![ - text("Show Start tab at startup").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Show Start tab at startup"), space::horizontal(), toggler(state.show_start_tab_draft).on_toggle(Message::SetShowStartTab), ] @@ -7784,24 +8616,25 @@ fn startup_panel(state: &Moorly) -> Element<'_, Message> { fn appearance_panel(state: &Moorly) -> Element<'_, Message> { column![ theme_picker(state), + typography_panel(state), advanced_panel(state), - text("Tab bar").size(SETTINGS_H2_SIZE), + styled_text(&state.typography.h2, "Tab bar"), row![ - text("Show new Session Button").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Show new Session Button"), space::horizontal(), toggler(state.show_new_session_button_draft) .on_toggle(Message::SetShowNewSessionButton), ] .align_y(Alignment::Center), row![ - text("Hide tab close icon").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Hide tab close icon"), space::horizontal(), toggler(state.hide_tab_close_draft).on_toggle(Message::SetHideTabClose), ] .align_y(Alignment::Center), - text("Command Palette").size(SETTINGS_H2_SIZE), + styled_text(&state.typography.h2, "Command Palette"), row![ - text("Show command palette description").size(SETTINGS_H3_SIZE), + styled_text(&state.typography.h3, "Show command palette description"), space::horizontal(), toggler(state.show_command_palette_description_draft) .on_toggle(Message::SetShowCommandPaletteDescription), @@ -7903,7 +8736,10 @@ fn settings_page(state: &Moorly, window: window::Id) -> Element<'_, Message> { .spacing(12) .width(Length::Fill) .padding(Padding::ZERO.right(if scrolls { 12.0 } else { 0.0 })); - content = content.push(text(state.settings_section.label()).size(SETTINGS_H1_SIZE)); + content = content.push(styled_text( + &state.typography.h1, + state.settings_section.label(), + )); if state.settings_section == SettingsSection::General { content = content.push(general_panel(state)); } @@ -7971,6 +8807,7 @@ fn settings_page(state: &Moorly, window: window::Id) -> Element<'_, Message> { || state.hide_tab_close_draft != state.hide_tab_close || state.show_new_session_button_draft != state.show_new_session_button || state.show_command_palette_description_draft != state.show_command_palette_description + || state.typography_draft != state.typography || state.command_palette_enabled_draft != state.command_palette_enabled || state.theme_choice != state.saved_theme_choice || state.shortcuts != state.saved_shortcuts; @@ -8661,6 +9498,11 @@ mod tests { show_new_session_button_draft: true, show_command_palette_description: true, show_command_palette_description_draft: true, + typography: super::Typography::default(), + typography_draft: super::Typography::default(), + typography_size_inputs: super::typography_size_inputs(&super::Typography::default()), + typography_open: false, + typography_font_menu: None, command_palette_enabled: true, command_palette_enabled_draft: true, advanced_open: false, @@ -8825,6 +9667,79 @@ mod tests { assert!(super::tab_strip_width(1024.0, false) > super::tab_strip_width(1024.0, true)); } + #[test] + fn custom_typography_waits_for_save_or_undo() { + let mut state = state(); + let level = super::TypographyLevel::H1; + + let _ = update( + &mut state, + Message::TypographyFontSelected(level, "Test Font".to_owned()), + ); + let _ = update( + &mut state, + Message::TypographySizeInputChanged(level, "30".to_owned()), + ); + assert_eq!(state.typography.h1.font, super::default_ui_font()); + assert_eq!(state.typography.h1.size, super::SETTINGS_H1_SIZE); + assert_eq!(state.typography_draft.h1.font, "Test Font"); + assert_eq!(state.typography_draft.h1.size, 30.0); + + let _ = update(&mut state, Message::UndoChanges); + assert_eq!(state.typography_draft.h1.font, super::default_ui_font()); + assert_eq!(state.typography_draft.h1.size, super::SETTINGS_H1_SIZE); + assert_eq!(state.typography_size_inputs[0], "22"); + + let _ = update( + &mut state, + Message::TypographyFontSelected(level, "Test Font".to_owned()), + ); + let _ = update( + &mut state, + Message::TypographySizeInputChanged(level, "30".to_owned()), + ); + let _ = update(&mut state, Message::SaveSettings); + assert_eq!(state.typography.h1.font, "Test Font"); + assert_eq!(state.typography.h1.size, 30.0); + } + + #[test] + fn default_typography_uses_platform_and_bundled_fonts() { + let typography = super::Typography::default(); + + assert_eq!(typography.h1.font, super::default_ui_font()); + assert_eq!(typography.h2.font, super::default_ui_font()); + assert_eq!(typography.terminal.font, "JetBrains Mono"); + assert_eq!(typography.ssh.font, "JetBrains Mono"); + } + + #[test] + fn typography_style_flags_toggle_draft_only() { + let mut state = state(); + let level = super::TypographyLevel::H2; + + let _ = update( + &mut state, + Message::TypographyStyleToggled(level, super::TextStyleFlag::Bold), + ); + assert!(state.typography_draft.h2.bold); + assert!(!state.typography.h2.bold); + + let _ = update(&mut state, Message::SaveSettings); + assert!(state.typography.h2.bold); + } + + #[test] + fn typography_size_choices_mark_defaults_and_custom_sizes() { + let (choices, selected) = super::size_choices(super::TypographyLevel::H1, 22.0); + assert_eq!(choices.len(), super::FONT_SIZE_STEPS.len()); + assert_eq!(selected.label, "22 px (Default)"); + + let (choices, selected) = super::size_choices(super::TypographyLevel::H1, 23.0); + assert_eq!(selected.label, ""); + assert!(choices.iter().any(|choice| choice.label == "")); + } + #[test] fn hiding_command_palette_descriptions_waits_for_save_or_undo() { let mut state = state(); @@ -8902,6 +9817,8 @@ mod tests { state.hide_tab_close = false; state.show_new_session_button = false; state.show_command_palette_description = false; + state.typography.h1.font = "Test Font".to_owned(); + state.typography.h1.size = 30.0; state.command_palette_enabled = false; state.saved_theme = theme::Preference::Dark; state.saved_theme_choice = super::ThemeChoice::BuiltIn(theme::Preference::Dark); @@ -8917,6 +9834,9 @@ mod tests { assert!(text.contains("[appearance]\ntheme = \"dark\"")); assert!(text.contains("show_new_session_button = false")); assert!(text.contains("show_command_palette_description = false")); + assert!(text.contains("[appearance.typography.h1]")); + assert!(text.contains("font = \"Test Font\"")); + assert!(text.contains("size = 30.0")); assert!(text.contains("[appearance.toast]")); assert!(text.contains("[shortcuts]")); assert!(text.contains("[command_palette]\nenabled = false")); @@ -8957,7 +9877,7 @@ mod tests { ..super::AppearanceConfig::default() }; - let (choice, preference, _, _, _, missing) = super::resolve_theme(&appearance, &[]); + let (choice, preference, _, _, _, _, missing) = super::resolve_theme(&appearance, &[]); assert!(missing); assert_eq!(preference, theme::Preference::Dark); @@ -9049,6 +9969,7 @@ mod tests { &super::DEFAULT_COLORS, true, &super::builtin_toast_colors(theme::Preference::Dark), + &super::Typography::default(), ); let text = toml::to_string(&file).unwrap(); @@ -9056,6 +9977,8 @@ mod tests { assert!(text.contains("based_on = \"dark\"")); assert!(text.contains("accent = \"#010203\"")); assert!(!text.contains("active_tab_outline")); + assert!(text.contains("[typography.h1]")); + assert!(text.contains(&format!("font = \"{}\"", super::default_ui_font()))); assert!(text.contains("[toast]")); assert!(text.contains("enabled = true")); assert!(text.contains("background = \"#")); @@ -9145,6 +10068,7 @@ mod tests { &super::DEFAULT_COLORS, true, &toast, + &super::Typography::default(), ); let text = toml::to_string_pretty(&file).unwrap(); let loaded: super::ThemeFile = toml::from_str(&text).unwrap();