gooey-rs 0.12.1

Tile-based UI library with audio support
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::convert::TryFrom;
use std::sync::LazyLock;
use log;

use crate::prelude::*;

use super::set_layout;

pub use self::builder::TiledBuilder as Builder;

//
//  controls
//

pub static CONTROLS : LazyLock <Controls> = LazyLock::new (||
  controls::Builder::new()
    .buttons ({
      let mut v : Vec <controls::Button> = vec![
        controls::button::Builtin::FrameTiledIncreaseSize,
        controls::button::Builtin::FrameTiledDecreaseSize
      ].into_iter().map (Into::into).collect();
      v.extend (super::CONTROLS.buttons.iter().copied());
      v.into()
    })
    .build()
);

/// Builtin button control `FrameTiledIncreaseSize`.
///
/// Frame must have tiled layout.
pub fn increase_size (
  _             : &controls::button::Release,
  elements      : &Tree <Element>,
  node_id       : &NodeId,
  action_buffer : &mut Vec <(NodeId, Action)>
) {
  log::trace!("increase_size...");
  let Widget (layout, _, canvas) = Frame::try_get (elements, node_id).unwrap();
  let coordinate_kind = Some (canvas.coordinates.kind());
  let layout = {
    use controller::component::{layout, Layout};
    let mut tiled = layout::Tiled::try_from (layout.variant.clone()).unwrap();
    tiled.modify_size (1);
    Layout { variant: tiled.into(), .. layout.clone() }
  };
  set_layout (elements, node_id, layout, coordinate_kind, action_buffer);
  log::trace!("...increase_size");
}

/// Builtin button control `FrameTiledDecreaseSize`.
///
/// Frame must have tiled layout.
pub fn decrease_size (
  _             : &controls::button::Release,
  elements      : &Tree <Element>,
  node_id       : &NodeId,
  action_buffer : &mut Vec <(NodeId, Action)>
) {
  log::trace!("decrease_size...");
  let Widget (layout, _, canvas) = Frame::try_get (elements, node_id).unwrap();
  let coordinate_kind = Some (canvas.coordinates.kind());
  let layout  = {
    use controller::component::{layout, Layout};
    let mut tiled = layout::Tiled::try_from (layout.variant.clone()).unwrap();
    tiled.modify_size (-1);
    Layout { variant: tiled.into(), .. layout.clone() }
  };
  set_layout (elements, node_id, layout, coordinate_kind, action_buffer);
  log::trace!("...decrease_size");
}

//
//  crate
//

/// Returns the new coordinates of the target frame and any tiled siblings as
/// the result of setting the tiled layout
pub (crate) fn new_coordinates (
  elements : &Tree <Element>,
  frame_id : &NodeId,
  layout   : &layout::Tiled
) -> Vec <(NodeId, Coordinates)> {
  use controller::component::layout;
  log::trace!("coordinates...");
  let parent_id   = elements.get_parent_id (frame_id);
  let Widget (parent_layout, _, parent_canvas) = Frame::try_get (elements, parent_id)
    .unwrap();
  let parent_node = elements.get (parent_id).unwrap();
  let mut total_weights  = 0;
  let mut total_absolute = 0;
  let mut add_layout     = |layout : &layout::Tiled| match layout {
    layout::Tiled::Weighted (weight) => total_weights  += weight.get(),
    layout::Tiled::Absolute (size)   => total_absolute += size.get()
  };
  let tiled_children = parent_node.children().iter().filter_map (|child_id| {
    let Widget (child_layout, _, _) = Frame::try_get (elements, child_id).ok()?;
    if child_id == frame_id {
      // use the given tiled layout for the target frame
      add_layout (layout);
      Some ((child_id.clone(), layout))
    } else if let controller::component::layout::Variant::Tiled (tiled) =
      &child_layout.variant
    {
      add_layout (tiled);
      Some ((child_id.clone(), tiled))
    } else {
      None
    }
  }).collect::<Vec<_>>();
  let mut out = vec![];
  let mut start_weight   = 0;
  let mut start_absolute = 0;
  for (id, layout) in tiled_children.into_iter() {
    let coordinates = coordinates (
      &parent_layout.orientation, parent_canvas, layout, &mut start_weight,
      &mut start_absolute, total_weights, total_absolute);
    out.push ((id, coordinates));
  }
  log::trace!("...coordinates");
  out
}

