Repository navigation
Expand file tree
/
Copy pathcontrols.rs
More file actions
1075 lines (1002 loc) · 30.8 KB
/
Copy pathcontrols.rs
File metadata and controls
1075 lines (1002 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Settings controls on `rune-kit`, themed with OpenCode's design language
//! (`rune-theme-opencode`): the card, row, switch, segmented control,
//! select-style menus, buttons, keycaps, and note. Colours,
//! type, radii, elevation, and motion come from the kit's tokens.
use rune::{
Color, Edge, El, LoopSpec, Shadow, Spring, SpringPair, TextStyle, Transition, Tx, col, el, row,
text,
};
use rune_kit::{
Button, ButtonVariant, ControlSize, IconButton, IconButtonVariant, Kit, Menu, MenuItem,
Placement, SegmentedControl, Select, SelectAppearance, Switch,
};
use super::Ui;
/// Row descriptions and secondary copy.
pub(super) const DESCRIPTION: TextStyle = TextStyle::new(12.0, 440.0).lh(16.0);
const SELECT_WIDTH: f32 = 220.0;
/// The microphone field, wide enough for "System default" and a faint
/// "Studio Display Microphone".
pub(super) const DEVICE_SELECT_WIDTH: f32 = 320.0;
/// Narrower than this, a row moves its control below the copy instead of
/// squeezing the description to a word per line (tiling desktops ignore
/// the window's 860 px minimum width).
const COPY_MIN_WIDTH: f32 = 100.0;
/// An [`icon_row`]'s icon and the gap after it.
const ICON_LEAD: f32 = 28.0;
/// A settings page's measurements: raised cards of rows under muted
/// labels. The title, section labels, and row text share the gutter; cards
/// hang outward by `row_px` on both sides, so row controls end where the
/// title bar's actions do.
#[derive(Clone, Copy)]
pub(super) struct Metrics {
/// The page's text edge: the title's, section labels', and rows'.
pub(super) gutter: f32,
pub(super) section_gap: f32,
/// From a section label to its card.
pub(super) label_gap: f32,
/// A row's text inset inside its card, and how far cards hang past the
/// gutter.
pub(super) row_px: f32,
pub(super) row_h: f32,
pub(super) row_h_described: f32,
}
pub(super) const METRICS: Metrics = Metrics {
gutter: 32.0,
section_gap: 24.0,
label_gap: 8.0,
row_px: 16.0,
row_h: 48.0,
row_h_described: 60.0,
};
/// A settings page's column of sections: cards hang `row_px` past the
/// gutter, so their rows' text lands on it.
pub(super) fn page(key: &str) -> El {
col()
.key(key)
.px(METRICS.gutter - METRICS.row_px)
.gap(METRICS.section_gap)
}
/// A page of free-standing content with no cards, on the gutter itself.
pub(super) fn bare_page(key: &str) -> El {
col().key(key).px(METRICS.gutter).gap(METRICS.section_gap)
}
/// Everything a control needs to paint itself.
#[derive(Clone, Copy)]
pub(super) struct Paint {
pub(super) k: &'static Kit,
pub(super) hair: f32,
}
impl Paint {
pub(super) fn new(dark: bool, scale: f32) -> Self {
Self {
k: rune_theme_opencode::kit(dark),
hair: 1.0 / scale,
}
}
/// A section label: above its card, on the page's text edge.
pub(super) fn section_label(self, title: &str) -> El {
text(title)
.key("header")
.font(self.k.ty.ui_medium.lh(SECTION_LABEL_LINE))
.color(self.k.c.text_muted)
}
pub(super) fn text(self) -> Color {
self.k.c.text_base
}
pub(super) fn muted(self) -> Color {
self.k.c.text_muted
}
pub(super) fn faint(self) -> Color {
self.k.c.text_faint
}
pub(super) fn danger(self) -> Color {
self.k.c.danger.fg
}
pub(super) fn success(self) -> Color {
self.k.c.success.fg
}
pub(super) fn warning(self) -> Color {
self.k.c.warning.fg
}
pub(super) fn accent(self) -> Color {
self.k.c.text_accent
}
}
/// A row of a [`card`]: always there, or a slot of rows that folds away.
pub(super) enum CardRow {
Shown(Box<El>),
/// The slot stays in the card and clips its rows, so they close under
/// the rows below as those slide up, rather than fading over them, and
/// the separator above folds with them.
Folding {
key: &'static str,
rows: Vec<CardRow>,
},
}
impl From<El> for CardRow {
fn from(row: El) -> Self {
CardRow::Shown(Box::new(row))
}
}
/// A [`CardRow::Folding`] slot keyed `key`, open while it holds rows.
pub(super) fn folding<R: Into<CardRow>>(
key: &'static str,
rows: impl IntoIterator<Item = R>,
) -> CardRow {
CardRow::Folding {
key,
rows: rows.into_iter().map(Into::into).collect(),
}
}
/// A titled card of rows with hairline separators between them.
pub(super) fn section(p: Paint, key: &str, title: &str, rows: Vec<impl Into<CardRow>>) -> El {
col()
.key(key)
.gap(METRICS.label_gap)
.child(p.section_label(title).mx(METRICS.row_px))
.child(card(p, "card", rows))
}
/// A [`section`] with a control on its label's line (Advanced, Add). The
/// line keeps the label's height, so a taller control overhangs it rather
/// than pushing the card down, and the control ends where the rows'
/// controls do.
pub(super) fn section_with(
p: Paint,
key: &str,
title: &str,
accessory: El,
rows: Vec<impl Into<CardRow>>,
) -> El {
col()
.key(key)
.gap(METRICS.label_gap)
.child(
row()
.key("header-row")
.h(SECTION_LABEL_LINE)
.mx(METRICS.row_px)
.gap(8.0)
.center_y()
.child(p.section_label(title).grow(1.0))
.child(accessory),
)
.child(card(p, "card", rows))
}
const SECTION_LABEL_LINE: f32 = 16.0;
/// A raised card of rows with hairline separators between them.
pub(super) fn card(p: Paint, key: &str, rows: Vec<impl Into<CardRow>>) -> El {
raised(p, key).children(stack(p, rows.into_iter().map(Into::into).collect()))
}
/// The card's raised surface, empty.
pub(super) fn raised(p: Paint, key: &str) -> El {
let mut surface = col().key(key).fill(p.k.c.bg_base).radius(p.k.radius.lg);
for shadow in &p.k.elevation.raised {
surface = surface.box_shadow(*shadow);
}
surface
}
/// `rows` as a card's children, a separator between each pair.
fn stack(p: Paint, rows: Vec<CardRow>) -> Vec<El> {
let mut children = Vec::new();
for (index, row) in rows.into_iter().enumerate() {
match row {
CardRow::Shown(row) => {
children.extend((index > 0).then(|| separator(p, format!("sep{}", index - 1))));
children.push(*row);
}
CardRow::Folding { key, rows } => {
let lead =
(index > 0 && !rows.is_empty()).then(|| separator(p, "sep-above".into()));
let rows = lead.into_iter().chain(stack(p, rows));
children.push(
col()
.key(key)
.clip_soft(0.0)
.children(rows.map(|row| row.transition(folds()))),
);
}
}
}
children
}
/// How a folding slot's rows come and go. The slot's clip does most of the
/// work, so they fade only partway, and leave on a slower spring than the
/// slot closes on, so no sliver of them pops away.
pub(super) fn folds() -> Transition {
let mut transition = Transition::opacity().springs(SpringPair::new(
rune::Spring::new(0.3, 0.0),
rune::Spring::new(0.45, 0.0),
));
transition.insertion.opacity = 0.4;
transition.removal.opacity = 0.4;
transition
}
fn separator(p: Paint, key: String) -> El {
el().key(key)
.h(p.hair)
.ml(METRICS.row_px)
.mr(METRICS.row_px)
.fill(p.k.c.border_muted)
}
/// A setting: title and optional description on the left, its control on
/// the right.
pub(super) fn setting_row(
p: Paint,
key: &str,
title: &str,
description: Option<&str>,
control: El,
) -> El {
described_row(
p,
key,
title,
description.map(|text| (text.to_string(), p.muted())),
control,
)
}
/// A [`setting_row`] whose description has its own colour (errors, progress).
pub(super) fn described_row(
p: Paint,
key: &str,
title: &str,
description: Option<(String, Color)>,
control: El,
) -> El {
let description = description.map(|(copy, color)| {
text(copy)
.key("description")
.font(DESCRIPTION)
.color(color)
.wrap()
});
copy_row(p, key, None, title, description, control, false)
}
/// A [`setting_row`] led by an icon in the icon colour.
pub(super) fn icon_row(
p: Paint,
key: &str,
glyph: &'static rune::Icon,
title: &str,
description: Option<&str>,
control: El,
) -> El {
icon_row_tagged(p, key, glyph, title, None, description, control)
}
/// An [`icon_row`] with a small tag after its title.
pub(super) fn icon_row_tagged(
p: Paint,
key: &str,
glyph: &'static rune::Icon,
title: &str,
tag: Option<El>,
description: Option<&str>,
control: El,
) -> El {
let description = description.map(|copy| {
text(copy)
.key("description")
.font(DESCRIPTION)
.color(p.muted())
.wrap()
});
copy_row_tagged(p, key, Some(glyph), title, tag, description, control, false)
}
/// An [`icon_row`] whose description has its own colour (errors, progress).
pub(super) fn icon_described_row(
p: Paint,
key: &str,
glyph: &'static rune::Icon,
title: &str,
description: Option<(String, Color)>,
control: El,
) -> El {
let description = description.map(|(copy, color)| {
text(copy)
.key("description")
.font(DESCRIPTION)
.color(color)
.wrap()
});
copy_row(p, key, Some(glyph), title, description, control, false)
}
/// A [`select`]'s field showing its one choice, disabled.
pub(super) fn fixed_select(p: Paint, key: &str, label: &str) -> El {
Select::new::<&str>(format!("{key}-field"), &[])
.appearance(SelectAppearance::Field)
.width(SELECT_WIDTH)
.current(Some(label))
.disabled(true)
.trigger(p.k)
.key(key)
}
/// A switch an organization holds off (`crate::policy`): shown off and
/// disabled with the managed note; pressing it does nothing.
pub(super) fn managed_row(
p: Paint,
key: &str,
glyph: Option<&'static rune::Icon>,
title: &str,
) -> El {
let switch = managed_switch(p);
match glyph {
Some(glyph) => icon_row(p, key, glyph, title, Some(super::managed_note()), switch),
None => setting_row(p, key, title, Some(super::managed_note()), switch),
}
}
/// An off switch that can't be turned on.
pub(super) fn managed_switch(p: Paint) -> El {
rune_kit::Switch::new(false)
.disabled(true)
.build(p.k)
.key("switch")
.focusable(false)
}
/// A whole-row switch led by an icon.
pub(super) fn icon_toggle_row<'a>(
p: Paint,
key: &str,
glyph: &'static rune::Icon,
title: &str,
description: impl Into<Option<&'a str>>,
on: bool,
flip: impl Fn(&mut Ui) + 'static,
) -> El {
pressable_row(icon_row(
p,
key,
glyph,
title,
description.into(),
row_switch(p, on),
))
.on_press(flip)
}
pub(super) fn copy_row(
p: Paint,
key: &str,
glyph: Option<&'static rune::Icon>,
title: &str,
description: Option<El>,
control: El,
compact: bool,
) -> El {
copy_row_tagged(p, key, glyph, title, None, description, control, compact)
}
/// A [`copy_row`] with a small tag after its title.
#[allow(clippy::too_many_arguments)]
pub(super) fn copy_row_tagged(
p: Paint,
key: &str,
glyph: Option<&'static rune::Icon>,
title: &str,
tag: Option<El>,
description: Option<El>,
control: El,
compact: bool,
) -> El {
let m = METRICS;
let height = if description.is_some() && !compact {
m.row_h_described
} else {
m.row_h
};
let copy = col()
.key("copy")
.w(COPY_MIN_WIDTH)
.min_w(COPY_MIN_WIDTH)
.grow(1.0)
.gap(if compact { 0.0 } else { 2.0 })
.child(match tag {
None => text(title).key("label").font(p.k.ty.ui).color(p.text()),
Some(tag) => row()
.key("title")
.gap(6.0)
.center_y()
.child(text(title).key("label").font(p.k.ty.ui).color(p.text()))
.child(tag),
})
.children(description);
row()
.key(key)
.min_h(height)
.px(m.row_px)
.py(if compact { 4.0 } else { 8.0 })
.gaps(8.0, 24.0)
.center_y()
.justify_between()
.flex_wrap()
.radius(p.k.radius.md)
.label(title)
.child(match glyph {
None => copy,
Some(glyph) => row()
.key("lead")
.w(COPY_MIN_WIDTH + ICON_LEAD)
.min_w(COPY_MIN_WIDTH + ICON_LEAD)
.grow(1.0)
.gap(ICON_LEAD - 16.0)
.center_y()
.child(rune::icon(glyph, 16.0).key("icon").color(p.k.c.icon_base))
.child(copy),
})
.child(control)
}
/// The switch of a row that toggles: the row takes focus and presses, and
/// the switch shows its state and the focus ring.
pub(super) fn row_switch(p: Paint, on: bool) -> El {
Switch::new(on).build(p.k).key("switch").focusable(false)
}
/// A whole-row switch: the row presses, the switch shows the state.
pub(super) fn toggle_row<'a>(
p: Paint,
key: &str,
title: &str,
description: impl Into<Option<&'a str>>,
on: bool,
flip: impl Fn(&mut Ui) + 'static,
) -> El {
pressable_row(setting_row(
p,
key,
title,
description.into(),
row_switch(p, on),
))
.on_press(flip)
}
/// A settings row that toggles when pressed anywhere. Only its switch
/// reacts to the pointer; the row itself never paints a hover or press
/// fill.
pub(super) fn pressable_row(row: El) -> El {
row.role("toggle").focusable(true)
}
/// A few exclusive choices: equal segments sized for the longest label.
pub(super) fn segmented(
p: Paint,
key: &str,
labels: &[&str],
selected: usize,
pick: impl Fn(&mut Ui, usize) + Clone + 'static,
) -> El {
segmented_tx(p, key, labels, selected, move |ui, index, _| {
pick(ui, index)
})
}
/// A [`segmented`] whose pick also gets the transaction, to swap views
/// without animating.
pub(super) fn segmented_tx(
p: Paint,
key: &str,
labels: &[&str],
selected: usize,
pick: impl Fn(&mut Ui, usize, &mut Tx) + Clone + 'static,
) -> El {
SegmentedControl::new(labels, Some(selected))
.fit()
.on_pick(pick)
.build(p.k)
.key(key)
.role("segmented")
}
/// One choice of a [`select`].
pub(super) struct Choice {
pub(super) label: String,
/// Faint text after the label, such as what a choice resolves to.
pub(super) detail: Option<String>,
pub(super) selected: bool,
pub(super) choose: Box<dyn Fn(&mut Ui)>,
}
/// A select field whose choices open in an anchored menu above the page.
pub(super) fn select(
p: Paint,
key: &str,
label: &str,
open: bool,
choices: Vec<Choice>,
toggle: impl Fn(&mut Ui) + 'static,
layer: &mut Vec<El>,
) -> El {
select_sized(
p,
key,
label,
None,
open,
choices,
toggle,
layer,
SELECT_WIDTH,
)
}
/// A [`select`] `width` points wide, for labels such as device names that
/// would truncate at the usual width. A `detail` follows the field's label
/// in faint text.
#[allow(clippy::too_many_arguments)]
pub(super) fn select_sized(
p: Paint,
key: &str,
label: &str,
detail: Option<&str>,
open: bool,
choices: Vec<Choice>,
toggle: impl Fn(&mut Ui) + 'static,
layer: &mut Vec<El>,
width: f32,
) -> El {
let trigger = match detail {
None => Select::new::<&str>(format!("{key}-field"), &[])
.appearance(SelectAppearance::Field)
.width(width)
.current(Some(label))
.open(open)
.on_toggle(toggle)
.trigger(p.k),
Some(detail) => detailed_field(p, key, label, detail, open, width).on_press(toggle),
};
let mut menu = Menu::new(key)
.trigger(trigger)
.placement(Placement::BottomEnd)
.gutter(4.0)
.min_width(width)
.open(open)
.on_close(|ui: &mut Ui| ui.open = None);
for choice in choices {
let mut item = MenuItem::new(choice.label).checked(choice.selected);
if let Some(detail) = choice.detail {
item = item.shortcut(detail);
}
menu = menu.item(item.on_select(move |ui: &mut Ui| (choice.choose)(ui)));
}
menu.build(p.k, layer)
}
/// The kit's Field select trigger with a faint `detail` after its label,
/// which the kit's `Select` cannot show.
fn detailed_field(p: Paint, key: &str, label: &str, detail: &str, open: bool, width: f32) -> El {
let c = &p.k.c;
let chevron = if open {
p.k.glyph.chevron_up
} else {
p.k.glyph.chevron_down
};
let mut field = row()
.key(format!("{key}-field-trigger"))
.role("combobox")
.label(format!("{label}, {detail}"))
.center_y()
.focusable(true)
.focus_ring(1.0, 0.0, c.border_focus)
.w(width)
.h(28.0)
.pr(6.0)
.gap(8.0)
.radius(p.k.radius.md)
.fill(if open {
source_over(c.hover, c.bg_base)
} else {
c.bg_base
})
.gradient(p.k.light(0.02), p.k.light(0.0))
.child(
row()
.key("value")
.grow(1.0)
.shrink()
.pl(8.0)
.gap(6.0)
.center_y()
.child(
text(label)
.key("label")
.font(p.k.ty.ui_compact)
.color(c.text_base)
.truncate(),
)
.child(
text(detail)
.key("detail")
.font(p.k.ty.ui_compact)
.color(p.faint())
.shrink()
.truncate(),
),
)
.child(
el().size(20.0, 20.0)
.center()
.child(rune::icon(chevron, 16.0).key("chevron").color(c.icon_muted)),
);
for shadow in p.k.shadow_layers(&p.k.elevation.button_neutral) {
field = field.box_shadow(shadow);
}
if !open {
field = field.hover(c.hover);
}
field
}
/// Source-over of straight-alpha colours, for a forced hover fill.
fn source_over(top: Color, bottom: Color) -> Color {
let a = top.a + bottom.a * (1.0 - top.a);
if a <= 0.0 {
return Color::TRANSPARENT;
}
let mix = |t: f32, b: f32| (t * top.a + b * bottom.a * (1.0 - top.a)) / a;
Color {
r: mix(top.r, bottom.r),
g: mix(top.g, bottom.g),
b: mix(top.b, bottom.b),
a,
}
}
#[derive(Clone, Copy, PartialEq)]
pub(super) enum ButtonKind {
/// The one main action on screen: the kit's contrast button.
Primary,
Secondary,
/// Destructive or armed confirmations.
Danger,
}
/// Builds push buttons at Hex's sizes, which keep tighter padding and
/// corners than the kit's `ControlSize`s of the same height.
pub(super) trait PushSize {
/// 28 pt, the kit's Large height.
fn build_push(self, k: &Kit) -> El;
/// 24 pt, for actions inside list rows.
fn build_small_push(self, k: &Kit) -> El;
}
impl PushSize for Button {
fn build_push(self, k: &Kit) -> El {
self.control_size(ControlSize::Large)
.build(k)
.px(11.0)
.radius(k.radius.md)
}
fn build_small_push(self, k: &Kit) -> El {
self.control_size(ControlSize::Regular)
.build(k)
.px(9.0)
.radius(k.radius.sm)
}
}
pub(super) fn button(
p: Paint,
key: &str,
label: &str,
kind: ButtonKind,
press: impl Fn(&mut Ui, &mut Tx) + 'static,
) -> El {
let variant = match kind {
ButtonKind::Primary => ButtonVariant::Contrast,
ButtonKind::Secondary => ButtonVariant::Neutral,
ButtonKind::Danger => ButtonVariant::Danger,
};
Button::new(label)
.variant(variant)
.on_press_tx(press)
.build_push(p.k)
.key(key)
}
/// An icon-only Secondary button the height of [`button`].
pub(super) fn icon_button(
p: Paint,
key: &str,
icon: &'static rune::Icon,
label: &str,
press: impl Fn(&mut Ui) + 'static,
) -> El {
IconButton::new(icon)
.variant(IconButtonVariant::Neutral)
.control_size(ControlSize::Large)
.label(label)
.on_press(press)
.build(p.k)
.key(key)
}
/// How a modal's surface enters and leaves: scaled from 0.96 about its
/// centre, with a fade. Its text scales with its boxes, so the panel moves
/// as one surface.
pub(super) fn modal_transition(p: Paint) -> Transition {
Transition::scale(0.96).springs(overlay_springs(p))
}
/// The scrim behind a modal: the surface's springs, fading only.
pub(super) fn modal_scrim_transition(p: Paint) -> Transition {
Transition::opacity().springs(overlay_springs(p))
}
fn overlay_springs(p: Paint) -> SpringPair {
SpringPair::new(p.k.motion.overlay, p.k.motion.overlay)
}
/// How big a keycap is: `Small` in a row's tray, `Large` in the explainer.
///
/// Every key is a low-profile scissor key, after Apple's Magic Keyboard: a
/// flat face on a cap whose skirt shows below it. A press sinks the face
/// into the skirt; the key's bounds never change.
#[derive(Clone, Copy, PartialEq)]
pub(super) enum KeySize {
Small,
Large,
}
impl KeySize {
/// Height, skirt included.
pub(super) const fn height(self) -> f32 {
match self {
Self::Small => 22.0,
Self::Large => 28.0,
}
}
/// A one-glyph key is square.
pub(super) const fn min_width(self) -> f32 {
self.height()
}
/// The skirt showing below a key at rest.
const fn skirt(self) -> f32 {
match self {
Self::Small => 1.5,
Self::Large => 2.0,
}
}
/// A press sinks the face most of the way, leaving a hairline of skirt.
const fn travel(self) -> f32 {
self.skirt() - KEY_RIM
}
const fn face_radius(self) -> f32 {
match self {
Self::Small => 4.0,
Self::Large => 5.0,
}
}
pub(super) fn legend(self) -> TextStyle {
match self {
Self::Small => TextStyle::new(11.0, 400.0).lh(14.0),
Self::Large => TextStyle::new(13.0, 400.0).lh(16.0),
}
}
/// Beside the legend on the face.
const fn face_padding(self) -> f32 {
match self {
Self::Small => 6.0,
Self::Large => 8.0,
}
}
}
/// The cap's rim beside and above the face.
const KEY_RIM: f32 = 0.5;
/// A face sinks fast and comes back up a little slower, both without bounce.
const KEY_DOWN: Spring = Spring::new(0.08, 0.0);
const KEY_UP: Spring = Spring::new(0.18, 0.0);
/// Between the keycaps of a chord.
pub(super) const KEY_GAP: f32 = 3.0;
/// Between a tray's edge and its keys: a `Small` tray is as tall as a
/// button.
pub(super) const TRAY_PADDING: f32 = 3.0;
/// The keys' material, from the theme's tokens: in light a warm off-white
/// face (the warning cream warms the neutral layers) on a cap that shades
/// to warm grey; in dark a space-black face on a cap that falls to the
/// deepest background.
pub(super) struct KeyMaterial {
pub(super) cap_top: Color,
pub(super) cap_bottom: Color,
pub(super) face: Color,
pub(super) ink: Color,
/// The tight shadow under the cap and the soft one it casts at rest.
pub(super) contact: f32,
pub(super) cast: f32,
}
impl KeyMaterial {
pub(super) fn new(k: &Kit) -> Self {
let c = &k.c;
if k.dark {
let face = c.bg_base.mix(c.layer_01, 0.6);
Self {
cap_top: c.layer_03.mix(c.layer_04, 0.35),
cap_bottom: c.bg_deep,
face,
ink: c.text_base.mix(c.text_faint, 0.15),
contact: 0.8,
cast: 0.5,
}
} else {
let warm = c.warning.bg;
Self {
cap_top: c.white,
cap_bottom: c.layer_04.mix(c.text_faint, 0.28).mix(c.warning.fg, 0.05),
face: c.layer_02.mix(warm, 0.35),
ink: c.text_base.mix(c.text_muted, 0.15),
contact: 0.2,
cast: 0.12,
}
}
}
}
/// One tray of keys per shortcut: the keys sit in a well recessed into
/// the surface, lit from above so its top edge falls into shadow. The well
/// clips its keys, so while it springs to a new shortcut's width they never
/// poke out over what is beside it.
pub(super) fn key_tray(
p: Paint,
key: &str,
size: KeySize,
keys: impl IntoIterator<Item = El>,
) -> El {
let k = p.k;
let c = &k.c;
let (top, bottom, rim, well) = if k.dark {
(
c.bg_deep,
c.bg_base.mix(c.layer_01, 0.7),
k.light(0.07),
0.6,
)
} else {
(
c.layer_04.mix(c.layer_03, 0.35),
c.layer_03.mix(c.layer_02, 0.6),
k.dark_a(0.07),
0.14,
)
};
// Concentric with the caps inside it.
let radius = size.face_radius() + KEY_RIM + TRAY_PADDING;
row()
.key(key)
.padding(TRAY_PADDING)
.gap(KEY_GAP)
.center_y()
.radius(radius)
.continuous_corners()
.clip_rounded(radius)
.fill(bottom)
.gradient(top, bottom)
.border(p.hair.max(0.5), rim)
.box_shadow(Shadow {
y: 1.0,
blur: 2.5,
color: k.dark_a(well),
inset: true,
..Shadow::default()
})
.children(keys)
}
/// A shortcut's keys at rest, in one `Small` tray. Keys a new shortcut
/// adds rise into the well, and keys it drops leave through its top.
pub(super) fn keycaps(p: Paint, key: &str, parts: &[String]) -> El {
key_tray(
p,
key,
KeySize::Small,
parts.iter().enumerate().map(|(index, part)| {
keycap(p, index, part, KeySize::Small, false).transition(tray_roll())
}),
)
}
/// The width [`keycaps`] draws `parts` at, with `measure` giving a
/// legend's width.
#[cfg(hex_capture)]
pub(super) fn keycaps_width(parts: &[String], measure: impl Fn(&str, TextStyle) -> f32) -> f32 {
let size = KeySize::Small;
let caps: f32 = parts
.iter()
.map(|part| {
(measure(part, size.legend()) + 2.0 * (size.face_padding() + KEY_RIM))
.max(size.min_width())
})
.sum();
caps + KEY_GAP * parts.len().saturating_sub(1) as f32 + 2.0 * TRAY_PADDING
}
/// In from the well's bottom, out through its top, inside its clip.
pub(super) fn tray_roll() -> Transition {
Transition::push(Edge::Bottom)
}
/// One key: a face on a cap whose skirt shows below it. A press sinks the
/// face into the skirt, tightens the cap's shadow and shades the face
/// toward its bottom edge.
pub(super) fn keycap(p: Paint, index: usize, label: &str, size: KeySize, down: bool) -> El {
let k = p.k;
let m = KeyMaterial::new(k);
let pressed = if down { 1.0 } else { 0.0 };
let travel = size.travel() * pressed;
let face_height = size.height() - KEY_RIM - size.skirt();
// Shadow offsets never spring, so the resting and pressed casts are two
// layers that trade strength.
let cast = |y: f32, blur: f32, strength: f32| Shadow {
y,
blur,
color: k.dark_a(m.cast * strength),
..Shadow::default()
};
let cap = col()
.key("cap")
.px(KEY_RIM)
.pt(KEY_RIM)
.pb(size.skirt() - travel)
.radius(size.face_radius() + KEY_RIM)
.continuous_corners()
.fill(m.cap_bottom)
.gradient(m.cap_top, m.cap_bottom)
.shadow(0.5, 0.5, k.dark_a(m.contact))
.box_shadow(cast(size.skirt(), 2.0 * size.skirt(), 1.0 - pressed))
.box_shadow(cast(KEY_RIM, 2.0, pressed))
.child(
row()
.key("face")
.min_w(size.min_width() - 2.0 * KEY_RIM)
.h(face_height)
.px(size.face_padding())
.center()
.radius(size.face_radius())
.continuous_corners()
.fill(m.face)
.gradient(
k.light(0.03 - 0.03 * pressed),
k.dark_a(0.02 + 0.06 * pressed),
)
.child(
text(label.to_string())
.key("label")