Skip to content

Commit c46fa2b

Browse files
committed
nuon: Centering helpers
1 parent b942d72 commit c46fa2b

2 files changed

Lines changed: 17 additions & 22 deletions

File tree

nuon/src/lib.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ pub type Rect = euclid::default::Rect<f32>;
1010
mod settings;
1111
pub use settings::*;
1212

13+
pub fn center_y(container_h: f32, item_h: f32) -> f32 {
14+
container_h / 2.0 - item_h / 2.0
15+
}
16+
17+
pub fn center_x(container_w: f32, item_w: f32) -> f32 {
18+
container_w / 2.0 - item_w / 2.0
19+
}
20+
1321
#[derive(Default, Debug, Clone, Copy, PartialEq)]
1422
pub struct Color {
1523
pub r: f32,
@@ -890,24 +898,16 @@ impl Button {
890898

891899
if self.label.is_empty() {
892900
let icon_size = 20.0;
893-
let half_size = icon_size / 2.0;
894901
let pad_x = pad_x * 2.0;
895902

896-
let (x, y) = match self.text_justify {
897-
TextJustify::Left => {
898-
let y = rect.origin.y + rect.size.height / 2.0 - half_size;
899-
(rect.origin.x + pad_x, y)
900-
}
903+
let y = rect.origin.y + self::center_y(rect.size.height, icon_size);
904+
let x = match self.text_justify {
905+
TextJustify::Left => rect.origin.x + pad_x,
901906
TextJustify::Right => {
902907
let x = rect.origin.x + rect.size.width - icon_size;
903-
let y = rect.origin.y + rect.size.height / 2.0 - half_size;
904-
(x + pad_x, y)
905-
}
906-
TextJustify::Center => {
907-
let x = rect.origin.x + rect.size.width / 2.0 - half_size;
908-
let y = rect.origin.y + rect.size.height / 2.0 - half_size;
909-
(x, y)
908+
x + pad_x
910909
}
910+
TextJustify::Center => rect.origin.x + center_x(rect.size.width, icon_size),
911911
};
912912

913913
layer.icons.push(IconRenderElement {
@@ -1042,12 +1042,8 @@ impl Label {
10421042
}
10431043

10441044
if !self.icon.is_empty() {
1045-
let (x, y) = {
1046-
let half_size = self.font_size / 2.0;
1047-
let x = rect.origin.x + rect.size.width / 2.0 - half_size;
1048-
let y = rect.origin.y + rect.size.height / 2.0 - half_size;
1049-
(x, y)
1050-
};
1045+
let x = rect.origin.x + center_x(rect.size.width, self.font_size);
1046+
let y = rect.origin.y + center_x(rect.size.height, self.font_size);
10511047

10521048
layer.icons.push(IconRenderElement {
10531049
origin: Point::new(x, y),

nuon/src/settings.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ impl<'a> SettingsRow<'a> {
128128
.size(row_inner_w, row_h)
129129
.build(ui);
130130
} else {
131-
let gap = 8.0;
131+
let gap = 5.0;
132132
let sum_h = title_h + subtitle_h + gap;
133-
let y = row_h / 2.0 - sum_h / 2.0;
133+
let y = super::center_y(row_h, sum_h);
134134

135135
nuon::label()
136136
.y(y)
@@ -145,7 +145,6 @@ impl<'a> SettingsRow<'a> {
145145
.color([1.0, 1.0, 1.0, 0.5])
146146
.text_justify(nuon::TextJustify::Left)
147147
.font_size(subtitle_h)
148-
.y(row_h / 2.0)
149148
.size(row_inner_w, subtitle_h)
150149
.build(ui);
151150
}

0 commit comments

Comments
 (0)