mkgraphic 0.2.1

A Rust port of the cycfi/elements GUI framework
Documentation
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
//! Layer elements for stacking children on top of each other.

use std::any::Any;
use super::{Element, ElementPtr, ViewLimits, FocusRequest, share};
use super::context::{BasicContext, Context};
use super::composite::{Storage, CompositeBase, Composite};
use crate::support::point::Point;
use crate::support::rect::Rect;
use crate::view::{MouseButton, KeyInfo, TextInfo};

/// Layer element - stacks children on top of each other.
///
/// All children occupy the same bounds. The last child is drawn on top.
pub struct Layer {
    inner: Composite,
}

impl Layer {
    /// Creates a new empty layer.
    pub fn new() -> Self {
        Self {
            inner: Composite::new(),
        }
    }

    /// Creates a layer from a vector of elements.
    pub fn from_vec(children: Vec<ElementPtr>) -> Self {
        Self {
            inner: Composite::from_vec(children),
        }
    }

    /// Adds an element on top.
    pub fn push(&mut self, element: ElementPtr) {
        self.inner.push(element);
    }

    /// Removes and returns the top element.
    pub fn pop(&mut self) -> Option<ElementPtr> {
        self.inner.pop()
    }

    /// Clears all elements.
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    /// Returns the number of layers.
    pub fn count(&self) -> usize {
        self.inner.len()
    }
}

impl Default for Layer {
    fn default() -> Self {
        Self::new()
    }
}

impl Storage for Layer {
    fn len(&self) -> usize {
        self.inner.len()
    }

    fn at(&self, index: usize) -> Option<&dyn Element> {
        self.inner.at(index)
    }

    fn at_mut(&mut self, index: usize) -> Option<&mut dyn Element> {
        self.inner.at_mut(index)
    }
}

impl CompositeBase for Layer {
    fn bounds_of(&self, ctx: &Context, index: usize) -> Rect {
        // All layers have the same bounds
        ctx.bounds
    }

    fn reverse_index(&self) -> bool {
        true // Hit test from top to bottom
    }
}

impl Element for Layer {
    fn limits(&self, ctx: &BasicContext) -> ViewLimits {
        // Return the union of all children's limits
        let mut limits = ViewLimits::new(
            Point::new(0.0, 0.0),
            Point::new(super::FULL_EXTENT, super::FULL_EXTENT),
        );

        for i in 0..self.inner.len() {
            if let Some(child) = self.inner.at(i) {
                let child_limits = child.limits(ctx);
                limits.min.x = limits.min.x.max(child_limits.min.x);
                limits.min.y = limits.min.y.max(child_limits.min.y);
                // For max, we use min of maxes to be safe
                limits.max.x = limits.max.x.min(child_limits.max.x);
                limits.max.y = limits.max.y.min(child_limits.max.y);
            }
        }

        // Ensure max >= min
        limits.max.x = limits.max.x.max(limits.min.x);
        limits.max.y = limits.max.y.max(limits.min.y);

        limits
    }

    fn draw(&self, ctx: &Context) {
        // Draw from bottom to top
        for i in 0..self.inner.len() {
            if let Some(child) = self.inner.at(i) {
                child.draw(ctx);
            }
        }
    }

    fn layout(&mut self, ctx: &Context) {
        // All children get the same bounds
        // In a real implementation, we'd update each child's layout
    }

    fn hit_test(&self, ctx: &Context, p: Point, leaf: bool, control: bool) -> Option<&dyn Element> {
        if !ctx.bounds.contains(p) {
            return None;
        }

        // Hit test from top to bottom
        for i in (0..self.inner.len()).rev() {
            if let Some(child) = self.inner.at(i) {
                if let Some(hit) = child.hit_test(ctx, p, leaf, control) {
                    return Some(hit);
                }
            }
        }

        if leaf { None } else { Some(self) }
    }

    fn wants_control(&self) -> bool {
        self.inner.wants_control()
    }

    fn click(&mut self, ctx: &Context, btn: MouseButton) -> bool {
        // Delegate to focused layer or top layer
        false
    }

    fn handle_click(&self, ctx: &Context, btn: MouseButton) -> bool {
        // Forward click to topmost child that accepts it
        for i in (0..self.inner.len()).rev() {
            if let Some(child) = self.inner.at(i) {
                if child.handle_click(ctx, btn) {
                    return true;
                }
            }
        }
        false
    }

    fn handle_drag(&self, ctx: &Context, btn: MouseButton) {
        for i in (0..self.inner.len()).rev() {
            if let Some(child) = self.inner.at(i) {
                if child.hit_test(ctx, btn.pos, false, false).is_some() {
                    child.handle_drag(ctx, btn);
                    return;
                }
            }
        }
    }

    fn handle_key(&self, ctx: &Context, k: KeyInfo) -> bool {
        for i in (0..self.inner.len()).rev() {
            if let Some(child) = self.inner.at(i) {
                if child.handle_key(ctx, k) {
                    return true;
                }
            }
        }
        false
    }

    fn handle_text(&self, ctx: &Context, info: TextInfo) -> bool {
        for i in (0..self.inner.len()).rev() {
            if let Some(child) = self.inner.at(i) {
                if child.handle_text(ctx, info) {
                    return true;
                }
            }
        }
        false
    }

