Skip to main content

waft_test/
windows.rs

1//! Requests for views that a desktop answers at once, with a window each.
2
3use std::cell::{Cell, RefCell};
4use std::rc::{Rc, Weak};
5use std::time::Duration;
6
7use waft_embedder::{
8    HostWindow, IconImage, RawWindowHandle, Rect, RequestId, ScreenId, Size, ViewId, ViewRef,
9    WindowConfig, WindowSizeLimits,
10};
11
12use crate::TestView;
13
14/// The number of the first view made on request, out of the way of the views a test gives.
15const FIRST_VIEW_ID: u64 = 1000;
16
17/// The number of the view made for request `id`: a window's, or one added on a layer.
18pub(crate) fn view_of_request(id: RequestId) -> ViewId {
19    ViewId(FIRST_VIEW_ID + id.0)
20}
21
22/// A desktop's answer to requests for views: each request makes a window at once, over a view
23/// that carries the request's id and initial data. A test hands it to a platform
24/// ([`TestPlatform::with_windows`](crate::TestPlatform::with_windows)), which lists the views
25/// of the windows still open; the test tells the framework of each new view itself, as a host
26/// would through `EmbedderClient::view_added`.
27pub struct TestWindows {
28    made: RefCell<Vec<Rc<TestWindow>>>,
29    /// The screen every window is made on; none by default.
30    screen: Option<ScreenId>,
31}
32
33impl TestWindows {
34    pub fn new() -> TestWindows {
35        TestWindows {
36            made: RefCell::default(),
37            screen: None,
38        }
39    }
40
41    /// The same desktop making every window on `screen`, which each view's metrics name.
42    pub fn on_screen(mut self, screen: ScreenId) -> TestWindows {
43        self.screen = Some(screen);
44        self
45    }
46
47    /// The windows made so far, in the order they were asked for, closed ones included.
48    pub fn made(&self) -> Vec<Rc<TestWindow>> {
49        self.made.borrow().clone()
50    }
51
52    /// Makes the window configured as `config` that request `id` asks for, over a view carrying
53    /// `initial_data`.
54    pub(crate) fn request(
55        &self,
56        id: RequestId,
57        config: WindowConfig,
58        initial_data: Option<String>,
59    ) {
60        let [width, height] = config.size;
61        let mut view = TestView::new(width, height)
62            .with_id(view_of_request(id))
63            .with_request(id, initial_data);
64        if let Some(screen) = self.screen {
65            view = view.on_screen(screen);
66        }
67        self.made
68            .borrow_mut()
69            .push(TestWindow::around(view, config));
70    }
71
72    /// The views of the windows still open.
73    pub(crate) fn open_views(&self) -> Vec<ViewRef> {
74        self.made
75            .borrow()
76            .iter()
77            .filter(|window| window.closes() == 0)
78            .map(|window| Rc::clone(&window.view) as ViewRef)
79            .collect()
80    }
81}
82
83impl Default for TestWindows {
84    fn default() -> TestWindows {
85        TestWindows::new()
86    }
87}
88
89/// A window that shows nothing, keeps the frame, the title and the icon it was last given, whether
90/// it was last shown or hidden, whether it is maximized or full screen and the size limits it was
91/// last given, and
92/// counts how often it
93/// was closed and dragged: one of
94/// [`TestWindows`], or one a test gives the app a view in. A closed window's view is in no window any more, and is no longer among the
95/// views of [`TestWindows`]. It is neither minimized nor occluded until a test says so, and the
96/// test then reports the change itself, as a host would through `EmbedderClient::window_changed`.
97pub struct TestWindow {
98    view: Rc<TestView>,
99    config: WindowConfig,
100    frame: Cell<Rect>,
101    title: RefCell<String>,
102    icon: RefCell<Option<IconImage>>,
103    shown: Cell<bool>,
104    closes: Cell<u32>,
105    drags: Cell<u32>,
106    activations: Cell<u32>,
107    minimized: Cell<bool>,
108    maximized: Cell<bool>,
109    fullscreen: Cell<bool>,
110    occluded: Cell<bool>,
111    min_size: Cell<Option<Size>>,
112    max_size: Cell<Option<Size>>,
113}
114
115impl TestWindow {
116    /// A window configured as `config` around `view`, which is in it from now on: as a desktop
117    /// makes for a request, or as WaOS shows a program's view it gives. It starts where the
118    /// configuration places it, or at the origin, and shown as the configuration says.
119    pub fn around(view: TestView, config: WindowConfig) -> Rc<TestWindow> {
120        let [width, height] = config.size;
121        let [left, top] = config.position.unwrap_or([0.0, 0.0]);
122        Rc::new_cyclic(|window: &Weak<TestWindow>| {
123            view.set_window(Some(window.clone()));
124            TestWindow {
125                view: Rc::new(view),
126                frame: Cell::new(Rect::from_ltwh(left, top, width, height)),
127                title: RefCell::new(config.title.clone()),
128                icon: RefCell::new(config.icon.clone()),
129                shown: Cell::new(config.visible),
130                config,
131                closes: Cell::new(0),
132                drags: Cell::new(0),
133                activations: Cell::new(0),
134                minimized: Cell::new(false),
135                maximized: Cell::new(false),
136                fullscreen: Cell::new(false),
137                occluded: Cell::new(false),
138                min_size: Cell::new(None),
139                max_size: Cell::new(None),
140            }
141        })
142    }
143
144    /// The view the window shows, which counts the pictures presented to it.
145    pub fn test_view(&self) -> &Rc<TestView> {
146        &self.view
147    }
148
149    /// The configuration the window was asked for with.
150    pub fn config(&self) -> &WindowConfig {
151        &self.config
152    }
153
154    /// The title the window was last given, its configuration's until the app sets another.
155    pub fn title(&self) -> String {
156        self.title.borrow().clone()
157    }
158
159    /// The icon the window was last given, its configuration's until the app sets another.
160    pub fn icon(&self) -> Option<IconImage> {
161        self.icon.borrow().clone()
162    }
163
164    /// Whether the window was last shown, rather than hidden.
165    pub fn is_shown(&self) -> bool {
166        self.shown.get()
167    }
168
169    /// How many times the window was closed.
170    pub fn closes(&self) -> u32 {
171        self.closes.get()
172    }
173
174    /// How many times the window was dragged.
175    pub fn drags(&self) -> u32 {
176        self.drags.get()
177    }
178
179    /// How many times the window was asked to be activated.
180    pub fn activations(&self) -> u32 {
181        self.activations.get()
182    }
183
184    /// The window is minimized, or restored, from now on.
185    pub fn set_minimized(&self, minimized: bool) {
186        self.minimized.set(minimized);
187    }
188
189    /// The window is occluded, or shown, from now on.
190    pub fn set_occluded(&self, occluded: bool) {
191        self.occluded.set(occluded);
192    }
193
194    /// The smallest size the window was last given for its view; none until one is set.
195    pub fn min_size(&self) -> Option<Size> {
196        self.min_size.get()
197    }
198
199    /// The largest size the window was last given for its view; none until one is set.
200    pub fn max_size(&self) -> Option<Size> {
201        self.max_size.get()
202    }
203}
204
205impl HostWindow for TestWindow {
206    fn view(&self) -> ViewRef {
207        Rc::clone(&self.view) as ViewRef
208    }
209
210    fn frame(&self) -> Rect {
211        self.frame.get()
212    }
213
214    fn set_frame(&self, frame: Rect, _animate: Option<Duration>) {
215        self.frame.set(frame);
216    }
217
218    fn set_title(&self, title: &str) {
219        *self.title.borrow_mut() = title.to_owned();
220    }
221
222    fn set_icon(&self, icon: Option<&IconImage>) {
223        *self.icon.borrow_mut() = icon.cloned();
224    }
225
226    fn show(&self) {
227        self.shown.set(true);
228    }
229
230    fn hide(&self) {
231        self.shown.set(false);
232    }
233
234    /// Shown, restored where it is minimized, and counted.
235    fn activate(&self) {
236        self.shown.set(true);
237        self.minimized.set(false);
238        self.activations.set(self.activations.get() + 1);
239    }
240
241    fn is_minimized(&self) -> bool {
242        self.minimized.get()
243    }
244
245    fn is_occluded(&self) -> bool {
246        self.occluded.get()
247    }
248
249    fn close(&self) {
250        self.closes.set(self.closes.get() + 1);
251        self.view.set_window(None);
252    }
253
254    fn size_limits(&self) -> Option<&dyn WindowSizeLimits> {
255        Some(self)
256    }
257
258    fn drag(&self) {
259        self.drags.set(self.drags.get() + 1);
260    }
261
262    fn is_maximized(&self) -> bool {
263        self.maximized.get()
264    }
265
266    fn set_maximized(&self, maximized: bool) {
267        self.maximized.set(maximized);
268    }
269
270    fn set_minimized(&self, minimized: bool) {
271        self.minimized.set(minimized);
272    }
273
274    fn is_fullscreen(&self) -> bool {
275        self.fullscreen.get()
276    }
277
278    /// Full screen at once, on whichever screen: a test window is on none.
279    fn set_fullscreen(&self, fullscreen: bool, _screen: Option<ScreenId>) {
280        self.fullscreen.set(fullscreen);
281    }
282
283    fn native_handle(&self) -> Option<RawWindowHandle> {
284        None
285    }
286}
287
288impl WindowSizeLimits for TestWindow {
289    fn set_min_size(&self, size: Option<Size>) {
290        self.min_size.set(size);
291    }
292
293    fn set_max_size(&self, size: Option<Size>) {
294        self.max_size.set(size);
295    }
296}