Files
wldash/src/config.rs
T

213 lines
6.7 KiB
Rust
Raw Normal View History

2019-09-06 21:54:04 +02:00
use crate::cmd::Cmd;
2019-09-07 00:33:24 +02:00
use crate::color::Color;
2019-09-06 21:54:04 +02:00
use crate::widget;
use crate::widgets;
use serde::{Deserialize, Serialize};
use std::default::Default;
2019-09-06 22:48:50 +02:00
use std::sync::mpsc::Sender;
2019-09-06 21:54:04 +02:00
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum Widget {
Margin {
2019-09-06 22:48:50 +02:00
margins: (u32, u32, u32, u32),
2019-09-06 21:54:04 +02:00
widget: Box<Widget>,
},
Fixed {
width: u32,
height: u32,
widget: Box<Widget>,
},
HorizontalLayout(Vec<Box<Widget>>),
VerticalLayout(Vec<Box<Widget>>),
Clock {
font_size: f32,
},
Date {
font_size: f32,
},
Calendar {
font_size: f32,
sections: u32,
},
Launcher {
font_size: f32,
length: u32,
2019-09-06 22:48:50 +02:00
#[serde(default)]
2019-09-06 21:54:04 +02:00
app_opener: String,
2019-09-06 22:48:50 +02:00
#[serde(default)]
2019-09-06 21:54:04 +02:00
term_opener: String,
2019-09-06 22:48:50 +02:00
#[serde(default)]
2019-09-06 21:54:04 +02:00
url_opener: String,
},
Battery {
font_size: f32,
length: u32,
},
Backlight {
font_size: f32,
length: u32,
},
PulseAudio {
font_size: f32,
length: u32,
},
}
impl Widget {
pub fn construct(self, tx: Sender<Cmd>) -> Option<Box<dyn widget::Widget + Send>> {
match self {
2019-09-06 22:48:50 +02:00
Widget::Margin { margins, widget } => match widget.construct(tx.clone()) {
2019-09-06 21:54:04 +02:00
Some(w) => Some(widget::Margin::new(margins, w)),
None => None,
},
Widget::Fixed {
width,
height,
widget,
} => match widget.construct(tx.clone()) {
Some(w) => Some(widget::Fixed::new((width, height), w)),
None => None,
},
Widget::HorizontalLayout(widgets) => Some(widget::HorizontalLayout::new(
widgets
.into_iter()
.map(|x| x.construct(tx.clone()))
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.collect(),
)),
Widget::VerticalLayout(widgets) => Some(widget::VerticalLayout::new(
widgets
.into_iter()
.map(|x| x.construct(tx.clone()))
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.collect(),
)),
Widget::Clock { font_size } => Some(widgets::clock::Clock::new(font_size, tx.clone())),
Widget::Date { font_size } => Some(widgets::date::Date::new(font_size)),
Widget::Calendar {
font_size,
sections,
} => Some(widgets::calendar::Calendar::new(font_size, sections)),
2019-09-06 22:48:50 +02:00
Widget::Launcher {
font_size,
length,
app_opener,
term_opener,
url_opener,
} => Some(widgets::launcher::Launcher::new(
2019-09-06 21:54:04 +02:00
font_size,
length,
tx.clone(),
app_opener,
term_opener,
2019-09-06 22:48:50 +02:00
if url_opener.len() == 0 {
"xdg_open ".to_string()
} else {
url_opener
},
2019-09-06 21:54:04 +02:00
)),
Widget::Battery { font_size, length } => {
match widgets::battery::UpowerBattery::new(font_size, length, tx.clone()) {
Ok(w) => Some(w),
Err(_) => None,
}
}
Widget::Backlight { font_size, length } => {
match widgets::backlight::Backlight::new(font_size, length) {
Ok(w) => Some(w),
Err(_) => None,
}
}
Widget::PulseAudio { font_size, length } => {
match widgets::sound::PulseAudio::new(font_size, length, tx.clone()) {
Ok(w) => Some(w),
Err(_) => None,
}
}
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum OutputMode {
2019-09-06 22:48:50 +02:00
All,
Active,
2019-09-06 21:54:04 +02:00
}
impl Default for OutputMode {
2019-09-06 22:48:50 +02:00
fn default() -> Self {
OutputMode::Active
}
2019-09-06 21:54:04 +02:00
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Config {
pub widget: Widget,
pub output_mode: OutputMode,
pub scale: u32,
2019-09-07 00:24:58 +02:00
pub background: Color,
2019-09-06 21:54:04 +02:00
}
impl Default for Config {
2019-09-06 22:48:50 +02:00
fn default() -> Self {
Config {
widget: Widget::Margin {
margins: (20, 20, 20, 20),
widget: Box::new(Widget::VerticalLayout(vec![
Box::new(Widget::HorizontalLayout(vec![
Box::new(Widget::Margin {
margins: (0, 88, 0, 32),
widget: Box::new(Widget::VerticalLayout(vec![
Box::new(Widget::Date { font_size: 64.0 }),
Box::new(Widget::Clock { font_size: 256.0 }),
])),
}),
Box::new(Widget::VerticalLayout(vec![
Box::new(Widget::Margin {
margins: (0, 0, 0, 8),
widget: Box::new(Widget::Battery {
font_size: 24.0,
length: 600,
}),
}),
Box::new(Widget::Margin {
margins: (0, 0, 0, 8),
widget: Box::new(Widget::Backlight {
font_size: 24.0,
length: 600,
}),
}),
Box::new(Widget::Margin {
margins: (0, 0, 0, 8),
widget: Box::new(Widget::PulseAudio {
font_size: 24.0,
length: 600,
}),
}),
])),
])),
Box::new(Widget::Calendar {
font_size: 16.0,
sections: 3,
}),
Box::new(Widget::Launcher {
font_size: 32.0,
length: 1200,
app_opener: "".to_string(),
term_opener: "".to_string(),
url_opener: "".to_string(),
}),
])),
},
output_mode: Default::default(),
scale: 1,
2019-09-07 00:24:58 +02:00
background: Color::new(0.0, 0.0, 0.0, 0.9),
2019-09-06 22:48:50 +02:00
}
}
}