    fn handle_scroll(&self, ctx: &Context, dir: Point, p: Point) -> bool {
        for i in (0..self.inner.len()).rev() {
            if let Some(child) = self.inner.at(i) {
                if child.handle_scroll(ctx, dir, p) {
                    return true;
                }
            }
        }
        false
    }

    fn is_enabled(&self) -> bool {
        self.inner.is_enabled()
    }

    fn enable(&mut self, state: bool) {
        self.inner.enable(state);
    }

    fn wants_focus(&self) -> bool {
        self.inner.wants_focus()
    }

    fn begin_focus(&mut self, req: FocusRequest) {
        self.inner.begin_focus(req);
    }

    fn end_focus(&mut self) -> bool {
        self.inner.end_focus()
    }

    fn focus(&self) -> Option<&dyn Element> {
        self.inner.focus()
    }

    fn clear_focus(&self) {
        for i in 0..self.inner.len() {
            if let Some(child) = self.inner.at(i) {
                child.clear_focus();
            }
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Creates a layer from elements.
pub fn layer<E: Element + 'static>(elements: Vec<E>) -> Layer {
    let ptrs: Vec<ElementPtr> = elements.into_iter().map(|e| share(e)).collect();
    Layer::from_vec(ptrs)
}

/// Macro for creating layers.
#[macro_export]
macro_rules! layer {
    ($($elem:expr),* $(,)?) => {{
        let mut l = $crate::element::layer::Layer::new();
        $(
            l.push($crate::element::share($elem));
        )*
        l
    }};
}

/// Deck element - only shows one child at a time.
pub struct Deck {
    inner: Composite,
    active_index: usize,
}

impl Deck {
    /// Creates a new empty deck.
    pub fn new() -> Self {
        Self {
            inner: Composite::new(),
            active_index: 0,
        }
    }

    /// Creates a deck from a vector of elements.
    pub fn from_vec(children: Vec<ElementPtr>) -> Self {
        Self {
            inner: Composite::from_vec(children),
            active_index: 0,
        }
    }

    /// Adds an element to the deck.
    pub fn push(&mut self, element: ElementPtr) {
        self.inner.push(element);
    }

    /// Returns the active index.
    pub fn active_index(&self) -> usize {
        self.active_index
    }

    /// Sets the active index.
    pub fn set_active(&mut self, index: usize) {
        if index < self.inner.len() {
            self.active_index = index;
        }
    }

    /// Returns the active element.
    pub fn active(&self) -> Option<&dyn Element> {
        self.inner.at(self.active_index)
    }

    /// Returns the number of cards in the deck.
    pub fn count(&self) -> usize {
        self.inner.len()
    }
}

impl Default for Deck {
    fn default() -> Self {
        Self::new()
    }
}

impl Element for Deck {
    fn limits(&self, ctx: &BasicContext) -> ViewLimits {
        // Return limits of active child
        if let Some(child) = self.inner.at(self.active_index) {
            child.limits(ctx)
        } else {
            ViewLimits::full()
        }
    }

    fn draw(&self, ctx: &Context) {
        // Only draw active child
        if let Some(child) = self.inner.at(self.active_index) {
            child.draw(ctx);
        }
    }

    fn hit_test(&self, ctx: &Context, p: Point, leaf: bool, control: bool) -> Option<&dyn Element> {
        if let Some(child) = self.inner.at(self.active_index) {
            child.hit_test(ctx, p, leaf, control)
        } else {
            None
        }
    }

    fn wants_control(&self) -> bool {
        if let Some(child) = self.inner.at(self.active_index) {
            child.wants_control()
        } else {
            false
        }
    }

    fn handle_click(&self, ctx: &Context, btn: MouseButton) -> bool {
        if let Some(child) = self.inner.at(self.active_index) {
            child.handle_click(ctx, btn)
        } else {
            false
        }
    }

    fn handle_drag(&self, ctx: &Context, btn: MouseButton) {
        if let Some(child) = self.inner.at(self.active_index) {
            child.handle_drag(ctx, btn);
        }
    }

    fn handle_key(&self, ctx: &Context, k: KeyInfo) -> bool {
        if let Some(child) = self.inner.at(self.active_index) {
            child.handle_key(ctx, k)
        } else {
            false
        }
    }

    fn handle_text(&self, ctx: &Context, info: TextInfo) -> bool {
        if let Some(child) = self.inner.at(self.active_index) {
            child.handle_text(ctx, info)
        } else {
            false
        }
    }

    fn handle_scroll(&self, ctx: &Context, dir: Point, p: Point) -> bool {
        if let Some(child) = self.inner.at(self.active_index) {
            child.handle_scroll(ctx, dir, p)
        } else {
            false
        }
    }

    fn is_enabled(&self) -> bool {
        self.inner.is_enabled()
    }

    fn enable(&mut self, state: bool) {
        self.inner.enable(state);
    }

    fn wants_focus(&self) -> bool {
        if let Some(child) = self.inner.at(self.active_index) {
            child.wants_focus()
        } else {
            false
        }
    }

    fn clear_focus(&self) {
        for i in 0..self.inner.len() {
            if let Some(child) = self.inner.at(i) {
                child.clear_focus();
            }
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}