Skip to main content

ferroui_controls/
controls.rs

1use crate::Control;
2use ferroui_base::collections::{FerroList, ResetBehavior};
3use ferroui_base::{IntoRef, Ref};
4use std::ops::Deref;
5
6/// A collection of [`Control`]s.
7///
8/// The value is a shared handle: clones refer to the same collection, and
9/// handles compare by identity.
10#[derive(Clone, PartialEq, Debug)]
11pub struct Controls {
12    list: FerroList<Ref<Control>>,
13}
14
15impl Default for Controls {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl Controls {
22    /// Creates an empty collection.
23    pub fn new() -> Self {
24        let list = FerroList::new();
25        list.set_reset_behavior(ResetBehavior::Remove);
26        Self { list }
27    }
28
29    /// Creates a collection with the initial items.
30    pub fn from_items(items: impl IntoIterator<Item = Ref<Control>>) -> Self {
31        let result = Self::new();
32        result.list.add_range(items);
33        result
34    }
35
36    /// Adds a control (of any class) to the collection.
37    pub fn add(&self, item: impl IntoRef<Control>) {
38        self.list.add(item.into_ref());
39    }
40
41    /// Inserts a control (of any class) at the specified index.
42    pub fn insert(&self, index: usize, item: impl IntoRef<Control>) {
43        self.list.insert(index, item.into_ref());
44    }
45
46    /// Removes a control (of any class) from the collection. Returns true if
47    /// it was present.
48    pub fn remove(&self, item: impl IntoRef<Control>) -> bool {
49        self.list.remove(&item.into_ref())
50    }
51
52    /// Replaces the control at `index`.
53    pub fn set(&self, index: usize, item: impl IntoRef<Control>) {
54        self.list.set(index, item.into_ref());
55    }
56}
57
58impl Deref for Controls {
59    type Target = FerroList<Ref<Control>>;
60
61    fn deref(&self) -> &Self::Target {
62        &self.list
63    }
64}