Now passing WindowBuilder to implwin::Window::new

This commit is contained in:
Tomaka17 2014-08-02 10:42:17 +02:00
parent 5dda167021
commit 49b0a20170
4 changed files with 23 additions and 31 deletions

View file

@ -62,12 +62,7 @@ impl WindowBuilder {
/// Error should be very rare and only occur in case of permission denied, incompatible system, /// Error should be very rare and only occur in case of permission denied, incompatible system,
/// out of memory, etc. /// out of memory, etc.
pub fn build(self) -> Result<Window, String> { pub fn build(self) -> Result<Window, String> {
let win = try!(winimpl::Window::new(Some(self.dimensions), winimpl::Window::new(self).map(|w| Window { window: w })
self.title.as_slice(), self.monitor));
Ok(Window{
window: win,
})
} }
} }

View file

@ -5,8 +5,8 @@ use std::task::TaskBuilder;
use std::sync::atomics::AtomicBool; use std::sync::atomics::AtomicBool;
use std::ptr; use std::ptr;
use super::{event, ffi}; use super::{event, ffi};
use super::{MonitorID, Window}; use super::Window;
use Event; use {Event, WindowBuilder};
/// Stores the current window and its events dispatcher. /// Stores the current window and its events dispatcher.
/// ///
@ -14,15 +14,13 @@ use Event;
/// receive an event for another window. /// receive an event for another window.
local_data_key!(WINDOW: (ffi::HWND, Sender<Event>)) local_data_key!(WINDOW: (ffi::HWND, Sender<Event>))
pub fn new_window(dimensions: Option<(uint, uint)>, title: &str, pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
monitor: Option<MonitorID>)
-> Result<Window, String>
{
use std::mem; use std::mem;
use std::os; use std::os;
// initializing variables to be sent to the task // initializing variables to be sent to the task
let title = title.utf16_units().collect::<Vec<u16>>().append_one(0); // title to utf16 let title = builder.title.as_slice().utf16_units()
.collect::<Vec<u16>>().append_one(0); // title to utf16
//let hints = hints.clone(); //let hints = hints.clone();
let (tx, rx) = channel(); let (tx, rx) = channel();
@ -61,15 +59,15 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
// building a RECT object with coordinates // building a RECT object with coordinates
let mut rect = ffi::RECT { let mut rect = ffi::RECT {
left: 0, right: dimensions.map(|(w, _)| w as ffi::LONG).unwrap_or(1024), left: 0, right: builder.dimensions.val0() as ffi::LONG,
top: 0, bottom: dimensions.map(|(_, h)| h as ffi::LONG).unwrap_or(768), top: 0, bottom: builder.dimensions.val1() as ffi::LONG,
}; };
// switching to fullscreen if necessary // switching to fullscreen if necessary
// this means adjusting the window's position so that it overlaps the right monitor, // this means adjusting the window's position so that it overlaps the right monitor,
// and change the monitor's resolution if necessary // and change the monitor's resolution if necessary
if monitor.is_some() { if builder.monitor.is_some() {
let monitor = monitor.as_ref().unwrap(); let monitor = builder.monitor.as_ref().unwrap();
// adjusting the rect // adjusting the rect
{ {
@ -97,7 +95,7 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
} }
// computing the style and extended style of the window // computing the style and extended style of the window
let (ex_style, style) = if monitor.is_some() { let (ex_style, style) = if builder.monitor.is_some() {
(ffi::WS_EX_APPWINDOW, ffi::WS_POPUP | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN) (ffi::WS_EX_APPWINDOW, ffi::WS_POPUP | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN)
} else { } else {
(ffi::WS_EX_APPWINDOW | ffi::WS_EX_WINDOWEDGE, (ffi::WS_EX_APPWINDOW | ffi::WS_EX_WINDOWEDGE,
@ -212,8 +210,8 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
let handle = ffi::CreateWindowExW(ex_style, class_name.as_ptr(), let handle = ffi::CreateWindowExW(ex_style, class_name.as_ptr(),
title.as_ptr() as ffi::LPCWSTR, title.as_ptr() as ffi::LPCWSTR,
style | ffi::WS_VISIBLE | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN, style | ffi::WS_VISIBLE | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN,
if monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT}, if builder.monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT},
if monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT}, if builder.monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT},
rect.right - rect.left, rect.bottom - rect.top, rect.right - rect.left, rect.bottom - rect.top,
ptr::mut_null(), ptr::mut_null(), ffi::GetModuleHandleW(ptr::null()), ptr::mut_null(), ptr::mut_null(), ffi::GetModuleHandleW(ptr::null()),
ptr::mut_null()); ptr::mut_null());
@ -273,7 +271,7 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
}; };
// calling SetForegroundWindow if fullscreen // calling SetForegroundWindow if fullscreen
if monitor.is_some() { if builder.monitor.is_some() {
unsafe { ffi::SetForegroundWindow(real_window) }; unsafe { ffi::SetForegroundWindow(real_window) };
} }

View file

@ -1,6 +1,6 @@
use std::sync::atomics::AtomicBool; use std::sync::atomics::AtomicBool;
use std::ptr; use std::ptr;
use Event; use {Event, WindowBuilder};
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
@ -35,11 +35,8 @@ pub struct Window {
impl Window { impl Window {
/// See the docs if the crate root file. /// See the docs if the crate root file.
pub fn new(dimensions: Option<(uint, uint)>, title: &str, pub fn new(builder: WindowBuilder) -> Result<Window, String> {
monitor: Option<MonitorID>) init::new_window(builder)
-> Result<Window, String>
{
init::new_window(dimensions, title, monitor)
} }
/// See the docs if the crate root file. /// See the docs if the crate root file.

View file

@ -1,4 +1,4 @@
use Event; use {Event, WindowBuilder};
use libc; use libc;
use std::{mem, ptr}; use std::{mem, ptr};
use std::sync::atomics::AtomicBool; use std::sync::atomics::AtomicBool;
@ -33,9 +33,11 @@ impl MonitorID {
} }
impl Window { impl Window {
pub fn new(dimensions: Option<(uint, uint)>, title: &str, _: Option<MonitorID>) pub fn new(builder: WindowBuilder) -> Result<Window, String> {
-> Result<Window, String> // TODO: temporary
{ let dimensions = Some(builder.dimensions);
let title = builder.title.as_slice();
// calling XOpenDisplay // calling XOpenDisplay
let display = unsafe { let display = unsafe {
let display = ffi::XOpenDisplay(ptr::null()); let display = ffi::XOpenDisplay(ptr::null());