cosmic-edit/src/config.rs

129 lines
3.6 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};
use std::{collections::VecDeque, path::PathBuf};
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 {
2024-05-30 15:01:29 -04:00
Self::Dark => {
let mut t = theme::system_dark();
t.theme_type.prefer_dark(Some(true));
t
}
Self::Light => {
let mut t = theme::system_light();
t.theme_type.prefer_dark(Some(false));
t
}
2023-11-13 09:08:31 -07:00
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,
pub find_case_sensitive: bool,
pub find_use_regex: bool,
2024-10-05 21:04:22 -04:00
pub find_wrap_around: 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,
2025-02-17 05:17:21 +02:00
pub font_size_zoom_step_mul_100: u16,
pub highlight_current_line: bool,
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,
find_case_sensitive: false,
find_use_regex: false,
2024-10-05 21:04:22 -04:00
find_wrap_around: true,
2025-02-20 11:13:10 -07:00
font_name: "Noto Sans Mono".to_string(),
2023-11-03 15:58:26 -06:00
font_size: 14,
2025-02-17 05:17:21 +02:00
font_size_zoom_step_mul_100: 100,
highlight_current_line: true,
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,
2024-02-02 10:42:45 -07:00
word_wrap: true,
}
}
2023-11-03 16:16:24 -06:00
}
2023-11-03 16:16:24 -06:00
impl Config {
2025-02-17 05:17:21 +02:00
pub fn font_size_adjusted(&self, zoom_adj: i8) -> f32 {
let font_size = f32::from(self.font_size).max(1.0);
let adj = f32::from(zoom_adj);
let adj_step = f32::from(self.font_size_zoom_step_mul_100) / 100.0;
(font_size + adj * adj_step).max(1.0)
}
pub fn find_regex(&self, pattern: &str) -> Result<regex::Regex, regex::Error> {
let mut builder = if self.find_use_regex {
regex::RegexBuilder::new(pattern)
} else {
regex::RegexBuilder::new(&regex::escape(pattern))
};
builder.case_insensitive(!self.find_case_sensitive);
builder.build()
}
2023-11-03 15:58:26 -06:00
// Calculate metrics from font size
2025-02-17 05:17:21 +02:00
pub fn metrics(&self, zoom_adj: i8) -> Metrics {
let font_size = self.font_size_adjusted(zoom_adj);
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
}
}
}
2024-02-23 09:55:31 -07:00
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ConfigState {
pub recent_files: VecDeque<PathBuf>,
pub recent_projects: VecDeque<PathBuf>,
}
impl Default for ConfigState {
fn default() -> Self {
Self {
recent_files: VecDeque::new(),
recent_projects: VecDeque::new(),
}
}
}