feat(appearance): add font configuration settings

This commit is contained in:
Michael Aaron Murphy 2024-10-05 00:03:02 +02:00 committed by Michael Murphy
parent 752c4bd427
commit d90074045f
8 changed files with 525 additions and 93 deletions

View file

@ -0,0 +1,368 @@
use cosmic::{
config::{CosmicTk, FontConfig},
widget::{self, settings},
Apply, Command, Element,
};
use cosmic_config::ConfigSet;
use cosmic_settings_page::Section;
use ustr::Ustr;
const INTERFACE_FONT: &str = "interface_font";
const MONOSPACE_FONT: &str = "monospace_font";
pub fn section() -> Section<crate::pages::Message> {
crate::slab!(descriptions {
font_family_txt = fl!("font-family");
font_weight_txt = fl!("font-weight");
font_stretch_txt = fl!("font-stretch");
font_style_txt = fl!("font-style");
interface_font_txt = fl!("interface-font");
monospace_font_txt = fl!("monospace-font");
});
Section::default()
.title(fl!("font-config"))
.descriptions(descriptions)
.view::<super::Page>(move |_binder, page, section| {
let descriptions = &section.descriptions;
let interface_font_family = settings::item::builder(&descriptions[font_family_txt])
.control(widget::dropdown(
&page.font_config.interface_font_families,
page.font_config.interface_font_family,
|id| super::Message::FontConfig(Message::InterfaceFontFamily(id)),
));
let interface_font_weight = settings::item::builder(&descriptions[font_weight_txt])
.control(widget::dropdown(
&page.font_config.font_weights,
page.font_config.interface_font_weight,
|id| super::Message::FontConfig(Message::InterfaceFontWeight(id)),
));
let interface_font_stretch = settings::item::builder(&descriptions[font_stretch_txt])
.control(widget::dropdown(
&page.font_config.font_stretches,
page.font_config.interface_font_stretch,
|id| super::Message::FontConfig(Message::InterfaceFontStretch(id)),
));
let interface_font_style = settings::item::builder(&descriptions[font_style_txt])
.control(widget::dropdown(
&page.font_config.font_styles,
page.font_config.interface_font_style,
|id| super::Message::FontConfig(Message::InterfaceFontStyle(id)),
));
let monospace_font_family = settings::item::builder(&descriptions[font_family_txt])
.control(widget::dropdown(
&page.font_config.monospace_font_families,
page.font_config.monospace_font_family,
|id| super::Message::FontConfig(Message::MonospaceFontFamily(id)),
));
let monospace_font_weight = settings::item::builder(&descriptions[font_weight_txt])
.control(widget::dropdown(
&page.font_config.font_weights,
page.font_config.monospace_font_weight,
|id| super::Message::FontConfig(Message::MonospaceFontWeight(id)),
));
let interface_font = settings::section()
.title(&descriptions[interface_font_txt])
.add(interface_font_family)
.add(interface_font_weight)
.add(interface_font_stretch)
.add(interface_font_style);
let monospace_font = settings::section()
.title(&descriptions[monospace_font_txt])
.add(monospace_font_family)
.add(monospace_font_weight);
widget::column::with_capacity(2)
.push(interface_font)
.push(monospace_font)
.spacing(8)
.apply(Element::from)
.map(crate::pages::Message::Appearance)
})
}
#[derive(Debug, Clone)]
pub enum Message {
InterfaceFontFamily(usize),
InterfaceFontStretch(usize),
InterfaceFontStyle(usize),
InterfaceFontWeight(usize),
LoadedFonts(Vec<String>, Vec<String>),
MonospaceFontFamily(usize),
MonospaceFontWeight(usize),
}
#[derive(Debug, Default)]
pub struct Model {
pub font_weights: Vec<String>,
pub font_stretches: Vec<String>,
pub font_styles: Vec<String>,
pub interface_font_families: Vec<String>,
pub interface_font_family: Option<usize>,
pub interface_font_weight: Option<usize>,
pub interface_font_stretch: Option<usize>,
pub interface_font_style: Option<usize>,
pub monospace_font_families: Vec<String>,
pub monospace_font_family: Option<usize>,
pub monospace_font_weight: Option<usize>,
}
impl Model {
pub fn new() -> Model {
Model {
font_weights: vec![
fl!("font-weight", "thin"),
fl!("font-weight", "extra-light"),
fl!("font-weight", "light"),
fl!("font-weight", "normal"),
fl!("font-weight", "medium"),
fl!("font-weight", "semibold"),
fl!("font-weight", "bold"),
fl!("font-weight", "extra-bold"),
fl!("font-weight", "black"),
],
font_stretches: vec![
fl!("font-stretch", "condensed"),
fl!("font-stretch", "normal"),
fl!("font-stretch", "expanded"),
],
font_styles: vec![
fl!("font-style", "normal"),
fl!("font-style", "italic"),
fl!("font-style", "oblique"),
],
interface_font_families: Vec::new(),
interface_font_family: None,
interface_font_stretch: None,
interface_font_style: None,
interface_font_weight: None,
monospace_font_families: Vec::new(),
monospace_font_family: None,
monospace_font_weight: None,
}
}
pub fn update(&mut self, message: Message) -> Command<crate::app::Message> {
match message {
Message::InterfaceFontFamily(id) => {
if let Some(family) = self.interface_font_families.get(id) {
update_config(
INTERFACE_FONT,
FontConfig {
family: Ustr::from(family),
..cosmic::config::interface_font()
},
);
self.interface_font_family = Some(id);
}
}
Message::InterfaceFontStretch(id) => {
update_config(
INTERFACE_FONT,
FontConfig {
stretch: font_stretch_by_id(id),
..cosmic::config::interface_font()
},
);
self.interface_font_stretch = Some(id);
}
Message::InterfaceFontStyle(id) => {
update_config(
INTERFACE_FONT,
FontConfig {
style: font_style_by_id(id),
..cosmic::config::interface_font()
},
);
self.interface_font_style = Some(id);
}
Message::InterfaceFontWeight(id) => {
update_config(
INTERFACE_FONT,
FontConfig {
weight: font_weight_by_id(id),
..cosmic::config::interface_font()
},
);
self.interface_font_weight = Some(id);
}
Message::LoadedFonts(interface, mono) => {
self.interface_font_families = interface;
self.monospace_font_families = mono;
let interface_font = cosmic::config::interface_font();
let monospace_font = cosmic::config::monospace_font();
self.interface_font_stretch = font_stretch_to_pos(interface_font.stretch);
self.interface_font_style = font_style_to_pos(interface_font.style);
self.interface_font_weight = font_weight_to_pos(interface_font.weight);
self.interface_font_family =
font_family_to_pos(&self.interface_font_families, &interface_font.family);
self.monospace_font_weight = font_weight_to_pos(monospace_font.weight);
self.monospace_font_family =
font_family_to_pos(&self.monospace_font_families, &monospace_font.family);
}
Message::MonospaceFontFamily(id) => {
if let Some(family) = self.monospace_font_families.get(id) {
update_config(
MONOSPACE_FONT,
FontConfig {
family: Ustr::from(family),
..cosmic::config::monospace_font()
},
);
self.monospace_font_family = Some(id);
}
}
Message::MonospaceFontWeight(id) => {
update_config(
MONOSPACE_FONT,
FontConfig {
weight: font_weight_by_id(id),
..cosmic::config::monospace_font()
},
);
self.monospace_font_weight = Some(id);
}
}
Command::none()
}
}
fn font_family_to_pos(families: &[String], family: &str) -> Option<usize> {
families.iter().position(|f| f.as_str() == family)
}
fn font_weight_by_id(id: usize) -> cosmic::iced::font::Weight {
match id {
0 => cosmic::iced::font::Weight::Thin,
1 => cosmic::iced::font::Weight::ExtraLight,
2 => cosmic::iced::font::Weight::Light,
3 => cosmic::iced::font::Weight::Normal,
4 => cosmic::iced::font::Weight::Medium,
5 => cosmic::iced::font::Weight::Semibold,
6 => cosmic::iced::font::Weight::Bold,
7 => cosmic::iced::font::Weight::ExtraBold,
8 => cosmic::iced::font::Weight::Black,
_ => cosmic::iced::font::Weight::Normal,
}
}
fn font_weight_to_pos(weight: cosmic::iced::font::Weight) -> Option<usize> {
match weight {
cosmic::iced::font::Weight::Thin => Some(0),
cosmic::iced::font::Weight::Light => Some(1),
cosmic::iced::font::Weight::ExtraLight => Some(2),
cosmic::iced::font::Weight::Normal => Some(3),
cosmic::iced::font::Weight::Medium => Some(4),
cosmic::iced::font::Weight::Semibold => Some(5),
cosmic::iced::font::Weight::Bold => Some(6),
cosmic::iced::font::Weight::ExtraBold => Some(7),
cosmic::iced::font::Weight::Black => Some(8),
}
}
fn font_stretch_by_id(id: usize) -> cosmic::iced::font::Stretch {
match id {
0 => cosmic::iced::font::Stretch::Condensed,
1 => cosmic::iced::font::Stretch::Normal,
2 => cosmic::iced::font::Stretch::Expanded,
_ => cosmic::iced::font::Stretch::Normal,
}
}
fn font_stretch_to_pos(stretch: cosmic::iced::font::Stretch) -> Option<usize> {
match stretch {
cosmic::iced::font::Stretch::Condensed => Some(0),
cosmic::iced::font::Stretch::Normal => Some(1),
cosmic::iced::font::Stretch::Expanded => Some(2),
_ => None,
}
}
fn font_style_by_id(id: usize) -> cosmic::iced::font::Style {
match id {
0 => cosmic::iced::font::Style::Normal,
1 => cosmic::iced::font::Style::Italic,
2 => cosmic::iced::font::Style::Oblique,
_ => cosmic::iced::font::Style::Normal,
}
}
fn font_style_to_pos(style: cosmic::iced::font::Style) -> Option<usize> {
match style {
cosmic::iced::font::Style::Normal => Some(0),
cosmic::iced::font::Style::Italic => Some(1),
cosmic::iced::font::Style::Oblique => Some(2),
}
}
fn update_config(variant: &str, font: FontConfig) {
if let Ok(config) = CosmicTk::config() {
_ = config.set(variant, font);
}
}
pub fn load_font_families() -> (Vec<String>, Vec<String>) {
let mut font_system = cosmic::iced::advanced::graphics::text::font_system()
.write()
.unwrap();
let (mut interface, mut mono) = font_system.raw().db().faces().fold(
(Vec::new(), Vec::new()),
|(mut interface, mut mono), face| {
if face.stretch != fontdb::Stretch::Normal
|| face.weight != fontdb::Weight::NORMAL
|| face.style != fontdb::Style::Normal
{
return (interface, mono);
}
let font_name = match face.families.first() {
Some(name) => &name.0,
None => return (interface, mono),
};
if face.monospaced {
if mono.last().map_or(true, |name| name != font_name) {
mono.push(font_name.clone());
}
} else if interface.last().map_or(true, |name| name != font_name) {
interface.push(font_name.clone());
}
(interface, mono)
},
);
interface.sort_unstable();
interface.dedup();
mono.sort_unstable();
mono.dedup();
(interface, mono)
}

