1 // Copyright 2023 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 cfg_if::cfg_if! { 6 if #[cfg(unix)] { 7 pub mod unix; 8 pub use unix as platform; 9 pub(crate) use self::unix::StubWindowEventLoop as PlatformWindowEventLoop; 10 } else if #[cfg(windows)] { 11 pub mod windows; 12 pub use windows as platform; 13 pub(crate) use self::windows::WindowsWindowEventLoop as PlatformWindowEventLoop; 14 } 15 } 16 17 use std::any::Any; 18 use std::sync::Arc; 19 20 use anyhow::Result; 21 use euclid::Box2D; 22 use euclid::Size2D; 23 use euclid::UnknownUnit; 24 use vulkano::instance::Instance; 25 use vulkano::swapchain; 26 27 type Surface = swapchain::Surface<Arc<dyn Any + Send + Sync>>; 28 29 pub trait Window: Any + Send + Sync { get_inner_size(&self) -> Result<Size2D<u32, UnknownUnit>>30 fn get_inner_size(&self) -> Result<Size2D<u32, UnknownUnit>>; create_vulkan_surface(self: Arc<Self>, instance: Arc<Instance>) -> Result<Arc<Surface>>31 fn create_vulkan_surface(self: Arc<Self>, instance: Arc<Instance>) -> Result<Arc<Surface>>; 32 } 33 34 pub trait ApplicationState { 35 type UserEvent: Send + 'static; 36 process_event(&self, event: WindowEvent<Self::UserEvent>)37 fn process_event(&self, event: WindowEvent<Self::UserEvent>); 38 } 39 40 pub trait ApplicationStateBuilder: Send + 'static { 41 type Target: ApplicationState; 42 build<T: Window>(self, window: Arc<T>) -> Result<Self::Target>43 fn build<T: Window>(self, window: Arc<T>) -> Result<Self::Target>; 44 } 45 46 // Some platform may not support all the events. 47 #[allow(dead_code)] 48 pub enum WindowEvent<T: Send> { 49 Resized, 50 User(T), 51 } 52 53 pub trait WindowEventLoop<State: ApplicationState>: Sized + Send { 54 type WindowType: Window; 55 56 /// # Safety 57 /// The parent window must outlive the lifetime of this object. create<Builder>( parent: platform::NativeWindowType, initial_window_size: &Size2D<i32, UnknownUnit>, application_state_builder: Builder, ) -> Result<Self> where Builder: ApplicationStateBuilder<Target = State>58 unsafe fn create<Builder>( 59 parent: platform::NativeWindowType, 60 initial_window_size: &Size2D<i32, UnknownUnit>, 61 application_state_builder: Builder, 62 ) -> Result<Self> 63 where 64 Builder: ApplicationStateBuilder<Target = State>; 65 send_event(&self, event: State::UserEvent) -> Result<()>66 fn send_event(&self, event: State::UserEvent) -> Result<()>; 67 move_window(&self, pos: &Box2D<i32, UnknownUnit>) -> Result<()>68 fn move_window(&self, pos: &Box2D<i32, UnknownUnit>) -> Result<()>; 69 } 70