1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Public items common to all backends
use crate::draw::DrawSharedImpl;
use crate::draw::{color::Rgba, DrawIface, WindowCommon};
use crate::geom::Size;
use crate::theme::Theme;
use raw_window_handle as raw;
use std::time::Instant;
use thiserror::Error;
/// Possible failures from constructing an [`Application`](super::Application)
///
/// Some variants are undocumented. Users should not match these variants since
/// they are not considered part of the public API.
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum Error {
/// Failure from the graphics sub-system
#[error("error from graphics sub-system")]
Graphics(Box<dyn std::error::Error + 'static>),
/// Config load/save error
#[error("config load/save error")]
Config(#[from] kas::config::Error),
/// Event loop error
#[error("event loop")]
EventLoop(#[from] winit::error::EventLoopError),
}
impl From<winit::error::OsError> for Error {
fn from(error: winit::error::OsError) -> Self {
Error::EventLoop(winit::error::EventLoopError::Os(error))
}
}
/// A `Result` type representing `T` or [`enum@Error`]
pub type Result<T> = std::result::Result<T, Error>;
/// Enumeration of platforms
///
/// Each option is compile-time enabled only if that platform is possible.
/// Methods like [`Self::is_wayland`] are available on all platforms.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Platform {
#[cfg(target_os = "android")]
Android,
#[cfg(target_os = "ios")]
IOS,
#[cfg(target_os = "macos")]
MacOS,
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
Wayland,
#[cfg(target_arch = "wasm32")]
Web,
#[cfg(target_os = "windows")]
Windows,
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
X11,
}
impl Platform {
/// True if the platform is Android
pub fn is_android(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(target_os = "android")] {
true
} else {
false
}
}
}
/// True if the platform is IOS
pub fn is_ios(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(target_os = "ios")] {
true
} else {
false
}
}
}
/// True if the platform is MacOS
pub fn is_macos(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
true
} else {
false
}
}
}
/// True if the platform is Wayland
pub fn is_wayland(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))] {
matches!(self, Platform::Wayland)
} else {
false
}
}
}
/// True if the platform is Web
pub fn is_web(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
true
} else {
false
}
}
}
/// True if the platform is Windows
pub fn is_windows(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] {
true
} else {
false
}
}
}
/// True if the platform is X11
pub fn is_x11(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))] {
matches!(self, Platform::X11)
} else {
false
}
}
}
}
/// Builder for a graphics backend
///
/// See also [`Application`](super::Application).
#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
pub trait AppGraphicsBuilder {
/// The default theme
type DefaultTheme: Default + Theme<Self::Shared>;
/// Shared draw state
type Shared: DrawSharedImpl;
/// Window surface
type Surface: WindowSurface<Shared = Self::Shared> + 'static;
/// Construct shared state
fn build(self) -> Result<Self::Shared>;
}
/// Window graphical surface requirements
#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
pub trait WindowSurface {
/// Shared draw state
type Shared: kas::draw::DrawSharedImpl;
/// Construct an instance from a window handle
///
/// It is required to call [`WindowSurface::do_resize`] after this.
fn new<W>(shared: &mut Self::Shared, window: W) -> Result<Self>
where
W: raw::HasRawWindowHandle + raw::HasRawDisplayHandle,
Self: Sized;
/// Get current surface size
fn size(&self) -> Size;
/// Resize surface
///
/// Returns `true` when the new `size` did not match the old surface size.
fn do_resize(&mut self, shared: &mut Self::Shared, size: Size) -> bool;
/// Construct a DrawIface object
fn draw_iface<'iface>(
&'iface mut self,
shared: &'iface mut kas::draw::SharedState<Self::Shared>,
) -> DrawIface<'iface, Self::Shared>;
/// Access common data
fn common_mut(&mut self) -> &mut WindowCommon;
/// Present frame
///
/// Return time at which render finishes
fn present(&mut self, shared: &mut Self::Shared, clear_color: Rgba) -> Instant;
}