86 lines
2.2 KiB
Rust
86 lines
2.2 KiB
Rust
use crate::{Axis, AxisT, Len, Painter, SizeCtx, Ui};
|
|
use std::any::Any;
|
|
|
|
mod data;
|
|
mod handle;
|
|
mod like;
|
|
mod tag;
|
|
mod widgets;
|
|
|
|
pub use data::*;
|
|
pub use handle::*;
|
|
pub use like::*;
|
|
pub use tag::*;
|
|
pub use widgets::*;
|
|
|
|
pub trait Widget<State>: Any {
|
|
fn draw(&mut self, painter: &mut Painter<State>);
|
|
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len;
|
|
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len;
|
|
}
|
|
|
|
pub trait WidgetAxisFns<State> {
|
|
fn desired_len<A: AxisT>(&mut self, ctx: &mut SizeCtx<State>) -> Len;
|
|
}
|
|
|
|
impl<State, W: Widget<State> + ?Sized> WidgetAxisFns<State> for W {
|
|
fn desired_len<A: AxisT>(&mut self, ctx: &mut SizeCtx<State>) -> Len {
|
|
match A::get() {
|
|
Axis::X => self.desired_width(ctx),
|
|
Axis::Y => self.desired_height(ctx),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<State> Widget<State> for () {
|
|
fn draw(&mut self, _: &mut Painter<State>) {}
|
|
fn desired_width(&mut self, _: &mut SizeCtx<State>) -> Len {
|
|
Len::ZERO
|
|
}
|
|
fn desired_height(&mut self, _: &mut SizeCtx<State>) -> Len {
|
|
Len::ZERO
|
|
}
|
|
}
|
|
|
|
impl<State> dyn Widget<State> {
|
|
pub fn as_any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
|
|
pub fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
/// A function that returns a widget given a UI.
|
|
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
|
/// don't need to be IDs yet
|
|
pub trait WidgetFn<State, W: Widget<State> + ?Sized>: FnOnce(&mut Ui<State>) -> W {}
|
|
impl<State, W: Widget<State> + ?Sized, F: FnOnce(&mut Ui<State>) -> W> WidgetFn<State, W> for F {}
|
|
|
|
pub struct WidgetArr<State, const LEN: usize> {
|
|
pub arr: [WidgetHandle<State>; LEN],
|
|
}
|
|
|
|
impl<State, const LEN: usize> WidgetArr<State, LEN> {
|
|
pub fn new(arr: [WidgetHandle<State>; LEN]) -> Self {
|
|
Self { arr }
|
|
}
|
|
}
|
|
|
|
pub trait WidgetOption<State> {
|
|
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>>;
|
|
}
|
|
|
|
impl<State> WidgetOption<State> for () {
|
|
fn get(self, _: &mut Ui<State>) -> Option<WidgetHandle<State>> {
|
|
None
|
|
}
|
|
}
|
|
|
|
impl<State, F: FnOnce(&mut Ui<State>) -> Option<WidgetHandle<State>>> WidgetOption<State> for F {
|
|
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>> {
|
|
self(ui)
|
|
}
|
|
}
|