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::sync::Arc;
19
20/// A marker trait that combines windowing handle requirements for use in trait objects.
21///
22/// Rust's trait object safety rules require that a trait's supertraits must also be
23/// object-safe. `HasWindowHandle` and `HasDisplayHandle` are, but creating a direct
24/// `dyn HasWindowHandle + HasDisplayHandle` is complex. This trait serves as a simple,
25/// unified supertrait to make creating the `KhoraWindowHandle` type alias possible.
26pub trait WindowHandle: HasWindowHandle + HasDisplayHandle {}
27
28// A blanket implementation automatically implements `WindowHandle` for any type
29// that already satisfies its requirements.
30impl<T: HasWindowHandle + HasDisplayHandle> WindowHandle for T {}
31
32/// A thread-safe, reference-counted trait object representing a window.
33///
34/// This type alias is used to pass a handle to a window across thread boundaries,
35/// for example, from the main application thread to a rendering thread.
36pub type KhoraWindowHandle = Arc<dyn WindowHandle + Send + Sync>;
37
38/// A trait that abstracts the behavior of an application window.
39///
40/// This is the primary contract for windowing integration in Khora. Any windowing
41/// backend (like the Winit implementation in `khora-infra`) must implement this trait
42/// to be usable by the engine's rendering and input systems.
43pub trait KhoraWindow: HasWindowHandle + HasDisplayHandle + Send + Sync {
44    /// Returns the physical dimensions (width, height) in pixels of the window's inner client area.
45    fn inner_size(&self) -> (u32, u32);
46
47    /// Returns the display's scale factor, used for HiDPI rendering.
48    fn scale_factor(&self) -> f64;
49
50    /// Requests that the operating system schedule a redraw for the window.
51    fn request_redraw(&self);
52
53    /// Clones a thread-safe, reference-counted handle to the window.
54    ///
55    /// This is the primary mechanism for the renderer to obtain a handle it can use
56    /// to create a render surface, without needing to know the concrete window type.
57    fn clone_handle_arc(&self) -> KhoraWindowHandle;
58
59    /// Returns a unique identifier for the window.
60    fn id(&self) -> u64;
61}