iced_af 0.4.1

The iced application framework project.
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// This file is part of `iced_af` crate. For the terms of use, please see the file
// called LICENSE-BSD-3-Clause at the top level of the `iced_af` crate.

//! The state manager of the multi-window application.
//!
//! Add new window states under the `src/windows/` directory.

use crate::{
    application::{
        constants::WINDOW_DEFAULT_DATA, session::WindowData, ApplicationError,
        Message, Session, WindowType
    },
    core::{
        error::CoreError,
        traits::{AnyWindowTrait, WindowTrait},
    },
    window::{default, fatal_error},
};
use iced::{window, Point, Size, Task};
use std::{collections::BTreeMap, usize};

#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};

/// The manager is responsible for creating new windows, and removing windows.
/// Keeping the window state and window Id in sync, and the windows are in a
/// safe hierarchy, allowing lower windows to be disabled, or all other windows
/// disabled except the active window (useful when changes may affects all
/// opened files, databases, etc). Also included is a cache for re-usable
/// states.
pub struct Manager {
    // All active window states
    states: BTreeMap<window::Id, Entry>,

    // Window IDs placed in main window threads
    threads: VecOption<Vec<window::Id>>,

    // Reusable states cache
    reusable: BTreeMap<WindowType, Box<dyn AnyWindowTrait>>,
}

impl Manager {
    /// Initialise the state manager.
    /// 
    /// The reusable Default window state is added to the cache.
    /// 
    /// The return `Result` is used instead of `Manager` instance, just in case
    /// the default window creation produces an error.
    pub fn try_new() -> Result<Manager, ApplicationError> {
        let mut reusable = BTreeMap::<WindowType, Box<dyn AnyWindowTrait>>::new();
        reusable.insert(WindowType::Default, Box::new(default::State::new()));
        Ok(Manager {
            states: BTreeMap::<window::Id, Entry>::new(),
            threads: VecOption::<Vec<window::Id>>::new(),
            reusable,
        })
    }

    //
    // ----- Retrieve/information methods
    //

    /// Retrieve a reference to the state instance for the specified window
    /// Id.
    pub fn state(&self, id: &window::Id) -> Option<&Box<dyn AnyWindowTrait>> {
        self.states.get(id).map(|x| &x.state)
    }

    /// Retrieve a mutable reference to the mutable state instance for the
    /// specified window Id.
    pub fn state_mut(&mut self, id: &window::Id) -> Option<&mut Box<dyn AnyWindowTrait>> {
        self.states.get_mut(id).map(|x| &mut x.state)
    }

    /// Return the number of window threads.
    pub fn thread_count(&self) -> usize {
        self.threads.count
    }

    /// Return a list of the threads' root window Id.
    pub fn thread_list(&self) -> Vec<window::Id> {
        let mut _vec = Vec::<window::Id>::new();
        for option in self.threads.vec.iter() {
            if option.is_some() {
                _vec.push(option.as_ref().unwrap()[0].clone());
            }
        }
        _vec
    }

    /// Indicates whether the window is enabled for events.
    pub fn is_enabled(&self, id: &window::Id) -> Option<bool> {
        self.states.get(id).map(|x| x.enabled)
    }

    /// Return the parent window Id of the specified window Id if available.
    pub fn parent(&self, id: &window::Id) -> Option<window::Id> {
        self.states.get(id).map(|x| x.parent)?
    }

    //
    // ----- Spawning methods
    //

    /// Disable the parent window of a thread, or all threads by passing
    /// `None`. Disabling all threads is usually done for window that
    /// contains settings, that can affect all threads.
    fn disable_windows(&mut self, parent: &Option<window::Id>) -> Vec<window::Id> {
        match parent {
            None => {
                trace!("disable(): all threads");
                let mut disabled = Vec::<window::Id>::with_capacity(self.threads.vec.len());
                for thread in &self.threads.vec {
                    if thread.is_some() {
                        if let Some(id) = thread.as_ref().unwrap().last() {
                            let entry = self.states.get_mut(id).unwrap();
                            entry.enabled = false;
                            disabled.push(*id);
                        }
                    }
                }
                disabled
            }
            Some(parent) => {
                trace!("disable(): for parent {:?}", parent);
                let entry = self.states.get_mut(&parent).unwrap();
                entry.enabled = false;
                vec![*parent]
            }
        }
    }

