cosmic-edit/src/config.rs

78 lines
2 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
2023-11-03 16:16:24 -06:00
use cosmic::{
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
2023-11-13 09:08:31 -07:00
theme,
2023-11-03 16:16:24 -06:00
};
2023-11-03 15:58:26 -06:00
use cosmic_text::Metrics;
2023-11-03 16:16:24 -06:00
use serde::{Deserialize, Serialize};
2023-11-03 18:45:03 -06:00
pub const CONFIG_VERSION: u64 = 1;
2023-11-13 09:08:31 -07:00
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum AppTheme {
Dark,
Light,
System,
}
impl AppTheme {
pub fn theme(&self) -> theme::Theme {
match self {
Self::Dark => theme::Theme::dark(),
Self::Light => theme::Theme::light(),
Self::System => theme::system_preference(),
}
}
}
2023-11-03 16:16:24 -06:00
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Config {
2023-11-13 09:08:31 -07:00
pub app_theme: AppTheme,
2023-11-16 09:00:48 -07:00
pub auto_indent: bool,
2023-11-03 19:00:41 -06:00
pub font_name: String,
2023-11-03 15:58:26 -06:00
pub font_size: u16,
2023-11-30 14:24:58 -07:00
pub line_numbers: bool,
pub syntax_theme_dark: String,
pub syntax_theme_light: String,
2023-11-16 08:44:23 -07:00
pub tab_width: u16,
pub vim_bindings: bool,
2023-11-01 09:44:11 -06:00
pub word_wrap: bool,
}
2023-11-03 16:16:24 -06:00
impl Default for Config {
fn default() -> Self {
Self {
2023-11-13 09:08:31 -07:00
app_theme: AppTheme::System,
2023-11-16 09:00:48 -07:00
auto_indent: true,
2023-11-03 19:00:41 -06:00
font_name: "Fira Mono".to_string(),
2023-11-03 15:58:26 -06:00
font_size: 14,
2023-11-30 14:24:58 -07:00
line_numbers: true,
syntax_theme_dark: "COSMIC Dark".to_string(),
syntax_theme_light: "COSMIC Light".to_string(),
2023-11-16 08:44:23 -07:00
tab_width: 4,
vim_bindings: false,
2023-11-01 09:44:11 -06:00
word_wrap: false,
}
}
2023-11-03 16:16:24 -06:00
}
2023-11-03 16:16:24 -06:00
impl Config {
2023-11-03 15:58:26 -06:00
// Calculate metrics from font size
pub fn metrics(&self) -> Metrics {
2023-11-03 19:00:41 -06:00
let font_size = self.font_size.max(1) as f32;
2023-11-03 15:58:26 -06:00
let line_height = (font_size * 1.4).ceil();
Metrics::new(font_size, line_height)
}
// Get current syntax theme based on dark mode
2023-11-13 09:08:31 -07:00
pub fn syntax_theme(&self) -> &str {
let dark = self.app_theme.theme().theme_type.is_dark();
if dark {
&self.syntax_theme_dark
} else {
&self.syntax_theme_light
}
}
}