/// Returns the coordinates of any tiled siblings resulting from destroying the target
/// tiled frame.
pub (crate) fn destroy_coordinates (
  elements : &Tree <Element>,
  frame_id : &NodeId
) -> Vec <(NodeId, Coordinates)> {
  use controller::component::layout;
  log::trace!("destroy_coordinates...");
  let parent_id = elements.get_parent_id (frame_id);
  let Widget (parent_layout, _, parent_canvas) = Frame::try_get (elements, parent_id)
    .unwrap();
  let parent_node = elements.get (parent_id).unwrap();
  let mut total_weights  = 0;
  let mut total_absolute = 0;
  let mut add_layout     = |layout : &layout::Tiled| match layout {
    layout::Tiled::Weighted (weight) => total_weights  += weight.get(),
    layout::Tiled::Absolute (size)   => total_absolute += size.get()
  };
  let tiled_children = parent_node.children().iter().filter_map (|child_id| {
    if child_id == frame_id {
      None
    } else {
      let Widget (child_layout, _, _) = Frame::try_get (elements, child_id).ok()?;
      if let controller::component::layout::Variant::Tiled (tiled) =
        &child_layout.variant
      {
        add_layout (tiled);
        Some ((child_id.clone(), tiled))
      } else {
        None
      }
    }
  }).collect::<Vec <_>>();
  let mut out = vec![];
  let mut start_weight = 0;
  let mut start_absolute = 0;
  for (id, layout) in tiled_children.into_iter() {
    let coordinates = coordinates (
      &parent_layout.orientation, parent_canvas, layout, &mut start_weight,
      &mut start_absolute, total_weights, total_absolute);
    out.push ((id, coordinates));
  }
  log::trace!("...destroy_coordinates");
  out
}

/// Compute the child coordinates for the given parent layout and orientation and list
/// of tiled child weights
pub (crate) fn child_coordinates (
  orientation : &(Orientation, Area),
  canvas      : &Canvas,
  layouts     : Vec <layout::Tiled>
) -> Vec <Coordinates> {
  use controller::component::layout;
  let mut child_coordinates = vec![];
  let mut total_weights  = 0;
  let mut total_absolute = 0;
  let add_layout = |layout : &layout::Tiled| match layout {
    layout::Tiled::Weighted (weight) => total_weights  += weight.get(),
    layout::Tiled::Absolute (size)   => total_absolute += size.get()
  };
  layouts.iter().for_each (add_layout);
  let mut start_weight = 0;
  let mut start_absolute = 0;
  for layout in layouts {
    child_coordinates.push (
      coordinates (orientation, canvas, &layout, &mut start_weight,
        &mut start_absolute, total_weights, total_absolute));
  }
  child_coordinates
}

//
//  private
//

