-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better ref example
example-widger-refs3
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
library/CushyStudio/examples/tutorial/widgets/example-widget-refs3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { FormBuilder, Widget_choice, Widget_group, Widget_list, Widget_selectOne } from 'src' | ||
|
||
app({ | ||
ui: (form) => ({ | ||
samplerUI: form.list({ | ||
label: 'Sampler', | ||
showID: true, | ||
startCollapsed: true, | ||
defaultLength: 1, | ||
min: 1, | ||
element: () => ui_sampler(form), | ||
}), | ||
}), | ||
run(f, r) { | ||
// | ||
}, | ||
}) | ||
|
||
const ui_sampler = (form: FormBuilder) => | ||
form.group({ | ||
label: '', | ||
topLevel: true, | ||
verticalLabels: true, | ||
className: 'relative', | ||
items: () => ({ | ||
// name: form.string({ default: '<step name>' }), | ||
source: form.choice({ | ||
items: () => ({ | ||
sampler_output: form.selectOne({ | ||
showID: true, | ||
// if choices is a function, the form root is injected as first parameter | ||
choices: (formRoot: Maybe<Widget_group<any>>) => { | ||
// handle the case where the form is not yet initialized | ||
if (formRoot == null) return [] | ||
|
||
// get the list widget | ||
// (no type-safety, because the input references itself) | ||
const listW = formRoot.values.samplerUI as Widget_list<any> | ||
|
||
return listW.items.map((item: Widget_group<any>, ix: number) => { | ||
const _choice = item.values.source as Widget_choice<any> | ||
const _selectOne = _choice.child as Widget_selectOne<any> | ||
const _actualChoice = _selectOne.result | ||
return { | ||
id: _selectOne.id, | ||
disabled: _actualChoice == null, | ||
name: _selectOne.type, | ||
label: `${ix + 1}th (${_choice.pick})`, | ||
} | ||
}) | ||
}, | ||
}), | ||
empty_latent: form.group({ | ||
label: '', | ||
layout: 'H', | ||
topLevel: true, | ||
items: () => ({ | ||
width: form.int({ default: 512, max: 1512, step: 32, hideSlider: true }), | ||
height: form.int({ default: 512, max: 1512, step: 32, hideSlider: true }), | ||
batch: form.int({ default: 1, min: 1, max: 32, hideSlider: true }), | ||
}), | ||
}), | ||
pick_image: form.image({}), | ||
}), | ||
}), | ||
}), | ||
}) |