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
// Copyright 2026 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0
use std::any::TypeId;
use accesskit::{Node, Role};
use include_doc_path::include_doc_path;
use masonry_core::debug_panic;
use smallvec::SmallVec;
use tracing::{Span, trace_span};
use crate::core::{
AccessCtx, ArcStr, ChildrenIds, LayoutCtx, MeasureCtx, NewWidget, NoAction, PaintCtx,
PropertiesMut, PropertiesRef, Property, RegisterCtx, Update, UpdateCtx, UsesProperty, Widget,
WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Cap, Join, Line, Size, Stroke};
use crate::layout::LenReq;
use crate::layout::{LayoutSize, Length, SizeDef, UnitPoint};
use crate::properties::ContentColor;
use crate::widgets::Label;
// TODO: Use snap-aware hairline layout/paint once Masonry has presentation snapping helpers.
/// A line to divide your content.
///
/// By default it is a thin solid line. A dash pattern can be configured with [`dash_pattern`].
///
/// There is support for a [`label`] and even arbitrary [`content`].
///
/// It has the following properties:
/// * [`ContentColor`] - defines the color of the dividing line.
///
/// [`dash_pattern`]: Self::dash_pattern
/// [`label`]: Self::label
/// [`content`]: Self::content
/// [`ContentColor`]: ContentColor
///
#[doc = concat!(
",
")",
)]
pub struct Divider {
axis: Axis,
/// No set thickness means hairline - 1 device pixel.
thickness: Option<Length>,
dash_fit: DashFit,
dash_pattern: SmallVec<[Length; 2]>,
start_cap: Cap,
end_cap: Cap,
placement: Placement,
content: Option<WidgetPod<dyn Widget>>,
pad: Length,
lines: SmallVec<[LineLayout; 2]>,
}
/// Describes the strategy how to display a dashed divider.
///
/// It has no effect on a solid line divider.
#[derive(Default, Copy, Clone, Debug, PartialEq)]
pub enum DashFit {
/// Clip the end edge.
///
/// The start edge is flush with a dash.
/// Depending on the dash pattern and available space, the end edge might have a larger gap
/// or the end edge might have a shorter partially drawn dash.
///
/// Can look unpolished in static scenarios due to a break in the pattern at the end.
/// Has stable visuals when resizing the divider,
/// with end edge dashes gradually appearing or disappearing.
#[default]
Clip,
/// Stretch the inner gap sizes to ensure only whole dashes are shown.
///
/// The start and end edges are flush with dashes.
///
/// Has a polished look in static scenarios due to a consistent dash and gap pattern.
/// Has a wobble effect when resizing the divider,
/// subtle at the edges and significant in the middle.
Stretch,
/// Draw only whole dashes aligned to the start.
///
/// The start edge will be flush with a dash.
/// The end edge will have a gap.
///
/// Has a good look in static scenarios due to a consistent dash pattern. However,
/// if there is a background color or a border then those may reveal a larger gap at the end.
/// For such static scenarios [`Center`] and [`Stretch`] will work better.
/// Has stable visuals when resizing the divider,
/// with end edge dashes popping in and out of existence.
///
/// [`Center`]: DashFit::Center
/// [`Stretch`]: DashFit::Stretch
Start,
/// Draw only whole dashes aligned to the center.
///
/// The start and edge edges will have equally sized gaps.
///
/// Has a polished look in static scenarios due to a consistent dash and gap pattern.
/// Has a wobble effect when resizing the divider,
/// uniformly affecting the whole line.
Center,
/// Draw only whole dashes aligned to the end.
///
/// The start edge will have a gap.
/// The end edge will be flush with a dash.
///
/// Has a good look in static scenarios due to a consistent dash pattern. However,
/// if there is a background color or a border then those may reveal a larger gap at the start.
/// For such static scenarios [`Center`] and [`Stretch`] will work better.
/// Has a dragging effect when resizing the divider,
/// with start edge dashes popping in and out of existence.
///
/// [`Center`]: DashFit::Center
/// [`Stretch`]: DashFit::Stretch
End,
}
/// Describes the strategy where to place the divider's content.
#[derive(Default, Copy, Clone, Debug, PartialEq)]
pub enum Placement {
/// Place the content at the start.
///
/// There will be a single divider line after the content until the end edge.
Start,
/// Place the content at the center.
///
/// There will be two divider lines on each side of the content until the start/end edges.
#[default]
Center,
/// Place the content at the end.
///
/// There will be a single divider line from the start until the content's start edge.
End,
}
/// Cached layout of divider lines.
#[derive(Clone, Debug)]
struct LineLayout {
line: Line,
dashes: SmallVec<[f64; 4]>,
}
// --- MARK: BUILDERS
impl Divider {
/// Creates a new [`Divider`] parallel with the given `axis`.
pub fn new(axis: Axis) -> Self {
Self {
axis,
thickness: None,
dash_fit: DashFit::default(),
dash_pattern: SmallVec::default(),
start_cap: Cap::Butt,
end_cap: Cap::Butt,
placement: Placement::default(),
content: None,
pad: Length::const_px(5.),
lines: SmallVec::default(),
}
}
/// Creates a new horizontal [`Divider`].
pub fn horizontal() -> Self {
Self::new(Axis::Horizontal)
}
/// Creates a new vertical [`Divider`].
pub fn vertical() -> Self {
Self::new(Axis::Vertical)
}
/// Returns `self` with the given line `thickness`.
pub fn thickness(mut self, thickness: Length) -> Self {
self.thickness = Some(thickness);
self
}
/// Returns `self` with the line thickness set to hairline, i.e. 1 device pixel.
pub fn hairline(mut self) -> Self {
self.thickness = None;
self
}
/// Returns `self` with the given `dash_fit`.
pub fn dash_fit(mut self, dash_fit: DashFit) -> Self {
self.dash_fit = dash_fit;
self
}
/// Returns `self` with the given `dash_pattern`.
///
/// The pattern defines the lengths of dashes in alternating on/off order.
/// * `10` - 10px dashes and 10px gaps
/// * `10, 5` - 10px dashes with 5px gaps
/// * `10, 5, 20, 30` - 10 px dash, 5px gap, 20px dash, 30px gap
///
/// The pattern can be even longer and in any case will repeat to fill the whole divider space.
///
/// The pattern must contain an even number of lengths. With exceptions for zero and one, where
/// zero lengths means a solid line and one length will be used for both the dash and the gap.
/// When given any other uneven number of the lengths, the last length will be ignored.
///
/// # Panics
///
/// Panics if `dash_pattern` contains an uneven number of entries of 3 or more
/// and debug assertions are enabled.
pub fn dash_pattern(mut self, dash_pattern: &[Length]) -> Self {
let mut dash_pattern = SmallVec::from_slice(dash_pattern);
// Paint code assumes an even number for simplicity of implementation.
let len = dash_pattern.len();
if len == 1 {
dash_pattern.push(dash_pattern[0]);
} else if len > 0 && !len.is_multiple_of(2) {
debug_panic!(
"The divider dash pattern must have an even number of lengths. Received {len}"
);
dash_pattern.pop();
}
self.dash_pattern = dash_pattern;
self
}
/// Returns `self` with the given `cap` used both for start and end.
///
/// Use [`start_cap`] or [`end_cap`] to set different edge caps.
///
/// Defaults to [`Cap::Butt`].
///
/// [`start_cap`]: Self::start_cap
/// [`end_cap`]: Self::end_cap
pub fn cap(mut self, cap: Cap) -> Self {
self.start_cap = cap;
self.end_cap = cap;
self
}
/// Returns `self` with the given starting `cap`.
///
/// Use [`cap`] to set the cap for both the start and the end.
///
/// Defaults to [`Cap::Butt`].
///
/// [`cap`]: Self::cap
pub fn start_cap(mut self, cap: Cap) -> Self {
self.start_cap = cap;
self
}
/// Returns `self` with the given ending `cap`.
///
/// Use [`cap`] to set the cap for both the start and the end.
///
/// Defaults to [`Cap::Butt`].
///
/// [`cap`]: Self::cap
pub fn end_cap(mut self, cap: Cap) -> Self {
self.end_cap = cap;
self
}
/// Returns `self` with the given content `placement`.
///
/// Defaults to [`Placement::Center`].
pub fn placement(mut self, placement: Placement) -> Self {
self.placement = placement;
self
}
/// Returns `self` with the given `content`.
///
/// For simple text labels use [`label`].
///
/// [`label`]: Self::label
pub fn content(mut self, content: NewWidget<impl Widget + ?Sized>) -> Self {
self.content = Some(content.erased().to_pod());
self
}
/// Returns `self` with the given `text` label.
///
/// For more control over the label use [`content`].
///
/// [`content`]: Self::content
pub fn label(self, text: impl Into<ArcStr>) -> Self {
self.content(Label::new(text).prepare())
}
/// Returns `self` with the given `pad`.
///
/// This `pad` determines the amount of space between the divider line and the content.
/// It does nothing when there is no content.
///
/// The default value is 5px.
pub fn pad(mut self, pad: Length) -> Self {
self.pad = pad;
self
}
}
// --- MARK: WIDGETMUT
impl Divider {
/// Sets the direction `axis`.
pub fn set_direction(this: &mut WidgetMut<'_, Self>, axis: Axis) {
this.widget.axis = axis;
this.ctx.request_layout();
}
/// Sets the line `thickness`.
pub fn set_thickness(this: &mut WidgetMut<'_, Self>, thickness: Length) {
this.widget.thickness = Some(thickness);
this.ctx.request_layout();
}
/// Sets the line thickness to hairline, i.e. 1 device pixel.
pub fn set_hairline(this: &mut WidgetMut<'_, Self>) {
this.widget.thickness = None;
this.ctx.request_layout();
}
/// Sets the `dash_fit`.
pub fn set_dash_fit(this: &mut WidgetMut<'_, Self>, dash_fit: DashFit) {
this.widget.dash_fit = dash_fit;
this.ctx.request_layout();
}
/// Sets the `dash_pattern`.
///
/// See [`dash_pattern`] for more details.
///
/// # Panics
///
/// Panics if `dash_pattern` contains an uneven number of entries of 3 or more
/// and debug assertions are enabled.
///
/// [`dash_pattern`]: Self::dash_pattern
pub fn set_dash_pattern(this: &mut WidgetMut<'_, Self>, dash_pattern: &[Length]) {
let mut dash_pattern = SmallVec::from_slice(dash_pattern);
// Paint code assumes an even number for simplicity of implementation.
let len = dash_pattern.len();
if len == 1 {
dash_pattern.push(dash_pattern[0]);
} else if len > 0 && !len.is_multiple_of(2) {
debug_panic!(
"The divider dash pattern must have an even number of lengths. Received {len}"
);
dash_pattern.pop();
}
this.widget.dash_pattern = dash_pattern;
this.ctx.request_layout();
}
/// Sets the `cap` used both for start and end.
///
/// Use [`set_start_cap`] or [`set_end_cap`] to set different edge caps.
///
/// Defaults to [`Cap::Butt`].
///
/// [`set_start_cap`]: Self::set_start_cap
/// [`set_end_cap`]: Self::set_end_cap
pub fn set_cap(this: &mut WidgetMut<'_, Self>, cap: Cap) {
this.widget.start_cap = cap;
this.widget.end_cap = cap;
this.ctx.request_layout();
}
/// Sets the starting `cap`.
///
/// Use [`set_cap`] to set the cap for both the start and the end.
///
/// Defaults to [`Cap::Butt`].
///
/// [`set_cap`]: Self::set_cap
pub fn set_start_cap(this: &mut WidgetMut<'_, Self>, cap: Cap) {
this.widget.start_cap = cap;
this.ctx.request_layout();
}
/// Sets the ending `cap`.
///
/// Use [`set_cap`] to set the cap for both the start and the end.
///
/// Defaults to [`Cap::Butt`].
///
/// [`set_cap`]: Self::set_cap
pub fn set_end_cap(this: &mut WidgetMut<'_, Self>, cap: Cap) {
this.widget.end_cap = cap;
this.ctx.request_layout();
}
/// Sets the content `placement`.
///
/// Defaults to [`Placement::Center`].
pub fn set_placement(this: &mut WidgetMut<'_, Self>, placement: Placement) {
this.widget.placement = placement;
this.ctx.request_layout();
}
/// Sets the `content`.
pub fn set_content(this: &mut WidgetMut<'_, Self>, content: NewWidget<impl Widget + ?Sized>) {
if let Some(old_content) = this.widget.content.replace(content.erased().to_pod()) {
this.ctx.remove_child(old_content);
}
this.ctx.children_changed();
}
/// Removes any existing content.
pub fn clear_content(this: &mut WidgetMut<'_, Self>) {
if let Some(old_content) = this.widget.content.take() {
this.ctx.remove_child(old_content);
}
}
/// Sets the `pad`.
///
/// This `pad` determines the amount of space between the divider line and the content.
/// It does nothing when there is no content.
///
/// The default value is 5px.
pub fn set_pad(this: &mut WidgetMut<'_, Self>, pad: Length) {
this.widget.pad = pad;
this.ctx.request_layout();
}
/// Returns a mutable reference to the content widget.
pub fn content_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> Option<WidgetMut<'t, dyn Widget>> {
this.widget.content.as_mut().map(|c| this.ctx.get_mut(c))
}
}
/// Returns `(length, gap_counts)`.
///
/// `length` is the length of the stroke that will result in only whole dashes.
/// `gap_counts` specifies how many times each gap was used.
fn length_with_whole_dashes(space: f64, dashes: &[f64]) -> (f64, SmallVec<[f64; 3]>) {
let n = dashes.len();
let mut return_zero = false;
if n < 2 || !n.is_multiple_of(2) {
debug_panic!("Can only calculate whole dashes for actual dash pattern pairs");
return_zero = true;
}
if dashes.iter().any(|&v| v < 0.) {
debug_panic!("Negative dash lengths are not allowed");
return_zero = true;
}
let cycle_length: f64 = dashes.iter().sum();
if cycle_length <= 0. || space <= 0. {
return_zero = true;
}
if return_zero {
return (0., SmallVec::from_elem(0., n / 2));
}
// Start from the maximum number of full cycles that fit the space.
let full_cycles = (space / cycle_length).floor();
let mut gap_counts = SmallVec::from_elem(full_cycles, n / 2);
let mut space_used = full_cycles * cycle_length;
let mut space_free = space - space_used;
// Track the index of the (dash, gap) pair of at the end of the line.
let mut end_pair = n / 2 - 1;
// Add as many (dash, gap) pairs as possible to form the final partial cycle.
for (i, pair) in dashes.chunks_exact(2).enumerate() {
let (dash, gap) = (pair[0], pair[1]);
if dash > space_free {
break;
}
space_used += dash + gap;
space_free = space - space_used;
gap_counts[i] += 1.;
end_pair = i;
}
// We're measuring the length that ends with a dash, so remove the last gap.
let length = if space_used > 0. {
gap_counts[end_pair] -= 1.;
let end_gap = dashes[end_pair * 2 + 1];
space_used - end_gap
} else {
0.
};
(length, gap_counts)
}
/// Distributes the available `space` between all the gaps.
///
/// Takes into account both the occurrence of the gaps and their relative significance,
/// i.e. larger gaps get more of the available space.
fn stretch_gaps(dashes: &mut [f64], gap_counts: &[f64], space: f64) {
if space <= 0. {
return;
}
let n = dashes.len();
let gaps: SmallVec<[f64; 3]> = (1..n).step_by(2).map(|i| dashes[i]).collect();
if gaps.len() != gap_counts.len() {
debug_panic!("Every gap needs to have an occurrence count");
return;
}
let gaps_sum: f64 = gaps
.iter()
.zip(gap_counts.iter())
.map(|(gap, count)| gap * count)
.sum();
let gap_counts_sum: f64 = gap_counts.iter().sum();
for (idx, (gap, &count)) in gaps.into_iter().zip(gap_counts.iter()).enumerate() {
if count <= 0. {
continue;
}
// Guard against the case where all gaps have a zero length
let gap_fraction = if gaps_sum != 0. {
gap * count / gaps_sum
} else {
count / gap_counts_sum
};
let add_per_gap_type = space * gap_fraction;
let add_per_occurrence = add_per_gap_type / count;
dashes[idx * 2 + 1] += add_per_occurrence;
}
}
/// Returns the `(start, end)` of the line in relation to the given `space`.
///
/// This ensures that a dashed line, described by `dashes`, will honor its `dash_fit` strategy.
/// For a solid line it will just return `(0., space)`.
///
/// Depending on the fit strategy `dashes` might be modified as well.
fn line_length(space: f64, dashes: &mut [f64], dash_fit: DashFit) -> (f64, f64) {
let (start, length) = if dashes.is_empty() {
// Solid line
(0., space)
} else {
// Dashed line
if let DashFit::Clip = dash_fit {
(0., space)
} else {
let (length, gap_counts) = length_with_whole_dashes(space, dashes);
let extra = space - length;
match dash_fit {
DashFit::Clip => unreachable!(),
DashFit::Stretch => {
stretch_gaps(dashes, &gap_counts, extra);
(0., space)
}
DashFit::Start => (0., length),
DashFit::Center => (extra * 0.5, length),
DashFit::End => (extra, length),
}
}
};
(start, start + length)
}
impl Divider {
/// Returns the total cap overhang of both ends of the divider line.
fn total_cap_overhang(&self, thickness: f64) -> f64 {
let mut overhang = 0.;
for cap in [self.start_cap, self.end_cap] {
match cap {
Cap::Butt => (),
Cap::Square | Cap::Round => {
overhang += thickness * 0.5;
}
}
}
overhang
}
}
impl UsesProperty<ContentColor> for Divider {}
// --- MARK: IMPL WIDGET
impl Widget for Divider {
type Action = NoAction;
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
if let Some(content) = &mut self.content {
ctx.register_child(content);
}
}
fn property_changed(&mut self, ctx: &mut UpdateCtx<'_>, property_type: TypeId) {
if ContentColor::matches(property_type) {
ctx.request_paint_only();
}
}
fn update(
&mut self,
_ctx: &mut UpdateCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &Update,
) {
}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
const DEFAULT_LENGTH: Length = Length::const_px(100.);
let thickness = self.thickness.unwrap_or(Length::const_px(1.));
let content_length = if let Some(content) = &mut self.content {
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);
ctx.compute_length(content, auto_length, context_size, axis, cross_length)
} else {
Length::ZERO
};
if axis == self.axis {
match len_req {
LenReq::MinContent => content_length,
LenReq::MaxContent => DEFAULT_LENGTH.max(content_length),
LenReq::FitContent(space) => space.max(content_length),
}
} else {
thickness.max(content_length)
}
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
// Clear any previously laid out lines.
self.lines.clear();
/// Stores the line layout info for painting.
fn lay_out_line(
this: &mut Divider,
start: f64,
end: f64,
cross_pos: f64,
dashes: SmallVec<[f64; 4]>,
thickness: f64,
) {
let cap_offset = match this.start_cap {
Cap::Butt => 0.,
Cap::Square | Cap::Round => thickness * 0.5,
};
let p1 = this.axis.pack_point(cap_offset + start, cross_pos);
let p2 = this.axis.pack_point(cap_offset + end, cross_pos);
this.lines.push(LineLayout {
line: Line::new(p1, p2),
dashes,
});
}
let thickness = self.thickness.map(|t| t.get()).unwrap_or(1.);
let cross_pos = size.get_coord(self.axis.cross()) * 0.5;
let mut dashes: SmallVec<[f64; 4]> = self.dash_pattern.iter().map(|l| l.get()).collect();
if let Some(content) = &mut self.content {
let content_size = ctx.compute_size(content, SizeDef::fit(size), size.into());
ctx.run_layout(content, content_size);
let placement = match self.placement {
Placement::Start => match self.axis {
Axis::Horizontal => UnitPoint::LEFT,
Axis::Vertical => UnitPoint::TOP,
},
Placement::Center => UnitPoint::CENTER,
Placement::End => match self.axis {
Axis::Horizontal => UnitPoint::RIGHT,
Axis::Vertical => UnitPoint::BOTTOM,
},
};
let content_origin = placement.resolve((size - content_size).to_rect());
ctx.place_child(content, content_origin);
ctx.derive_baselines(content);
let pad = self.pad.get();
let mut line_space = size.get_coord(self.axis)
- self.total_cap_overhang(thickness)
- content_size.get_coord(self.axis)
- pad;
if line_space < 0. {
// No space for line drawing
return;
}
let (start, end) = match self.placement {
// Content at the start, line has a start offset
Placement::Start => {
let (start, end) = line_length(line_space, &mut dashes, self.dash_fit);
let offset = content_size.get_coord(self.axis) + pad;
(start + offset, end + offset)
}
// Content in the middle, two lines surrounding it
Placement::Center => {
// Need to account for an extra pad, and divide the space in two
line_space = ((line_space - pad) * 0.5).max(0.);
let (start, end) = line_length(line_space, &mut dashes, self.dash_fit);
// Lay out the first line at the start
lay_out_line(self, start, end, cross_pos, dashes.clone(), thickness);
// Lay out the second line on the other side of the content,
// using the general code path.
let offset = content_origin.get_coord(self.axis)
+ content_size.get_coord(self.axis)
+ pad;
(start + offset, end + offset)
}
// Content at the end, line has no offset
Placement::End => line_length(line_space, &mut dashes, self.dash_fit),
};
lay_out_line(self, start, end, cross_pos, dashes, thickness);
} else {
// Single line, no content
ctx.clear_baselines();
let line_space = size.get_coord(self.axis) - self.total_cap_overhang(thickness);
if line_space < 0. {
// No space for line drawing
return;
}
let (start, end) = line_length(line_space, &mut dashes, self.dash_fit);
lay_out_line(self, start, end, cross_pos, dashes, thickness);
}
}
fn paint(
&mut self,
ctx: &mut PaintCtx<'_>,
props: &PropertiesRef<'_>,
painter: &mut Painter<'_>,
) {
// TODO: Replace with snap-aware paint helper once that exists.
let one_dp = 1. / ctx.scale_factor();
let cache = ctx.property_cache();
let color = props.get::<ContentColor>(cache);
let thickness = self.thickness.map(|t| t.get()).unwrap_or(one_dp);
for line in &self.lines {
let style = Stroke {
width: thickness,
join: Join::Miter,
dash_pattern: line.dashes.clone(),
start_cap: self.start_cap,
end_cap: self.end_cap,
..Default::default()
};
painter.stroke(line.line, &style, color.color).draw();
}
}
fn accessibility_role(&self) -> Role {
Role::GenericContainer
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
_node: &mut Node,
) {
}
fn children_ids(&self) -> ChildrenIds {
let mut ids = ChildrenIds::new();
if let Some(content) = &self.content {
ids.push(content.id());
}
ids
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("Divider", id = id.trace())
}
}
// --- MARK: TESTS
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::AsUnit;
use crate::palette;
use crate::properties::types::{CrossAxisAlignment, MainAxisAlignment};
use crate::properties::{Background, Dimensions, Gap, Padding};
use crate::testing::{TestHarness, assert_render_snapshot};
use crate::theme::test_property_set;
use crate::widgets::{Flex, SizedBox, Spinner};
fn pattern(values: &[usize]) -> Vec<Length> {
values.iter().map(|&v| Length::px(v as f64)).collect()
}
#[test]
fn simple() {
let root = Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Stretch)
.with_fixed(
Flex::column()
.with_fixed(Label::new("Above").prepare())
.with_fixed(Divider::horizontal().prepare())
.with_fixed(Label::new("Below").prepare())
.prepare(),
)
.with_fixed(
Divider::vertical()
.thickness(5.px())
.prepare()
.with_props(ContentColor::new(palette::css::DARK_SALMON)),
)
.with(
Flex::column()
.with_fixed(
Label::new("Another above")
.prepare()
.with_props(Dimensions::height(50.px())),
)
.with_fixed(Divider::horizontal().prepare())
.prepare(),
1.,
)
.prepare()
.with_props(Gap::ZERO);
let mut harness = TestHarness::create_with_size(test_property_set(), root, (350, 80));
assert_render_snapshot!(harness, "divider_simple");
}
#[test]
fn styled() {
let root = SizedBox::new(
Flex::column()
.cross_axis_alignment(CrossAxisAlignment::Stretch)
.with_fixed(
Divider::horizontal()
.thickness(5.px())
.dash_pattern(&pattern(&[1, 10, 5, 20]))
.cap(Cap::Round)
.prepare()
.with_props(ContentColor::new(palette::css::BLANCHED_ALMOND)),
)
.with_fixed(
Divider::horizontal()
.thickness(10.px())
.dash_pattern(&pattern(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
.prepare()
.with_props(ContentColor::new(palette::css::LAVENDER)),
)
.with_fixed(
Divider::horizontal()
.thickness(10.px())
.dash_pattern(&pattern(&[5, 20]))
.start_cap(Cap::Round)
.end_cap(Cap::Square)
.prepare()
.with_props(ContentColor::new(palette::css::ORANGE)),
)
.with_fixed(
Divider::horizontal()
.hairline()
.dash_pattern(&pattern(&[5, 1]))
.prepare()
.with_props(ContentColor::new(palette::css::CRIMSON)),
)
.with_fixed(
Divider::horizontal()
.thickness(3.px())
.dash_pattern(&pattern(&[1, 6, 1, 12]))
.cap(Cap::Round)
.prepare()
.with_props(ContentColor::new(palette::css::GOLD)),
)
.with_fixed(
Divider::horizontal()
.thickness(2.px())
.dash_pattern(&pattern(&[5, 5]))
.dash_fit(DashFit::Stretch)
.pad(20.px())
.label("O")
.prepare()
.with_props(ContentColor::new(palette::css::HOT_PINK)),
)
.prepare(),
)
.prepare()
.with_props(Padding::all(10.px()));
let mut harness = TestHarness::create_with_size(test_property_set(), root, (150, 120));
assert_render_snapshot!(harness, "divider_styled");
}
#[test]
fn dash_fit() {
let divider_fit = |fit: DashFit| {
Divider::horizontal()
.thickness(5.px())
.dash_pattern(&pattern(&[20, 10, 10, 20]))
.dash_fit(fit)
.prepare()
.with_props(ContentColor::new(palette::css::MAGENTA))
};
let root = SizedBox::new(
Flex::column()
.main_axis_alignment(MainAxisAlignment::Center)
.cross_axis_alignment(CrossAxisAlignment::Stretch)
.with_fixed(divider_fit(DashFit::Clip))
.with_fixed(divider_fit(DashFit::Stretch))
.with_fixed(divider_fit(DashFit::Start))
.with_fixed(divider_fit(DashFit::Center))
.with_fixed(divider_fit(DashFit::End))
.prepare()
.with_props(Background::Color(palette::css::MIDNIGHT_BLUE)),
)
.prepare()
.with_props(Padding::all(10.px()));
let mut harness = TestHarness::create_with_size(test_property_set(), root, (155, 90));
assert_render_snapshot!(harness, "divider_dash_fit");
}
#[test]
fn label() {
let root = Flex::column()
.cross_axis_alignment(CrossAxisAlignment::Stretch)
.with_fixed(
Divider::horizontal()
.label("Start")
.placement(Placement::Start)
.prepare(),
)
.with_fixed(
Divider::horizontal()
.label("Center")
.placement(Placement::Center)
.prepare(),
)
.with_fixed(
Divider::horizontal()
.label("End")
.placement(Placement::End)
.prepare(),
)
.with(
Flex::row()
.main_axis_alignment(MainAxisAlignment::Center)
.cross_axis_alignment(CrossAxisAlignment::Stretch)
.with_fixed(
Divider::vertical()
.label("Start")
.placement(Placement::Start)
.prepare(),
)
.with_fixed(
Divider::vertical()
.label("Center")
.placement(Placement::Center)
.prepare(),
)
.with_fixed(
Divider::vertical()
.label("End")
.placement(Placement::End)
.prepare(),
)
.prepare(),
1.,
)
.prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), root, (200, 200));
assert_render_snapshot!(harness, "divider_label");
}
#[test]
fn content() {
let content = Spinner::new()
.prepare()
.with_props(Dimensions::fixed(30.px(), 30.px()));
let root = Divider::horizontal()
.content(content)
.prepare()
.with_props(Dimensions::STRETCH);
let mut harness = TestHarness::create_with_size(test_property_set(), root, (100, 60));
assert_render_snapshot!(harness, "divider_content");
}
}