cosmic/widget/rectangle_tracker/
mod.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
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
mod subscription;

use iced::futures::channel::mpsc::UnboundedSender;
use iced::widget::Container;
use iced::Vector;
pub use subscription::*;

use iced_core::event::{self, Event};
use iced_core::layout;
use iced_core::mouse;
use iced_core::overlay;
use iced_core::renderer;
use iced_core::widget::Tree;
use iced_core::{Alignment, Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget};
use std::{fmt::Debug, hash::Hash};

pub use iced_widget::container::{Catalog, Style};

pub fn rectangle_tracking_container<'a, Message, I, T>(
    content: T,
    id: I,
    tx: UnboundedSender<(I, Rectangle)>,
) -> RectangleTrackingContainer<'a, Message, crate::Renderer, I>
where
    I: Hash + Copy + Send + Sync + Debug + 'a,
    T: Into<Element<'a, Message, crate::Theme, crate::Renderer>>,
{
    RectangleTrackingContainer::new(content, id, tx)
}

pub fn subscription<
    I: 'static + Hash + Copy + Send + Sync + Debug,
    R: 'static + Hash + Copy + Send + Sync + Debug + Eq,
>(
    id: I,
) -> iced::Subscription<(I, RectangleUpdate<R>)> {
    subscription::rectangle_tracker_subscription(id)
}

#[derive(Clone, Debug)]
pub struct RectangleTracker<I> {
    tx: UnboundedSender<(I, Rectangle)>,
}

impl<I> RectangleTracker<I>
where
    I: Hash + Copy + Send + Sync + Debug,
{
    pub fn container<'a, Message: 'static, T>(
        &self,
        id: I,
        content: T,
    ) -> RectangleTrackingContainer<'a, Message, crate::Renderer, I>
    where
        I: 'a,
        T: Into<Element<'a, Message, crate::Theme, crate::Renderer>>,
    {
        RectangleTrackingContainer::new(content, id, self.tx.clone())
    }
}

/// An element decorating some content.
///
/// It is normally used for alignment purposes.
#[allow(missing_debug_implementations)]
pub struct RectangleTrackingContainer<'a, Message, Renderer, I>
where
    Renderer: iced_core::Renderer,
{
    tx: UnboundedSender<(I, Rectangle)>,
    id: I,
    container: Container<'a, Message, crate::Theme, Renderer>,
    ignore_bounds: bool,
}

impl<'a, Message, Renderer, I> RectangleTrackingContainer<'a, Message, Renderer, I>
where
    Renderer: iced_core::Renderer,
    I: 'a + Hash + Copy + Send + Sync + Debug,
{
    /// Creates an empty [`Container`].
    pub(crate) fn new<T>(content: T, id: I, tx: UnboundedSender<(I, Rectangle)>) -> Self
    where
        T: Into<Element<'a, Message, crate::Theme, Renderer>>,
    {
        RectangleTrackingContainer {
            id,
            tx,
            container: Container::new(content),
            ignore_bounds: false,
        }
    }

    pub fn diff(&mut self, tree: &mut Tree) {
        self.container.diff(tree);
    }

    /// Sets the [`Padding`] of the [`Container`].
    #[must_use]
    pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
        self.container = self.container.padding(padding);
        self
    }

    /// Sets the width of the [`self.`].
    #[must_use]
    pub fn width(mut self, width: Length) -> Self {
        self.container = self.container.width(width);
        self
    }

    /// Sets the height of the [`Container`].
    #[must_use]
    pub fn height(mut self, height: Length) -> Self {
        self.container = self.container.height(height);
        self
    }

    /// Sets the maximum width of the [`Container`].
    #[must_use]
    pub fn max_width(mut self, max_width: f32) -> Self {
        self.container = self.container.max_width(max_width);
        self
    }

    /// Sets the maximum height of the [`Container`] in pixels.
    #[must_use]
    pub fn max_height(mut self, max_height: f32) -> Self {
        self.container = self.container.max_height(max_height);
        self
    }

    /// Sets the content alignment for the horizontal axis of the [`Container`].
    #[must_use]
    pub fn align_x(mut self, alignment: Alignment) -> Self {
        self.container = self.container.align_x(alignment);
        self
    }

    /// Sets the content alignment for the vertical axis of the [`Container`].
    #[must_use]
    pub fn align_y(mut self, alignment: Alignment) -> Self {
        self.container = self.container.align_y(alignment);
        self
    }

    /// Centers the contents in the horizontal axis of the [`Container`].
    #[must_use]
    pub fn center_x(mut self, width: Length) -> Self {
        self.container = self.container.center_x(width);
        self
    }

    /// Centers the contents in the vertical axis of the [`Container`].
    #[must_use]
    pub fn center_y(mut self, height: Length) -> Self {
        self.container = self.container.center_y(height);
        self
    }

    /// Centers the contents in the horizontal and vertical axis of the [`Container`].
    #[must_use]
    pub fn center(mut self, length: Length) -> Self {
        self.container = self.container.center(length);
        self
    }

    /// Sets the style of the [`Container`].
    #[must_use]
    pub fn style(mut self, style: impl Into<<crate::Theme as Catalog>::Class<'a>>) -> Self {
        self.container = self.container.class(style);
        self
    }

    /// Set to true to ignore parent container bounds when performing layout.
    /// This can be useful for widgets that are in auto-sized surfaces.
    #[must_use]
    pub fn ignore_bounds(mut self, ignore_bounds: bool) -> Self {
        self.ignore_bounds = ignore_bounds;
        self
    }
}

