Attribute Macro zng::widget::widget_mixin

source ·
#[widget_mixin]
Expand description

Expands a struct to a widget mix-in.

Widget mix-ins can be inserted on a widgets inheritance chain, but they cannot be instantiated directly. Unlike the full widgets it defines its parent as a generic type, that must be filled with a real widget when used.

By convention mix-ins have the suffix Mix and the generic parent is named P. The P must not have any generic bounds in the declaration, the expansion will bound it to WidgetImpl.

§Examples

use zng::prelude_wgt::*;

/// Make a widget capable of receiving keyboard focus.
#[widget_mixin]
pub struct FocusableMix<P>(P);
impl<P: WidgetImpl> FocusableMix<P> {
    fn widget_intrinsic(&mut self) {
        widget_set! {
            self;
            focusable = true;
        }
    }
     
    widget_impl! {
        /// If the widget can receive focus, enabled by default.
        pub zng::focus::focusable(enabled: impl IntoVar<bool>);
    }
}

/// Foo is focusable.
#[widget($crate::Foo)]
pub struct Foo(FocusableMix<WidgetBase>);

The example above declares a mix-in FocusableMix<P> and a widget Foo, the mix-in is used as a parent of the widget, only the Foo! { } widget can be instantiated, and it will have the strongly associated property focusable from the mix-in.

All widget impl items can be declared in a mix-in, including the fn widget_build(&mut self) -> T. Multiple mix-ins can be inherited by nesting the types in a full widget Foo(AMix<BMix<Base>>). Mix-ins cannot inherit from other mix-ins.

Expands a generic struct to a widget mixin.

§Full Documentation

Read the documentation in the zng::widget::widget_mixin page.