hlctl/src/panel.rs

82 lines
2.3 KiB
Rust

use cnx::{
text::{Attributes, Font, Padding, PagerAttributes},
widgets::{ActiveWindowTitle, Clock, Pager},
Cnx,
};
use thiserror::Error;
use crate::{
config::Config,
hlwm::{
color::{Color, X11Color},
command::CommandError,
},
};
type Result<T> = std::result::Result<T, PanelError>;
#[derive(Debug, Error)]
pub enum PanelError {
#[error("command error: [{0}]")]
CommandError(#[from] CommandError),
#[error("io lib error: [{0}]")]
IoError(#[from] std::io::Error),
#[error("cnx panel error: [{0}]")]
CnxPanelError(#[from] anyhow::Error),
}
pub fn panel(config: &Config) -> Result<()> {
let font = Font::new(&config.font_pango);
let font_bold = Font::new(&config.font_pango_bold);
let (active, normal, text) = (
config
.theme
.active_color()
.expect("could not get theme's active color"),
config
.theme
.normal_color()
.expect("could not get theme's normal color"),
config
.theme
.text_color()
.expect("could not get theme's text color"),
);
let attr = Attributes {
font: font.clone(),
fg_color: text.into(),
bg_color: Some(Color::BLACK.into()),
padding: Padding::new(10.0, 10.0, 0.0, 0.0),
};
let pager_attr = PagerAttributes {
active_attr: Attributes {
font: font_bold.clone(),
fg_color: text.into(),
bg_color: Some(active.into()),
padding: Padding::new(8.0, 8.0, 0.0, 0.0),
},
inactive_attr: Attributes {
font: font.clone(),
fg_color: Color::X11(X11Color::DimGray).into(),
bg_color: Some(Color::BLACK.into()),
padding: Padding::new(5.0, 5.0, 0.0, 0.0),
},
non_empty_attr: Attributes {
font: font.clone(),
fg_color: text.into(),
bg_color: Some(normal.into()),
padding: Padding::new(5.0, 5.0, 0.0, 0.0),
},
};
let mut cnx = Cnx::new(cnx::Position::Top);
cnx.add_widget(Pager::new(pager_attr));
cnx.add_widget(ActiveWindowTitle::new(attr.clone()));
cnx.add_widget(Clock::new(
attr.clone(),
Some(String::from("%H:%M %a %Y-%m-%d")),
));
cnx.run()?;
Ok(())
}