Driver

Trait Driver 

Source
pub trait Driver<Key, Item> {
    type Widget: Widget<Data = Item>;

    // 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 crate::DataClerk::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 {
    type Widget = Text<f32, String>;
    fn make(&mut self, _: &Key) -> Self::Widget {
        Text::new(|_, data: &f32| data.to_string())
    }
    fn set_key(&mut self, _: &mut Self::Widget, _: &Key) {
        // Text has no metadata that needs to be reset
    }
}

Required Associated Types§

Source

type Widget: Widget<Data = Item>

Type of the widget used to view data

Required Methods§

Source

fn make(&mut self, key: &Key) -> Self::Widget

Construct a new view widget

Source

fn navigable(widget: &Self::Widget) -> bool

Whether the Widget wrapper should be keyboard navigable

Provided Methods§

Source

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.

Source

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.

Implementations on Foreign Types§

Source§

impl<T: Driver<Key, Item> + ?Sized, Key, Item> Driver<Key, Item> for &mut T

Source§

type Widget = <T as Driver<Key, Item>>::Widget

Source§

fn make(&mut self, key: &Key) -> Self::Widget

Source§

fn set_key(&mut self, widget: &mut Self::Widget, key: &Key)

Source§

fn navigable(widget: &Self::Widget) -> bool

Source§

fn label(widget: &Self::Widget) -> Option<TextOrSource<'_>>

Source§

impl<T: Driver<Key, Item> + ?Sized, Key, Item> Driver<Key, Item> for Box<T>

Source§

type Widget = <T as Driver<Key, Item>>::Widget

Source§

fn make(&mut self, key: &Key) -> Self::Widget

Source§

fn set_key(&mut self, widget: &mut Self::Widget, key: &Key)

Source§

fn navigable(widget: &Self::Widget) -> bool

Source§

fn label(widget: &Self::Widget) -> Option<TextOrSource<'_>>

Implementors§

Source§

impl<Key> Driver<Key, &'static str> for View

Source§

type Widget = Text<&'static str, String>

Source§

impl<Key> Driver<Key, bool> for View

Source§

impl<Key> Driver<Key, f32> for View

Source§

impl<Key> Driver<Key, f64> for View

Source§

impl<Key> Driver<Key, i8> for View

Source§

impl<Key> Driver<Key, i16> for View

Source§

impl<Key> Driver<Key, i32> for View

Source§

impl<Key> Driver<Key, i64> for View

Source§

impl<Key> Driver<Key, i128> for View

Source§

impl<Key> Driver<Key, isize> for View

Source§

impl<Key> Driver<Key, u8> for View

Source§

impl<Key> Driver<Key, u16> for View

Source§

impl<Key> Driver<Key, u32> for View

Source§

impl<Key> Driver<Key, u64> for View

Source§

impl<Key> Driver<Key, u128> for View

Source§

impl<Key> Driver<Key, usize> for View

Source§

impl<Key> Driver<Key, String> for View