kas_core/runner/
common.rs1use super::HasDisplayAndWindowHandle;
9use crate::draw::color::Rgba;
10use crate::draw::{DrawIface, DrawSharedImpl, WindowCommon};
11use crate::geom::Size;
12use std::time::Instant;
13use thiserror::Error;
14
15#[non_exhaustive]
17#[derive(Error, Debug)]
18pub enum Error {
19 #[error("config load/save error")]
21 Config(#[from] kas::config::Error),
22
23 #[error("event loop")]
25 EventLoop(#[from] winit::error::EventLoopError),
26}
27
28pub type Result<T> = std::result::Result<T, Error>;
30
31#[non_exhaustive]
33#[derive(Error, Debug)]
34pub enum RunError {
35 #[error("error from graphics sub-system")]
37 Graphics(Box<dyn std::error::Error + 'static>),
38
39 #[error("operation failed")]
41 RequestError(#[from] winit::error::RequestError),
42}
43
44#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
49#[non_exhaustive]
50pub enum Platform {
51 #[cfg(target_os = "android")]
52 Android,
53 #[cfg(target_os = "ios")]
54 IOS,
55 #[cfg(target_os = "macos")]
56 MacOS,
57 #[cfg(any(
58 target_os = "linux",
59 target_os = "dragonfly",
60 target_os = "freebsd",
61 target_os = "netbsd",
62 target_os = "openbsd"
63 ))]
64 Wayland,
65 #[cfg(target_arch = "wasm32")]
66 Web,
67 #[cfg(target_os = "windows")]
68 Windows,
69 #[cfg(any(
70 target_os = "linux",
71 target_os = "dragonfly",
72 target_os = "freebsd",
73 target_os = "netbsd",
74 target_os = "openbsd"
75 ))]
76 X11,
77}
78
79impl Platform {
80 pub fn is_android(&self) -> bool {
82 cfg_if::cfg_if! {
83 if #[cfg(target_os = "android")] {
84 true
85 } else {
86 false
87 }
88 }
89 }
90
91 pub fn is_ios(&self) -> bool {
93 cfg_if::cfg_if! {
94 if #[cfg(target_os = "ios")] {
95 true
96 } else {
97 false
98 }
99 }
100 }
101
102 pub fn is_macos(&self) -> bool {
104 cfg_if::cfg_if! {
105 if #[cfg(target_os = "macos")] {
106 true
107 } else {
108 false
109 }
110 }
111 }
112
113 pub fn is_wayland(&self) -> bool {
115 cfg_if::cfg_if! {
116 if #[cfg(any(
117 target_os = "linux",
118 target_os = "dragonfly",
119 target_os = "freebsd",
120 target_os = "netbsd",
121 target_os = "openbsd"
122 ))] {
123 matches!(self, Platform::Wayland)
124 } else {
125 false
126 }
127 }
128 }
129
130 pub fn is_web(&self) -> bool {
132 cfg_if::cfg_if! {
133 if #[cfg(target_arch = "wasm32")] {
134 true
135 } else {
136 false
137 }
138 }
139 }
140
141 pub fn is_windows(&self) -> bool {
143 cfg_if::cfg_if! {
144 if #[cfg(target_os = "windows")] {
145 true
146 } else {
147 false
148 }
149 }
150 }
151
152 pub fn is_x11(&self) -> bool {
154 cfg_if::cfg_if! {
155 if #[cfg(any(
156 target_os = "linux",
157 target_os = "dragonfly",
158 target_os = "freebsd",
159 target_os = "netbsd",
160 target_os = "openbsd"
161 ))] {
162 matches!(self, Platform::X11)
163 } else {
164 false
165 }
166 }
167 }
168}
169
170pub struct GraphicsFeatures {
172 pub subpixel_rendering: bool,
174}
175
176pub trait GraphicsInstance {
178 type Shared: DrawSharedImpl;
180
181 type Surface: WindowSurface<Shared = Self::Shared>;
183
184 fn new_shared(
191 &mut self,
192 surface: Option<&Self::Surface>,
193 features: GraphicsFeatures,
194 ) -> std::result::Result<Self::Shared, RunError>;
195
196 fn new_surface(
200 &mut self,
201 window: std::sync::Arc<dyn HasDisplayAndWindowHandle + Send + Sync>,
202 transparent: bool,
203 ) -> std::result::Result<Self::Surface, RunError>
204 where
205 Self: Sized;
206}
207
208pub trait WindowSurface {
210 type Shared: kas::draw::DrawSharedImpl;
212
213 fn size(&self) -> Size;
215
216 fn configure(&mut self, shared: &mut Self::Shared, size: Size) -> bool;
220
221 fn draw_iface<'iface>(
223 &'iface mut self,
224 shared: &'iface mut kas::draw::SharedState<Self::Shared>,
225 ) -> DrawIface<'iface, Self::Shared>;
226
227 fn common_mut(&mut self) -> &mut WindowCommon;
229
230 fn present(&mut self, shared: &mut Self::Shared, clear_color: Rgba) -> Instant;
234}