Skip to content

fix: collect Ruflet.* helpers called inside DSL blocks - #50

Merged
AdamMusa merged 4 commits into
mainfrom
fix/dsl-prefixed-block-children
Nov 2, 2026
Merged

AdamMusa merged 4 commits into
mainfrom
fix/dsl-prefixed-block-children

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Depends on #48 (fix/dsl-block-prop-children). This branch is stacked on it, and the base is main, so the diff includes #48's two commits until #48 merges. This PR's commits are:

  1. fix: collect Ruflet.* helpers called inside DSL blocks
  2. fix: keep overlays and services out of implicit DSL block children (lead decision after QA review, amended to un-exclude the inline pickers and derive services from the engine)

Problem 1: Ruflet.* inside blocks

Each Ruflet.* helper built its control with a fresh WidgetBuilder (Ruflet's control_delegate in lib/ruflet_ui.rb). Inside a block, only the block's return value was captured:

Code Children before After
Ruflet.column { Ruflet.text("a"); Ruflet.text("b") } b a, b
Ruflet.column { Ruflet.text("top"); Ruflet.row { ... }; Ruflet.text("bottom") } bottom top, Row, bottom
bare column { Ruflet.text("a"); Ruflet.text("b") } (DSL app) none, because App#control ignores block results a, b
Ruflet::DSL.app { Ruflet.text("one"); ... } no roots Text, ...
page.mount { Ruflet.text("a"); Ruflet.container(...) } no controls Text, Container

Fix

  • New Ruflet::UI::BuildScope (ui/build_scope.rb) has within(builder) { ... } and current. Every builder pushes itself around its instance_eval and pops in ensure:
    • WidgetBuilder#control
    • center { }
    • Page#mount
    • Ruflet::DSL.app { }
    • App#page { }
    • App#control { }
  • Ruflet's control_delegate is now UI::BuildScope.current || WidgetBuilder.new. Inside a block, Ruflet.text(...) goes through the enclosing builder, exactly like a bare text(...). That includes fix: keep DSL prop controls out of block children #48's prop reclaiming. Outside every block, Ruflet.* still returns a standalone control and touches nothing else.
  • Related fix: App#control pushed its parent on @stack but did not pop it if the block raised. Later top-level controls then attached to the failed parent. It now pops in ensure.

Why the stack is per thread. The API is module level (BuildScope.within/current), but the storage is Thread.current[:ruflet_build_scope]:

  • In CRuby the server builds each session on its own thread (start_client_thread). A process-global stack would let one session's Ruflet.text(...) land in another session's open block during a thread switch.
  • Under mruby, Thread is always the Fiber-backed polyfill (ruby_runtime/vm/stdlib/thread.rb, installed as mrblib/01_thread.rb), which implements Thread.current, Thread#[] and Thread#[]= per logical thread. QA confirmed this in the embedded VM.

Problem 2: overlays and services captured as children (lead decision)

Every helper attached what it built to the enclosing block, overlays and services included. That happened with bare helpers before this PR, and after commit 1 with Ruflet.* too. In the QA repro:

column do
  dlg = Ruflet.alert_dialog(title: Ruflet.text("Delete?"))
  button("Open", on_click: ->(e) { e.page.open(dlg) })
end

dlg stayed a column child and was mounted in the page's dialogs, so the same wire id appeared twice in the client tree.

This also fixes the pre-existing bare-helper double mount. On main:

  • column { alert_dialog(...); ... } made the dialog a column child.
  • A top-level dlg = alert_dialog(...) made it a page body control.
  • A later page.open(dlg) then mounted it a second time.

Fix

  • Page::PRESENTED_TYPES (page.rb, next to DIALOG_PROP_KEYS and the open/show_dialog logic) is the single set. It is keyed on wire type, so the Cupertino aliases are covered (for example cupertino_alert_dialog → AlertDialog). It is the union of two parts:
    • Page::PRESENTED_OVERLAY_TYPES: the overlays the page presents (AboutDialog, AlertDialog, Banner, BottomSheet, DatePicker, DateRangePicker, SnackBar, TimePicker).
      • Picker, TimerPicker and ActionSheet are not in it. The engine renders all three inline (LayoutControl around a plain widget), so column { picker(...) } keeps working.
    • Ruflet::UI::ServiceTypes::ENGINE_SERVICE_TYPES: every wire type the engine registers with createService, in packages/ruflet and every packages/ruflet_* extension.
  • How the service set is derived:
    • It is not a hand list. tools/sync_engine_service_types.rb --engine <ruflet-engine> scans the engine's lib/**/*.dart, collects the case "Wire": labels inside each createService method, and rewrites the marked block in ui/service_types.rb.
    • --check exits non-zero when the block is stale. dsl_presented_controls_test.rb runs it when RUFLET_ENGINE_ROOT is set.
    • At engine c9b9d76 the set has 28 types: the 21 core services plus 7 extension services (Audio, AudioRecorder, InterstitialAd, Flashlight, Geolocator, PermissionHandler, SecureStorage).
    • Compared with RufletServices::CLASS_MAP, this adds Audio, InterstitialAd and Window.
    • The Camera-style exception falls out of the derivation: the engine registers Camera with createWidget, so it is not in the set and stays a child.
    • A test checks that every RufletServices::CLASS_MAP entry is an engine service except Page::VISUAL_SERVICE_TYPES (Camera), so the Ruby service registry and the engine cannot drift apart silently.
  • Centralized:
    • Page#service_control_type? dropped its "audio" special case and uses ServiceTypes.service_name?. As a result page.service(:interstitial_ad) is accepted too.
    • The ruflet_rails HTML transformer dropped its hand-written SERVICE_TAGS list and uses the same predicate. <audio> and the existing tags behave as before. <interstitial-ad>, <browser-context-menu> and <window> now route to services as well, and <camera> still renders inline.
  • PropControls.attach, the single implicit-attach point for WidgetBuilder and the DSL app, skips Page.presented_control?(control). This applies to bare helpers and Ruflet.* alike, at every block level and at the app's top level.
  • Explicit placement still wins: controls:/children: and a block's explicit return value still put an overlay in a layout.
  • Control#wire_type exposes the wire type that to_patch already computed.

Tests

packages/ruflet_core/test/dsl_prefixed_block_children_test.rb, 13 tests. 12 failed on the #48 base.

  • Multiple Ruflet.* children are kept in order.
  • Mixed Ruflet.* and bare helpers stay in order, with no duplicated prop controls.
  • Nested blocks three levels deep.
  • Ruflet.text outside any block has no side effects.
  • A raising block pops the scope, and a rescued inner raise restores the outer block.
  • Another thread does not see an active block.
  • Page#mount, center, DSL app blocks, DSL.app { } roots, a raising App block popping the scope and @stack, and the bare Kernel DSL.

packages/ruflet_core/test/dsl_presented_controls_test.rb, 15 tests. One is skipped unless RUFLET_ENGINE_ROOT is set; it passes against engine c9b9d76.

  • PRESENTED_TYPES = the overlays + ENGINE_SERVICE_TYPES. Audio, AudioRecorder, Flashlight, InterstitialAd, PermissionHandler, SecureStorage and Window are in it. Picker, TimerPicker, ActionSheet, Camera and layout controls are not.
  • The Ruby service registry matches the engine services except visual Camera, and service_name? handles DSL names.
  • Inline picker, timer_picker and action_sheet (canonical and Cupertino aliases, bare and Ruflet.*, WidgetBuilder and DSL app) stay children.
  • Extension and core engine services are not captured: control(:audio), interstitial_ad, window, widget(:flashlight). page.service(:audio | :interstitial_ad) works without a hand list.
  • The QA repro: after the button's click opens the dialog, the column holds only the Button, the dialog is in page.dialogs, and "_c":"AlertDialog" appears exactly once in the full page patch.
  • Overlays are not captured by bare or Ruflet.* helpers: alert_dialog, cupertino_alert_dialog, snack_bar, banner, bottom_sheet, date_picker, time_picker.
  • Not captured by DSL app blocks or app roots (the top-level double mount), the Kernel top level, or Page#mount.
  • Services are not captured, and Camera stays a child. Explicit controls: and the block's return value still place overlays. Overlay props are still reclaimed.

packages/ruflet_rails/test/html_dsl_test.rb: a new test checks that <interstitial-ad>, <browser-context-menu> and <audio-recorder> route to services and <picker> renders inline. The existing <audio>/<camera> test is unchanged and passes.

verify_pr.sh "" <ruby-wt> with RUBY_PKGS="ruflet_core ruflet ruflet_server ruflet_rails ruflet_record", all PASS:

Explorer tree check, using QA's r48_tree_dump.rb over all 67 ruflet_explorer standalone apps plus the explorer main app, with object addresses ignored:

Behaviour notes

  • Inside a block, Ruflet.* now behaves like the bare helpers: a control built there is a child unless it is consumed as a prop, or it is an overlay or service.
  • An overlay you build inside a block and open later (page.open(dlg)) is no longer also laid out in the block.
  • After an engine update that adds or removes a service, run ruby tools/sync_engine_service_types.rb --engine <ruflet-engine> (or --check in CI).

Usage (now correct)

Ruflet.column do
  Ruflet.text("Title", size: 20)
  Ruflet.row do
    Ruflet.icon("star")
    Ruflet.text("Starred")
  end
  confirm = Ruflet.alert_dialog(title: Ruflet.text("Delete?"))        # not a column child
  Ruflet.button("Delete", on_click: ->(e) { e.page.open(confirm) })
end
# => Column[Text, Row[Icon, Text], Button]; the dialog is mounted once, in page.dialogs

Inside a DSL block every helper attaches the control it builds to the
enclosing builder. Arguments are evaluated first, so in
column { list_tile(title: text("T"), subtitle: text("S")) } both Text
controls were attached as column children before the ListTile consumed them,
and the column rendered Text, Text, ListTile. The same happened for content:,
actions: [...], children:/controls: and nested hashes, in WidgetBuilder blocks
(Ruflet.column { ... }, Page#mount, center) and in the Ruflet::DSL app used by
the bare top-level helpers. There, top-level props and page(appbar: ...) also
leaked into the app's root controls.

Both builders now record the controls they attach. After building a control
(or merging page props), they walk its prop values through Hash and Array and
take back, by identity, any control they had attached. Explicit block children,
the block-result child, and children:/controls: handling are unchanged.
Each nested WidgetBuilder only took back prop controls that it had
attached itself. A control created in an outer block and used as a prop one or
more levels deeper stayed a child of the outer block:
column { t = text("x"); row { container(content: t) } } still gave
column [Text, Row].

Builders now attach implicit children through PropControls.attach, which
records the owning list on the control (Control#dsl_owner, internal and never
serialized). PropControls.reclaim takes a prop control back from that owner,
matching by identity, whichever builder or enclosing block attached it. This
also covers a WidgetBuilder nested in a DSL app block (center { } inside
app.column { }). Explicit children are never given an owner, so they are never
taken back.
Each Ruflet.* helper built its control with a fresh WidgetBuilder, so inside
Ruflet.column { Ruflet.text("a"); Ruflet.text("b") } only the block's return
value ("b") became a child. Inside a bare column { } block, which is
evaluated by the DSL app, Ruflet.* children were dropped entirely.

Builders now push themselves onto Ruflet::UI::BuildScope around instance_eval
and pop in ensure. This covers WidgetBuilder blocks, center, Page#mount,
Ruflet::DSL.app, App#page and App#control. Ruflet's control_delegate uses the
current builder when there is one, so Ruflet.* appends exactly like the bare
helpers and gets the prop-control de-duplication from the previous fix.
Outside every block it still returns a standalone control with no side
effects.

The stack is per thread (Thread.current[]), because the server builds each
session on its own thread. The mruby Thread polyfill provides the same API.
App#control now also pops its parent stack in ensure, so a raising block no
longer leaves later top-level controls attached to the failed parent.
@AdamMusa
AdamMusa force-pushed the fix/dsl-prefixed-block-children branch from 8c1fba5 to 92eb7cf Compare October 28, 2026 15:52
Every helper attached the control it built to the enclosing block, overlays
and services included. With Ruflet.* now captured as well,
column { dlg = Ruflet.alert_dialog(...); button(on_click: ->(e) { page.open(dlg) }) }
left the dialog in the column and also mounted it in the page's dialogs, with
one wire id in two places. Bare helpers already did this before, and a
top-level alert_dialog(...) became a body control that page.open mounted a
second time.

Page::PRESENTED_TYPES, keyed on wire type, is the set PropControls.attach (the
single implicit-attach point for WidgetBuilder and the DSL app) skips:

- the presented overlays: AboutDialog, AlertDialog, Banner, BottomSheet,
  DatePicker, DateRangePicker, SnackBar, TimePicker. Picker, TimerPicker and
  ActionSheet render inline in the engine and stay children.
- every wire type the engine registers with createService, core and extension
  packages: Ruflet::UI::ServiceTypes::ENGINE_SERVICE_TYPES, generated from the
  engine sources by tools/sync_engine_service_types.rb (--check verifies it).
  That adds Audio, InterstitialAd and Window to the RufletServices entries.
  Camera is a createWidget control, so it is not listed and stays a child.

Page#service_control_type? and the ruflet_rails HTML transformer's service tags
now use the same engine-derived set instead of their own hand lists. Explicit
controls:/children: and a block's return value still place an overlay in a
layout. Control#wire_type exposes the wire type that to_patch already
computed.
@AdamMusa
AdamMusa force-pushed the fix/dsl-prefixed-block-children branch from 92eb7cf to b1bf9b7 Compare October 28, 2026 22:38
@AdamMusa
AdamMusa merged commit f2be451 into main Nov 2, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant