Skip to content
Snippets Groups Projects
Commit 7700b34d authored by David Sehnal's avatar David Sehnal
Browse files

wip, params

parent 920a9da9
No related branches found
No related tags found
No related merge requests found
...@@ -53,5 +53,5 @@ export function UpdateRepresentationVisibility(ctx: PluginContext) { ...@@ -53,5 +53,5 @@ export function UpdateRepresentationVisibility(ctx: PluginContext) {
} }
function updateVisibility(e: State.ObjectEvent, r: Representation<any>) { function updateVisibility(e: State.ObjectEvent, r: Representation<any>) {
r.setVisibility(!e.state.tree.cellStates.get(e.ref).isHidden); r.setVisibility(!e.state.cellStates.get(e.ref).isHidden);
} }
\ No newline at end of file
...@@ -64,11 +64,11 @@ export function ToggleExpanded(ctx: PluginContext) { ...@@ -64,11 +64,11 @@ export function ToggleExpanded(ctx: PluginContext) {
} }
export function ToggleVisibility(ctx: PluginContext) { export function ToggleVisibility(ctx: PluginContext) {
PluginCommands.State.ToggleVisibility.subscribe(ctx, ({ state, ref }) => setVisibility(state, ref, !state.tree.cellStates.get(ref).isHidden)); PluginCommands.State.ToggleVisibility.subscribe(ctx, ({ state, ref }) => setVisibility(state, ref, !state.cellStates.get(ref).isHidden));
} }
function setVisibility(state: State, root: Transform.Ref, value: boolean) { function setVisibility(state: State, root: Transform.Ref, value: boolean) {
StateTree.doPreOrder(state.tree, state.tree.transforms.get(root), { state, value }, setVisibilityVisitor); StateTree.doPreOrder(state.tree, state.transforms.get(root), { state, value }, setVisibilityVisitor);
} }
function setVisibilityVisitor(t: Transform, tree: StateTree, ctx: { state: State, value: boolean }) { function setVisibilityVisitor(t: Transform, tree: StateTree, ctx: { state: State, value: boolean }) {
......
...@@ -47,6 +47,7 @@ const ParseTrajectoryFromMmCif = PluginStateTransform.Create<SO.Data.Cif, SO.Mol ...@@ -47,6 +47,7 @@ const ParseTrajectoryFromMmCif = PluginStateTransform.Create<SO.Data.Cif, SO.Mol
}); });
export { CreateModelFromTrajectory } export { CreateModelFromTrajectory }
const plus1 = (v: number) => v + 1, minus1 = (v: number) => v - 1;
namespace CreateModelFromTrajectory { export interface Params { modelIndex: number } } namespace CreateModelFromTrajectory { export interface Params { modelIndex: number } }
const CreateModelFromTrajectory = PluginStateTransform.Create<SO.Molecule.Trajectory, SO.Molecule.Model, CreateModelFromTrajectory.Params>({ const CreateModelFromTrajectory = PluginStateTransform.Create<SO.Molecule.Trajectory, SO.Molecule.Model, CreateModelFromTrajectory.Params>({
name: 'create-model-from-trajectory', name: 'create-model-from-trajectory',
...@@ -58,7 +59,7 @@ const CreateModelFromTrajectory = PluginStateTransform.Create<SO.Molecule.Trajec ...@@ -58,7 +59,7 @@ const CreateModelFromTrajectory = PluginStateTransform.Create<SO.Molecule.Trajec
to: [SO.Molecule.Model], to: [SO.Molecule.Model],
params: { params: {
default: () => ({ modelIndex: 0 }), default: () => ({ modelIndex: 0 }),
definition: a => ({ modelIndex: PD.Numeric(0, { min: 0, max: Math.max(0, a.data.length - 1), step: 1 }, { description: 'Model Index' }) }) definition: a => ({ modelIndex: PD.Converted(plus1, minus1, PD.Numeric(1, { min: 1, max: a.data.length, step: 1 }, { description: 'Model Index' })) })
}, },
isApplicable: a => a.data.length > 0, isApplicable: a => a.data.length > 0,
apply({ a, params }) { apply({ a, params }) {
......
...@@ -87,7 +87,10 @@ export class BoolControl extends SimpleParam<PD.Boolean> { ...@@ -87,7 +87,10 @@ export class BoolControl extends SimpleParam<PD.Boolean> {
export class NumberControl extends SimpleParam<PD.Numeric> { export class NumberControl extends SimpleParam<PD.Numeric> {
onChange = (e: React.ChangeEvent<HTMLInputElement>) => { this.update(+e.target.value); } onChange = (e: React.ChangeEvent<HTMLInputElement>) => { this.update(+e.target.value); }
renderControl() { renderControl() {
return <input type='range' value={'' + this.props.value} min={this.props.param.min} max={this.props.param.max} step={this.props.param.step} onChange={this.onChange} disabled={this.props.isDisabled} />; return <span>
<input type='range' value={'' + this.props.value} min={this.props.param.min} max={this.props.param.max} step={this.props.param.step} onChange={this.onChange} disabled={this.props.isDisabled} />
<br />{this.props.value}
</span>
} }
} }
...@@ -243,7 +246,7 @@ export class ConvertedControl extends React.PureComponent<ParamProps<PD.Converte ...@@ -243,7 +246,7 @@ export class ConvertedControl extends React.PureComponent<ParamProps<PD.Converte
this.props.onChange({ this.props.onChange({
name: this.props.name, name: this.props.name,
param: this.props.param, param: this.props.param,
value: { name: e.value, params: this.props.param.toValue(e.value) } value: this.props.param.toValue(e.value)
}); });
} }
......
...@@ -129,7 +129,7 @@ export class CurrentObject extends PluginComponent { ...@@ -129,7 +129,7 @@ export class CurrentObject extends PluginComponent {
const type = obj && obj.obj ? obj.obj.type : void 0; const type = obj && obj.obj ? obj.obj.type : void 0;
const transform = current.state.tree.transforms.get(ref); const transform = current.state.transforms.get(ref);
const actions = type const actions = type
? current.state.actions.fromType(type) ? current.state.actions.fromType(type)
......
...@@ -33,7 +33,7 @@ class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, { ...@@ -33,7 +33,7 @@ class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, {
} }
get cellState() { get cellState() {
return this.props.state.tree.cellStates.get(this.props.nodeRef); return this.props.state.cellStates.get(this.props.nodeRef);
} }
componentDidMount() { componentDidMount() {
...@@ -104,7 +104,7 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State ...@@ -104,7 +104,7 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State
} else if (isCurrent) { } else if (isCurrent) {
isCurrent = false; isCurrent = false;
// have to check the node wasn't remove // have to check the node wasn't remove
if (e.state.tree.transforms.has(this.props.nodeRef)) this.forceUpdate(); if (e.state.transforms.has(this.props.nodeRef)) this.forceUpdate();
} }
}); });
} }
...@@ -125,7 +125,7 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State ...@@ -125,7 +125,7 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State
} }
render() { render() {
const n = this.props.state.tree.transforms.get(this.props.nodeRef)!; const n = this.props.state.transforms.get(this.props.nodeRef)!;
const cell = this.props.state.cells.get(this.props.nodeRef)!; const cell = this.props.state.cells.get(this.props.nodeRef)!;
const isCurrent = this.is(this.props.state.behaviors.currentObject.value); const isCurrent = this.is(this.props.state.behaviors.currentObject.value);
...@@ -141,7 +141,7 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State ...@@ -141,7 +141,7 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State
label = <><a href='#' onClick={this.setCurrent}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>; label = <><a href='#' onClick={this.setCurrent}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>;
} }
const cellState = this.props.state.tree.cellStates.get(this.props.nodeRef); const cellState = this.props.state.cellStates.get(this.props.nodeRef);
const visibility = <>[<a href='#' onClick={this.toggleVisible}>{cellState.isHidden ? 'H' : 'V'}</a>]</>; const visibility = <>[<a href='#' onClick={this.toggleVisible}>{cellState.isHidden ? 'H' : 'V'}</a>]</>;
return <> return <>
......
...@@ -22,7 +22,8 @@ namespace ApplyActionContol { ...@@ -22,7 +22,8 @@ namespace ApplyActionContol {
} }
export interface ComponentState { export interface ComponentState {
nodeRef: Transform.Ref, ref: Transform.Ref,
version: string,
params: any, params: any,
error?: string, error?: string,
busy: boolean, busy: boolean,
...@@ -38,24 +39,28 @@ class ApplyActionContol extends TransformContolBase<ApplyActionContol.Props, App ...@@ -38,24 +39,28 @@ class ApplyActionContol extends TransformContolBase<ApplyActionContol.Props, App
ref: this.props.nodeRef ref: this.props.nodeRef
}); });
} }
getInfo() { return this._getInfo(this.props.nodeRef); } getInfo() { return this._getInfo(this.props.nodeRef, this.props.state.transforms.get(this.props.nodeRef).version); }
getHeader() { return this.props.action.definition.display; } getHeader() { return this.props.action.definition.display; }
getHeaderFallback() { return this.props.action.id; } getHeaderFallback() { return this.props.action.id; }
isBusy() { return !!this.state.error || this.state.busy; } isBusy() { return !!this.state.error || this.state.busy; }
applyText() { return 'Apply'; } applyText() { return 'Apply'; }
private _getInfo = memoizeOne((t: Transform.Ref) => StateTransformParameters.infoFromAction(this.plugin, this.props.state, this.props.action, this.props.nodeRef)); private _getInfo = memoizeOne((t: Transform.Ref, v: string) => StateTransformParameters.infoFromAction(this.plugin, this.props.state, this.props.action, this.props.nodeRef));
state = { nodeRef: this.props.nodeRef, error: void 0, isInitial: true, params: this.getInfo().initialValues, busy: false }; state = { ref: this.props.nodeRef, version: this.props.state.transforms.get(this.props.nodeRef).version, error: void 0, isInitial: true, params: this.getInfo().initialValues, busy: false };
static getDerivedStateFromProps(props: ApplyActionContol.Props, state: ApplyActionContol.ComponentState) { static getDerivedStateFromProps(props: ApplyActionContol.Props, state: ApplyActionContol.ComponentState) {
if (props.nodeRef === state.nodeRef) return null; if (props.nodeRef === state.ref) return null;
const version = props.state.transforms.get(props.nodeRef).version;
if (version === state.version) return null;
const source = props.state.cells.get(props.nodeRef)!.obj!; const source = props.state.cells.get(props.nodeRef)!.obj!;
const definition = props.action.definition.params || { }; const definition = props.action.definition.params || { };
const initialValues = definition.default ? definition.default(source, props.plugin) : {}; const initialValues = definition.default ? definition.default(source, props.plugin) : {};
const newState: Partial<ApplyActionContol.ComponentState> = { const newState: Partial<ApplyActionContol.ComponentState> = {
nodeRef: props.nodeRef, ref: props.nodeRef,
version,
params: initialValues, params: initialValues,
isInitial: true, isInitial: true,
error: void 0 error: void 0
......
...@@ -45,7 +45,7 @@ class StateTransformParameters extends PurePluginComponent<StateTransformParamet ...@@ -45,7 +45,7 @@ class StateTransformParameters extends PurePluginComponent<StateTransformParamet
namespace StateTransformParameters { namespace StateTransformParameters {
export interface Props { export interface Props {
info: { info: {
definition: Transformer.ParamsProvider, definition: Transformer.ParamsDefinition,
params: PD.Params, params: PD.Params,
initialValues: any, initialValues: any,
source: StateObject, source: StateObject,
......
...@@ -46,7 +46,7 @@ namespace StateAction { ...@@ -46,7 +46,7 @@ namespace StateAction {
*/ */
apply(params: ApplyParams<A, P>, globalCtx: unknown): T | Task<T>, apply(params: ApplyParams<A, P>, globalCtx: unknown): T | Task<T>,
readonly params?: Transformer.ParamsProvider<A, P> readonly params?: Transformer.ParamsDefinition<A, P>
/** Test if the transform can be applied to a given node */ /** Test if the transform can be applied to a given node */
isApplicable?(a: A, globalCtx: unknown): boolean isApplicable?(a: A, globalCtx: unknown): boolean
......
...@@ -52,6 +52,8 @@ class State { ...@@ -52,6 +52,8 @@ class State {
readonly actions = new StateActionManager(); readonly actions = new StateActionManager();
get tree(): StateTree { return this._tree; } get tree(): StateTree { return this._tree; }
get transforms() { return (this._tree as StateTree).transforms; }
get cellStates() { return (this._tree as StateTree).cellStates; }
get current() { return this.behaviors.currentObject.value.ref; } get current() { return this.behaviors.currentObject.value.ref; }
build() { return this._tree.build(); } build() { return this._tree.build(); }
......
...@@ -47,7 +47,7 @@ export namespace Transformer { ...@@ -47,7 +47,7 @@ export namespace Transformer {
export enum UpdateResult { Unchanged, Updated, Recreate } export enum UpdateResult { Unchanged, Updated, Recreate }
export interface ParamsProvider<A extends StateObject = StateObject, P = any> { export interface ParamsDefinition<A extends StateObject = StateObject, P = any> {
/** Check the parameters and return a list of errors if the are not valid. */ /** Check the parameters and return a list of errors if the are not valid. */
default?(a: A, globalCtx: unknown): P, default?(a: A, globalCtx: unknown): P,
/** Specify default control descriptors for the parameters */ /** Specify default control descriptors for the parameters */
...@@ -75,7 +75,7 @@ export namespace Transformer { ...@@ -75,7 +75,7 @@ export namespace Transformer {
*/ */
update?(params: UpdateParams<A, B, P>, globalCtx: unknown): Task<UpdateResult> | UpdateResult, update?(params: UpdateParams<A, B, P>, globalCtx: unknown): Task<UpdateResult> | UpdateResult,
readonly params?: ParamsProvider<A, P>, readonly params?: ParamsDefinition<A, P>,
/** Test if the transform can be applied to a given node */ /** Test if the transform can be applied to a given node */
isApplicable?(a: A, globalCtx: unknown): boolean, isApplicable?(a: A, globalCtx: unknown): boolean,
......
...@@ -138,8 +138,8 @@ export namespace ParamDefinition { ...@@ -138,8 +138,8 @@ export namespace ParamDefinition {
fromValue(v: T): C, fromValue(v: T): C,
toValue(v: C): T toValue(v: C): T
} }
export function Converted<T, C extends Any>(defaultValue: T, converted: C, fromValue: (v: T) => C, toValue: (v: C) => T): Converted<T, C> { export function Converted<T, C extends Any>(fromValue: (v: T) => C['defaultValue'], toValue: (v: C['defaultValue']) => T, converted: C): Converted<T, C['defaultValue']> {
return { type: 'converted', defaultValue, converted, fromValue, toValue }; return { type: 'converted', defaultValue: toValue(converted.defaultValue), converted, fromValue, toValue };
} }
export type Any = Value<any> | Select<any> | MultiSelect<any> | Boolean | Text | Color | Numeric | Interval | LineGraph | Group<any> | Mapped<any> | Converted<any, any> export type Any = Value<any> | Select<any> | MultiSelect<any> | Boolean | Text | Color | Numeric | Interval | LineGraph | Group<any> | Mapped<any> | Converted<any, any>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment