1
- use crate :: { self as nuon, TextJustify , Ui } ;
1
+ use std:: hash:: Hash ;
2
+
3
+ use crate :: { self as nuon, Id , TextJustify , Ui } ;
2
4
3
5
pub struct SettingsSection {
4
6
label : String ,
@@ -168,16 +170,20 @@ pub enum SettingsRowSpinResult {
168
170
169
171
pub struct SettingsRowSpin < ' a > {
170
172
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
+ }
173
180
}
174
181
175
182
impl < ' a > SettingsRowSpin < ' a > {
176
183
pub fn new ( ) -> Self {
177
184
Self {
178
185
row : settings_row ( ) ,
179
- up_id : String :: new ( ) ,
180
- down_id : String :: new ( ) ,
186
+ id : String :: new ( ) ,
181
187
}
182
188
}
183
189
@@ -191,13 +197,8 @@ impl<'a> SettingsRowSpin<'a> {
191
197
self
192
198
}
193
199
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 ( ) ;
201
202
self
202
203
}
203
204
@@ -224,6 +225,15 @@ impl<'a> SettingsRowSpin<'a> {
224
225
225
226
let mut res = SettingsRowSpinResult :: Idle ;
226
227
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
+
227
237
self . row
228
238
. body ( |ui, row_w, row_h| {
229
239
let w = 30.0 ;
@@ -233,7 +243,7 @@ impl<'a> SettingsRowSpin<'a> {
233
243
nuon:: translate ( ) . x ( row_w - w) . add_to_current ( ui) ;
234
244
235
245
if button ( )
236
- . id ( self . up_id )
246
+ . id ( plus_id )
237
247
. y ( nuon:: center_y ( row_h, h) )
238
248
. size ( w, h)
239
249
. icon ( plus_icon ( ) )
@@ -246,7 +256,7 @@ impl<'a> SettingsRowSpin<'a> {
246
256
nuon:: translate ( ) . x ( -gap) . add_to_current ( ui) ;
247
257
248
258
if button ( )
249
- . id ( self . down_id )
259
+ . id ( minus_id )
250
260
. y ( nuon:: center_y ( row_h, h) )
251
261
. size ( w, h)
252
262
. icon ( minus_icon ( ) )
@@ -264,3 +274,112 @@ impl<'a> SettingsRowSpin<'a> {
264
274
pub fn settings_row_spin < ' a > ( ) -> SettingsRowSpin < ' a > {
265
275
SettingsRowSpin :: new ( )
266
276
}
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