Skip to main content

Widget

Trait Widget 

Source
pub trait Widget:
    Sized
    + Send
    + Sync
    + 'static {
    // Required method
    fn render(cx: &Cx) -> impl Future<Output = Result<impl View>> + Send;

    // Provided method
    fn declare() -> WidgetDef { ... }
}
Expand description

One block of a page’s widget grid: a row of stats, a chart, a list of recent records.

A resource adds widgets above its list with ResourceDef::list_widget. A page adds them with PageDef::widget, and the page’s default render shows them under its header:

struct Overview;

impl Widget for Overview {
    fn declare() -> WidgetDef {
        WidgetDef::new().full_width()
    }

    async fn render(cx: &Cx) -> Result<impl View> {
        Ok(tablo_core::stats(
            cx,
            [Stat::new("Orders", "1,204").detail("12 today")],
        ))
    }
}

struct Dashboard;

impl Page for Dashboard {
    fn declare() -> PageDef {
        PageDef::new().widget::<Overview>()
    }
}

A widget reads what it shows itself. One that reads a resource’s records asks can_list before it reads, shows or links anything, and reads them through scoped_query, which keeps the caller’s tenant and the resource’s Query but not its policy.

Required Methods§

Source

fn render(cx: &Cx) -> impl Future<Output = Result<impl View>> + Send

Render the widget’s body. The grid checks WidgetDef::access before calling it. An error fails the whole page’s response, as a page’s own error does: the page answers with the error’s status, and none of its other widgets render.

Provided Methods§

Source

fn declare() -> WidgetDef

The widget’s span and access. Defaults to WidgetDef::new: one column, for every user who may open the page.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§