window/
no_window.rs

1//! An implementation of Window that runs without a window at all.
2//!
3//! It saves just enough information to implement the window interface,
4//! but otherwise does nothing.
5//!
6//! Often used in servers as an event loop.
7
8use std::{error::Error, time::Duration};
9
10use input::Event;
11
12use crate::{AdvancedWindow, BuildFromWindowSettings, Position, Size, Window, WindowSettings};
13
14/// A window without user interface, often used in server event loops.
15///
16/// This structure holds just enough state to return values that were set.
17/// The size can be changed because the event loop does not emit
18/// [`Render`](../event_loop/trait.RenderEvent.html)
19/// events when the width or height is zero.
20#[derive(Debug)]
21pub struct NoWindow {
22    should_close: bool,
23    automatic_close: bool,
24    title: String,
25    size: Size,
26    pos: Position,
27}
28
29impl NoWindow {
30    /// Creates a new `NoWindow`.
31    pub fn new(settings: &WindowSettings) -> NoWindow {
32        NoWindow {
33            should_close: false,
34            automatic_close: settings.automatic_close,
35            title: settings.get_title(),
36            size: settings.get_size(),
37            pos: Position { x: 0, y: 0 },
38        }
39    }
40}
41
42impl Window for NoWindow {
43    fn set_should_close(&mut self, value: bool) {
44        self.should_close = value;
45    }
46
47    fn should_close(&self) -> bool {
48        self.should_close
49    }
50
51    fn size(&self) -> Size {
52        self.size
53    }
54
55    fn swap_buffers(&mut self) {}
56
57    fn wait_event(&mut self) -> Event {
58        panic!("NoWindow will never return an input event");
59    }
60
61    fn wait_event_timeout(&mut self, _timeout: Duration) -> Option<Event> {
62        None
63    }
64
65    fn poll_event(&mut self) -> Option<Event> {
66        None
67    }
68
69    fn draw_size(&self) -> Size {
70        self.size()
71    }
72}
73
74impl BuildFromWindowSettings for NoWindow {
75    /// # Errors
76    ///
77    /// This function will always return without error.
78    fn build_from_window_settings(settings: &WindowSettings) -> Result<Self, Box<dyn Error>> {
79        Ok(NoWindow::new(settings))
80    }
81}
82
83impl AdvancedWindow for NoWindow {
84    fn get_title(&self) -> String {
85        self.title.clone()
86    }
87
88    fn set_title(&mut self, value: String) {
89        self.title = value;
90    }
91
92    fn get_exit_on_esc(&self) -> bool {
93        false
94    }
95
96    fn set_exit_on_esc(&mut self, _value: bool) {}
97
98    fn get_automatic_close(&self) -> bool {
99        self.automatic_close
100    }
101
102    fn set_automatic_close(&mut self, value: bool) {
103        self.automatic_close = value;
104    }
105
106    fn set_capture_cursor(&mut self, _value: bool) {}
107
108    fn show(&mut self) {}
109
110    fn hide(&mut self) {}
111
112    fn get_position(&self) -> Option<Position> {
113        Some(self.pos)
114    }
115
116    fn set_position<P: Into<Position>>(&mut self, val: P) {
117        self.pos = val.into();
118    }
119
120    fn set_size<S: Into<Size>>(&mut self, val: S) {
121        self.size = val.into();
122    }
123}