java.lang.Object
com.ultikits.ultitools.abstracts.gui.declarative.core.Element
Direct Known Subclasses:
ComponentElement, ContainerElement, GridViewElement, RenderObjectElement

public abstract class Element extends Object
Element 是 Widget 的实例化表示,负责管理 Widget 的生命周期和更新。

Element 是持久化的对象,当 Widget 重建时,Element 会被复用(如果类型匹配)。 这种复用机制是框架高效更新的关键。

Element 的职责:

  • 持有 Widget 引用(会随重建更新)
  • 管理子 Element 树
  • 协调 Widget 的创建、更新和销毁
  • 为 RenderObjectElement 提供 RenderNode

Element 类型:

Since:
6.2.0
Version:
1.0.0
Author:
UltiTools Team
See Also:
  • Field Details

    • _widget

      @Nullable private @Nullable Widget _widget
    • _parent

      @Nullable private @Nullable Element _parent
    • _children

      @Nullable private @Nullable List<Element> _children
    • _dirty

      private boolean _dirty
    • _mounted

      private boolean _mounted
    • _context

      @Nullable private @Nullable BuildContext _context
  • Constructor Details

    • Element

      protected Element(@NotNull @NotNull Widget widget)
  • Method Details

    • assignContext

      public void assignContext(@NotNull @NotNull BuildContext context)
      为此 Element 分配 Context(仅用于根 Element 或测试)。
      Parameters:
      context - BuildContext
    • mount

      public void mount(@Nullable @Nullable Element parent)
      将此 Element 挂载到树中。

      这个方法在 Element 首次插入树中时调用。 子类应该在此处执行初始化工作,并挂载子 Element。

      Parameters:
      parent - 父 Element,如果为 null 表示这是根 Element
    • update

      public void update(@NotNull @NotNull Widget newWidget)
      使用新的 Widget 更新此 Element。

      这个方法在 Widget 重建时被调用。 子类应该更新对 Widget 的引用,并执行必要的更新。

      Parameters:
      newWidget - 新的 Widget 实例
    • canUpdate

      public boolean canUpdate(@NotNull @NotNull Widget newWidget)
      检查此 Element 是否可以更新为给定的 Widget。
      Parameters:
      newWidget - 新的 Widget
      Returns:
      如果可以更新则返回 true
    • unmount

      public void unmount()
      从树中移除此 Element。

      这个方法在 Element 不再需要时被调用。 子类应该在此处执行清理工作,并卸载子 Element。

    • markNeedsBuild

      public void markNeedsBuild()
      标记此 Element 需要重建。

      这会安排框架在下一个帧中重建此 Element。

    • markChildNeedsBuild

      public void markChildNeedsBuild(@NotNull @NotNull Element child)
      标记子 Element 需要重建。
      Parameters:
      child - 子 Element
    • performRebuild

      public abstract void performRebuild()
      执行重建。

      子类应该实现此方法来构建或重建 Widget 树。

    • getWidget

      @NotNull public @NotNull Widget getWidget()
      获取与此 Element 关联的 Widget。
      Returns:
      Widget 实例
      Throws:
      IllegalStateException - 如果 Widget 为 null
    • getParent

      @Nullable public @Nullable Element getParent()
      获取父 Element。
      Returns:
      父 Element,如果是根则返回 null
    • getChildren

      @NotNull public @NotNull List<Element> getChildren()
      获取子 Element 列表。
      Returns:
      子 Element 列表,如果没有则返回空列表
    • getContext

      @NotNull public @NotNull BuildContext getContext()
      获取构建上下文。
      Returns:
      BuildContext
    • isMounted

      public boolean isMounted()
      检查是否已挂载。
      Returns:
      如果已挂载则返回 true
    • isDirty

      public boolean isDirty()
      检查是否需要重建。
      Returns:
      如果需要重建则返回 true
    • clearDirty

      public void clearDirty()
      清除 dirty 标记。
    • addChild

      protected void addChild(@NotNull @NotNull Element child)
      添加子 Element。
      Parameters:
      child - 子 Element
    • updateChild

      @Nullable protected @Nullable Element updateChild(@Nullable @Nullable Widget newWidget, @Nullable @Nullable Element oldChild)
      更新单个子 Element。

      这是协调(reconciliation)的核心方法。 它决定是复用现有 Element 还是创建新的。

      Parameters:
      newWidget - 新的 Widget
      oldChild - 旧的子 Element,可能为 null
      Returns:
      更新后的 Element
    • toString

      public String toString()
      Overrides:
      toString in class Object