diff --git a/docs/parameter.md b/docs/parameter.md index 67caed7d..7dde1c65 100644 --- a/docs/parameter.md +++ b/docs/parameter.md @@ -13,14 +13,14 @@ both. Each parameter is framed as a JSON object encompassing the subsequent standard fields: -| Key | Type | Default value | Description | -|--------------|--------------------------------|---------------|------------------------------------------------------------------------------------------------------------| -| type | String | (Required) | Types: `integer`, `float`, `boolean`, `string`, `enum`, etc. | -| name | String | (Required) | A distinct parameter name for referencing within your scripts. | -| label | String (Localized) | (Required) | The display name for the parameter in the configuration dialog. | -| description | String (Localized) | null | null | A brief note displayed adjacent to the label. | -| enableIf | String | null | null | Activates this parameter only if the specified parameter has a value that equals to the JavaScript `true`. | -| defaultValue | (Dependent on `type`) | (Required) | The parameter's default value. | +| Key | Type | Default value | Description | +|--------------|--------------------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------| +| type | String | (Required) | Types: `integer`, `float`, `boolean`, `string`, `enum`, etc. | +| name | String | (Required) | A distinct parameter name for referencing within your scripts. | +| label | String (Localized) | (Required) | The display name for the parameter in the configuration dialog. | +| description | String (Localized) | null | null | A brief note displayed adjacent to the label. | +| enableIf | String | null | null | Activates this parameter only if the specified condition is met. See [Configure Enabled Condition](#configure-enabled-condition) for more details. | +| defaultValue | (Dependent on `type`) | (Required) | The parameter's default value. | Note that the `label` and `description` fields can be [Localized strings in vLabeler](localized-string.md). @@ -145,7 +145,8 @@ A sample `entrySelector` parameter is provided below: // nullable, `name` of any property defined in the labeler } ], - "expression": "#1 or #2" // optional, default to null, which combines all filters with `and` operator + "expression": "#1 or #2" + // optional, default to null, which combines all filters with `and` operator }, // ... } @@ -192,3 +193,37 @@ the file's content. In addition to standard fields and the `file` type fields, `rawFile` has the `isFolder` field. When set to `true`, only folder selection will be allowed through the input box. + +## Configure Enabled Condition + +The `enableIf` field allows you to conditionally enable a parameter based on the value of another parameter. Any of the +following formats are accepted: + +- ``: + In this case, the parameter will be enabled if the specified parameter named `paramName` is equal to + JavaScript `true`. +- `=||...`: + In this case, the parameter will be enabled if the specified parameter named `paramName` is equal to any of the + specified values. + +### Examples + +The following `enableIf` field will enable the parameter if the `param1` parameter is set to `true`. + +```json5 +{ + // ..., + "enableIf": "param1" + // ... +} +``` + +The following `enableIf` field will enable the parameter if the `param1` parameter is set to either `a` or `b`. + +```json5 +{ + // ..., + "enableIf": "param1=a|b" + // ... +} +``` diff --git a/src/jvmMain/kotlin/com/sdercolin/vlabeler/ui/dialog/plugin/PluginDialog.kt b/src/jvmMain/kotlin/com/sdercolin/vlabeler/ui/dialog/plugin/PluginDialog.kt index afaf3aa1..8ea7cedb 100644 --- a/src/jvmMain/kotlin/com/sdercolin/vlabeler/ui/dialog/plugin/PluginDialog.kt +++ b/src/jvmMain/kotlin/com/sdercolin/vlabeler/ui/dialog/plugin/PluginDialog.kt @@ -80,10 +80,7 @@ import com.sdercolin.vlabeler.ui.common.ReversedRow import com.sdercolin.vlabeler.ui.common.SingleClickableText import com.sdercolin.vlabeler.ui.dialog.OpenFileDialog import com.sdercolin.vlabeler.ui.dialog.SaveFileDialog -import com.sdercolin.vlabeler.ui.string.LocalizedJsonString -import com.sdercolin.vlabeler.ui.string.Strings -import com.sdercolin.vlabeler.ui.string.string -import com.sdercolin.vlabeler.ui.string.toLocalized +import com.sdercolin.vlabeler.ui.string.* import com.sdercolin.vlabeler.ui.theme.AppTheme import com.sdercolin.vlabeler.ui.theme.White20 import com.sdercolin.vlabeler.ui.theme.getSwitchColors @@ -450,10 +447,15 @@ private fun Params(state: BasePluginDialogState, js: JavaScript?, coroutineScope ) { state.params.indices.map { i -> if (state.isChangeable(state.paramDefs[i].name).not()) return@map i to false - val dependingParamName = state.paramDefs[i].enableIf ?: return@map i to true + val dependingParamName = state.paramDefs[i].enableIf?.split("=")?.first() ?: return@map i to true val dependingParam = state.paramDefs.firstOrNull { it.name == dependingParamName } ?: return@map i to false val dependingParamValue = state.params[state.paramDefs.indexOf(dependingParam)] - i to dependingParam.eval(dependingParamValue) + val dependingParamTrueValues = state.paramDefs[i].enableIf?.split("=")?.getOrNull(1)?.split("|") + if (dependingParamTrueValues != null) { + i to dependingParamTrueValues.contains(dependingParamValue.toString()) + } else { + i to dependingParam.eval(dependingParamValue) + } }.forEach { (i, enabled) -> val labelInRow = state.isParamInRow(i) Column(Modifier.heightIn(min = 60.dp)) {