Skip to content

Add .with() to ElementBuilder #67

@TheRedDeveloper

Description

@TheRedDeveloper

Summary

Requires: #66

Add a .with() method to ElementBuilder that accepts a closure, enabling the modification of the builder after the element's ID has been established in .empty() or .children(), but before finalization.

Motivation

Currently, for example for buttons you need an extra wrapper when dealing with automatic IDs, since the automatic ID is only known after the building.

.with() allows custom builder logic that require the element's ID without redundant wrappers.

Proposed API

pub fn with(mut self, f: impl FnOnce(&mut Ui, ElementBuilder<'ply, WithId, T>) -> ElementBuilder<'ply, WithId, T> + 'static) -> Self

Usage:

ui.element()
  .with(|ui, el| {
    if ui.pressed() {
      el.background_color(0x123456)
    } else {
      el
    }
  })
  .empty();

Behavior

  • .with should register a closure to run right after the element's ID has been established in .empty() or .children()
  • Inside the closure methods like ui.hovered() should refer to that element
  • The WithId ElementBuilder should be passed to and out of the closure
  • You should be able to call .with() multiple times to register multiple closures

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedA feature that has been approvedblockedSomething else needs to be resolved firstfeatNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions