Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15a99a9

Browse files
committed
nuon: SettingsRowToggler
1 parent d1205b0 commit 15a99a9

File tree

2 files changed

+135
-14
lines changed

2 files changed

+135
-14
lines changed

nuon/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ impl<H: Hash> From<H> for Id {
9191
}
9292

9393
impl Id {
94+
const NULL: Self = Id(0);
95+
9496
pub fn hash(v: impl Hash) -> Self {
9597
let mut hasher = std::hash::DefaultHasher::new();
9698
v.hash(&mut hasher);

nuon/src/settings.rs

Lines changed: 133 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{self as nuon, TextJustify, Ui};
1+
use std::hash::Hash;
2+
3+
use crate::{self as nuon, Id, TextJustify, Ui};
24

35
pub struct SettingsSection {
46
label: String,
@@ -168,16 +170,20 @@ pub enum SettingsRowSpinResult {
168170

169171
pub struct SettingsRowSpin<'a> {
170172
row: SettingsRow<'a>,
171-
up_id: String,
172-
down_id: String,
173+
id: String,
174+
}
175+
176+
impl<'a> Default for SettingsRowSpin<'a> {
177+
fn default() -> Self {
178+
Self::new()
179+
}
173180
}
174181

175182
impl<'a> SettingsRowSpin<'a> {
176183
pub fn new() -> Self {
177184
Self {
178185
row: settings_row(),
179-
up_id: String::new(),
180-
down_id: String::new(),
186+
id: String::new(),
181187
}
182188
}
183189

@@ -191,13 +197,8 @@ impl<'a> SettingsRowSpin<'a> {
191197
self
192198
}
193199

194-
pub fn plus_id(mut self, id: impl Into<String>) -> Self {
195-
self.up_id = id.into();
196-
self
197-
}
198-
199-
pub fn minus_id(mut self, id: impl Into<String>) -> Self {
200-
self.down_id = id.into();
200+
pub fn id(mut self, id: impl Into<String>) -> Self {
201+
self.id = id.into();
201202
self
202203
}
203204

@@ -224,6 +225,15 @@ impl<'a> SettingsRowSpin<'a> {
224225

225226
let mut res = SettingsRowSpinResult::Idle;
226227

228+
let plus_id = Id::hash_with(|h| {
229+
self.id.hash(h);
230+
"-plus".hash(h);
231+
});
232+
let minus_id = Id::hash_with(|h| {
233+
self.id.hash(h);
234+
"-minus".hash(h);
235+
});
236+
227237
self.row
228238
.body(|ui, row_w, row_h| {
229239
let w = 30.0;
@@ -233,7 +243,7 @@ impl<'a> SettingsRowSpin<'a> {
233243
nuon::translate().x(row_w - w).add_to_current(ui);
234244

235245
if button()
236-
.id(self.up_id)
246+
.id(plus_id)
237247
.y(nuon::center_y(row_h, h))
238248
.size(w, h)
239249
.icon(plus_icon())
@@ -246,7 +256,7 @@ impl<'a> SettingsRowSpin<'a> {
246256
nuon::translate().x(-gap).add_to_current(ui);
247257

248258
if button()
249-
.id(self.down_id)
259+
.id(minus_id)
250260
.y(nuon::center_y(row_h, h))
251261
.size(w, h)
252262
.icon(minus_icon())
@@ -264,3 +274,112 @@ impl<'a> SettingsRowSpin<'a> {
264274
pub fn settings_row_spin<'a>() -> SettingsRowSpin<'a> {
265275
SettingsRowSpin::new()
266276
}
277+
278+
pub struct SettingsRowToggler<'a> {
279+
row: SettingsRow<'a>,
280+
id: Id,
281+
value: bool,
282+
}
283+
284+
impl<'a> Default for SettingsRowToggler<'a> {
285+
fn default() -> Self {
286+
Self::new()
287+
}
288+
}
289+
290+
impl<'a> SettingsRowToggler<'a> {
291+
pub fn new() -> Self {
292+
Self {
293+
row: settings_row(),
294+
id: Id::NULL,
295+
value: false,
296+
}
297+
}
298+
299+
pub fn id(mut self, id: impl Into<Id>) -> Self {
300+
self.id = id.into();
301+
self
302+
}
303+
304+
pub fn value(mut self, v: bool) -> Self {
305+
self.value = v;
306+
self
307+
}
308+
309+
pub fn title(mut self, label: impl Into<String>) -> Self {
310+
self.row = self.row.title(label);
311+
self
312+
}
313+
314+
pub fn subtitle(mut self, label: impl Into<String>) -> Self {
315+
self.row = self.row.subtitle(label);
316+
self
317+
}
318+
319+
pub fn build(self, ui: &mut Ui, add: &dyn Fn(&mut Ui, SettingsRow<'_>)) -> bool {
320+
let mut clicked = false;
321+
322+
let id = if self.id == Id::NULL {
323+
Id::hash_with(|h| {
324+
self.row.title.hash(h);
325+
self.row.subtitle.hash(h);
326+
})
327+
} else {
328+
self.id
329+
};
330+
331+
self.row
332+
.body(|ui, row_w, row_h| {
333+
let w = 30.0;
334+
let h = 15.0;
335+
if nuon::button()
336+
.border_radius([8.0; 4])
337+
.color(if self.value {
338+
[160, 81, 255]
339+
} else {
340+
[74, 68, 88]
341+
})
342+
.hover_color(if self.value {
343+
[170, 91, 255]
344+
} else {
345+
[87, 81, 101]
346+
})
347+
.preseed_color(if self.value {
348+
[180, 101, 255]
349+
} else {
350+
[97, 91, 111]
351+
})
352+
.x(row_w - w)
353+
.y(nuon::center_y(row_h, h))
354+
.size(w, h)
355+
.id(id)
356+
.build(ui)
357+
{
358+
clicked = true;
359+
}
360+
361+
let head_w = 12.0;
362+
let head_h = 12.0;
363+
let gap = 2.0;
364+
365+
nuon::quad()
366+
.border_radius([5.0; 4])
367+
.x(if self.value {
368+
row_w - head_w - gap
369+
} else {
370+
row_w - w + gap
371+
})
372+
.y(nuon::center_y(row_h, head_h))
373+
.size(head_w, head_h)
374+
.color([255, 255, 255])
375+
.build(ui);
376+
})
377+
.build(ui, add);
378+
379+
clicked
380+
}
381+
}
382+
383+
pub fn settings_row_toggler<'a>() -> SettingsRowToggler<'a> {
384+
SettingsRowToggler::new()
385+
}

0 commit comments

Comments
 (0)