// Typed window API over the virtual backend.
//
// Events and the remaining window operations (cursor, clipboard, IME) still live
// in `tycho_window`; they move here incrementally so that every target — not just
// the three desktop ones — shares one implementation.
pub suberror WindowError {
InitFailed
CreateFailed
}
///|
/// Callers report a failed window creation by printing the error, so it has to
/// be formattable -- the same reason `RhiError` carries a `Show` impl.
pub impl Show for WindowError with output(self, logger) {
match self {
InitFailed => logger.write_string("WindowError: backend init failed")
CreateFailed => logger.write_string("WindowError: window creation failed")
}
}
///|
/// What a backend needs to hand to a graphics API in order to create a surface.
/// `a` / `b` are the native display and window handles (or the `CAMetalLayer`
/// pointer, or a canvas id on the web); their meaning is fixed per `kind`.
#derive(Eq, Debug)
pub(all) struct Surface {
kind : SurfaceKind
a : UInt64
b : UInt64
}
pub struct Window {
priv id : UInt64
}
///|
pub fn Window::new(w : Int, h : Int, title : String) -> Window raise WindowError {
if @be.backend_init() == (0 : Int32) {
raise WindowError::InitFailed
}
let id = @be.backend_win_create(w, h, title)
if id == (0 : UInt64) {
raise WindowError::CreateFailed
}
{ id, }
}
///|
pub fn Window::handle(self : Window) -> UInt64 {
self.id
}
///|
pub fn Window::width(self : Window) -> Int {
@be.backend_win_width(self.id)
}
///|
pub fn Window::height(self : Window) -> Int {
@be.backend_win_height(self.id)
}
///|
pub fn Window::framebuffer_size(self : Window) -> (Int, Int) {
(self.width(), self.height())
}
///|
pub fn Window::content_scale(self : Window) -> Double {
@be.backend_win_dpi_scale(self.id)
}
///|
pub fn Window::should_close(self : Window) -> Bool {
@be.backend_win_should_close(self.id)
}
///|
pub fn Window::set_should_close(self : Window, close : Bool) -> Unit {
@be.backend_win_set_should_close(self.id, close)
}
///|
pub fn Window::set_title(self : Window, title : String) -> Unit {
@be.backend_win_set_title(self.id, title)
}
///|
pub fn Window::set_fullscreen(self : Window, on : Bool) -> Unit {
@be.backend_win_set_fullscreen(self.id, on)
}
///|
pub fn Window::surface(self : Window) -> Surface {
{
kind: surface_kind_of(@be.backend_win_surface_kind(self.id)),
a: @be.backend_win_surface_a(self.id),
b: @be.backend_win_surface_b(self.id),
}
}
///|
pub fn Window::destroy(self : Window) -> Unit {
@be.backend_win_destroy(self.id)
}
///|
pub fn SurfaceKind::name(self : SurfaceKind) -> String {
match self {
Xlib => "xlib"
Xcb => "xcb"
Wayland => "wayland"
Win32 => "win32"
Metal => "metal"
Android => "android"
CanvasGpu => "canvas-gpu"
Headless => "headless"
}
}
fn surface_kind_of(code : Int) -> SurfaceKind {
match code {
1 => Wayland
2 => Win32
3 => Metal
4 => Android
5 => CanvasGpu
6 => Headless
_ => Xlib
}
}