pub struct WidgetId(_);
Expand description
A unique identifier for a single Widget
.
WidgetId
s are generated automatically for all widgets that participate
in layout. More specifically, each WidgetPod
has a unique WidgetId
.
These ids are used internally to route events, and can be used to communicate
between widgets, by submitting a command (as with EventCtx::submit_command
)
and passing a WidgetId
as the Target
.
A widget can retrieve its id via methods on the various contexts, such as
LifeCycleCtx::widget_id
.
Explicit WidgetId
s.
Sometimes, you may want to know a widget’s id when constructing the widget.
You can give a widget an explicit id by wrapping it in an IdentityWrapper
widget, or by using the WidgetExt::with_id
convenience method.
If you set a WidgetId
directly, you are responsible for ensuring that it
is unique in time. That is: only one widget can exist with a given id at a
given time.
Implementations§
source§impl WidgetId
impl WidgetId
sourcepub fn next() -> WidgetId
pub fn next() -> WidgetId
Allocate a new, unique WidgetId
.
All widgets are assigned ids automatically; you should only create an explicit id if you need to know it ahead of time, for instance if you want two sibling widgets to know each others’ ids.
You must ensure that a given WidgetId
is only ever used for one
widget at a time.
Examples found in repository?
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
fn make_ui() -> impl Widget<OurData> {
// We can also generate these dynamically whenever we need it.
let id_two = WidgetId::next();
// We have a column with 2 labels and 2 buttons.
// Each of the 2 labels only has access to its own counter and is given a `WidgetId`.
// Both labels have a controller, this handles commands send to children.
// The 2 buttons send a command when clicked. Both send the exact same command.
// The key diference is that they both give a different `WidgetId` as target.
// This means that only the corresponding controller gets the command, and increments their counter.
Flex::column()
.with_child(
Label::dynamic(|data, _| format!("One: {data}"))
.controller(LabelControler)
.with_id(ID_ONE)
.lens(OurData::counter_one)
.padding(2.0),
)
.with_child(
Label::dynamic(|data, _| format!("Two: {data}"))
.controller(LabelControler)
.with_id(id_two)
.lens(OurData::counter_two)
.padding(2.0),
)
.with_child(
Button::<OurData>::new("Increment one")
.on_click(|ctx, _data, _env| ctx.submit_command(INCREMENT.to(ID_ONE)))
.padding(2.0),
)
.with_child(
Button::<OurData>::new("Increment two")
.on_click(move |ctx, _data, _env| ctx.submit_command(INCREMENT.to(id_two)))
.padding(2.0),
)
.padding(10.0)
}
sourcepub const fn reserved(raw: u16) -> WidgetId
pub const fn reserved(raw: u16) -> WidgetId
Create a reserved WidgetId
, suitable for reuse.
The caller is responsible for ensuring that this ID is in fact assigned to a single widget at any time, or your code may become haunted.
The actual inner representation of the returned WidgetId
will not
be the same as the raw value that is passed in; it will be
u64::max_value() - raw
.