| 1 |
mod widgets;
|
| 1 | 2 |
mod window;
|
| 2 | 3 |
|
| 3 | 4 |
use std::os::unix::io::AsRawFd;
|
| 4 | 5 |
use pango::LayoutExt;
|
| 5 | 6 |
|
| 7 |
use widgets::Widget;
|
| 6 | 8 |
use window::{Display,Event,Size,Window};
|
| 7 | 9 |
|
| 8 | 10 |
fn main() {
|
|
| 107 | 109 |
|
| 108 | 110 |
/// Do our Cairo drawing. This needs to be refactored to allow for
|
| 109 | 111 |
/// more configurability in terms of what gets written!
|
| 110 | |
fn draw(ctx: &cairo::Context, left: &str, Size { wd: width, ..}: Size) {
|
| 111 | |
// for the current time on the right
|
| 112 | |
let now = time::now();
|
| 113 | |
|
| 112 |
fn draw(ctx: &cairo::Context, left: &str, size: Size) {
|
| 114 | 113 |
// the background is... gray-ish? this'll be configurable eventually
|
| 115 | 114 |
ctx.set_source_rgb(0.1, 0.1, 0.1);
|
| 116 | 115 |
ctx.paint();
|
|
| 120 | 119 |
|
| 121 | 120 |
// Our pango layout handles placing and drawing text
|
| 122 | 121 |
let layout = pangocairo::functions::create_layout(&ctx).unwrap();
|
| 123 | |
// for the time, we want to align it to the right
|
| 124 | |
layout.set_alignment(pango::Alignment::Right);
|
| 125 | 122 |
// allow for the whole width of the bar, minus a small fixed amount
|
| 126 | |
layout.set_width((width - 20) * pango::SCALE);
|
| 123 |
layout.set_width((size.wd - 20) * pango::SCALE);
|
| 127 | 124 |
// this should also be configurable, but Fira Mono is a good font
|
| 128 | 125 |
let mut font = pango::FontDescription::from_string("Fira Mono 18");
|
| 129 | 126 |
font.set_weight(pango::Weight::Bold);
|
| 130 | 127 |
layout.set_font_description(&font);
|
| 131 | |
// start drawing in the top-left
|
| 132 | |
ctx.move_to(10.0, 4.0);
|
| 133 | |
//The text here is just the nicely-rendered current time
|
| 134 | |
layout.set_text(&time::strftime("%a %b %d %H:%M", &now).unwrap());
|
| 135 | |
// and draw it
|
| 136 | |
pangocairo::functions::show_layout(&ctx, &layout);
|
| 137 | 128 |
|
| 138 | |
// We can reuse the same layout, but starting from the left
|
| 139 | |
layout.set_alignment(pango::Alignment::Left);
|
| 140 | |
// and now we write whatever the "current text" is...
|
| 141 | |
layout.set_text(left);
|
| 142 | |
// and draw that!
|
| 143 | |
pangocairo::functions::show_layout(&ctx, &layout);
|
| 129 |
// set up a struct with everything that widgets need to draw
|
| 130 |
let drawing = widgets::Drawing {
|
| 131 |
ctx: ctx,
|
| 132 |
lyt: &layout,
|
| 133 |
size,
|
| 134 |
};
|
| 135 |
// set up our widgets
|
| 136 |
let text = widgets::Text::new(left);
|
| 137 |
let time = widgets::Time::new();
|
| 138 |
let sbox = widgets::SmallBox;
|
| 139 |
|
| 140 |
// and create a 'config' which tells us which widgets to draw from
|
| 141 |
// the left, and which from the right
|
| 142 |
let config = widgets::Config {
|
| 143 |
left: vec![
|
| 144 |
&text as &Widget,
|
| 145 |
],
|
| 146 |
right: vec![
|
| 147 |
&sbox as &Widget,
|
| 148 |
&time as &Widget,
|
| 149 |
],
|
| 150 |
};
|
| 151 |
// and draw them!
|
| 152 |
config.draw(&drawing);
|
| 153 |
|
| 144 | 154 |
}
|