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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
#![doc = include_str!("../.crate-docs.md")]
#![warn(clippy::pedantic, missing_docs)]
#![allow(
clippy::module_name_repetitions,
clippy::missing_errors_doc,
clippy::doc_lazy_continuation
)]
// for proc-macros
extern crate self as cushy;
#[macro_use]
mod utils;
pub mod animation;
pub mod context;
mod graphics;
mod names;
#[macro_use]
pub mod styles;
mod app;
pub mod debug;
pub mod fonts;
mod tick;
mod tree;
pub mod value;
pub mod widget;
pub mod widgets;
pub mod window;
pub mod dialog;
#[doc(hidden)]
pub mod example;
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[cfg(feature = "tokio")]
pub use app::TokioRuntime;
pub use app::{
App, AppRuntime, Application, Cushy, DefaultRuntime, Open, PendingApp, Run, ShutdownGuard,
};
/// A macro to create a `main()` function with less boilerplate.
///
/// When creating applications that support multiple windows, this attribute
/// macro can be used to remove a few lines of code.
///
/// The function body is executed during application startup, and the app will
/// continue running until the last window is closed.
///
/// This attribute must be attached to a `main(&mut PendingApp)` or `main(&mut
/// App)` function. Either form supports a return type or no return type.
///
/// ## `&mut PendingApp`
///
/// When using a [`PendingApp`], the function body is invoked before the app is
/// run. While the example shown below does not require the runtime
/// initialization, some programs do and using the macro means the developer
/// will never forget to add the extra code.
///
/// These two example programs are functionally identical:
///
/// ### Without Macro
///
/// ```rust
/// # fn test() {
/// use cushy::{Open, PendingApp, Run};
///
/// fn main() -> cushy::Result {
/// let mut app = PendingApp::default();
/// let cushy = app.cushy().clone();
/// let _guard = cushy.enter_runtime();
///
/// "Hello World".open(&mut app)?;
///
/// app.run()
/// }
/// # }
/// ```
///
/// ### With Macro
///
/// ```rust
/// # fn test() {
/// use cushy::{Open, PendingApp};
///
/// #[cushy::main]
/// fn main(app: &mut PendingApp) -> cushy::Result {
/// "Hello World".open(app)?;
/// Ok(())
/// }
/// # }
/// ```
///
/// ## `&mut App`
///
/// When using an [`App`], the function body is invoked after the app's event
/// loop has begun executing. This is important if the application wants to
/// access monitor information to either position windows precisely or use a
/// full screen video mode.
///
/// These two example programs are functionally identical:
///
/// ### Without Macro
///
/// ```rust
/// # fn test() {
/// use cushy::{App, Open, PendingApp, Run};
///
/// fn main() -> cushy::Result {
/// let mut app = PendingApp::default();
/// app.on_startup(|app| -> cushy::Result {
/// "Hello World".open(app)?;
/// Ok(())
/// });
/// app.run()
/// }
/// # }
/// ```
///
/// ### With Macro
///
/// ```rust
/// # fn test() {
/// use cushy::{App, Open};
///
/// #[cushy::main]
/// fn main(app: &mut App) -> cushy::Result {
/// "Hello World".open(app)?;
/// Ok(())
/// }
/// # }
/// ```
pub use cushy_macros::main;
use figures::units::UPx;
use figures::{IntoUnsigned, Size, Zero};
use kludgine::app::winit::error::EventLoopError;
pub use names::Name;
pub use utils::{Lazy, ModifiersExt, ModifiersStateExt, WithClone};
pub use {figures, kludgine};
pub use self::graphics::{Graphics, RenderOperation, SimpleRenderOperation};
pub use self::tick::{InputState, Tick};
/// Starts running a Cushy application, invoking `app_init` after the event loop
/// has started.
pub fn run<F>(app_init: F) -> Result
where
F: FnOnce(&mut App) + Send + 'static,
{
let mut app = PendingApp::default();
app.on_startup(app_init);
app.run()
}
/// A limit used when measuring a widget.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ConstraintLimit {
/// The widget is expected to occupy a known size.
Fill(UPx),
/// The widget is expected to resize itself to fit its contents, trying to
/// stay within the size given.
SizeToFit(UPx),
}
impl ConstraintLimit {
/// Returns `UPx::ZERO` when sizing to fit, otherwise it returns the size
/// being filled.
#[must_use]
pub fn min(self) -> UPx {
match self {
ConstraintLimit::Fill(v) => v,
ConstraintLimit::SizeToFit(_) => UPx::ZERO,
}
}
/// Returns the maximum measurement that will fit the constraint.
#[must_use]
pub fn max(self) -> UPx {
match self {
ConstraintLimit::Fill(v) | ConstraintLimit::SizeToFit(v) => v,
}
}
/// Converts `measured` to unsigned pixels, and adjusts it according to the
/// constraint's intentions.
///
/// If this constraint is of a known size, it will return the maximum of the
/// measured size and the constraint. If it is of an unknown size, it will
/// return the measured size.
pub fn fit_measured<Unit>(self, measured: Unit) -> UPx
where
Unit: IntoUnsigned<Unsigned = UPx>,
{
match self {
ConstraintLimit::Fill(size) => size.max(measured.into_unsigned()),
ConstraintLimit::SizeToFit(_) => measured.into_unsigned(),
}
}
/// When `self` is `SizeToFit`, the smallest of the constraint and
/// `measured` will be returned. When `self` is `Fill`, the fill size will
/// be returned.
pub fn fill_or_fit<Unit>(self, measured: Unit) -> UPx
where
Unit: IntoUnsigned<Unsigned = UPx>,
{
match self {
ConstraintLimit::Fill(size) => size,
ConstraintLimit::SizeToFit(size) => size.min(measured.into_unsigned()),
}
}
}
/// An extension trait for `Size<ConstraintLimit>`.
pub trait FitMeasuredSize {
/// Returns the result of calling [`ConstraintLimit::fit_measured`] for each
/// matching component in `self` and `measured`.
fn fit_measured<Unit>(self, measured: Size<Unit>) -> Size<UPx>
where
Unit: IntoUnsigned<Unsigned = UPx>;
}
impl FitMeasuredSize for Size<ConstraintLimit> {
fn fit_measured<Unit>(self, measured: Size<Unit>) -> Size<UPx>
where
Unit: IntoUnsigned<Unsigned = UPx>,
{
Size::new(
self.width.fit_measured(measured.width),
self.height.fit_measured(measured.height),
)
}
}
impl Add<UPx> for ConstraintLimit {
type Output = Self;
fn add(mut self, rhs: UPx) -> Self::Output {
self += rhs;
self
}
}
impl AddAssign<UPx> for ConstraintLimit {
fn add_assign(&mut self, rhs: UPx) {
*self = match *self {
ConstraintLimit::Fill(px) => ConstraintLimit::Fill(px.saturating_add(rhs)),
ConstraintLimit::SizeToFit(px) => ConstraintLimit::SizeToFit(px.saturating_add(rhs)),
};
}
}
impl Sub<UPx> for ConstraintLimit {
type Output = Self;
fn sub(mut self, rhs: UPx) -> Self::Output {
self -= rhs;
self
}
}
impl SubAssign<UPx> for ConstraintLimit {
fn sub_assign(&mut self, rhs: UPx) {
*self = match *self {
ConstraintLimit::Fill(px) => ConstraintLimit::Fill(px.saturating_sub(rhs)),
ConstraintLimit::SizeToFit(px) => ConstraintLimit::SizeToFit(px.saturating_sub(rhs)),
};
}
}
/// A result alias that defaults to the result type commonly used throughout
/// this crate.
pub type Result<T = (), E = EventLoopError> = std::result::Result<T, E>;
/// Counts the number of expressions passed to it.
///
/// This is used inside of Cushy macros to preallocate collections.
#[macro_export]
#[doc(hidden)]
macro_rules! count {
($value:expr ;) => {
1
};
($value:expr , $($remaining:expr),+ ;) => {
1 + $crate::count!($($remaining),+ ;)
}
}
/// Creates a [`Styles`](crate::styles::Styles) instance with the given
/// name/component pairs.
#[macro_export]
macro_rules! styles {
() => {{
$crate::styles::Styles::new()
}};
($($component:expr => $value:expr),*) => {{
let mut styles = $crate::styles::Styles::with_capacity($crate::count!($($value),* ;));
$(styles.insert(&$component, $value);)*
styles
}};
($($component:expr => $value:expr),* ,) => {{
$crate::styles!($($component => $value),*)
}};
}
fn initialize_tracing() {
#[cfg(feature = "tracing-output")]
{
use tracing::Level;
use tracing_subscriber::filter::{LevelFilter, Targets};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
#[cfg(debug_assertions)]
const MAX_LEVEL: Level = Level::INFO;
#[cfg(not(debug_assertions))]
const MAX_LEVEL: Level = Level::ERROR;
let _result = tracing_subscriber::fmt::fmt()
.with_max_level(MAX_LEVEL)
.finish()
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::from_level(MAX_LEVEL).into())
.from_env_lossy(),
)
.with(
Targets::new()
.with_target("winit", Level::ERROR)
.with_target("wgpu", Level::ERROR)
.with_target("naga", Level::ERROR),
)
.try_init();
}
}