Skip to main content

waft_material/
no_splash.rs

1//! Flutter counterpart: material_ui's `no_splash.dart`.
2
3use std::rc::Rc;
4
5use waft_embedder::{Canvas, Color, Matrix4, Offset, TextDirection};
6use waft_foundation::{App, Handle, Listener, Ref};
7use waft_painting::{BorderRadius, ShapeBorderRef};
8use waft_rendering::AnyRenderBox;
9
10use crate::ink_well::{
11    InteractiveInkFeatureData, InteractiveInkFeatureFactory, InteractiveInkFeatureFactoryRef,
12    InteractiveInkFeatureLeaf, InteractiveInkFeatureLeafData, InteractiveInkFeatureRef,
13};
14use crate::material::{
15    AnyMaterialInkController, InkFeatureData, InkFeatureLeaf, InkFeatureLeafData, RectCallback,
16};
17
18/// Dart's `_NoSplashFactory`.
19#[derive(Debug)]
20struct NoSplashFactory;
21
22impl InteractiveInkFeatureFactory for NoSplashFactory {
23    fn create(
24        &self,
25        app: &mut App,
26        controller: AnyMaterialInkController,
27        reference_box: AnyRenderBox,
28        _position: Offset,
29        color: Color,
30        _text_direction: TextDirection,
31        _contained_ink_well: bool,
32        _rect_callback: Option<RectCallback>,
33        _border_radius: Option<BorderRadius>,
34        _custom_border: Option<ShapeBorderRef>,
35        _radius: Option<f64>,
36        on_removed: Option<Listener>,
37    ) -> InteractiveInkFeatureRef {
38        NoSplash::new(app, controller, reference_box, color, on_removed).into_erased()
39    }
40}
41
42/// An [`InteractiveInkFeature`](crate::InteractiveInkFeature) that doesn't paint a splash.
43///
44/// Use [`NoSplash::splash_factory`] to defeat the default ink splash drawn by
45/// an [`InkWell`](crate::InkWell) or `ButtonStyle`. For example, to create an `ElevatedButton`
46/// that does not draw the default "ripple" ink splash when it's tapped:
47///
48/// ```text
49/// ElevatedButton::new()
50///     .style(ElevatedButton::style_from().splash_factory(NoSplash::splash_factory()))
51///     .on_pressed(..)
52///     .child(Text::new("Run"))
53/// ```
54pub struct NoSplash {
55    ink_feature: InkFeatureData,
56    interactive_ink_feature: InteractiveInkFeatureData,
57}
58
59impl NoSplash {
60    /// Create an [`InteractiveInkFeature`](crate::InteractiveInkFeature) that doesn't paint a
61    /// splash.
62    pub fn new(
63        app: &mut App,
64        controller: AnyMaterialInkController,
65        reference_box: AnyRenderBox,
66        color: Color,
67        on_removed: Option<Listener>,
68    ) -> Ref<NoSplash> {
69        let ink_feature = InkFeatureData::new(app, controller, reference_box, on_removed);
70        app.create(NoSplash {
71            ink_feature,
72            interactive_ink_feature: InteractiveInkFeatureData::new(color, None),
73        })
74    }
75
76    /// Used to specify this type of ink splash for an [`InkWell`](crate::InkWell),
77    /// [`InkResponse`](crate::InkResponse), material `Theme`, or `ButtonStyle`.
78    ///
79    /// Dart's `static const`: every call hands out the same factory, so they compare equal.
80    pub fn splash_factory() -> InteractiveInkFeatureFactoryRef {
81        thread_local! {
82            static FACTORY: InteractiveInkFeatureFactoryRef = Rc::new(NoSplashFactory);
83        }
84        FACTORY.with(Rc::clone)
85    }
86}
87
88impl InkFeatureLeafData for NoSplash {
89    fn ink_feature(&self) -> &InkFeatureData {
90        &self.ink_feature
91    }
92
93    fn ink_feature_mut(&mut self) -> &mut InkFeatureData {
94        &mut self.ink_feature
95    }
96}
97
98impl InteractiveInkFeatureLeafData for NoSplash {
99    fn interactive_ink_feature(&self) -> &InteractiveInkFeatureData {
100        &self.interactive_ink_feature
101    }
102
103    fn interactive_ink_feature_mut(&mut self) -> &mut InteractiveInkFeatureData {
104        &mut self.interactive_ink_feature
105    }
106}
107
108impl InkFeatureLeaf for NoSplash {
109    fn paint_feature(
110        self: Handle<Self>,
111        _app: &mut App,
112        _canvas: &mut Canvas,
113        _transform: &Matrix4,
114    ) {
115    }
116}
117
118impl InteractiveInkFeatureLeaf for NoSplash {
119    fn confirm(self: Handle<Self>, app: &mut App) {
120        InkFeatureLeaf::dispose(self, app);
121    }
122
123    fn cancel(self: Handle<Self>, app: &mut App) {
124        InkFeatureLeaf::dispose(self, app);
125    }
126}