    /// Try to create a new window thread, using the provided `iced` window
    /// settings located in the application's session data, and the window
    /// state.
    pub fn try_create_thread(
        &mut self,
        session: &mut Session,
        state: Box<dyn AnyWindowTrait>,
    ) -> Result<Task<Message>, CoreError> {
        debug!(
            "try_create_thread(): for window type ‘{:?}’",
            state.window_type()
        );

        // Set `iced` window settings, and spawn.
        let id = try_create(session, state.window_type())?;

        // Insert state and open the window.
        let entry = Entry {state, enabled: true, parent: None, disabled: None};
        let _ = self.threads.push(vec![id.0]);
        self.states.insert(id.0, entry);
        trace!("try_create_thread(): inserted state for {:?}, next open window", id.0);
        Ok(id.1.map(move |id| Message::WindowOpened(id)))
    }

    /// Try to create a window on an existing main window thread, using the
    /// provided `iced` window settings located in the application's session
    /// data, the window state, and parent window Id (used to locate the main
    /// thread to attached window to).
    pub fn try_create_window(
        &mut self,
        session: &mut Session,
        state: Box<dyn AnyWindowTrait>,
        parent: window::Id, // Typically be the calling window
    ) -> Result<Task<Message>, CoreError> {
        debug!(
            "try_create_window(): for window type ‘{:?}’",
            state.window_type()
        );
        if !self.states.contains_key(&parent) {
            return Err(CoreError::WindowIdNotFound(
                parent,
                "Manager.state".to_string(),
            ));
        }
        let parent = Some(parent);
        let disabled = if state.is_global_disable() {
            self.disable_windows(&None)
        } else {
            self.disable_windows(&parent)
        };

        // Set `iced` window settings, and spawn
        let id = try_create(session, state.window_type())?;

        // Insert state and open the window.
        let entry = Entry {state, enabled: true, parent, disabled: Some(disabled)};
        for thread in &mut self.threads.vec {
            if thread.is_some() {
                let actual = thread.as_mut().unwrap();
                if actual.last() == entry.parent.as_ref() {
                    actual.push(id.0);
                    break;
                }
            }
        }
        self.states.insert(id.0, entry);
        trace!("try_create_window(): inserted state for {:?}, next open window", id.0);
        Ok(id.1.map(move |id| Message::WindowOpened(id)))
    }

    /// Obtain reusable state if available, for the specified window type.
    pub fn use_reusable(&mut self, window_type: WindowType) -> Option<Box<dyn AnyWindowTrait>> {
        self.reusable.remove(&window_type)
    }

    /// Create the fatal error window.
    ///
    /// Every window is disable to prevent any additional fatal errors from occurring, before
    /// spawning the `FatalError` window.
    pub fn create_fatal_error_window(
        &mut self,
        session: &mut Session,
    ) -> Task<Message> {
        let state = Box::new(fatal_error::State::new());
        debug!(
            "create_fatal_window(): for window type ‘{:?}’",
            state.window_type()
        );
        let id = match try_create(session, state.window_type()) {
            Ok(value) => value,
            Err(_) => {
                let settings = window::Settings {
                    size: Size::new(500f32, 200f32),
                    resizable: false,
                    position: window::Position::Centered,
                    exit_on_close_request: false,
                    ..Default::default()
                };
                window::open(settings)
            }
        };
        let _ = self.disable_windows(&None);

        // Insert state and open the window.
        let entry = Entry {state, enabled: true, parent: None, disabled: None};
        for thread in &mut self.threads.vec {
            if thread.is_some() {
                let actual = thread.as_mut().unwrap();
                if actual.last() == entry.parent.as_ref() {
                    actual.push(id.0);
                    break;
                }
            }
        }
        self.states.insert(id.0, entry);
        trace!("create_fatal_error_window(): inserted state for {:?}, next open window", id.0);
        id.1.map(move |id| Message::WindowOpened(id))
    }

