Replies: 3 comments 15 replies
-
2 makes the most sense to me, and it feels the closest to the web as well. |
Beta Was this translation helpful? Give feedback.
-
As a baseline for how easy to use system 2 should ideally be, I'll share a possible implementation of system 3 here: The caller checkbox(is_checked, move |value| {
set_is_checked.set(value);
}), The widget implementation pub fn checkbox(
checked: ReadSignal<bool>,
on_update: impl Fn(bool) -> () + Copy + 'static,
) -> impl View {
checkbox_svg(checked)
.keyboard_navigatable()
.on_click_stop(move |_| {
on_update(!checked.get());
})
.on_key_down(
Key::Named(NamedKey::Enter),
ModifiersState::empty(),
move |_| {
on_update(!checked.get());
},
)
} To account for sneaky caveats like lifetimes I checked that this compiles. As expected the syntax is fairly straightforward, if inconsistent with the current Some fine print: I noticed that even without a key down handler, checkbox responds to Space and Enter when focused. I assume Floem implicitly fires the click handler to handle Space/Enter. This may work for checkbox, but isn't perfect from a semantic perspective and does not cover the needs of every widget. Dropdowns for example tend to react when you press the first letter of one of the dropdown items. So an |
Beta Was this translation helpful? Give feedback.
-
The update mechanism involving |
Beta Was this translation helpful? Give feedback.
-
The dropdown pull request got me thinking about Floem's event handling model. Consider that:
The function creating the widget doesn't care how the value was changed, only what its new value is. For ergonomics reasons, I wouldn't want the caller to have to manually attach mouse and keyboard handlers every time.
I currently see three options:
on_click
handlers for reporting data modifications and switch toRwSignal
. This would apply to any widget that triggers modifications including checkboxes. While simple to implement, it goes against a commonly applied convention of "data bubbles down, events bubble up". Although I'm inclined to repeat what I'm accustomed to, I have no strong opinion about the convention itself, nor have I researched its pros and cons.on_update
event handler that emits the updated signal value to the caller so they can decide what to do with it. This seems ergonomic, flexible and consistent with how other event handlers work. The main hurdle I see here is that Rust will need to know the type of the new value, and it might be tricky or impossible to express the typing constraints in the current architecture.System 1 is in line with what the text input currently does. As a user I think I'd prefer system 2. Systems 2 and 3 offer the user the most control over what happens when a change occurs. System 3 might be an interesting alternative for 2 if it's too complex to implement.
Do you guys have any thoughts on what the best approach would be? Are there any other interesting options I may have missed?
Beta Was this translation helpful? Give feedback.
All reactions