separate state from rsc
This commit is contained in:
@@ -22,7 +22,7 @@ impl Widget for Image {
|
||||
pub fn image<State: HasUi>(image: impl LoadableImage) -> impl WidgetFn<State, Image> {
|
||||
let image = image.get_image().expect("Failed to load image");
|
||||
move |state| Image {
|
||||
handle: state.get_mut().add_texture(image),
|
||||
handle: state.ui_mut().add_texture(image),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -159,15 +159,15 @@ pub struct SpanBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Ta
|
||||
_pd: PhantomData<(State, Tag)>,
|
||||
}
|
||||
|
||||
impl<State: StateLike<State>, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag>
|
||||
WidgetFnTrait<State> for SpanBuilder<State, LEN, Wa, Tag>
|
||||
impl<Rsc, const LEN: usize, Wa: WidgetArrLike<Rsc, LEN, Tag>, Tag> WidgetFnTrait<Rsc>
|
||||
for SpanBuilder<Rsc, LEN, Wa, Tag>
|
||||
{
|
||||
type Widget = Span;
|
||||
|
||||
#[track_caller]
|
||||
fn run(self, state: &mut State) -> Self::Widget {
|
||||
fn run(self, rsc: &mut Rsc) -> Self::Widget {
|
||||
Span {
|
||||
children: self.children.add(state).arr.into_iter().collect(),
|
||||
children: self.children.add(rsc).arr.into_iter().collect(),
|
||||
dir: self.dir,
|
||||
gap: self.gap,
|
||||
}
|
||||
|
||||
@@ -48,14 +48,15 @@ pub struct StackBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, T
|
||||
_pd: PhantomData<(State, Tag)>,
|
||||
}
|
||||
|
||||
impl<State: StateLike<State>, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
|
||||
for StackBuilder<State, LEN, Wa, Tag>
|
||||
impl<Rsc, const LEN: usize, Wa: WidgetArrLike<Rsc, LEN, Tag>, Tag> WidgetFnTrait<Rsc>
|
||||
for StackBuilder<Rsc, LEN, Wa, Tag>
|
||||
{
|
||||
type Output = Stack;
|
||||
type Widget = Stack;
|
||||
|
||||
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output {
|
||||
#[track_caller]
|
||||
fn run(self, rsc: &mut Rsc) -> Self::Widget {
|
||||
Stack {
|
||||
children: self.children.add(args.0).arr.into_iter().collect(),
|
||||
children: self.children.add(rsc).arr.into_iter().collect(),
|
||||
size: self.size,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,15 +51,15 @@ impl<State, O, H: WidgetOption<State>> TextBuilder<State, O, H> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: HasUi + StateLike<State>, O> TextBuilder<State, O> {
|
||||
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
|
||||
impl<Rsc: HasUi, O> TextBuilder<Rsc, O> {
|
||||
pub fn hint<W: WidgetLike<Rsc, Tag>, Tag>(
|
||||
self,
|
||||
hint: W,
|
||||
) -> TextBuilder<State, O, impl WidgetOption<State>> {
|
||||
) -> TextBuilder<Rsc, O, impl WidgetOption<Rsc>> {
|
||||
TextBuilder {
|
||||
content: self.content,
|
||||
attrs: self.attrs,
|
||||
hint: move |ui: &mut State| Some(hint.add_strong(ui).any()),
|
||||
hint: move |rsc: &mut Rsc| Some(hint.add_strong(rsc).any()),
|
||||
output: self.output,
|
||||
state: PhantomData,
|
||||
}
|
||||
@@ -75,19 +75,19 @@ pub trait TextBuilderOutput<State>: Sized {
|
||||
}
|
||||
|
||||
pub struct TextOutput;
|
||||
impl<State: HasUi> TextBuilderOutput<State> for TextOutput {
|
||||
impl<Rsc: HasUi> TextBuilderOutput<Rsc> for TextOutput {
|
||||
type Output = Text;
|
||||
|
||||
fn run<H: WidgetOption<State>>(
|
||||
state: &mut State,
|
||||
builder: TextBuilder<State, Self, H>,
|
||||
fn run<H: WidgetOption<Rsc>>(
|
||||
state: &mut Rsc,
|
||||
builder: TextBuilder<Rsc, Self, H>,
|
||||
) -> Self::Output {
|
||||
let mut buf = TextBuffer::new_empty(Metrics::new(
|
||||
builder.attrs.font_size,
|
||||
builder.attrs.line_height,
|
||||
));
|
||||
let hint = builder.hint.get(state);
|
||||
let font_system = &mut state.get_mut().text.font_system;
|
||||
let font_system = &mut state.ui_mut().text.font_system;
|
||||
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
let mut text = Text {
|
||||
content: builder.content.into(),
|
||||
@@ -118,7 +118,7 @@ impl<State: HasUi> TextBuilderOutput<State> for TextEditOutput {
|
||||
TextView::new(buf, builder.attrs, builder.hint.get(state)),
|
||||
builder.output.mode,
|
||||
);
|
||||
let font_system = &mut state.get_mut().text.font_system;
|
||||
let font_system = &mut state.ui_mut().text.font_system;
|
||||
text.buf
|
||||
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
builder.attrs.apply(font_system, &mut text.buf, None);
|
||||
|
||||
@@ -3,35 +3,35 @@ use crate::prelude::*;
|
||||
|
||||
// these methods should "not require any context" (require unit) because they're in core
|
||||
widget_trait! {
|
||||
pub trait CoreWidget<State: HasUi + StateLike<State> + 'static>;
|
||||
pub trait CoreWidget<Rsc: HasUi + 'static>;
|
||||
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> {
|
||||
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<State, Aligned> {
|
||||
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<State, Aligned> {
|
||||
fn center(self) -> impl WidgetFn<Rsc, Aligned> {
|
||||
self.align(Align::CENTER)
|
||||
}
|
||||
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<State, WL::Widget> {
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
||||
|state| {
|
||||
let id = self.add(state);
|
||||
state.get_mut().set_label(id, label.into());
|
||||
state.ui_mut().set_label(id, label.into());
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<State, Sized> {
|
||||
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<Rsc, Sized> {
|
||||
let size = size.into();
|
||||
move |state| Sized {
|
||||
inner: self.add_strong(state),
|
||||
@@ -40,7 +40,7 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> {
|
||||
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, MaxSize> {
|
||||
let len = len.into();
|
||||
move |state| MaxSize {
|
||||
inner: self.add_strong(state),
|
||||
@@ -49,7 +49,7 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> {
|
||||
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, MaxSize> {
|
||||
let len = len.into();
|
||||
move |state| MaxSize {
|
||||
inner: self.add_strong(state),
|
||||
@@ -58,7 +58,7 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn width(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> {
|
||||
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, Sized> {
|
||||
let len = len.into();
|
||||
move |state| Sized {
|
||||
inner: self.add_strong(state),
|
||||
@@ -67,7 +67,7 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn height(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> {
|
||||
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Rsc, Sized> {
|
||||
let len = len.into();
|
||||
move |state| Sized {
|
||||
inner: self.add_strong(state),
|
||||
@@ -76,59 +76,59 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<State, Offset> {
|
||||
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<State, Scroll> where State: HasEvents {
|
||||
fn scrollable(self) -> impl WidgetIdFn<Rsc, Scroll> where Rsc: HasEvents {
|
||||
use eventable::*;
|
||||
move |state| {
|
||||
Scroll::new(self.add_strong(state), Axis::Y)
|
||||
.on(CursorSense::Scroll, |ctx: &mut EventIdCtx<'_, State::State, CursorData<'_>, Scroll>| {
|
||||
.on(CursorSense::Scroll, |ctx, rsc| {
|
||||
let delta = ctx.data.scroll_delta.y * 50.0;
|
||||
ctx.widget().scroll(delta);
|
||||
ctx.widget(rsc).scroll(delta);
|
||||
})
|
||||
.add(state)
|
||||
}
|
||||
}
|
||||
|
||||
fn masked(self) -> impl WidgetFn<State, Masked> {
|
||||
fn masked(self) -> impl WidgetFn<Rsc, Masked> {
|
||||
move |state| Masked {
|
||||
inner: self.add_strong(state),
|
||||
}
|
||||
}
|
||||
|
||||
fn background<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> {
|
||||
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<State, T>) -> impl WidgetFn<State, Stack> {
|
||||
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<State, LayerOffset> {
|
||||
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<State> {
|
||||
fn to_any(self) -> impl WidgetIdFn<Rsc> {
|
||||
|state| self.add(state)
|
||||
}
|
||||
|
||||
fn set_ptr(self, ptr: WidgetRef<WidgetPtr>, state: &mut State) {
|
||||
fn set_ptr(self, ptr: WidgetRef<WidgetPtr>, state: &mut Rsc) {
|
||||
let id = self.add_strong(state);
|
||||
state.get_mut()[ptr].inner = Some(id);
|
||||
state.ui_mut()[ptr].inner = Some(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user