nuit_core/compose/view/
ext.rs

1use crate::{Alignment, Angle, DragEvent, DragGesture, Event, Font, Frame, Gesture, Handler, Insets, Modified, ModifierNode, Style, TapGesture, UnitPoint, Vec2, View};
2
3use super::{Gestured, Overlay};
4
5/// An extension trait with various convenience methods for views.
6pub trait ViewExt: Sized {
7    fn modifier(self, modifier: ModifierNode) -> Modified<Self> {
8        Modified::new(self, modifier)
9    }
10
11    fn gesture<G>(self, gesture: G) -> Gestured<Self, G> where G: Gesture {
12        Gestured::new(self, gesture)
13    }
14
15    fn on_taps<F>(self, count: usize, action: F) -> Gestured<Self, TapGesture<F>> where F: Fn() {
16        self.gesture(TapGesture::new(count, action))
17    }
18
19    fn on_tap<F>(self, action: F) -> Gestured<Self, TapGesture<F>> where F: Fn() {
20        self.gesture(TapGesture::new_single(action))
21    }
22
23    fn on_drag_by<F>(self, minimum_distance: impl Into<f64>, action: F) -> Gestured<Self, DragGesture<F>> where F: Fn(&DragEvent) {
24        self.gesture(DragGesture::new(minimum_distance.into(), action))
25    }
26
27    fn on_drag<F>(self, action: F) -> Gestured<Self, DragGesture<F>> where F: Fn(&DragEvent) {
28        self.gesture(DragGesture::new_default(action))
29    }
30
31    fn overlay_at<O>(self, alignment: Alignment, overlayed: O) -> Overlay<Self, O> where O: View {
32        Overlay::new(self, alignment, overlayed)
33    }
34
35    fn overlay<O>(self, overlayed: O) -> Overlay<Self, O> where O: View {
36        self.overlay_at(Alignment::Center, overlayed)
37    }
38
39    fn padding(self, insets: impl Into<Insets>) -> Modified<Self> {
40        self.modifier(ModifierNode::Padding { insets: insets.into() })
41    }
42
43    fn position(self, position: Vec2<f64>) -> Modified<Self> {
44        self.modifier(ModifierNode::Position { position })
45    }
46
47    fn offset(self, delta: Vec2<f64>) -> Modified<Self> {
48        self.modifier(ModifierNode::Offset { delta })
49    }
50
51    fn opacity(self, opacity: impl Into<f64>) -> Modified<Self> {
52        self.modifier(ModifierNode::Opacity { opacity: opacity.into() })
53    }
54
55    fn frame_with(self, alignment: Alignment, frame: impl Into<Frame>) -> Modified<Self> {
56        self.modifier(ModifierNode::Frame { frame: frame.into(), alignment })
57    }
58
59    fn frame(self, frame: impl Into<Frame>) -> Modified<Self> {
60        self.frame_with(Alignment::Center, frame)
61    }
62
63    fn font(self, font: impl Into<Font>) -> Modified<Self> {
64        self.modifier(ModifierNode::Font { font: font.into() })
65    }
66
67    fn foreground_style(self, style: impl Into<Style>) -> Modified<Self> {
68        self.modifier(ModifierNode::ForegroundStyle { style: style.into() })
69    }
70
71    fn scale_effect_around(self, anchor: UnitPoint, factor: impl Into<f64>) -> Modified<Self> {
72        self.modifier(ModifierNode::ScaleEffect { factor: factor.into(), anchor })
73    }
74
75    fn scale_effect(self, factor: impl Into<f64>) -> Modified<Self> {
76        self.scale_effect_around(UnitPoint::CENTER, factor)
77    }
78
79    fn rotation_effect_around(self, anchor: UnitPoint, angle: impl Into<Angle>) -> Modified<Self> {
80        self.modifier(ModifierNode::RotationEffect { angle: angle.into(), anchor })
81    }
82
83    fn rotation_effect(self, angle: impl Into<Angle>) -> Modified<Self> {
84        self.rotation_effect_around(UnitPoint::CENTER, angle)
85    }
86
87    fn on_appear(self, action: impl Fn() + 'static) -> Handler<Self, impl Fn(Event)> {
88        Handler::new(self, move |e| {
89            if let Event::Appear = e {
90                action();
91            }
92        })
93    }
94
95    fn on_disappear(self, action: impl Fn() + 'static) -> Handler<Self, impl Fn(Event)> {
96        Handler::new(self, move |e| {
97            if let Event::Disappear = e {
98                action();
99            }
100        })
101    }
102}
103
104impl<T> ViewExt for T where T: View {}