Browse Source

custom property improvements

- isHidden (always attached)
- getParams uses current props as defaultValues
- add .createSimple to model & structure property provider
Alexander Rose 4 years ago
parent
commit
5e41e959f8

+ 24 - 1
src/mol-model-props/common/custom-model-property.ts

@@ -9,6 +9,7 @@ import { ParamDefinition as PD } from '../../mol-util/param-definition';
 import { ValueBox } from '../../mol-util';
 import { CustomProperty } from './custom-property';
 import { CustomPropertyDescriptor } from '../../mol-model/custom-property';
+import { stringToWords } from '../../mol-util/string';
 
 export { CustomModelProperty };
 
@@ -18,6 +19,7 @@ namespace CustomModelProperty {
     export interface ProviderBuilder<Params extends PD.Params, Value> {
         readonly label: string
         readonly descriptor: CustomPropertyDescriptor
+        readonly isHidden?: boolean
         readonly defaultParams: Params
         readonly getParams: (data: Model) => Params
         readonly isApplicable: (data: Model) => boolean
@@ -49,7 +51,12 @@ namespace CustomModelProperty {
         return {
             label: builder.label,
             descriptor: builder.descriptor,
-            getParams: builder.getParams,
+            isHidden: builder.isHidden,
+            getParams: (data: Model) => {
+                const params = PD.clone(builder.getParams(data));
+                PD.setDefaultValues(params, get(data).props);
+                return params;
+            },
             defaultParams: builder.defaultParams,
             isApplicable: builder.isApplicable,
             attach: async (ctx: CustomProperty.Context, data: Model, props: Partial<PD.Values<Params>> = {}, addRef) => {
@@ -77,4 +84,20 @@ namespace CustomModelProperty {
             props: (data: Model) => get(data).props,
         };
     }
+
+    export function createSimple<T>(name: string, type: 'static' | 'dynamic', defaultValue?: T) {
+        const defaultParams = { value: PD.Value(defaultValue, { isHidden: true }) };
+        return createProvider({
+            label: stringToWords(name),
+            descriptor: CustomPropertyDescriptor({ name }),
+            isHidden: true,
+            type,
+            defaultParams,
+            getParams: () => ({ value: PD.Value(defaultValue, { isHidden: true }) }),
+            isApplicable: () => true,
+            obtain: async (ctx: CustomProperty.Context, data: Model, props: Partial<PD.Values<typeof defaultParams>>) => {
+                return { value: props.value ?? defaultValue };
+            }
+        });
+    }
 }

+ 8 - 4
src/mol-model-props/common/custom-property.ts

@@ -29,6 +29,8 @@ namespace CustomProperty {
     export interface Provider<Data, Params extends PD.Params, Value> {
         readonly label: string
         readonly descriptor: CustomPropertyDescriptor
+        /** hides property in ui and always attaches */
+        readonly isHidden?: boolean
         readonly getParams: (data: Data) => Params
         readonly defaultParams: Params
         readonly isApplicable: (data: Data) => boolean
@@ -57,14 +59,16 @@ namespace CustomProperty {
                     const provider = v.value;
                     if (!provider.isApplicable(data)) continue;
 
-                    autoAttachOptions.push([provider.descriptor.name, provider.label]);
-                    if (this.defaultAutoAttachValues.get(provider.descriptor.name)) {
-                        autoAttachDefault.push(provider.descriptor.name);
+                    if (!provider.isHidden) {
+                        autoAttachOptions.push([provider.descriptor.name, provider.label]);
+                        if (this.defaultAutoAttachValues.get(provider.descriptor.name)) {
+                            autoAttachDefault.push(provider.descriptor.name);
+                        }
                     }
 
                     propertiesParams[provider.descriptor.name] = PD.Group({
                         ...provider.getParams(data)
-                    }, { label: provider.label });
+                    }, { label: provider.label, isHidden: provider.isHidden });
                 }
             }
             return {

+ 24 - 1
src/mol-model-props/common/custom-structure-property.ts

@@ -9,6 +9,7 @@ import { ParamDefinition as PD } from '../../mol-util/param-definition';
 import { ValueBox } from '../../mol-util';
 import { CustomProperty } from './custom-property';
 import { CustomPropertyDescriptor } from '../../mol-model/custom-property';
+import { stringToWords } from '../../mol-util/string';
 
 export { CustomStructureProperty };
 
@@ -18,6 +19,7 @@ namespace CustomStructureProperty {
     export interface ProviderBuilder<Params extends PD.Params, Value> {
         readonly label: string
         readonly descriptor: CustomPropertyDescriptor
+        readonly isHidden?: boolean
         readonly defaultParams: Params
         readonly getParams: (data: Structure) => Params
         readonly isApplicable: (data: Structure) => boolean
@@ -49,7 +51,12 @@ namespace CustomStructureProperty {
         return {
             label: builder.label,
             descriptor: builder.descriptor,
-            getParams: builder.getParams,
+            isHidden: builder.isHidden,
+            getParams: (data: Structure) => {
+                const params = PD.clone(builder.getParams(data));
+                PD.setDefaultValues(params, get(data).props);
+                return params;
+            },
             defaultParams: builder.defaultParams,
             isApplicable: builder.isApplicable,
             attach: async (ctx: CustomProperty.Context, data: Structure, props: Partial<PD.Values<Params>> = {}, addRef) => {
@@ -80,4 +87,20 @@ namespace CustomStructureProperty {
             props: (data: Structure) => get(data).props,
         };
     }
+
+    export function createSimple<T>(name: string, type: 'root' | 'local', defaultValue?: T) {
+        const defaultParams = { value: PD.Value(defaultValue, { isHidden: true }) };
+        return createProvider({
+            label: stringToWords(name),
+            descriptor: CustomPropertyDescriptor({ name }),
+            isHidden: true,
+            type,
+            defaultParams,
+            getParams: () => ({ value: PD.Value(defaultValue, { isHidden: true }) }),
+            isApplicable: () => true,
+            obtain: async (ctx: CustomProperty.Context, data: Structure, props: Partial<PD.Values<typeof defaultParams>>) => {
+                return { ...PD.getDefaultValues(defaultParams), ...props };
+            }
+        });
+    }
 }

+ 2 - 2
src/mol-plugin-state/transforms/model.ts

@@ -887,7 +887,7 @@ async function attachModelProps(model: Model, ctx: PluginContext, taskCtx: Runti
     for (const name of Object.keys(properties)) {
         const property = ctx.customModelProperties.get(name);
         const props = properties[name];
-        if (autoAttach.includes(name)) {
+        if (autoAttach.includes(name) || property.isHidden) {
             try {
                 await property.attach(propertyCtx, model, props, true);
             } catch (e) {
@@ -940,7 +940,7 @@ async function attachStructureProps(structure: Structure, ctx: PluginContext, ta
     for (const name of Object.keys(properties)) {
         const property = ctx.customStructureProperties.get(name);
         const props = properties[name];
-        if (autoAttach.includes(name)) {
+        if (autoAttach.includes(name) || property.isHidden) {
             try {
                 await property.attach(propertyCtx, structure, props, true);
             } catch (e) {