use std::collections::vec_deque::IntoIter as VecDequeIter;
use CreationError;
use CursorState;
use Event;
use MouseCursor;
use Window;
use WindowBuilder;
use native_monitor::NativeMonitorId;
use libc;
use platform;
impl WindowBuilder {
#[inline]
pub fn new() -> WindowBuilder {
WindowBuilder {
window: Default::default(),
platform_specific: Default::default(),
}
}
#[inline]
pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder {
self.window.dimensions = Some((width, height));
self
}
#[inline]
pub fn with_min_dimensions(mut self, width: u32, height: u32) -> WindowBuilder {
self.window.min_dimensions = Some((width, height));
self
}
#[inline]
pub fn with_max_dimensions(mut self, width: u32, height: u32) -> WindowBuilder {
self.window.max_dimensions = Some((width, height));
self
}
#[inline]
pub fn with_title<T: Into<String>>(mut self, title: T) -> WindowBuilder {
self.window.title = title.into();
self
}
#[inline]
pub fn with_fullscreen(mut self, monitor: MonitorId) -> WindowBuilder {
let MonitorId(monitor) = monitor;
self.window.monitor = Some(monitor);
self
}
#[inline]
pub fn with_visibility(mut self, visible: bool) -> WindowBuilder {
self.window.visible = visible;
self
}
#[inline]
pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder {
self.window.transparent = transparent;
self
}
#[inline]
pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder {
self.window.decorations = decorations;
self
}
#[inline]
pub fn with_multitouch(mut self) -> WindowBuilder {
self.window.multitouch = true;
self
}
pub fn build(mut self) -> Result<Window, CreationError> {
if self.window.dimensions.is_none() && self.window.monitor.is_some() {
self.window.dimensions = Some(self.window.monitor.as_ref().unwrap().get_dimensions())
}
if self.window.dimensions.is_none() {
self.window.dimensions = Some((1024, 768));
}
platform::Window::new(&self.window, &self.platform_specific).map(|w| Window { window: w })
}
#[inline]
pub fn build_strict(self) -> Result<Window, CreationError> {
self.build()
}
}
impl Default for Window {
#[inline]
fn default() -> Window {
Window::new().unwrap()
}
}
impl Window {
#[inline]
pub fn new() -> Result<Window, CreationError> {
let builder = WindowBuilder::new();
builder.build()
}
#[inline]
pub fn set_title(&self, title: &str) {
self.window.set_title(title)
}
#[inline]
pub fn show(&self) {
self.window.show()
}
#[inline]
pub fn hide(&self) {
self.window.hide()
}
#[inline]
pub fn get_position(&self) -> Option<(i32, i32)> {
self.window.get_position()
}
#[inline]
pub fn set_position(&self, x: i32, y: i32) {
self.window.set_position(x, y)
}
#[inline]
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
self.window.get_inner_size()
}
#[inline]
pub fn get_inner_size_points(&self) -> Option<(u32, u32)> {
self.window.get_inner_size()
}
#[inline]
pub fn get_inner_size_pixels(&self) -> Option<(u32, u32)> {
self.window.get_inner_size().map(|(x, y)| {
let hidpi = self.hidpi_factor();
((x as f32 * hidpi) as u32, (y as f32 * hidpi) as u32)
})
}
#[inline]
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
self.window.get_outer_size()
}
#[inline]
pub fn set_inner_size(&self, x: u32, y: u32) {
self.window.set_inner_size(x, y)
}
#[inline]
pub fn poll_events(&self) -> PollEventsIterator {
PollEventsIterator(self.window.poll_events())
}
#[inline]
pub fn wait_events(&self) -> WaitEventsIterator {
WaitEventsIterator(self.window.wait_events())
}
#[inline]
pub unsafe fn platform_display(&self) -> *mut libc::c_void {
self.window.platform_display()
}
#[inline]
pub unsafe fn platform_window(&self) -> *mut libc::c_void {
self.window.platform_window()
}
#[inline]
pub fn create_window_proxy(&self) -> WindowProxy {
WindowProxy {
proxy: self.window.create_window_proxy()
}
}
#[inline]
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
self.window.set_window_resize_callback(callback);
}
pub fn set_cursor(&self, cursor: MouseCursor) {
self.window.set_cursor(cursor);
}
#[inline]
pub fn hidpi_factor(&self) -> f32 {
self.window.hidpi_factor()
}
#[inline]
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
self.window.set_cursor_position(x, y)
}
#[inline]
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
self.window.set_cursor_state(state)
}
}
#[derive(Clone)]
pub struct WindowProxy {
proxy: platform::WindowProxy,
}
impl WindowProxy {
#[inline]
pub fn wakeup_event_loop(&self) {
self.proxy.wakeup_event_loop();
}
}
pub struct PollEventsIterator<'a>(platform::PollEventsIterator<'a>);
impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
#[inline]
fn next(&mut self) -> Option<Event> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct WaitEventsIterator<'a>(platform::WaitEventsIterator<'a>);
impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
#[inline]
fn next(&mut self) -> Option<Event> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct AvailableMonitorsIter {
data: VecDequeIter<platform::MonitorId>,
}
impl Iterator for AvailableMonitorsIter {
type Item = MonitorId;
#[inline]
fn next(&mut self) -> Option<MonitorId> {
self.data.next().map(|id| MonitorId(id))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.data.size_hint()
}
}
#[inline]
pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = platform::get_available_monitors();
AvailableMonitorsIter{ data: data.into_iter() }
}
#[inline]
pub fn get_primary_monitor() -> MonitorId {
MonitorId(platform::get_primary_monitor())
}
pub struct MonitorId(platform::MonitorId);
impl MonitorId {
#[inline]
pub fn get_name(&self) -> Option<String> {
let &MonitorId(ref id) = self;
id.get_name()
}
#[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId {
let &MonitorId(ref id) = self;
id.get_native_identifier()
}
#[inline]
pub fn get_dimensions(&self) -> (u32, u32) {
let &MonitorId(ref id) = self;
id.get_dimensions()
}
}