    //
    // ----- Closing methods
    //

    /// Close a single window.
    pub fn close_window(
        &mut self,
        id: window::Id,
    ) -> Result<Task<Message>, CoreError> {
        trace!("close_window(): id {:?}", id);
        let task = window::close(id);
        Ok(task.chain(Task::done(Message::WindowClosed(id))))
    }

    /// Allows for multiple windows to be closed at once.
    /// 
    /// Ensure the vector is ordered from newest to oldest window, else fatal error may occur.
    pub fn close_multiple(
        &mut self,
        ids: Vec<window::Id>,
    ) -> Result<Task<Message>, CoreError> {
        debug!("close_multiple()");
        let mut tasks = Task::none();
        for id in ids {
            tasks = tasks.chain(window::close(id));
            tasks = tasks.chain(Task::done(Message::WindowClosed(id)));
        }
        Ok(tasks)
    }

    /// Close an entire main window thread, using any window Id in the thread.
    ///
    /// This method is only called once checks for unsaved data is done
    pub fn close_thread(
        &mut self,
        id: window::Id,
    ) -> Result<Task<Message>, CoreError> {
        debug!("close_thread(): contains {:?}", id);
        if self.states.get(&id).is_none() {
            return Err(CoreError::WindowIdNotFound(id, "Manager.states".to_string()));
        };

        // Find the thread to close.
        let index = self.threads.vec.iter().position(|thread| {
            if thread.is_some() {
                let actual = thread.as_ref().unwrap();
                for id_ in actual {
                    if id_ == &id {
                        return true;
                    }
                }
            }
            false
        })
        .unwrap();

        // Close the thread.
        let mut thread = self.threads.vec[index].as_ref().unwrap().clone();
        thread.reverse();
        let mut tasks = Task::none();
        for state_id in thread {
            tasks = tasks.chain(window::close(state_id));
        }
        tasks = tasks.chain(Task::done(Message::ThreadClosed(index)));
        trace!("{:?}", self.threads.vec);
        Ok(tasks)
    }
    
    /// Re-enable windows that was disabled by this window, and remove the state.
    pub fn window_closed(
        &mut self,
        id: window::Id,
    ) -> Result<(), CoreError> {
        debug!("Window closed {:?}, now removing state.", id);

        // Re-enable disabled windows by this window.
        let mut _disabled: Option<Vec<window::Id>> = None;
        {
            let Some(entry) = self.states.get_mut(&id) else {
                return Err(CoreError::WindowIdNotFound(
                    id,
                    "Manager.states".to_string(),
                ));
            };
            _disabled = entry.disabled.take();
        }
        if _disabled.is_some() {
            let disabled = _disabled.unwrap();
            trace!("Disabled windows: {:?}", disabled);
            for disabled_id in disabled {
                let disabled_state = self.states.get_mut(&disabled_id).unwrap();
                disabled_state.enabled = true;
            }
        }

        // Remove window ID from the window thread
        for thread in &mut self.threads.vec {
            if thread.is_some() {
                let actual = thread.as_mut().unwrap();
                if actual.last() == Some(&id) {
                    _ = actual.pop();
                    break;
                }
            }
        }

        // Remove the state
        let entry = self.states.remove(&id).unwrap();
        if entry.state.is_reusable() {
            debug!("window_closed(): cached reusable state for {:?}", id);
            self.reusable.insert(entry.state.window_type(), entry.state);
        }
        trace!("window_closed(): removed state for {:?}", id);
        Ok(())
    }
    
