From 5a09e8ba213f3153d8b1072033e338b64e91b9c3 Mon Sep 17 00:00:00 2001 From: k-brac Date: Fri, 25 Nov 2016 17:05:39 +0100 Subject: [PATCH] first try to allow child window on windows --- examples/child_window.rs | 44 ++++++++++++++++++++++++++++++++++++ src/os/windows.rs | 9 +++++++- src/platform/windows/init.rs | 30 +++++++++++++++++------- src/platform/windows/mod.rs | 12 ++++++---- src/window.rs | 2 +- 5 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 examples/child_window.rs diff --git a/examples/child_window.rs b/examples/child_window.rs new file mode 100644 index 00000000..05469f4c --- /dev/null +++ b/examples/child_window.rs @@ -0,0 +1,44 @@ +extern crate winit; +use std::thread; +use winit::os::windows::WindowBuilderExt; + +fn resize_callback(width: u32, height: u32) { + println!("Window resized to {}x{}", width, height); +} + +fn main() { + let window = winit::WindowBuilder::new() + .with_title("A fantastic window!") + .with_window_resize_callback(resize_callback) + .build() + .unwrap(); + + let proxy = window.create_window_proxy(); + thread::spawn(move || { + let child = winit::WindowBuilder::new() + .with_title("child window!") + .with_window_resize_callback(resize_callback) + .with_decorations(false) + .with_parent_window(proxy) + .build() + .unwrap(); + + for event in child.wait_events() { + println!("child {:?}", event); + + match event { + winit::Event::Closed => break, + _ => (), + } + } + }); + + for event in window.wait_events() { + println!("parent {:?}", event); + + match event { + winit::Event::Closed => break, + _ => (), + } + } +} diff --git a/src/os/windows.rs b/src/os/windows.rs index e37b4185..dd4e595e 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -3,6 +3,7 @@ use libc; use Window; use WindowBuilder; +use window; /// Additional methods on `Window` that are specific to Windows. pub trait WindowExt { @@ -23,8 +24,14 @@ impl WindowExt for Window { /// Additional methods on `WindowBuilder` that are specific to Windows. pub trait WindowBuilderExt { - + fn with_parent_window(self, parent: window::WindowProxy) -> WindowBuilder; } impl WindowBuilderExt for WindowBuilder { + /// Sets a parent to the window to be created + #[inline] + fn with_parent_window(mut self, parent: window::WindowProxy) -> WindowBuilder { + self.platform_specific.parent = Some(parent); + self + } } diff --git a/src/platform/windows/init.rs b/src/platform/windows/init.rs index bc563847..fcfce2f4 100644 --- a/src/platform/windows/init.rs +++ b/src/platform/windows/init.rs @@ -9,6 +9,7 @@ use super::WindowState; use super::Window; use super::MonitorId; use super::WindowWrapper; +use super::PlatformSpecificWindowBuilderAttributes; use CreationError; use CreationError::OsError; @@ -24,9 +25,9 @@ use kernel32; use dwmapi; use user32; -pub fn new_window(window: &WindowAttributes) -> Result { +pub fn new_window(window: &WindowAttributes, pl_attribs: &PlatformSpecificWindowBuilderAttributes) -> Result { let window = window.clone(); - + let attribs = pl_attribs.clone(); // initializing variables to be sent to the task let title = OsStr::new(&window.title).encode_wide().chain(Some(0).into_iter()) @@ -39,7 +40,7 @@ pub fn new_window(window: &WindowAttributes) -> Result { thread::spawn(move || { unsafe { // creating and sending the `Window` - match init(title, &window) { + match init(title, &window, attribs) { Ok(w) => tx.send(Ok(w)).ok(), Err(e) => { tx.send(Err(e)).ok(); @@ -65,7 +66,7 @@ pub fn new_window(window: &WindowAttributes) -> Result { rx.recv().unwrap() } -unsafe fn init(title: Vec, window: &WindowAttributes) -> Result { +unsafe fn init(title: Vec, window: &WindowAttributes, pl_attribs: PlatformSpecificWindowBuilderAttributes) -> Result { // registering the window class let class_name = register_window_class(); @@ -84,8 +85,16 @@ unsafe fn init(title: Vec, window: &WindowAttributes) -> Result, window: &WindowAttributes) -> Result, +} #[derive(Clone, Default)] pub struct PlatformSpecificHeadlessBuilderAttributes; @@ -76,7 +80,7 @@ impl Drop for WindowWrapper { #[derive(Clone)] pub struct WindowProxy { - hwnd: winapi::HWND, + pub hwnd: winapi::HWND, } unsafe impl Send for WindowProxy {} @@ -93,10 +97,10 @@ impl WindowProxy { impl Window { /// See the docs in the crate root file. - pub fn new(window: &WindowAttributes, _: &PlatformSpecificWindowBuilderAttributes) + pub fn new(window: &WindowAttributes, pl_attribs: &PlatformSpecificWindowBuilderAttributes) -> Result { - init::new_window(window) + init::new_window(window, pl_attribs) } /// See the docs in the crate root file. diff --git a/src/window.rs b/src/window.rs index cbc9731a..75ef53cc 100644 --- a/src/window.rs +++ b/src/window.rs @@ -361,7 +361,7 @@ impl Window { /// threads. #[derive(Clone)] pub struct WindowProxy { - proxy: platform::WindowProxy, + pub proxy: platform::WindowProxy, } impl WindowProxy {