impl<'a, Message, Renderer, I> Widget<Message, crate::Theme, Renderer>
    for RectangleTrackingContainer<'a, Message, Renderer, I>
where
    Renderer: iced_core::Renderer,
    I: 'a + Hash + Copy + Send + Sync + Debug,
{
    fn children(&self) -> Vec<Tree> {
        self.container.children()
    }

    fn state(&self) -> iced_core::widget::tree::State {
        self.container.state()
    }

    fn diff(&mut self, tree: &mut Tree) {
        self.container.diff(tree);
    }

    fn size(&self) -> iced_core::Size<Length> {
        self.container.size()
    }

    fn layout(
        &self,
        tree: &mut Tree,
        renderer: &Renderer,
        limits: &layout::Limits,
    ) -> layout::Node {
        let layout = self.container.layout(
            tree,
            renderer,
            if self.ignore_bounds {
                &layout::Limits::NONE
            } else {
                limits
            },
        );

        layout
    }

    fn operate(
        &self,
        tree: &mut Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        operation: &mut dyn iced_core::widget::Operation<()>,
    ) {
        self.container.operate(tree, layout, renderer, operation);
    }

    fn on_event(
        &mut self,
        tree: &mut Tree,
        event: Event,
        layout: Layout<'_>,
        cursor_position: mouse::Cursor,
        renderer: &Renderer,
        clipboard: &mut dyn Clipboard,
        shell: &mut Shell<'_, Message>,
        viewport: &iced_core::Rectangle,
    ) -> event::Status {
        self.container.on_event(
            tree,
            event,
            layout,
            cursor_position,
            renderer,
            clipboard,
            shell,
            viewport,
        )
    }

    fn mouse_interaction(
        &self,
        tree: &Tree,
        layout: Layout<'_>,
        cursor_position: mouse::Cursor,
        viewport: &Rectangle,
        renderer: &Renderer,
    ) -> mouse::Interaction {
        self.container
            .mouse_interaction(tree, layout, cursor_position, viewport, renderer)
    }

    fn draw(
        &self,
        tree: &Tree,
        renderer: &mut Renderer,
        theme: &crate::Theme,
        renderer_style: &renderer::Style,
        layout: Layout<'_>,
        cursor_position: mouse::Cursor,
        viewport: &Rectangle,
    ) {
        let _ = self.tx.unbounded_send((self.id, layout.bounds()));
        self.container.draw(
            tree,
            renderer,
            theme,
            renderer_style,
            layout,
            cursor_position,
            viewport,
        );
    }

    fn overlay<'b>(
        &'b mut self,
        tree: &'b mut Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        translation: Vector,
    ) -> Option<overlay::Element<'b, Message, crate::Theme, Renderer>> {
        self.container.overlay(tree, layout, renderer, translation)
    }

    fn drag_destinations(
        &self,
        state: &Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles,
    ) {
        self.container
            .drag_destinations(state, layout, renderer, dnd_rectangles);
    }

    #[cfg(feature = "a11y")]
    /// get the a11y nodes for the widget
    fn a11y_nodes(
        &self,
        layout: Layout<'_>,
        state: &Tree,
        p: mouse::Cursor,
    ) -> iced_accessibility::A11yTree {
        self.container.a11y_nodes(layout, state, p)
    }
}

impl<'a, Message, Renderer, I> From<RectangleTrackingContainer<'a, Message, Renderer, I>>
    for Element<'a, Message, crate::Theme, Renderer>
where
    Message: 'a,
    Renderer: 'a + iced_core::Renderer,
    I: 'a + Hash + Copy + Send + Sync + Debug,
{
    fn from(
        column: RectangleTrackingContainer<'a, Message, Renderer, I>,
    ) -> Element<'a, Message, crate::Theme, Renderer> {
        Element::new(column)
    }
}