    /// Remove the thread from threads, now that all windows has been closed.
    pub fn thread_closed(
        &mut self,
        index: usize,
    ) -> Result<(), CoreError> {
        trace!("thread_closed(): removed thread {:?}", index);
        let _ = self.threads.take(index); 
        Ok(())
    }
}

//
// ----- Private functions
//

/// Try to create the `iced` window for the specified window type, using the
/// `iced` windows Settings located in the application's session data.
fn try_create(
    session: &mut Session,
    window_type: WindowType,
) -> Result<(window::Id, Task<window::Id>), CoreError> {
    let Some(defaults) = WINDOW_DEFAULT_DATA.get(&window_type.as_str()) else {
        return Err(CoreError::WindowTypeNotFound(
            window_type,
            "WINDOW_DEFAULT_DATA".to_string(),
        ));
    };
    trace!("try_create(): WindowType: {:?}; defaults: {:?}", window_type, defaults);
    if !session.windows.contains_key(&window_type) {
        session.windows.insert(
            window_type.clone(),
            WindowData {
                size: defaults.size.clone(),
                position: None,
            },
        );
    }
    let data = session.windows.get(&window_type).unwrap();
    let position = if data.position.is_none() {
        window::Position::Centered
    } else {
        let value = data.position.as_ref().unwrap();
        window::Position::Specific(Point {
            x: value.0,
            y: value.1,
        })
    };
    let settings = window::Settings {
        size: Size::new(data.size.0, data.size.1),
        resizable: defaults.resizable,
        position,
        exit_on_close_request: false,
        ..Default::default()
    };
    Ok(window::open(settings))
}

#[derive(Debug)]
struct VecOption<T> {
    count: usize,
    vec: Vec<Option<T>>
}

impl<T> VecOption<T> {
    fn new() -> Self {
        VecOption::<T> {
            count: 0,
            vec: Vec::<Option<T>>::new(),
        }
    }

    fn push(&mut self, element: T) -> usize {
        let mut len = self.vec.len();
        trace!("push(): start: len: {}, count: {}", len, self.count);
        if self.count < len {
            if let Some(index) = self.vec.iter().position(|x| x.is_none()) {
                self.vec[index] = Some(element);
                len = index;
            }
        } else {
            self.vec.push(Some(element));
        }
        self.count += 1;
        trace!("push(): end: len: {}, count: {}, index: {}", self.vec.len(), self.count, len);
        len
    }

    fn pop(&mut self) -> Option<T> {
        trace!("pop(): start: len: {}, count: {}", self.vec.len(), self.count);
        if self.count == 0 {
            return None;
        }
        let element = self.vec.pop().unwrap();
        if element.is_some() {
            self.count -= 1;
            if self.count == 0 {
                self.vec.clear();
            }
        }
        trace!("pop(): end: len: {}, count: {}", self.vec.len(), self.count);
        element
    }

    fn replace(&mut self, index: usize, element: Option<T>) -> Option<T> {
        trace!("replace(): start: len: {}, count: {}, index: {}", self.vec.len(), self.count, index);
        let old = self.vec[index].take();
        self.vec[index] = element;
        trace!("replace(): end: len: {}, count: {}", self.vec.len(), self.count);
        old
    }

    fn take(&mut self, index: usize) -> Option<T> {
        trace!("take(): start: len: {}, count: {}, index: {}", self.vec.len(), self.count, index);
        if self.count == 0 {
            return None;
        }
        let element = self.vec[index].take();
        if element.is_some() {
            self.count -= 1;
            if self.count == 0 {
                self.vec.clear();
            }
        }
        trace!("take(): end: len: {}, count: {}", self.vec.len(), self.count);
        element
    }
}

/// Just a simple struct with named fields.
struct Entry {
    state: Box<dyn AnyWindowTrait>,    // The window state
    enabled: bool,                     // This window enabled
    parent: Option<window::Id>,        // Parent
    disabled: Option<Vec<window::Id>>, // Windows disabled by this window::Id
}