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

//! An implementation of Window that runs without a window at all.
//!
//! It saves just enough information to implement the window interface,
//! but otherwise does nothing.
//!
//! Often used in servers as an event loop.

use {Window, WindowSettings, BuildFromWindowSettings, AdvancedWindow, Position, Size};

use input::Event;
use std::time::Duration;
use std::error::Error;

/// A window without user interface, often used in server event loops.
///
/// This structure holds just enough state to return values that were set.
/// The size can be changed because the event loop does not emit
/// [`Render`](../event_loop/trait.RenderEvent.html)
/// events when the width or height is zero.
pub struct NoWindow {
    should_close: bool,
    automatic_close: bool,
    title: String,
    size: Size,
    pos: Position,
}

impl NoWindow {
    /// Creates a new `NoWindow`.
    pub fn new(settings: &WindowSettings) -> NoWindow {
        NoWindow {
            should_close: false,
            automatic_close: settings.automatic_close,
            title: settings.get_title(),
            size: settings.get_size(),
            pos: Position { x: 0, y: 0 },
        }
    }
}

impl Window for NoWindow {
    fn should_close(&self) -> bool {
        self.should_close
    }

    fn set_should_close(&mut self, value: bool) {
        self.should_close = value;
    }

    fn size(&self) -> Size {
        self.size
    }

    fn swap_buffers(&mut self) {}

    fn wait_event(&mut self) -> Event {
        panic!("NoWindow will never return an input event");
    }

    fn wait_event_timeout(&mut self, _timeout: Duration) -> Option<Event> {
        None
    }

    fn poll_event(&mut self) -> Option<Event> {
        None
    }

    fn draw_size(&self) -> Size {
        self.size()
    }
}

impl BuildFromWindowSettings for NoWindow {
    /// # Errors
    ///
    /// This function will always return without error.
    fn build_from_window_settings(settings: &WindowSettings) -> Result<Self, Box<dyn Error>> {
        Ok(NoWindow::new(settings))
    }
}

impl AdvancedWindow for NoWindow {
    fn get_title(&self) -> String {
        self.title.clone()
    }

    fn set_title(&mut self, value: String) {
        self.title = value;
    }

    fn get_exit_on_esc(&self) -> bool {
        false
    }

    fn set_exit_on_esc(&mut self, _value: bool) {}

    fn get_automatic_close(&self) -> bool {
        self.automatic_close
    }

    fn set_automatic_close(&mut self, value: bool) {
        self.automatic_close = value;
    }

    fn set_capture_cursor(&mut self, _value: bool) {}

    fn show(&mut self) {}

    fn hide(&mut self) {}

    fn get_position(&self) -> Option<Position> {
        Some(self.pos)
    }

    fn set_position<P: Into<Position>>(&mut self, val: P) {
        self.pos = val.into();
    }

    fn set_size<S: Into<Size>>(&mut self, val: S) {
        self.size = val.into();
    }
}