149 lines
4.2 KiB
Rust
149 lines
4.2 KiB
Rust
use super::*;
|
|
use crate::prelude::*;
|
|
|
|
// these methods should "not require any context" (require unit) because they're in core
|
|
widget_trait! {
|
|
pub trait CoreWidget<Rsc: UiRsc + 'static>;
|
|
|
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Rsc, Pad> {
|
|
|state| Pad {
|
|
padding: padding.into(),
|
|
inner: self.add_strong(state),
|
|
}
|
|
}
|
|
|
|
fn align(self, align: impl Into<Align>) -> impl WidgetFn<Rsc, Aligned> {
|
|
move |state| Aligned {
|
|
inner: self.add_strong(state),
|
|
align: align.into(),
|
|
}
|
|
}
|
|
|
|
fn center(self) -> impl WidgetFn<Rsc, Aligned> {
|
|
self.align(Align::CENTER)
|
|
}
|
|
|
|
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
|
|state| {
|
|
let id = self.add(state);
|
|
state.ui_mut().widgets.set_label(id, label.into());
|
|
id
|
|
}
|
|
}
|
|
|
|
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<Rsc, Sized> {
|
|
let size = size.into();
|
|
move |state| Sized {
|
|
inner: self.add_strong(state),
|
|
x: Some(size.x),
|
|
y: Some(size.y),
|
|
}
|
|
}
|
|
|
|
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, MaxSize> {
|
|
let len = len.into();
|
|
move |state| MaxSize {
|
|
inner: self.add_strong(state),
|
|
x: Some(len),
|
|
y: None,
|
|
}
|
|
}
|
|
|
|
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, MaxSize> {
|
|
let len = len.into();
|
|
move |state| MaxSize {
|
|
inner: self.add_strong(state),
|
|
x: None,
|
|
y: Some(len),
|
|
}
|
|
}
|
|
|
|
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, Sized> {
|
|
let len = len.into();
|
|
move |state| Sized {
|
|
inner: self.add_strong(state),
|
|
x: Some(len),
|
|
y: None,
|
|
}
|
|
}
|
|
|
|
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, Sized> {
|
|
let len = len.into();
|
|
move |state| Sized {
|
|
inner: self.add_strong(state),
|
|
x: None,
|
|
y: Some(len),
|
|
}
|
|
}
|
|
|
|
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Rsc, Offset> {
|
|
move |state| Offset {
|
|
inner: self.add_strong(state),
|
|
amt: amt.into(),
|
|
}
|
|
}
|
|
|
|
fn scrollable(self) -> impl WidgetIdFn<Rsc, Scroll> where Rsc: HasEvents {
|
|
move |state| {
|
|
Scroll::new(self.add_strong(state), Axis::Y)
|
|
.on(CursorSense::Scroll, |ctx, rsc| {
|
|
let delta = ctx.data.scroll_delta.y * 50.0;
|
|
ctx.widget(rsc).scroll(delta);
|
|
})
|
|
.add(state)
|
|
}
|
|
}
|
|
|
|
fn masked(self) -> impl WidgetFn<Rsc, Masked> {
|
|
move |state| Masked {
|
|
inner: self.add_strong(state),
|
|
}
|
|
}
|
|
|
|
fn background<T>(self, w: impl WidgetLike<Rsc, T>) -> impl WidgetFn<Rsc, Stack> {
|
|
move |state| Stack {
|
|
children: vec![w.add_strong(state), self.add_strong(state)],
|
|
size: StackSize::Child(1),
|
|
}
|
|
}
|
|
|
|
fn foreground<T>(self, w: impl WidgetLike<Rsc, T>) -> impl WidgetFn<Rsc, Stack> {
|
|
move |state| Stack {
|
|
children: vec![self.add_strong(state), w.add_strong(state)],
|
|
size: StackSize::Child(0),
|
|
}
|
|
}
|
|
|
|
fn layer_offset(self, offset: usize) -> impl WidgetFn<Rsc, LayerOffset> {
|
|
move |state| LayerOffset {
|
|
inner: self.add_strong(state),
|
|
offset,
|
|
}
|
|
}
|
|
|
|
fn to_any(self) -> impl WidgetIdFn<Rsc> {
|
|
|state| self.add(state)
|
|
}
|
|
|
|
fn set_ptr(self, ptr: WeakWidget<WidgetPtr>, state: &mut Rsc) {
|
|
let id = self.add_strong(state);
|
|
state.ui_mut().widgets[ptr].inner = Some(id);
|
|
}
|
|
}
|
|
|
|
pub trait CoreWidgetArr<Rsc, const LEN: usize, Wa: WidgetArrLike<Rsc, LEN, Tag>, Tag> {
|
|
fn span(self, dir: Dir) -> SpanBuilder<Rsc, LEN, Wa, Tag>;
|
|
fn stack(self) -> StackBuilder<Rsc, LEN, Wa, Tag>;
|
|
}
|
|
|
|
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag>
|
|
CoreWidgetArr<State, LEN, Wa, Tag> for Wa
|
|
{
|
|
fn span(self, dir: Dir) -> SpanBuilder<State, LEN, Wa, Tag> {
|
|
SpanBuilder::new(self, dir)
|
|
}
|
|
fn stack(self) -> StackBuilder<State, LEN, Wa, Tag> {
|
|
StackBuilder::new(self)
|
|
}
|
|
}
|