Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/perry-codegen-js/src/web_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ function perry_ui_state_bind_toggle(stateH, widgetH) {
input.checked = !!stateGet(stateH);
}

// Issue #5076: programmatically set a Toggle's on/off state. `on` is
// 0 for off, non-zero for on.
function perry_ui_toggle_set_state(widgetH, on) {
const el = getHandle(widgetH);
if (!el) return;
const input = el._input || el.querySelector("input[type=checkbox]");
if (input) input.checked = !!on;
}

function perry_ui_state_bind_visibility(stateH, widgetH) {
const el = getHandle(widgetH);
if (!el) return;
Expand Down Expand Up @@ -3549,6 +3558,7 @@ window.__perry = {
perry_ui_textfield_create,
perry_ui_securefield_create,
perry_ui_toggle_create,
perry_ui_toggle_set_state,
perry_ui_slider_create,
perry_ui_scrollview_create,
perry_ui_spacer_create,
Expand Down
9 changes: 8 additions & 1 deletion crates/perry-codegen-wasm/src/wasm_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,13 @@ function perry_ui_state_bind_toggle(stateH, widgetH) {
if (inp) { inp.checked = !!s._value; s.subscribers.push(v => { inp.checked = !!v; }); }
}
}
// Issue #5076: programmatically set a Toggle's on/off state (0 = off, non-zero = on).
function perry_ui_toggle_set_state(widgetH, on) {
const wrap = uiGet(widgetH);
if (!wrap) return;
const inp = wrap._inp || wrap.querySelector("input");
if (inp) inp.checked = !!on;
}
function perry_ui_state_bind_visibility(stateH, widgetH) {
const el = uiGet(widgetH), s = uiStates.get(stateH);
if (el && s) {
Expand Down Expand Up @@ -4621,7 +4628,7 @@ const __perryUiDispatch = {
// Widget creation
perry_ui_app_create, perry_ui_vstack_create, perry_ui_hstack_create, perry_ui_zstack_create,
perry_ui_text_create, perry_ui_button_create, perry_ui_textfield_create, perry_ui_securefield_create,
perry_ui_toggle_create, perry_ui_slider_create, perry_ui_scrollview_create, perry_ui_spacer_create,
perry_ui_toggle_create, perry_ui_toggle_set_state, perry_ui_slider_create, perry_ui_scrollview_create, perry_ui_spacer_create,
perry_ui_divider_create, perry_ui_progressview_create, perry_ui_image_create, perry_ui_picker_create,
perry_ui_form_create, perry_ui_section_create, perry_ui_navigationstack_create, perry_ui_canvas_create,
perry_ui_lazyvstack_create, perry_ui_lazyvstack_update, perry_ui_table_create,
Expand Down
10 changes: 10 additions & 0 deletions crates/perry-dispatch/src/ui_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,16 @@ pub const PERRY_UI_TABLE: &[MethodRow] = &[
args: &[ArgKind::Str, ArgKind::Closure],
ret: ReturnKind::Widget,
},
// Programmatically set a Toggle's on/off state (issue #5076). `on`
// is a raw i64 (0 = off, non-zero = on); `Toggle(label, onChange)`
// has no initial-state param, so this is the documented way to show
// a non-default ON state in a rebuild/re-create render model.
MethodRow {
method: "toggleSetState",
runtime: "perry_ui_toggle_set_state",
args: &[ArgKind::Widget, ArgKind::I64Raw],
ret: ReturnKind::Void,
},
MethodRow {
method: "Slider",
runtime: "perry_ui_slider_create",
Expand Down
7 changes: 7 additions & 0 deletions crates/perry-ui-android/src/ffi/basic_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ pub extern "C" fn perry_ui_state_bind_toggle(state_handle: i64, toggle_handle: i
state::bind_toggle(state_handle, toggle_handle);
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
widgets::toggle::set_state(handle, on);
}

#[no_mangle]
pub extern "C" fn perry_ui_state_bind_text_template(
text_handle: i64,
Expand Down
7 changes: 7 additions & 0 deletions crates/perry-ui-gtk4/src/ffi/widget_tree_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ pub extern "C" fn perry_ui_state_bind_toggle(state_handle: i64, toggle_handle: i
state::bind_toggle(state_handle, toggle_handle);
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
widgets::toggle::set_state(handle, on);
}

/// Bind a text widget to multiple states with a template.
#[no_mangle]
pub extern "C" fn perry_ui_state_bind_text_template(
Expand Down
7 changes: 7 additions & 0 deletions crates/perry-ui-ios/src/ffi/widgets_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ pub extern "C" fn perry_ui_toggle_create(label_ptr: i64, on_change: f64) -> i64
widgets::toggle::create(label_ptr as *const u8, on_change)
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
widgets::toggle::set_state(handle, on);
}

#[no_mangle]
pub extern "C" fn perry_ui_slider_create(min: f64, max: f64, on_change: f64) -> i64 {
// Codegen emits 3-arg `Slider(min, max, onChange)`; default initial=min.
Expand Down
7 changes: 7 additions & 0 deletions crates/perry-ui-macos/src/lib_ffi/core_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ pub extern "C" fn perry_ui_toggle_create(label_ptr: i64, on_change: f64) -> i64
widgets::toggle::create(label_ptr as *const u8, on_change)
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
widgets::toggle::set_state(handle, on);
}

/// Create a Slider. min/max are f64, on_change = NaN-boxed closure.
/// Returns widget handle. Initial value defaults to `min` (the TS
/// surface `Slider(min, max, onChange)` doesn't expose `initial`, and
Expand Down
7 changes: 7 additions & 0 deletions crates/perry-ui-tvos/src/ffi/reactive_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pub extern "C" fn perry_ui_state_bind_toggle(state_handle: i64, toggle_handle: i
state::bind_toggle(state_handle, toggle_handle);
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
widgets::toggle::set_state(handle, on);
}

#[no_mangle]
pub extern "C" fn perry_ui_state_bind_text_template(
text_handle: i64,
Expand Down
7 changes: 7 additions & 0 deletions crates/perry-ui-visionos/src/ffi_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ pub extern "C" fn perry_ui_slider_create(min: f64, max: f64, initial: f64, on_ch
widgets::slider::create(min, max, initial, on_change)
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
widgets::toggle::set_state(handle, on);
}

// =============================================================================
// Phase 4: Advanced Reactive UI
// =============================================================================
Expand Down
10 changes: 10 additions & 0 deletions crates/perry-ui-watchos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,16 @@ pub extern "C" fn perry_ui_state_bind_toggle(state_handle: i64, toggle_handle: i
state::bind_toggle(state_handle, toggle_handle);
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on. Updates the node's `toggle_on` flag so the
/// next host render reflects the new state.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
tree::with_node_mut(handle, |node| {
node.toggle_on = on != 0;
});
}

#[no_mangle]
pub extern "C" fn perry_ui_state_bind_text_template(
text_handle: i64,
Expand Down
10 changes: 10 additions & 0 deletions crates/perry-ui-windows/src/ffi/widget_tree_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ pub extern "C" fn perry_ui_state_bind_toggle(state_handle: i64, toggle_handle: i
state::bind_toggle(state_handle, toggle_handle);
}

/// Set an existing Toggle's on/off state (issue #5076). `on` is 0 for
/// off, non-zero for on.
#[no_mangle]
pub extern "C" fn perry_ui_toggle_set_state(handle: i64, on: i64) {
// Normalize to 0/1 before narrowing: a plain `on as i32` truncates,
// so a non-zero i64 like 0x1_0000_0000 would wrap to 0 and read as
// "off", violating the documented non-zero-means-on contract.
widgets::toggle::set_state(handle, if on != 0 { 1 } else { 0 });
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/// Bind a text widget to multiple states with a template.
#[no_mangle]
pub extern "C" fn perry_ui_state_bind_text_template(
Expand Down
3 changes: 3 additions & 0 deletions types/perry/ui/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ export function SecureField(placeholder: string, onChange: (value: string) => vo
/** Boolean toggle switch. */
export function Toggle(label: string, onChange: (value: boolean) => void): Widget;

/** Programmatically set a Toggle's on/off state. Pass `1` for on, `0` for off. */
export function toggleSetState(widget: Widget, on: number): void;

/** Numeric slider. */
export function Slider(min: number, max: number, onChange: (value: number) => void): Widget;

Expand Down