Add Config module
- Load config from .toml file - Adjust code in slint and rust accordingly
This commit is contained in:
parent
1d222795a3
commit
95790db098
@ -7,8 +7,11 @@ authors = ["candifloss <candifloss.cc>"]
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
dirs = "6.0.0"
|
||||||
i-slint-backend-winit = { version = "1.14.1", features = ["x11"] }
|
i-slint-backend-winit = { version = "1.14.1", features = ["x11"] }
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
slint = { version = "1.14.1", features = ["backend-winit"] }
|
slint = { version = "1.14.1", features = ["backend-winit"] }
|
||||||
|
toml = "0.9.8"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
slint-build = "1.14.1"
|
slint-build = "1.14.1"
|
||||||
|
|||||||
51
src/config.rs
Normal file
51
src/config.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use dirs::config_dir;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct Config {
|
||||||
|
pub bar_width: i32,
|
||||||
|
pub bar_height: i32,
|
||||||
|
pub default_font_family: String,
|
||||||
|
pub default_font_size: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default values for the config.
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
bar_width: 1366,
|
||||||
|
bar_height: 27,
|
||||||
|
default_font_family: "IosevkaTermSlab Nerd Font Mono".to_string(),
|
||||||
|
default_font_size: 14,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
/// Load config from ~/.config/candywidgets/chocobar/chocobar.toml
|
||||||
|
/// Falls back to defaults if file is missing or broken.
|
||||||
|
pub fn load() -> Self {
|
||||||
|
let path = config_path();
|
||||||
|
|
||||||
|
// If file doesn't exist: return defaults.
|
||||||
|
if !path.exists() {
|
||||||
|
return Config::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read file; on failure return defaults.
|
||||||
|
let Ok(data) = fs::read_to_string(&path) else {
|
||||||
|
return Config::default();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parse TOML; if invalid, use defaults.
|
||||||
|
let parsed: Result<Config, _> = toml::from_str(&data);
|
||||||
|
parsed.unwrap_or_default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Config directory. Default: ~/.config/candywidgets/chocobar/chocobar.toml
|
||||||
|
fn config_path() -> PathBuf {
|
||||||
|
let base = config_dir().unwrap_or_else(|| PathBuf::from("."));
|
||||||
|
base.join("candywidgets/chocobar/chocobar.toml")
|
||||||
|
}
|
||||||
19
src/main.rs
19
src/main.rs
@ -5,11 +5,22 @@ use i_slint_backend_winit::{
|
|||||||
window::WindowAttributes,
|
window::WindowAttributes,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use slint::{LogicalPosition, LogicalSize};
|
use slint::{LogicalPosition, LogicalSize, SharedString};
|
||||||
|
mod config;
|
||||||
mod widgets;
|
mod widgets;
|
||||||
|
use config::Config;
|
||||||
slint::include_modules!();
|
slint::include_modules!();
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// Load config
|
||||||
|
let conf = Config::load();
|
||||||
|
|
||||||
|
// Bar dimensions.
|
||||||
|
let bar_height = conf.bar_height;
|
||||||
|
let bar_width = conf.bar_width;
|
||||||
|
let def_font_size = conf.default_font_size;
|
||||||
|
let def_font_fam = conf.default_font_family;
|
||||||
|
|
||||||
// Closure that adjusts winit WindowAttributes before Slint creates the window.
|
// Closure that adjusts winit WindowAttributes before Slint creates the window.
|
||||||
let window_attrs = |attrs: WindowAttributes| {
|
let window_attrs = |attrs: WindowAttributes| {
|
||||||
// Mark the X11 window as a DOCK so the WM treats it as a top bar/panel.
|
// Mark the X11 window as a DOCK so the WM treats it as a top bar/panel.
|
||||||
@ -24,15 +35,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Activate this customized backend for all Slint window creation and events.
|
// Activate this customized backend for all Slint window creation and events.
|
||||||
slint::platform::set_platform(Box::new(backend))?;
|
slint::platform::set_platform(Box::new(backend))?;
|
||||||
|
|
||||||
// Bar dimensions.
|
|
||||||
let bar_height = 27;
|
|
||||||
let bar_width = 1366;
|
|
||||||
|
|
||||||
// Create the Slint UI component.
|
// Create the Slint UI component.
|
||||||
let ui = TopBar::new()?;
|
let ui = TopBar::new()?;
|
||||||
// Set component properties.
|
// Set component properties.
|
||||||
ui.set_bar_width(bar_width);
|
ui.set_bar_width(bar_width);
|
||||||
ui.set_bar_height(bar_height);
|
ui.set_bar_height(bar_height);
|
||||||
|
ui.set_def_font_size(def_font_size);
|
||||||
|
ui.set_def_font_fam(SharedString::from(def_font_fam));
|
||||||
|
|
||||||
// Install all widget callbacks
|
// Install all widget callbacks
|
||||||
widgets::install_callbacks(&ui);
|
widgets::install_callbacks(&ui);
|
||||||
|
|||||||
@ -4,6 +4,8 @@ export component TopBar inherits Window {
|
|||||||
|
|
||||||
in property<int> bar_width;
|
in property<int> bar_width;
|
||||||
in property<int> bar_height;
|
in property<int> bar_height;
|
||||||
|
in property<int> def_font_size;
|
||||||
|
in property<string> def_font_fam;
|
||||||
|
|
||||||
callback show_clock();
|
callback show_clock();
|
||||||
|
|
||||||
@ -14,8 +16,8 @@ export component TopBar inherits Window {
|
|||||||
no-frame: true;
|
no-frame: true;
|
||||||
x: 0px;
|
x: 0px;
|
||||||
y: 0px;
|
y: 0px;
|
||||||
default-font-family: "IosevkaTermSlab Nerd Font Mono";
|
default-font-family: def_font_fam;
|
||||||
default-font-size: 14px;
|
default-font-size: def_font_size *1px;
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
x: 0px;
|
x: 0px;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user