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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Single view controller

use super::{driver, Driver};
use kas::model::SingleData;
#[allow(unused)]
use kas::model::{SharedData, SharedDataMut, SharedRc};
use kas::prelude::*;
use std::borrow::Borrow;

impl_scope! {
    /// View controller for 0D data (singe item)
    ///
    /// This widget supports a view over a single shared data item.
    ///
    /// The shared data type `T` must support [`SingleData`].
    /// One may use [`SharedRc`] or a custom shared data type.
    ///
    /// The driver `V` must implement [`Driver`] over `T`.
    /// The default driver is [`driver::View`]; others are available in the
    /// [`driver`] module or [`Driver`] may be implemented directly.
    ///
    /// # Messages
    ///
    /// When a view widget pushes a message, [`Driver::on_message`] is called.
    #[autoimpl(Debug ignore self.driver)]
    #[derive(Clone)]
    #[widget{
        layout = self.child;
    }]
    pub struct SingleView<T: SingleData, V: Driver<T::Item, T> = driver::View> {
        core: widget_core!(),
        driver: V,
        data: T,
        data_ver: u64,
        #[widget]
        child: V::Widget,
    }

    impl Default for Self
    where
        T: Default,
        V: Default,
    {
        fn default() -> Self {
            Self::new(T::default())
        }
    }
    impl Self
    where
        V: Default,
    {
        /// Construct a new instance
        pub fn new(data: T) -> Self {
            Self::new_with_driver(<V as Default>::default(), data)
        }
    }
    impl Self {
        /// Construct a new instance with explicit driver
        pub fn new_with_driver(driver: V, data: T) -> Self {
            let child = driver.make();
            let data_ver = data.version();
            SingleView {
                core: Default::default(),
                driver,
                data,
                data_ver,
                child,
            }
        }

        /// Access the data object
        pub fn data(&self) -> &T {
            &self.data
        }

        /// Access the data object (mut)
        pub fn data_mut(&mut self) -> &mut T {
            &mut self.data
        }

        /// Borrow a reference to the shared value
        pub fn borrow_value(&self) -> Option<impl Borrow<T::Item> + '_> {
            self.data.borrow(&())
        }

        /// Get a copy of the shared value
        pub fn get_value(&self) -> T::Item {
            self.data.get_cloned(&()).unwrap()
        }

        /// Set shared data
        ///
        /// This method updates the shared data, if supported (see
        /// [`SharedDataMut::borrow_mut`]). Other widgets sharing this data
        /// are notified of the update, if data is changed.
        pub fn set_value(&self, mgr: &mut EventMgr, data: T::Item)
        where
            T: SharedDataMut,
        {
            self.data.set(mgr, &(), data);
        }

        /// Update shared data
        ///
        /// This method updates the shared data, if supported (see
        /// [`SharedDataMut::with_ref_mut`]). Other widgets sharing this data
        /// are notified of the update, if data is changed.
        pub fn update_value<U>(
            &self,
            mgr: &mut EventMgr,
            f: impl FnOnce(&mut T::Item) -> U,
        ) -> Option<U>
        where
            T: SharedDataMut,
        {
            self.data.with_ref_mut(mgr, &(), f)
        }
    }

    impl Layout for Self {
        fn size_rules(&mut self, size_mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
            let mut rules = self.child.size_rules(size_mgr, axis);
            rules.set_stretch(rules.stretch().max(Stretch::Low));
            rules
        }
    }

    impl Widget for Self {
        fn configure(&mut self, mgr: &mut ConfigMgr) {
            // We set data now, after child is configured
            let item = self.data.borrow(&()).unwrap();
            *mgr |= self.driver.set(&mut self.child, &(), item.borrow());
        }

        fn handle_event(&mut self, mgr: &mut EventMgr, event: Event) -> Response {
            match event {
                Event::Update { .. } => {
                    let data_ver = self.data.version();
                    if data_ver > self.data_ver {
                        let item = self.data.borrow(&()).unwrap();
                        *mgr |= self.driver.set(&mut self.child, &(), item.borrow());
                        self.data_ver = data_ver;
                    }
                    Response::Used
                }
                _ => Response::Unused,
            }
        }

        fn handle_message(&mut self, mgr: &mut EventMgr, _: usize) {
            self.driver
                .on_message(mgr, &mut self.child, &self.data, &());
        }
    }
}