pub trait Driver<Key, Item> {
type Widget: Widget<Data = Item>;
const TAB_NAVIGABLE: bool;
// Required methods
fn make(&mut self, key: &Key) -> Self::Widget;
fn navigable(widget: &Self::Widget) -> bool;
// Provided methods
fn set_key(&mut self, widget: &mut Self::Widget, key: &Key) { ... }
fn label(widget: &Self::Widget) -> Option<TextOrSource<'_>> { ... }
}Expand description
View widget driver
Implementations of this trait make new view widgets and optionally reassign existing view widgets.
View widgets may also need to update themselves using Events::update.
Each view widget has an Id corresponding to its data item, and
handles events like any other widget. In order to associate a returned
message with a Key, either embed that key while constructing
the widget with Driver::make or handle the message in
AsyncClerk::handle_messages.
§Example implementations
It is expected that a custom implementation is created for each usage. A
simple example might just map input data to a Text widget:
use kas_view::Driver;
use kas_widgets::Text;
struct MyDriver;
impl<Key> Driver<Key, f32> for MyDriver {
const TAB_NAVIGABLE: bool = false;
type Widget = Text<f32>;
fn make(&mut self, _: &Key) -> Self::Widget {
Text::new_gen(|_, data: &f32| data.to_string())
}
fn set_key(&mut self, _: &mut Self::Widget, _: &Key) {
// Text has no metadata that needs to be reset
}
fn navigable(widget: &Self::Widget) -> bool {
true
}
}Required Associated Constants§
Sourceconst TAB_NAVIGABLE: bool
const TAB_NAVIGABLE: bool
If true, then Tab will navigate between list items,
otherwise Tab will only select the last-used item.
Arrow keys should be able to navigate between items regardless (if
Self::navigable returns true or another widget is navigable which
doesn’t handle arrow keys), thus it is usually recommended to set this
to false.
Required Associated Types§
Required Methods§
Whether the Widget wrapper should be keyboard navigable
Provided Methods§
Sourcefn set_key(&mut self, widget: &mut Self::Widget, key: &Key)
fn set_key(&mut self, widget: &mut Self::Widget, key: &Key)
Called to bind an existing view widget to a new key
This should reset widget metadata, for example so that when a view widget with a text selection is assigned to a new key it does not attempt to apply the old selection to the new text.
This does not need to set data; Events::update does that.
The default implementation simply replaces widget with self.make(key),
which is sufficient, if not always optimal.
Sourcefn label(widget: &Self::Widget) -> Option<TextOrSource<'_>>
fn label(widget: &Self::Widget) -> Option<TextOrSource<'_>>
Get optional label for widgets
This allows accessibility tools to read an item’s label on focus. For complex
widgets supporting focus this may not be wanted. Defaults to None.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.