better global state structure?

This commit is contained in:
2025-12-19 21:54:48 -05:00
parent 30bc55c78e
commit bae17235c6
23 changed files with 335 additions and 230 deletions

View File

@@ -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.ui().add_texture(image),
handle: state.get_mut().add_texture(image),
}
}

View File

@@ -51,7 +51,7 @@ impl<State, O, H: WidgetOption<State>> TextBuilder<State, O, H> {
}
}
impl<State: HasUi, O> TextBuilder<State, O> {
impl<State: HasUi + 'static, O> TextBuilder<State, O> {
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
self,
hint: W,
@@ -87,7 +87,7 @@ impl<State: HasUi> TextBuilderOutput<State> for TextOutput {
builder.attrs.line_height,
));
let hint = builder.hint.get(state);
let font_system = &mut state.ui().text.font_system;
let font_system = &mut state.get_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.ui().text.font_system;
let font_system = &mut state.get_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);

View File

@@ -617,11 +617,12 @@ impl DerefMut for TextEdit {
}
pub trait TextEditable {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a>;
fn edit<'a>(&self, ui: &'a mut impl HasUi) -> TextEditCtx<'a>;
}
impl<I: IdLike<Widget = TextEdit>> TextEditable for I {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a> {
fn edit<'a>(&self, ui: &'a mut impl HasUi) -> TextEditCtx<'a> {
let ui = ui.ui_mut();
TextEditCtx {
text: ui.widgets.get_mut(self).unwrap(),
font_system: &mut ui.text.font_system,

View File

@@ -3,7 +3,7 @@ use crate::prelude::*;
// these methods should "not require any context" (require unit) because they're in core
widget_trait! {
pub trait CoreWidget<State: HasUi>;
pub trait CoreWidget<State: HasUi + 'static>;
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> {
|state| Pad {
@@ -26,7 +26,7 @@ widget_trait! {
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<State, WL::Widget> {
|state| {
let id = self.add(state);
state.ui().set_label(&id, label.into());
state.get_mut().set_label(&id, label.into());
id
}
}
@@ -87,7 +87,7 @@ widget_trait! {
use eventable::*;
move |state| {
Scroll::new(self.add(state), Axis::Y)
.on(CursorSense::Scroll, |mut ctx| {
.on(CursorSense::Scroll, |ctx: &mut EventIdCtx<'_, State::State, CursorData<'_>, Scroll>| {
let delta = ctx.data.scroll_delta.y * 50.0;
ctx.widget().scroll(delta);
})
@@ -128,7 +128,7 @@ widget_trait! {
fn set_ptr(self, ptr: WidgetRef<WidgetPtr>, state: &mut State) {
let id = self.add(state);
state.ui()[ptr].inner = Some(id);
state.get_mut()[ptr].inner = Some(id);
}
}