Skip to content

Commit 210ea08

Browse files
committed
Use a fixed spacing between buttons specified in pixels
Right now the spacing between buttons is the equivalent of one additional button distributed all over the button row as spacing. This is kind of tricky to imagine and parse when reading the code, so let's us a fixed value specified in pixels instead.
1 parent c7ca56b commit 210ea08

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use pixel_shift::PIXEL_SHIFT_WIDTH_PX;
4242

4343
const DFR_WIDTH: i32 = 2008;
4444
const DFR_HEIGHT: i32 = 64;
45+
const BUTTON_SPACING_PX: i32 = 16;
4546
const BUTTON_COLOR_INACTIVE: f64 = 0.200;
4647
const BUTTON_COLOR_ACTIVE: f64 = 0.400;
4748
const TIMEOUT_MS: i32 = 10 * 1000;
@@ -115,8 +116,7 @@ impl FunctionLayer {
115116
let c = Context::new(&surface).unwrap();
116117
c.translate(DFR_HEIGHT as f64, 0.0);
117118
c.rotate((90.0f64).to_radians());
118-
let button_width = (DFR_WIDTH as u64 - PIXEL_SHIFT_WIDTH_PX) as f64 / (self.buttons.len() + 1) as f64;
119-
let spacing_width = ((DFR_WIDTH as u64 - PIXEL_SHIFT_WIDTH_PX) as f64 - self.buttons.len() as f64 * button_width) / (self.buttons.len() - 1) as f64;
119+
let button_width = ((DFR_WIDTH - PIXEL_SHIFT_WIDTH_PX as i32) - (BUTTON_SPACING_PX * (self.buttons.len() - 1) as i32)) as f64 / self.buttons.len() as f64;
120120
let radius = 8.0f64;
121121
let bot = (DFR_HEIGHT as f64) * 0.2;
122122
let top = (DFR_HEIGHT as f64) * 0.85;
@@ -127,7 +127,7 @@ impl FunctionLayer {
127127
c.select_font_face("sans-serif", FontSlant::Normal, FontWeight::Bold);
128128
c.set_font_size(32.0);
129129
for (i, button) in self.buttons.iter().enumerate() {
130-
let left_edge = i as f64 * (button_width + spacing_width) + pixel_shift_x + (PIXEL_SHIFT_WIDTH_PX / 2) as f64;
130+
let left_edge = i as f64 * (button_width + BUTTON_SPACING_PX as f64) + pixel_shift_x + (PIXEL_SHIFT_WIDTH_PX / 2) as f64;
131131
let color = if active_buttons[i] {
132132
BUTTON_COLOR_ACTIVE
133133
} else if config.show_button_outlines {
@@ -198,9 +198,8 @@ impl LibinputInterface for Interface {
198198

199199

200200
fn button_hit(num: u32, idx: u32, x: f64, y: f64) -> bool {
201-
let button_width = DFR_WIDTH as f64 / (num + 1) as f64;
202-
let spacing_width = (DFR_WIDTH as f64 - num as f64 * button_width) / (num - 1) as f64;
203-
let left_edge = idx as f64 * (button_width + spacing_width);
201+
let button_width = (DFR_WIDTH - (BUTTON_SPACING_PX * (num - 1) as i32)) as f64 / num as f64;
202+
let left_edge = idx as f64 * (button_width + BUTTON_SPACING_PX as f64);
204203
if x < left_edge || x > (left_edge + button_width) {
205204
return false
206205
}

0 commit comments

Comments
 (0)