audulus / rui

Declarative Rust UI library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

passing a view into a `state` closure

wtholliday opened this issue · comments

I'm trying to add state to button for hovering:

/// Calls a function when the button is tapped.
pub fn button<A: 'static, F: Fn(&mut Context) -> A + 'static>(view: impl View, f: F) -> impl View {
    state(
        || false,
        move |hover, _| {
            view.padding(Auto)
                .background(
                    rectangle()
                        .corner_radius(BUTTON_CORNER_RADIUS)
                        .color(BUTTON_BACKGROUND_COLOR),
                )
                .tap(move |cx| f(cx))
                .hover(|_, inside| {
                    println!("inside button: {}", inside);
                })
                .role(Role::Button)
        },
    )
}

The error here is that padding (or any other modifier) wants to move out of view, which is captured by the closure passed to state.

I'm not quite sure how to deal with this. I've tried making Views Clone, but it seems to cause a big ripple.