Skip to content

Commit

Permalink
Support enableIf for specified value
Browse files Browse the repository at this point in the history
  • Loading branch information
sdercolin committed Apr 5, 2024
1 parent 5259454 commit d7ef48d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
53 changes: 44 additions & 9 deletions docs/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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
},
// ...
}
Expand Down Expand Up @@ -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:

- `<paramName>`:
In this case, the parameter will be enabled if the specified parameter named `paramName` is equal to
JavaScript `true`.
- `<paramName>=<value1>|<value2>|...`:
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"
// ...
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit d7ef48d

Please sign in to comment.