This commit is contained in:
Adam Cosner 2025-01-10 22:37:40 -08:00
commit 0b0e143fe4
3 changed files with 31 additions and 14 deletions

View file

@ -114,7 +114,6 @@ tokio = { version = "1.24.2", optional = true }
tracing = "0.1.41" tracing = "0.1.41"
unicode-segmentation = "1.6" unicode-segmentation = "1.6"
url = "2.4.0" url = "2.4.0"
ustr = { version = "1.0.0", features = ["serde"] }
zbus = { version = "4.2.1", default-features = false, optional = true } zbus = { version = "4.2.1", default-features = false, optional = true }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]

View file

@ -6,14 +6,19 @@
use crate::cosmic_theme::Density; use crate::cosmic_theme::Density;
use cosmic_config::cosmic_config_derive::CosmicConfigEntry; use cosmic_config::cosmic_config_derive::CosmicConfigEntry;
use cosmic_config::{Config, CosmicConfigEntry}; use cosmic_config::{Config, CosmicConfigEntry};
use iced::font::Family;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::{LazyLock, RwLock}; use std::collections::BTreeSet;
use ustr::Ustr; use std::sync::{LazyLock, Mutex, RwLock};
/// ID for the `CosmicTk` config. /// ID for the `CosmicTk` config.
pub const ID: &str = "com.system76.CosmicTk"; pub const ID: &str = "com.system76.CosmicTk";
const MONO_FAMILY_DEFAULT: &str = "Fira Mono";
const SANS_FAMILY_DEFAULT: &str = "Fira Sans";
/// Stores static strings of the family names for `iced::Font` compatibility.
pub static FAMILY_MAP: LazyLock<Mutex<BTreeSet<&'static str>>> = LazyLock::new(|| Mutex::default());
pub static COSMIC_TK: LazyLock<RwLock<CosmicTk>> = LazyLock::new(|| { pub static COSMIC_TK: LazyLock<RwLock<CosmicTk>> = LazyLock::new(|| {
RwLock::new( RwLock::new(
CosmicTk::config() CosmicTk::config()
@ -67,12 +72,12 @@ pub fn interface_density() -> Density {
#[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_panics_doc)]
pub fn interface_font() -> FontConfig { pub fn interface_font() -> FontConfig {
COSMIC_TK.read().unwrap().interface_font COSMIC_TK.read().unwrap().interface_font.clone()
} }
#[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_panics_doc)]
pub fn monospace_font() -> FontConfig { pub fn monospace_font() -> FontConfig {
COSMIC_TK.read().unwrap().monospace_font COSMIC_TK.read().unwrap().monospace_font.clone()
} }
#[derive(Clone, CosmicConfigEntry, Debug, Eq, PartialEq)] #[derive(Clone, CosmicConfigEntry, Debug, Eq, PartialEq)]
@ -113,13 +118,13 @@ impl Default for CosmicTk {
header_size: Density::Standard, header_size: Density::Standard,
interface_density: Density::Standard, interface_density: Density::Standard,
interface_font: FontConfig { interface_font: FontConfig {
family: Ustr::from("Fira Sans"), family: SANS_FAMILY_DEFAULT.to_owned(),
weight: iced::font::Weight::Normal, weight: iced::font::Weight::Normal,
stretch: iced::font::Stretch::Normal, stretch: iced::font::Stretch::Normal,
style: iced::font::Style::Normal, style: iced::font::Style::Normal,
}, },
monospace_font: FontConfig { monospace_font: FontConfig {
family: Ustr::from("Fira Mono"), family: MONO_FAMILY_DEFAULT.to_owned(),
weight: iced::font::Weight::Normal, weight: iced::font::Weight::Normal,
stretch: iced::font::Stretch::Normal, stretch: iced::font::Stretch::Normal,
style: iced::font::Style::Normal, style: iced::font::Style::Normal,
@ -134,9 +139,9 @@ impl CosmicTk {
} }
} }
#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct FontConfig { pub struct FontConfig {
pub family: Ustr, pub family: String,
pub weight: iced::font::Weight, pub weight: iced::font::Weight,
pub stretch: iced::font::Stretch, pub stretch: iced::font::Stretch,
pub style: iced::font::Style, pub style: iced::font::Style,
@ -144,8 +149,19 @@ pub struct FontConfig {
impl From<FontConfig> for iced::Font { impl From<FontConfig> for iced::Font {
fn from(font: FontConfig) -> Self { fn from(font: FontConfig) -> Self {
let mut family_map = FAMILY_MAP.lock().unwrap();
let name: &'static str = family_map
.get(font.family.as_str())
.map(|&x| x)
.unwrap_or_else(|| {
let value = font.family.clone().leak();
family_map.insert(value);
value
});
Self { Self {
family: iced::font::Family::Name(font.family.as_str()), family: iced::font::Family::Name(name),
weight: font.weight, weight: font.weight,
stretch: font.stretch, stretch: font.stretch,
style: font.style, style: font.style,

View file

@ -1742,9 +1742,11 @@ where
{ {
let mut editor = Editor::new(unsecured_value, &mut state.cursor); let mut editor = Editor::new(unsecured_value, &mut state.cursor);
editor.insert( let character =
text.unwrap_or_default().chars().next().unwrap_or_default(), text.unwrap_or_default().chars().next().unwrap_or_default();
); if !character.is_control() {
editor.insert(character);
}
let contents = editor.contents(); let contents = editor.contents();
let unsecured_value = Value::new(&contents); let unsecured_value = Value::new(&contents);
let message = (on_input)(contents); let message = (on_input)(contents);