[][src]Macro kas_macros::make_widget

make_widget!() { /* proc-macro */ }

Macro to create a widget with anonymous type

This exists purely to save you some typing. You could instead make your own struct, derive Widget (with attributes to enable Core, Layout and Widget implementation), manually implement event::Handler, and instantiate an object.

Syntax should match the following Backus-Naur Form:

<input>     ::= <layout> "=>" <response> ";" <class_spec> <fields> ";" <funcs>
<layout>    ::= "single" | "horizontal" | "vertical" | "grid"
<response>  ::= <type>
<class_spec> ::= "" | "class" "=" <path> ";"
<fields>    ::= "" | <field> | <field> "," <fields>
<field>     ::= <w_attr> <opt_ident> <field_ty> = <expr>
<opt_ident> ::= "_" | <ident>
<field_ty>  ::= "" | ":" <type> | ":" impl <bound> | "->" <type> | ":" impl <bound> "->" <type>
<w_attr>    ::= "" | "#" "[" <widget> <w_params> "]"
<w_params>  ::= "" | "(" <w_args> ")"
<w_args>    ::= <w_arg> | <w_arg> "," <w_args>
<w_arg>     ::= <pos_arg> "=" <lit> | "handler" = <ident>
<pos_arg>   ::= "col" | "row" | "cspan" | "rspan"
<funcs>     ::= "" | <func> <funcs>

where <type> is a type expression, <expr> is a (value) expression, <ident> is an identifier, <lit> is a literal, <path> is a path, <bound> is a trait object bound, and <func> is a Rust method definition. "" is the empty string (i.e. nothing).

The effect of this macro is to create an anonymous struct with the above fields (plus an implicit core), implement Core, Layout, Widget and Handler (with the specified <response> type), implement the additional <funcs> listed on this type, then construct and return an instance using the given value expressions to initialise each field.

Each field is considered a child widget if the #[widget] attribute is present, or a simple data field otherwise. The specification of this attribute is identical to that used when deriving Widget.

The layout specifier should be self-explanatory, with the exception of grid, where each widget's position must be specified via attribute arguments (e.g. #[widget(col=1, row=2)]). The col and row parameters both default to 0, while cspan and rspan (column and row spans) both default to 1.

Fields may have an identifier or may be anonymous (via usage of _). This is often convenient for child widgets which don't need to be referred to.

Fields may have an explicit type (ident : type = ...), or the type may be skipped, or (for widgets only) just the response type can be specified via ident -> type = .... Note that some type specification is usually needed when referring to the field later.

Optionally, a message handler may be specified for child widgets via #[widget(handler = f)] ident = value where f is a method defined on the anonymous struct with signature fn f(&mut self, tk: &TkWidget, msg: M) -> R where M is the type of response received from the child widget, and R is the type of response sent from this widget.

Currently usage of this macro requires #![feature(proc_macro_hygiene)].