Skip to content

Commit b179508

Browse files
Merge remote-tracking branch 'jonas/align-pixel-grid'
2 parents 43bea0d + 55e529e commit b179508

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/main.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ 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;
48+
const ICON_SIZE: i32 = 48;
4749
const TIMEOUT_MS: i32 = 10 * 1000;
4850

4951
#[derive(Deserialize)]
@@ -82,24 +84,23 @@ impl Button {
8284
action, image: ButtonImage::Svg(svg)
8385
}
8486
}
85-
fn render(&self, c: &Context, button_left_edge: f64, button_width: f64, y_shift: f64) {
87+
fn render(&self, c: &Context, button_left_edge: f64, button_width: u64, y_shift: f64) {
8688
match &self.image {
8789
ButtonImage::Text(text) => {
8890
let extents = c.text_extents(text).unwrap();
8991
c.move_to(
90-
button_left_edge + button_width / 2.0 - extents.width() / 2.0,
91-
DFR_HEIGHT as f64 / 2.0 + extents.height() / 2.0
92+
button_left_edge + (button_width as f64 / 2.0 - extents.width() / 2.0).round(),
93+
y_shift + (DFR_HEIGHT as f64 / 2.0 + extents.height() / 2.0).round()
9294
);
9395
c.show_text(text).unwrap();
9496
},
9597
ButtonImage::Svg(svg) => {
9698
let renderer = CairoRenderer::new(&svg);
97-
let y = 0.12 * DFR_HEIGHT as f64;
98-
let size = DFR_HEIGHT as f64 - y * 2.0;
99-
let x = button_left_edge + button_width / 2.0 - size / 2.0;
99+
let x = button_left_edge + (button_width as f64 / 2.0 - (ICON_SIZE / 2) as f64).round();
100+
let y = y_shift + ((DFR_HEIGHT as f64 - ICON_SIZE as f64) / 2.0).round();
100101

101102
renderer.render_document(c,
102-
&Rectangle::new(x, y + y_shift, size, size)
103+
&Rectangle::new(x, y, ICON_SIZE as f64, ICON_SIZE as f64)
103104
).unwrap();
104105
}
105106
}
@@ -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)).floor() + 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 {
@@ -139,7 +139,7 @@ impl FunctionLayer {
139139
// draw box with rounded corners
140140
c.new_sub_path();
141141
let left = left_edge + radius;
142-
let right = left_edge + button_width - radius;
142+
let right = (left_edge + button_width.ceil()) - radius;
143143
c.arc(
144144
right,
145145
bot,
@@ -172,7 +172,7 @@ impl FunctionLayer {
172172

173173
c.fill().unwrap();
174174
c.set_source_rgb(1.0, 1.0, 1.0);
175-
button.render(&c, left_edge, button_width, pixel_shift_y);
175+
button.render(&c, left_edge, button_width.ceil() as u64, pixel_shift_y);
176176
}
177177
}
178178
}
@@ -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)