-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclipping.rs
79 lines (74 loc) · 2.49 KB
/
clipping.rs
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
use narui::{layout::layout_trait::*, *};
#[derive(Debug, PartialEq)]
struct RevealClipLayout {
reveal: f32,
}
impl Layout for RevealClipLayout {
fn layout(&self, constraint: BoxConstraints, children: LayoutableChildren) -> (Size, u32) {
assert_eq!(children.len(), 1);
if let Some(child) = children.into_iter().last() {
let mut size = child.layout(constraint);
child.set_pos(Offset::zero());
child.set_z_index_offset(0);
size.0 = constraint.maximal_bounded();
size.0.width *= self.reveal;
size
} else {
panic!()
}
}
}
#[widget]
pub fn reveal_box(
children: FragmentChildren,
reveal: f32,
context: &mut WidgetContext,
) -> FragmentInner {
FragmentInner::Node {
children,
layout: Box::new(RevealClipLayout { reveal }),
is_clipper: true,
subpass: None,
}
}
#[widget]
pub fn top(context: &mut WidgetContext) -> Fragment {
let slider_value = context.listenable(0.75);
rsx! {
<align>
<sized constraint=BoxConstraints::tight(500., 500.)>
<column cross_axis_alignment=CrossAxisAlignment::Start>
<flexible>
<padding padding=EdgeInsets::all(10.)>
<reveal_box reveal=context.listen(slider_value)>
<align alignment=Alignment::center()>
<rect fill=Some(Color::new(0., 0.7, 0.7, 1.0)) border_radius=Fraction(1.) do_clipping=true>
<text size=100.>
{"some really long text, that gets clipped..."}
</text>
</rect>
</align>
</reveal_box>
</padding>
</flexible>
<slider
val={context.listen(slider_value)}
on_change={move |context: &CallbackContext, new_val| {
context.shout(slider_value, new_val)
}}
min=0.0 max=1.0
/>
</column>
</sized>
</align>
}
}
fn main() {
env_logger::init();
app::render(
app::WindowBuilder::new().with_title("narui clipping demo"),
rsx_toplevel! {
<top />
},
);
}