Refactor slightly

This commit is contained in:
Kenny Levinsen
2023-01-13 21:53:07 +01:00
parent b2a9475e4c
commit 3a40bb18c5
9 changed files with 513 additions and 493 deletions
+2 -2
View File
@@ -29,7 +29,7 @@ impl Backlight {
self.max = read_file_as_u64(self.device_path.join("max_brightness").as_path()).unwrap();
}
pub fn new(path: &str) -> BarWidget {
pub fn new(path: &str, font: &'static str, size: f32) -> BarWidget {
let mut dev = Backlight {
device_path: Path::new("/sys/class/backlight").to_path_buf().join(path),
cur: 0,
@@ -38,7 +38,7 @@ impl Backlight {
};
dev.update();
BarWidget::new(Box::new(dev))
BarWidget::new(Box::new(dev), font, size)
}
}
+7 -8
View File
@@ -8,9 +8,6 @@ use crate::{
widgets::{Geometry, Widget},
};
const BAR_FONT: &str = "sans";
const BAR_SIZE: f32 = 24.;
pub trait BarWidgetImpl {
fn get_dirty(&self) -> bool;
fn set_dirty(&mut self, dirty: bool);
@@ -20,15 +17,17 @@ pub trait BarWidgetImpl {
}
pub struct BarWidget {
font: &'static str,
size: f32,
geometry: Geometry,
inner_widget: Box<dyn BarWidgetImpl>,
}
impl BarWidget {
pub fn new(inner_widget: Box<dyn BarWidgetImpl>) -> BarWidget {
pub fn new(inner_widget: Box<dyn BarWidgetImpl>, font: &'static str, size: f32) -> BarWidget {
BarWidget {
geometry: Default::default(),
inner_widget,
inner_widget, font, size,
}
}
}
@@ -47,13 +46,13 @@ impl Widget for BarWidget {
}
fn draw(&mut self, fonts: &mut FontMap, view: &mut BufferView) -> Geometry {
let font = fonts.get_font(BAR_FONT, BAR_SIZE);
let font = fonts.get_font(self.font, self.size);
let fg = Color::new(1., 1., 1., 1.);
let bg = Color::new(0., 0., 0., 1.);
font.auto_draw_text(view, &bg, &fg, self.inner_widget.name())
.unwrap();
let size = BAR_SIZE.ceil() as u32;
let size = self.size.ceil() as u32;
let bar_offset = 4 * size;
let val = self.inner_widget.value();
@@ -86,7 +85,7 @@ impl Widget for BarWidget {
x: x,
y: geometry.y,
width: w,
height: BAR_SIZE.ceil() as u32,
height: self.size.ceil() as u32,
};
self.geometry
}
+2 -2
View File
@@ -111,7 +111,7 @@ pub struct Battery {
}
impl Battery {
pub fn new(ping: Ping) -> BarWidget {
pub fn new(ping: Ping, font: &'static str, size: f32) -> BarWidget {
let battery = Battery {
inner: Arc::new(Mutex::new(InnerBattery {
value: 0.,
@@ -120,7 +120,7 @@ impl Battery {
dirty: false,
};
start_monitor(battery.inner.clone(), ping);
BarWidget::new(Box::new(battery))
BarWidget::new(Box::new(battery), font, size)
}
}
+7 -7
View File
@@ -9,10 +9,9 @@ use crate::{
use chrono::{Local, Timelike};
const CLOCK_FONT: &str = "sans";
const CLOCK_SIZE: f32 = 96.;
pub struct Clock {
font: &'static str,
size: f32,
dirty: bool,
geometry: Geometry,
digit_width: u32,
@@ -20,8 +19,9 @@ pub struct Clock {
}
impl<'a> Clock {
pub fn new() -> Clock {
pub fn new(font: &'static str, size: f32) -> Clock {
Clock {
font, size,
dirty: true,
geometry: Default::default(),
digit_width: 0,
@@ -49,7 +49,7 @@ impl<'a> Widget for Clock {
let digit = self.digit_width;
let colon = self.colon_width;
let font = fonts.get_font(CLOCK_FONT, CLOCK_SIZE);
let font = fonts.get_font(self.font, self.size);
font.draw_text_fixed_width(
view,
&bg,
@@ -62,14 +62,14 @@ impl<'a> Widget for Clock {
}
fn geometry_update(&mut self, fonts: &mut FontMap, geometry: &Geometry) -> Geometry {
let font = fonts.get_font(CLOCK_FONT, CLOCK_SIZE);
let font = fonts.get_font(self.font, self.size);
self.digit_width = font.auto_widest("0123456789").unwrap();
self.colon_width = font.auto_widest(":").unwrap();
self.geometry = Geometry {
x: geometry.x,
y: geometry.y,
width: self.digit_width * 4 + self.colon_width,
height: CLOCK_SIZE.ceil() as u32,
height: self.size.ceil() as u32,
};
self.geometry
}
+8 -7
View File
@@ -9,17 +9,17 @@ use crate::{
use chrono::{Datelike, Local};
const DATE_FONT: &str = "sans";
const DATE_SIZE: f32 = 40.;
pub struct Date {
font: &'static str,
size: f32,
dirty: bool,
geometry: Geometry,
}
impl Date {
pub fn new() -> Date {
pub fn new(font: &'static str, size: f32) -> Date {
Date {
font, size,
dirty: true,
geometry: Default::default(),
}
@@ -43,7 +43,7 @@ impl<'a> Widget for Date {
let fg = Color::new(1., 1., 1., 1.);
let bg = Color::new(0., 0., 0., 1.);
let mut date_line = view.offset((0, 8)).unwrap();
let font = fonts.get_font(DATE_FONT, DATE_SIZE);
let font = fonts.get_font(self.font, self.size);
font.auto_draw_text(
&mut date_line,
&bg,
@@ -61,11 +61,12 @@ impl<'a> Widget for Date {
}
fn geometry_update(&mut self, _fonts: &mut FontMap, geometry: &Geometry) -> Geometry {
let width = (3. + 2. + 2. + 2. + 4.) * self.size / 2.;
self.geometry = Geometry {
x: geometry.x,
y: geometry.y,
width: 256,
height: DATE_SIZE.ceil() as u32,
width: width.ceil() as u32,
height: self.size.ceil() as u32,
};
self.geometry
}
+29 -32
View File
@@ -1,7 +1,11 @@
use std::cmp::{min, Ordering};
use std::collections::HashMap;
use std::default::Default;
use std::process::{exit, Command};
use std::{
cmp::{min, max, Ordering},
collections::HashMap,
default::Default,
process::{exit, Command},
sync::{Arc, Mutex},
thread,
};
use crate::{
buffer::BufferView,
@@ -17,12 +21,6 @@ use fuzzy_matcher::FuzzyMatcher;
use unicode_segmentation::UnicodeSegmentation;
use wayland_client::{protocol::wl_keyboard, WEnum};
use std::sync::{Arc, Mutex};
use std::thread;
const LAUNCHER_FONT: &str = "monospace";
const LAUNCHER_SIZE: f32 = 32.;
struct Matcher {
matches: HashMap<Desktop, i64>,
}
@@ -284,7 +282,7 @@ impl InterfaceWidget for Launcher {
let fg = Color::new(1., 1., 1., 1.);
let bg = Color::new(0., 0., 0., 1.);
let line_height = LAUNCHER_SIZE.ceil() as u32;
let line_height = intf.size.ceil() as u32;
// Draw line
let mut prompt_offset = intf.geometry.height - line_height;
@@ -292,9 +290,8 @@ impl InterfaceWidget for Launcher {
let mut prompt_border = prompt_line.limit((intf.geometry.width, 1)).unwrap();
prompt_border.memset(&fg);
// Draw prompt
let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE);
font.auto_draw_text_with_cursor(
let font = fonts.get_font(intf.font, intf.size);
let mut x_max = font.auto_draw_text_with_cursor(
&mut prompt_line,
&bg,
&fg,
@@ -303,7 +300,6 @@ impl InterfaceWidget for Launcher {
)
.unwrap();
let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE);
font.auto_draw_text(&mut prompt_line, &bg, &fg, ">")
.unwrap();
@@ -332,11 +328,11 @@ impl InterfaceWidget for Launcher {
colors.push(Color::new(0.75, 0.75, 0.75, 1.0));
}
}
font.auto_draw_text_individual_colors(&mut line, &bg, &colors, &m.name)
.unwrap();
x_max = max(x_max, font.auto_draw_text_individual_colors(&mut line, &bg, &colors, &m.name)
.unwrap());
} else {
font.auto_draw_text(&mut line, &bg, &dimfg, &m.name)
.unwrap();
x_max = max(x_max, font.auto_draw_text(&mut line, &bg, &dimfg, &m.name)
.unwrap());
}
}
@@ -347,7 +343,7 @@ impl InterfaceWidget for Launcher {
Geometry {
x: intf.geometry.x,
y: intf.geometry.y + intf.geometry.height - content_height,
width: intf.geometry.width,
width: x_max.0+1,
height: content_height,
}
}
@@ -380,7 +376,7 @@ impl InterfaceWidget for Shell {
let fg = Color::new(1., 1., 1., 1.);
let bg = Color::new(0., 0., 0., 1.);
let line_height = LAUNCHER_SIZE.ceil() as u32;
let line_height = intf.size.ceil() as u32;
// Draw line
let prompt_offset = intf.geometry.height - line_height;
@@ -389,8 +385,8 @@ impl InterfaceWidget for Shell {
prompt_border.memset(&fg);
// Draw prompt
let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE);
font.auto_draw_text_with_cursor(
let font = fonts.get_font(intf.font, intf.size);
let x_max = font.auto_draw_text_with_cursor(
&mut prompt_line,
&bg,
&fg,
@@ -398,14 +394,13 @@ impl InterfaceWidget for Shell {
intf.prompt.cursor + 3,
)
.unwrap();
let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE);
font.auto_draw_text(&mut prompt_line, &bg, &Color::new(1., 0.75, 0.5, 1.), "!")
.unwrap();
Geometry {
x: intf.geometry.x,
y: intf.geometry.y + intf.geometry.height - line_height,
width: intf.geometry.width,
width: x_max.0 + 1,
height: line_height,
}
}
@@ -443,16 +438,16 @@ impl InterfaceWidget for Calc {
let fg = Color::new(1., 1., 1., 1.);
let bg = Color::new(0., 0., 0., 1.);
let line_height = LAUNCHER_SIZE.ceil() as u32;
let line_height = intf.size.ceil() as u32;
// Draw line
let mut prompt_offset = intf.geometry.height - (LAUNCHER_SIZE.ceil() as u32);
let mut prompt_offset = intf.geometry.height - (line_height);
let mut prompt_line = view.offset((0, prompt_offset)).unwrap();
let mut prompt_border = prompt_line.limit((intf.geometry.width, 1)).unwrap();
prompt_border.memset(&fg);
// Draw prompt
let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE);
let font = fonts.get_font(intf.font, intf.size);
let res_off = font
.auto_draw_text_with_cursor(
&mut prompt_line,
@@ -463,7 +458,6 @@ impl InterfaceWidget for Calc {
)
.unwrap();
let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE);
font.auto_draw_text(&mut prompt_line, &bg, &Color::new(1., 0.75, 0.5, 1.), "=")
.unwrap();
@@ -476,10 +470,10 @@ impl InterfaceWidget for Calc {
for m in self.old.iter().rev() {
let dimfg = Color::new(0.5, 0.5, 0.5, 1.0);
if prompt_offset < LAUNCHER_SIZE.ceil() as u32 {
if prompt_offset < line_height {
break;
}
prompt_offset -= LAUNCHER_SIZE.ceil() as u32;
prompt_offset -= line_height;
let mut result_line = view.offset((0, prompt_offset)).unwrap();
font.auto_draw_text(&mut result_line, &bg, &dimfg, &m)
.unwrap();
@@ -499,6 +493,8 @@ impl InterfaceWidget for Calc {
}
struct InnerInterface {
font: &'static str,
size: f32,
dirty: bool,
geometry: Geometry,
selection: usize,
@@ -513,7 +509,7 @@ pub struct Interface {
}
impl Interface {
pub fn new() -> Interface {
pub fn new(font: &'static str, size: f32) -> Interface {
Interface {
launcher: Launcher::new(),
shell: Shell {},
@@ -522,6 +518,7 @@ impl Interface {
result: None,
},
inner: InnerInterface {
font, size,
dirty: false,
geometry: Default::default(),
selection: 0,