Skip to main content

khora_core/platform/
window.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines the `KhoraWindow` trait and related types for windowing abstraction.
16
17use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
18use std::fmt::Debug;
19use std::sync::Arc;
20
21/// A marker trait that combines windowing handle requirements for use in trait objects.
22///
23/// Rust's trait object safety rules require that a trait's supertraits must also be
24/// object-safe. `HasWindowHandle` and `HasDisplayHandle` are, but creating a direct
25/// `dyn HasWindowHandle + HasDisplayHandle` is complex. This trait serves as a simple,
26/// unified supertrait to make creating the `KhoraWindowHandle` type alias possible.
27///
28/// `Debug` is required so the handle can be stored inside `wgpu::InstanceDescriptor`
29/// (its `display` field requires `WgpuHasDisplayHandle: Debug`).
30pub trait WindowHandle: HasWindowHandle + HasDisplayHandle + Debug {}
31
32// A blanket implementation automatically implements `WindowHandle` for any type
33// that already satisfies its requirements.
34impl<T: HasWindowHandle + HasDisplayHandle + Debug> WindowHandle for T {}
35
36/// A thread-safe, reference-counted trait object representing a window.
37///
38/// This type alias is used to pass a handle to a window across thread boundaries,
39/// for example, from the main application thread to a rendering thread.
40pub type KhoraWindowHandle = Arc<dyn WindowHandle + Send + Sync>;
41
42/// A trait that abstracts the behavior of an application window.
43///
44/// This is the primary contract for windowing integration in Khora. Any windowing
45/// backend (like the Winit implementation in `khora-infra`) must implement this trait
46/// to be usable by the engine's rendering and input systems.
47pub trait KhoraWindow: HasWindowHandle + HasDisplayHandle + Send + Sync {
48    /// Returns the physical dimensions (width, height) in pixels of the window's inner client area.
49    fn inner_size(&self) -> (u32, u32);
50
51    /// Returns the display's scale factor, used for HiDPI rendering.
52    fn scale_factor(&self) -> f64;
53
54    /// Requests that the operating system schedule a redraw for the window.
55    fn request_redraw(&self);
56
57    /// Clones a thread-safe, reference-counted handle to the window.
58    ///
59    /// This is the primary mechanism for the renderer to obtain a handle it can use
60    /// to create a render surface, without needing to know the concrete window type.
61    fn clone_handle_arc(&self) -> KhoraWindowHandle;
62
63    /// Returns a unique identifier for the window.
64    fn id(&self) -> u64;
65}