fn coordinates (
  (parent_orientation, parent_area) : &(Orientation, Area),
  parent_canvas  : &Canvas,
  child_layout   : &layout::Tiled,
  start_weight   : &mut u32,
  start_absolute : &mut u32,
  total_weights  : u32,
  total_absolute : u32
) -> Coordinates {
  use controller::component::layout;
  use view::{coordinates, dimensions, position};
  log::trace!("coordinates...");
  let parent_coordinates = match parent_area {
    Area::Exterior => parent_canvas.coordinates,
    Area::Interior => parent_canvas.body_coordinates()
  };
  let (position_horizontal, position_vertical, width, height) =
    match parent_orientation {
      Orientation::Horizontal => {
        let height = parent_coordinates.dimensions_vertical();
        let parent_width = parent_coordinates.dimensions_horizontal();
        let position_vertical = parent_coordinates.position_vertical();
        let parent_position_horizontal = parent_coordinates.position_horizontal();
        let position_horizontal = parent_position_horizontal + *start_absolute as i32 +
          ( (*start_weight as f64 / total_weights as f64) *
            parent_width.saturating_sub (total_absolute) as f64
          ) as i32;
        let width = match child_layout {
          layout::Tiled::Weighted (weight) => {
            let weight = weight.get();
            *start_weight += weight;
            let next_position = parent_position_horizontal + *start_absolute as i32 +
              ( (*start_weight as f64 / total_weights as f64) *
                parent_width.saturating_sub (total_absolute) as f64
              ) as i32;
            (next_position - position_horizontal) as u32
          }
          layout::Tiled::Absolute (size) => {
            let size = size.get();
            *start_absolute += size;
            size
          }
        };
        (position_horizontal, position_vertical, width, height)
      }
      Orientation::Vertical => {
        let width = parent_coordinates.dimensions_horizontal();
        let parent_height = parent_coordinates.dimensions_vertical();
        let position_horizontal = parent_coordinates.position_horizontal();
        let parent_position_vertical = parent_coordinates.position_vertical();
        let position_vertical = parent_position_vertical + *start_absolute as i32 +
          ( (*start_weight as f64 / total_weights as f64) *
            parent_height.saturating_sub (total_absolute) as f64
          ) as i32;
        let height = match child_layout {
          layout::Tiled::Weighted (weight) => {
            let weight = weight.get();
            *start_weight += weight;
            let next_position = parent_position_vertical + *start_absolute as i32 +
              ( (*start_weight as f64 / total_weights as f64) *
                parent_height.saturating_sub (total_absolute) as f64
              ) as i32;
            (next_position - position_vertical) as u32
          }
          layout::Tiled::Absolute (size) => {
            let size = size.get();
            *start_absolute += size;
            size
          }
        };
        (position_horizontal, position_vertical, width, height)
      }
    };
  let out = if parent_coordinates.kind() == coordinates::Kind::Tile {
    ( position::Tile::new_rc (position_vertical, position_horizontal),
      dimensions::Tile::new_rc (height, width)
    ).into()
  } else {
    debug_assert!(parent_coordinates.kind() == coordinates::Kind::Pixel);
    ( position::Pixel::new_xy (position_horizontal, position_vertical),
      dimensions::Pixel::new_wh (width, height)
    ).into()
  };
  log::trace!("...coordinates");
  out
}

//
//  builder
//

mod builder {
  use derive_builder::Builder;
  use crate::prelude::*;
  use frame::set_coordinates;

