[][src]Derive Macro kas_macros::Widget

#[derive(Widget)]
{
    // Attributes available to this derive:
    #[core]
    #[layout]
    #[widget]
    #[handler]
}

Macro to derive widget traits

Unlike normal derive macros, this one implements multiple traits. Core is always derived; other traits are optional.

One struct field must be marked with #[core] and implement the Core trait. It is recommended to use the CoreData type.

If there is a #[layout(...)] attribute on the struct, then the Layout trait will be implemented. This attribute expects one of the following arguments:

  • single — single child only
  • horizontal — widgets are laid out in a row from left to right in the order specified
  • vertical — same except top-to-bottom
  • grid — see per-field #[widget] attribute specification

If there is a #[widget(...)] attribute on the struct (in addition to the #[derive(Widget)] attribute), then the Widget trait will be implemented. All child widgets must be a field marked with #[widget]. The #[widget(...)] attribute on the struct itself supports the following parameters:

  • class = ... (required) — an expression yielding the widget's Class
  • label = ...(optional) — an expression yielding the widget's label

If there is a #[handler(...)] attribute on the struct, then the Handler trait will be implemented. This attribute expects the following arguments:

  • response = ... — the Handler<Response> type
  • generics = < X, Y, ... > where CONDS — extra generic types and where clauses for the Handler implementation. This is optional; the where CONDS part is also optional; if present, generics = ... must be the last argument. The X, Y, ... types and CONDS clauses are added to generics defined on the struct itself.

When deriving Layout, Widget or Handler, a #[widget] attribute should also be used on each field which is a child widget. This attribute accepts the following arguments (for use when using the grid layout).

  • col = ... — first column, from left (defaults to 0)
  • row = ... — first row, from top (defaults to 0)
  • cspan = ... — number of columns to span (defaults to 1)
  • rspan = ... — number of rows to span (defaults to 1)
  • handler = ... — the name (f) of a method defined on this type which handles a message from the child (type M) and converts it to the appropriate response type for this widget (R); this method should have signature fn f(&mut self, tk: &TkWidget, msg: M) -> R.

Example:

#[layout(single)]
#[widget(class = Class::Window)]
#[handler(response = MyResponse, generics = <> where W: Handler<Response = ChildMsg>)]
#[derive(Widget)]
pub struct SimpleWindow<W: Widget> {
    #[core] core: CoreData,
    min_size: Coord,
    #[widget(handler=handle_msg)] w: W
}
 
impl<W: Widget> SimpleWindow<W> {
    fn handle_msg(&mut self, tk: &TkWidget, msg: ChildMsg) -> MyResponse {
        println!("Recieved message: {:?}", msg);
        MyResponse::None
    }
}

Note: usage of this macro currently requires #![feature(unrestricted_attribute_tokens)].