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
use std::cell::RefCell;
use std::rc::Rc;

use fui_core::ControlObject;
use fui_core::{EventProcessor, WindowService};

use crate::DrawingWindowTarget;

pub struct Window {
    pub drawing_window_target: DrawingWindowTarget,
    pub event_processor: EventProcessor,
    pub is_dirty: bool,
    control_layers: Vec<Rc<RefCell<dyn ControlObject>>>,
}

impl Window {
    pub fn new(drawing_window_target: DrawingWindowTarget) -> Self {
        Window {
            drawing_window_target,
            event_processor: EventProcessor::new(),
            is_dirty: false,
            control_layers: Vec::new(),
        }
    }

    pub fn get_drawing_target(&self) -> &DrawingWindowTarget {
        &self.drawing_window_target
    }

    pub fn get_layers(&self) -> &Vec<Rc<RefCell<dyn ControlObject>>> {
        &self.control_layers
    }
}

impl WindowService for Window {
    fn add_layer(&mut self, control: Rc<RefCell<dyn ControlObject>>) {
        self.control_layers.push(control);
        self.is_dirty = true;
    }

    fn remove_layer(&mut self, control: &Rc<RefCell<dyn ControlObject>>) {
        let mut i = 0;
        while i != self.control_layers.len() {
            if Rc::ptr_eq(&self.control_layers[i], control) {
                self.control_layers.remove(i);
            } else {
                i += 1;
            }
        }
        self.is_dirty = true;
    }
}