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
use cursive_core::{
    View, Printer, Vec2,
    event::{Event, EventResult, AnyCb},
    direction::Direction,
    view::{
        CannotFocus,
        Selector,
        ViewNotFound
    },
    Rect
};

/// A view that lazily renders its contents
pub struct LazyView<V: View> {
    view_thunk: Option<Box<dyn FnOnce() -> V + Send + Sync>>,
    final_view: Option<V>,
    auto_init: bool
}

impl<V: View> LazyView<V> {
    /// Create a new `LazyView` with function to generate the inner view
    pub fn new<F: FnOnce() -> V + Send + Sync + 'static>(view_thunk: F) -> Self {
        LazyView {
            view_thunk: Some(Box::new(view_thunk)),
            final_view: None,
            auto_init: true
        }
    }

    /// Manually initialize the inner view
    pub fn initialize(&mut self) {
        if self.final_view.is_none() {
            let view_thunk = self.view_thunk.take().unwrap();
            self.final_view = Some(view_thunk());
        }
    }

    /// Is the view initialized?
    pub fn is_initialized(&self) -> bool {
        self.get_view().is_some()
    }

    /// Get access to the inner view
    /// 
    /// Returns `None` if the inner view isn't initialized yet
    pub fn get_view(&self) -> Option<&V> {
        self.final_view.as_ref()
    }

    /// Get mutable access to the inner view
    /// 
    /// Also initializes the inner view if it hasn't been done yet
    pub fn get_view_mut(&mut self) -> Option<&mut V> {
        self.initialize();
        self.final_view.as_mut()
    }

    /// Should the view be automatically initialized?
    pub fn set_auto_init(&mut self, auto_init: bool) {
        self.auto_init = auto_init;
    }

    /// Should the view be automatically initialized?
    ///
    /// Chainable version
    pub fn auto_init(mut self, auto_init: bool) -> Self {
        self.set_auto_init(auto_init);
        self
    }
}

impl<V: View> View for LazyView<V> {
    fn draw(&self, printer: &Printer) {
        if let Some(ref view) = self.final_view {
            view.draw(printer);
        }
    }

    fn layout(&mut self, size: Vec2) {
        if let Some(ref mut view) = self.final_view {
            view.layout(size);
        }
        else if self.auto_init {
            self.initialize();
        }
    }

    fn required_size(&mut self, constraint: Vec2) -> Vec2 {
        if let Some(ref mut view) = self.final_view {
            view.required_size(constraint)
        }
        else { Vec2::new(0, 0) }
    }

    fn on_event(&mut self, event: Event) -> EventResult {
        if let Some(ref mut view) = self.final_view {
            view.on_event(event)
        }
        else { EventResult::Ignored }
    }

    fn take_focus(&mut self, dir: Direction) -> Result<EventResult, CannotFocus> {
        if let Some(ref mut view) = self.final_view {
            view.take_focus(dir)
        }
        else {
            Err(CannotFocus)
        }
    }

    fn call_on_any(&mut self, sel: &Selector, cb: AnyCb) {
        if let Some(ref mut view) = self.final_view {
            view.call_on_any(sel, cb);
        }
    }

    fn focus_view(&mut self, sel: &Selector) -> Result<EventResult, ViewNotFound> {
        if let Some(ref mut view) = self.final_view {
            view.focus_view(sel)
        }
        else { Err(ViewNotFound) }
    }

    fn needs_relayout(&self) -> bool {
        if let Some(ref view) = self.final_view {
            view.needs_relayout()
        }
        else { true }
    }

    fn important_area(&self, size: Vec2) -> Rect {
        if let Some(ref view) = self.final_view {
            view.important_area(size)
        }
        else { Rect::from_point((1, 1)) }
    }
}