View file

@ -1,6 +1,8 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
pub mod font_config;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::PathBuf;
@ -85,12 +87,13 @@ pub struct Page {
interface_text: ColorPickerModel,
control_component: ColorPickerModel,
roundness: Roundness,
density: Density,
icon_theme_active: Option<usize>,
icon_themes: IconThemes,
icon_handles: IconHandles,
font_config: font_config::Model,
theme: Theme,
theme_mode: ThemeMode,
theme_mode_config: Option<Config>,
@ -99,7 +102,6 @@ pub struct Page {
auto_switch_descs: [Cow<'static, str>; 4],
tk: CosmicTk,
tk_config: Option<Config>,
day_time: bool,
@ -132,17 +134,15 @@ impl
Option<Config>,
ThemeBuilder,
Option<Config>,
CosmicTk,
)> for Page
{
fn from(
(theme_mode_config, theme_mode, theme_builder_config, theme_builder, tk_config, tk): (
(theme_mode_config, theme_mode, theme_builder_config, theme_builder, tk_config): (
Option<Config>,
ThemeMode,
Option<Config>,
ThemeBuilder,
Option<Config>,
CosmicTk,
),
) -> Self {
let theme = if theme_mode.is_dark {
@ -150,6 +150,7 @@ impl
} else {
Theme::light_default()
};
let custom_accent = theme_builder.accent.filter(|c| {
let c = Srgba::new(c.red, c.green, c.blue, 1.0);
c != theme.palette.accent_blue
@ -171,7 +172,6 @@ impl
},
context_view: None,
roundness: theme_builder.corner_radii.into(),
density: tk.interface_density.into(),
custom_accent: ColorPickerModel::new(
&*HEX,
&*RGB,
@ -209,6 +209,7 @@ impl
theme_builder.window_hint.map(Color::from),
),
no_custom_window_hint: theme_builder.window_hint.is_none(),
font_config: font_config::Model::new(),
icon_theme_active: None,
icon_themes: Vec::new(),
icon_handles: Vec::new(),
@ -218,7 +219,6 @@ impl
theme_mode,
theme_builder,
tk_config,
tk,
day_time: true,
auto_switch_descs: [
fl!("auto-switch", "sunrise").into(),
@ -258,25 +258,14 @@ impl From<(Option<Config>, ThemeMode)> for Page {
);
let tk_config = CosmicTk::config().ok();
let tk = match tk_config.as_ref().map(CosmicTk::get_entry) {
Some(Ok(c)) => c,
Some(Err((errs, c))) => {
for err in errs {
tracing::error!(?err, "Error loading CosmicTk");
}
c
}
None => CosmicTk::default(),
};
(
Self::from((
theme_mode_config,
theme_mode,
theme_builder_config,
theme_builder,
tk_config,
tk,
)
.into()
))
}
}
@ -296,6 +285,7 @@ pub enum Message {
ExportError,
ExportFile(Arc<SelectedFiles>),
ExportSuccess,
FontConfig(font_config::Message),
GapSize(spin_button::Message),
IconTheme(usize),
ImportError,
@ -386,7 +376,10 @@ impl Page {
settings::section().add(
settings::item::builder(fl!("enable-export"))
.description(fl!("enable-export", "desc"))
.toggler(self.tk.apply_theme_global, Message::ApplyThemeGlobal)
.toggler(
cosmic::config::apply_theme_global(),
Message::ApplyThemeGlobal
)
),
// Icon theme previews
cosmic::widget::column::with_children(vec![
@ -473,8 +466,7 @@ impl Page {
self.icon_theme_active = Some(id);
if let Some(ref config) = self.tk_config {
let _ = self.tk.write_entry(config);
_ = self.tk.set_icon_theme(config, theme.id.clone());
_ = config.set::<String>("icon-theme", theme.id);
}
tokio::spawn(set_gnome_icon_theme(theme.name));
@ -681,20 +673,19 @@ impl Page {
});
}
Message::Density(d) => {
Message::Density(density) => {
needs_sync = true;
self.density = d;
if let Some(config) = self.tk_config.as_mut() {
let _density = self.tk.set_interface_density(config, d);
let _header = self.tk.set_header_size(config, d);
_ = config.set("interface_density", density);
_ = config.set("header_size", density);
}
let Some(config) = self.theme_builder_config.as_ref() else {
return Command::none();
};
let spacing = self.density.into();
let spacing = density.into();
if self
.theme_builder
@ -705,19 +696,19 @@ impl Page {
}
tokio::task::spawn(async move {
Self::update_panel_spacing(d);
Self::update_panel_spacing(density);
});
}
Message::Entered((icon_themes, icon_handles)) => {
*self = Self::default();
let active_icon_theme = cosmic::config::icon_theme();
// Set the icon themes, and define the active icon theme.
self.icon_themes = icon_themes;
self.icon_theme_active = self
.icon_themes
.iter()
.position(|theme| &theme.id == &self.tk.icon_theme);
.position(|theme| &theme.id == &active_icon_theme);
self.icon_handles = icon_handles;
}
@ -979,8 +970,8 @@ impl Page {
}
Message::ApplyThemeGlobal(enabled) => {
if let Some(tk_config) = self.tk_config.as_ref() {
_ = self.tk.set_apply_theme_global(tk_config, enabled);
if let Some(config) = self.tk_config.as_ref() {
_ = config.set("apply_theme_global", enabled);
} else {
tracing::error!("Failed to apply theme to GNOME config because the CosmicTK config does not exist.");
}
@ -997,6 +988,10 @@ impl Page {
self.day_time = day_time;
return Command::none();
}
Message::FontConfig(message) => {
return self.font_config.update(message);
}
}
// If the theme builder changed, write a new theme to disk on a background thread.
@ -1078,6 +1073,7 @@ impl Page {
}
fn reload_theme_mode(&mut self) {
let font_config = std::mem::take(&mut self.font_config);
let icon_themes = std::mem::take(&mut self.icon_themes);
let icon_handles = std::mem::take(&mut self.icon_handles);
let icon_theme_active = self.icon_theme_active.take();
@ -1088,6 +1084,7 @@ impl Page {
self.icon_themes = icon_themes;
self.icon_handles = icon_handles;
self.icon_theme_active = icon_theme_active;
self.font_config = font_config;
}
fn update_color_picker(
@ -1304,6 +1301,7 @@ impl page::Page<crate::pages::Message> for Page {
sections.insert(mode_and_colors()),
sections.insert(style()),
sections.insert(interface_density()),
sections.insert(font_config::section()),
sections.insert(window_management()),
sections.insert(experimental()),
sections.insert(reset_button()),
@ -1335,7 +1333,16 @@ impl page::Page<crate::pages::Message> for Page {
_: page::Entity,
_sender: tokio::sync::mpsc::Sender<crate::pages::Message>,
) -> Command<crate::pages::Message> {
command::future(fetch_icon_themes()).map(crate::pages::Message::Appearance)
command::batch(vec![
// Load icon themes
command::future(fetch_icon_themes()).map(crate::pages::Message::Appearance),
// Load font families
command::future(async move {
let (mono, interface) = font_config::load_font_families();
Message::FontConfig(font_config::Message::LoadedFonts(mono, interface))
})
.map(crate::pages::Message::Appearance),
])
}
fn on_leave(&mut self) -> Command<crate::pages::Message> {
@ -1768,11 +1775,11 @@ pub fn style() -> Section<crate::pages::Message> {
}
pub fn interface_density() -> Section<crate::pages::Message> {
let mut descriptions = Slab::new();
let comfortable = descriptions.insert(fl!("interface-density", "comfortable"));
let compact = descriptions.insert(fl!("interface-density", "compact"));
let spacious = descriptions.insert(fl!("interface-density", "spacious"));
crate::slab!(descriptions {
comfortable = fl!("interface-density", "comfortable");
compact = fl!("interface-density", "compact");
spacious = fl!("interface-density", "spacious");
});
Section::default()
.title(fl!("interface-density"))
@ -1780,12 +1787,14 @@ pub fn interface_density() -> Section<crate::pages::Message> {
.view::<Page>(move |_binder, page, section| {
let descriptions = &section.descriptions;
let density = cosmic::config::interface_density();
settings::section()
.title(&section.title)
.add(settings::item_row(vec![radio(
text::body(&descriptions[compact]),
Density::Compact,
Some(page.density),
Some(density),
Message::Density,
)
.width(Length::Fill)
@ -1793,7 +1802,7 @@ pub fn interface_density() -> Section<crate::pages::Message> {
.add(settings::item_row(vec![radio(
text::body(&descriptions[comfortable]),
Density::Standard,
Some(page.density),
Some(density),
Message::Density,
)
.width(Length::Fill)
@ -1801,7 +1810,7 @@ pub fn interface_density() -> Section<crate::pages::Message> {
.add(settings::item_row(vec![radio(
text::body(&descriptions[spacious]),
Density::Spacious,
Some(page.density),
Some(density),
Message::Density,
)
.width(Length::Fill)

View file

@ -1191,7 +1191,7 @@ pub fn settings() -> Section<crate::pages::Message> {
if page.wallpaper_service_config.same_on_all {
let element = text(fl!("all-displays"))
.font(cosmic::font::FONT_SEMIBOLD)
.font(cosmic::font::semibold())
.horizontal_alignment(alignment::Horizontal::Center)
.vertical_alignment(alignment::Vertical::Center)
.width(Length::Fill)

View file

@ -353,7 +353,7 @@ impl<'a, Message: Clone> Widget<Message, cosmic::Theme, Renderer> for Arrangemen
content: itoa::Buffer::new().format(id),
size: core::Pixels(24.0),
line_height: core::text::LineHeight::Relative(1.2),
font: cosmic::font::FONT_BOLD,
font: cosmic::font::bold(),
bounds: id_bounds.size(),
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,