  #[derive(Builder)]
  #[builder(public, pattern="owned", build_fn(private), setter(strip_option))]
  struct Tiled <'a, A : Application> {
    elements     : &'a Tree <Element>,
    parent_id    : &'a NodeId,
    #[builder(default)]
    appearances  : Appearances,
    #[builder(default)]
    bindings     : Option <&'a Bindings <A>>,
    #[builder(default)]
    border       : Option <Border>,
    #[builder(default)]
    clear_color  : canvas::ClearColor,
    #[builder(default)]
    create_order : CreateOrder,
    #[builder(default)]
    disabled     : bool,
    #[builder(default)]
    layout       : layout::Tiled,
    #[builder(default)]
    name         : Option <String>,
    #[builder(default)]
    orientation  : (Orientation, Area),
  }

  impl <'a, A : Application> TiledBuilder <'a, A> {
    pub const fn new (elements : &'a Tree <Element>, parent_id : &'a NodeId) -> Self {
      TiledBuilder {
        elements:     Some (elements),
        parent_id:    Some (parent_id),
        appearances:  None,
        bindings:     None,
        border:       None,
        clear_color:  None,
        create_order: None,
        disabled:     None,
        layout:       None,
        name:         None,
        orientation:  None
      }
    }
  }

  impl <A : Application> BuildActions for TiledBuilder <'_, A> {
    /// Create a new tiled frame that is attached to the given parent frame.
    ///
    /// This returns the creation action, and any modifications required to tiled
    /// sibling frames attached to the same parent.
    fn build_actions (self) -> Vec <(NodeId, Action)> {
      use view::component::Canvas;
      log::trace!("build actions...");
      let Tiled {
        elements, parent_id, appearances, bindings, border, clear_color,
        create_order, disabled, layout, name, orientation
      } = self.build()
        .map_err(|err| log::error!("frame free builder error: {err:?}"))
        .unwrap();
      let bindings_empty = Bindings::empty();
      let bindings = bindings.unwrap_or (&bindings_empty)
        .get_bindings (&super::CONTROLS);
      let (new_coordinates, sibling_coordinates) =
        create_coordinates (elements, parent_id, &layout, create_order);
      let frame = {
        use controller::component::Layout;
        let controller = {
          let mut controller     = Controller::with_bindings (&bindings);
          controller.component   = Layout {
            orientation, variant: layout.into()
          }.into();
          controller.appearances = appearances;
          controller
        };
        let view = {
          let coordinates = new_coordinates;
          let appearance  = controller.get_appearance().clone();
          let component   = Canvas { coordinates, clear_color, border }.into();
          View { component, appearance, .. View::default() }
        };
        let name = name.unwrap_or ("Frame".to_string());
        let mut frame = Element::new (name, controller, Model::default(), view);
        let parent_node = elements.get (parent_id).unwrap();
        if parent_node.data().controller.state == State::Disabled || disabled {
          frame.disable()
        }
        frame
      };
      let mut out = vec![
        (parent_id.clone(), Action::create_singleton (frame, create_order))
      ];
      for (id, coordinates) in sibling_coordinates.into_iter() {
        set_coordinates (elements, &id, &coordinates, None, &mut out);
      }
      log::trace!("...build actions");
      out
    }
  }

  /// Returns the `NodeId` and coordinates of any tiled child frames of the given parent
  /// frame resulting from creating a new child frame with the given tiled weight.
  fn create_coordinates (
    elements        : &Tree <Element>,
    parent_frame_id : &NodeId,
    layout          : &layout::Tiled,
    order           : CreateOrder
  ) -> (Coordinates, Vec <(NodeId, Coordinates)>) {
    use controller::component::layout;
    log::trace!("create_coordinates...");
    let Widget (parent_layout, _, parent_canvas) =
      Frame::try_get (elements, parent_frame_id).unwrap();
    let parent_node = elements.get (parent_frame_id).unwrap();
    let mut total_weights  = 0;
    let mut total_absolute = 0;
    let mut add_layout     = |layout : &layout::Tiled| match layout {
      layout::Tiled::Weighted (weight) => total_weights  += weight.get(),
      layout::Tiled::Absolute (size)   => total_absolute += size.get()
    };
    add_layout (layout);
    let sibling_layouts = parent_node.children().iter().map (|child_id| {
      let Widget (child_layout, _, _) = Frame::try_get (elements, child_id).ok()?;
      if let layout::Variant::Tiled (tiled) = &child_layout.variant {
        add_layout (tiled);
        Some ((child_id, tiled))
      } else {
        None
      }
    }).collect::<Vec <_>>();
    let order_index = match order {
      CreateOrder::Prepend        => Some (0),
      CreateOrder::NthSibling (n) => Some (n),
      CreateOrder::Append         => None
    };
    let mut sibling_coordinates = vec![];
    let mut start_weight = 0;
    let mut start_absolute = 0;
    let mut new_coordinates = None;
    let mut index = 0;
    // sibling tiled frames
    for (i, maybe_tiled) in sibling_layouts.into_iter().enumerate() {
      if order_index == Some (i as u32) {
        new_coordinates = Some (super::coordinates (
          &parent_layout.orientation, parent_canvas, layout, &mut start_weight,
          &mut start_absolute, total_weights, total_absolute
        ));
      }
      if let Some ((id, tiled)) = maybe_tiled {
        let coordinates = super::coordinates (
          &parent_layout.orientation, parent_canvas, tiled, &mut start_weight,
          &mut start_absolute, total_weights, total_absolute);
        sibling_coordinates.push ((id.clone(), coordinates));
      }
      index = i + 1;
    }
    if new_coordinates.is_none() {
      if cfg!(debug_assertions) && let Some (n) = order_index {
        debug_assert_eq!(n, index as u32);
      }
      new_coordinates = Some (super::coordinates (
        &parent_layout.orientation, parent_canvas, layout,
        &mut start_weight, &mut start_absolute, total_weights, total_absolute
      ));
    }
    log::trace!("...create_coordinates");
    (new_coordinates.unwrap(), sibling_coordinates)
  }
}