Skip to content

feat: add imperative focus, paging, tabs and canvas methods - #34

Merged
AdamMusa merged 2 commits into
mainfrom
feat/control-methods
Oct 29, 2026
Merged

AdamMusa merged 2 commits into
mainfrom
feat/control-methods

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Engine counterpart: AdamMusa/ruflet-engine#18

What

Adds Ruby wrappers for control methods the Flutter client already handles, so apps can move focus, page, switch tabs and capture canvases from Ruby. The pattern is the SearchBar one: runtime_page&.invoke(self, name, args: {...}, timeout:, on_result:).

email = text_field(label: "Email", text_input_action: "next")
password = text_field(label: "Password", password: true)
email.focus
password.blur                      # also dismisses the keyboard

onboarding = page_view([step_one, step_two, step_three], height: 400)
onboarding.next_page(duration: 300, curve: "easeInOut")
onboarding.go_to_page(2)
onboarding.jump_to_page(0)

tabs_control.move_to(-1)           # last tab; negative counts from the end

canvas_control.capture(pixel_ratio: 2.0)
canvas_control.get_capture(on_result: ->(png_bytes, error) { save(png_bytes) })
canvas_control.clear_capture

API

New controls/shared/focusable_methods.rb:

  • FocusableMethods#focus(timeout: 10, on_result: nil) sends invoke focus.
  • TextInputMethods adds #blur(...), which sends invoke blur.

They are included (per SPEC R6, the contract classes are what the DSL returns) in:

  • Platform contracts: ButtonControl, FilledButtonControl, FilledTonalButtonControl, OutlinedButtonControl, TextButtonControl and DropdownControl get focus; TextFieldControl gets focus and blur.
  • Legacy Cupertino classes: CupertinoTextFieldControl (focus, blur) and CupertinoButtonControl, CupertinoFilledButtonControl, CupertinoTintedButtonControl (focus).
  • IconButton family: IconButtonControl, FilledIconButtonControl, FilledTonalIconButtonControl, OutlinedIconButtonControl (focus).

Each including file require_relatives the module, so the mruby framework bundle (ordered by the require graph in ruflet-framework/mrbgem.rake) loads it first. No defined?, no Regexp.

Paging, tab and canvas methods:

Control Method Invoke args
PageView go_to_page(index, duration: nil, curve: nil) index, duration, curve
PageView jump_to_page(index) index
PageView jump_to(value) value
PageView next_page(duration: nil, curve: nil), previous_page(...) duration, curve
Tabs move_to(index, duration: nil, curve: nil) index, duration, curve
Canvas capture(pixel_ratio: nil), get_capture, clear_capture pixel_ratio

All of these also take timeout: 10, on_result: nil.

  • duration is in milliseconds; curve is a Flutter curve name such as "easeInOut" (symbols are stringified).
  • Every wrapper sends a Hash (at least {}), never nil. The engine PR also makes PageView tolerate nil (SPEC §6 bug push new version to rubygem #3).
  • On a control that is not on a page, every method returns nil without raising.
  • blur is only offered by text inputs.

Events are unchanged. on_focus/on_blur fire from the client, and PageView/Tabs on_change carry the new index.

Note: pageview_control.rb also receives animation_curve/animation_duration keywords in a sibling PR. This PR only appends methods after initialize, so the two do not overlap.

Material vs Cupertino (client)

  • focus/blur behave the same on both designs. Material Dropdown only accepts focus when editable: true, because Flutter's DropdownMenu is not focusable otherwise.
  • move_to(-1) selects the last tab on both designs (the engine PR aligns Material with Cupertino).

Tests

test/control_methods_compatibility_test.rb (8 runs, 24 assertions), using the sender: page pattern:

  • text_field and cupertino_text_field focus/blur send name, {} args, control_id and timeout.
  • 15 controls take focus: every button, icon button and dropdown, including the Cupertino legacy spellings.
  • blur is only on text inputs.
  • PageView, in order: next_page → {}, next_page with duration/curve, previous_page, go_to_page with a Symbol curve, go_to_page(1), jump_to_page, jump_to.
  • Tabs move_to with and without animation args, including -1.
  • Canvas capture with and without pixel_ratio, get_capture with on_result returning the call id, and clear_capture.
  • Every method on a control that is not on a page returns nil and sends nothing.

verify_pr.sh with the engine PR:

PASS ruby:ruflet_core                   146/146 files, 626 runs
PASS engine:ruflet flutter test         +703: All tests passed!
PASS engine:ruflet analyze              No issues found!
INFO wire-key audit                     NEW vs baseline: 0
RESULT: PASS

Expose client control methods as Ruby methods: focus on buttons, icon
buttons, dropdowns and text fields, blur on text fields, PageView
go_to_page/jump_to_page/jump_to/next_page/previous_page, Tabs move_to,
and Canvas capture/get_capture/clear_capture. Every wrapper sends an
args Hash and is a no-op when the control is not on a page.
@AdamMusa
AdamMusa merged commit f83c9dd into main Oct 29, 2026
5 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