Skip to content

feat: drive button current step from expression #3128 #3225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions companion/lib/Controls/ControlTypes/Button/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { DrawStyleButtonStateProps } from '@companion-app/shared/Model/StyleMode
* Individual Contributor License Agreement for Companion along with
* this program.
*/
export abstract class ButtonControlBase<TJson, TOptions extends Record<string, any>>
export abstract class ButtonControlBase<TJson, TOptions extends ButtonOptionsBase>
extends ControlBase<TJson>
implements ControlWithOptions, ControlWithPushed
{
Expand All @@ -34,7 +34,10 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
/**
* The defaults options for a button
*/
static DefaultOptions: ButtonOptionsBase = {}
static DefaultOptions: ButtonOptionsBase = {
stepProgression: 'auto',
stepExpression: '',
}

/**
* Button hold state for each surface
Expand Down Expand Up @@ -73,6 +76,13 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
instanceDefinitions: deps.instance.definitions,
internalModule: deps.internalModule,
moduleHost: deps.instance.moduleHost,
executeExpressionInControl: (expression, requiredType, injectedVariableValues) =>
deps.variables.values.executeExpression(
expression,
deps.page.getLocationOfControlId(this.controlId),
requiredType,
injectedVariableValues
),
},
this.sendRuntimePropsChange.bind(this)
)
Expand Down Expand Up @@ -196,17 +206,14 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
cloud: false,
cloud_error: false,

step_cycle: undefined,
stepCurrent: this.entities.getActiveStepIndex() + 1,
stepCount: this.entities.getStepIds().length,

pushed: !!this.pushed,
action_running: this.actionRunner.hasRunningChains,
button_status: this.button_status,
}

if (this.entities.getStepIds().length > 1) {
result.step_cycle = this.entities.getActiveStepIndex() + 1
}

return result
}

Expand Down Expand Up @@ -244,7 +251,7 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
* @param force Trigger actions even if already in the state
*/
pressControl(pressed: boolean, surfaceId: string | undefined, force: boolean): void {
const [thisStepId, nextStepId] = this.entities.validateCurrentStepIdAndGetNext()
const [thisStepId, nextStepId] = this.entities.validateCurrentStepIdAndGetNextProgression()

let pressedDuration = 0
let pressedStep = thisStepId
Expand Down Expand Up @@ -279,7 +286,7 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
if (
thisStepId !== null &&
nextStepId !== null &&
this.options.stepAutoProgress &&
this.options.stepProgression === 'auto' &&
!pressed &&
(pressedStep === undefined || thisStepId === pressedStep)
) {
Expand Down
18 changes: 17 additions & 1 deletion companion/lib/Controls/ControlTypes/Button/Normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ControlButtonNormal
this.options = {
...cloneDeep(ButtonControlBase.DefaultOptions),
rotaryActions: false,
stepAutoProgress: true,
stepProgression: 'auto',
}

if (!storage) {
Expand All @@ -98,6 +98,7 @@ export class ControlButtonNormal
this.options = Object.assign(this.options, storage.options || {})
this.entities.setupRotaryActionSets(!!this.options.rotaryActions, true)
this.entities.loadStorage(storage, true, isImport)
this.entities.stepExpressionUpdate(this.options)

// Ensure control is stored before setup
if (isImport) setImmediate(() => this.postProcessImport())
Expand Down Expand Up @@ -213,6 +214,8 @@ export class ControlButtonNormal
* @param allChangedVariables - variables with changes
*/
onVariablesChanged(allChangedVariables: Set<string>): void {
this.entities.stepCheckExpressionOnVariablesChanged(allChangedVariables)

if (this.#last_draw_variables) {
for (const variable of allChangedVariables.values()) {
if (this.#last_draw_variables.has(variable)) {
Expand All @@ -225,6 +228,19 @@ export class ControlButtonNormal
}
}

/**
* Update an option field of this control
*/
optionsSetField(key: string, value: any): boolean {
const changed = super.optionsSetField(key, value)

if (key === 'stepProgression' || key === 'stepExpression') {
this.entities.stepExpressionUpdate(this.options)
}

return changed
}

/**
* Update the style fields of this control
* @param diff - config diff to apply
Expand Down
2 changes: 2 additions & 0 deletions companion/lib/Controls/ControlTypes/Triggers/Trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export class ControlTrigger
instanceDefinitions: deps.instance.definitions,
internalModule: deps.internalModule,
moduleHost: deps.instance.moduleHost,
executeExpressionInControl: (expression, requiredType, injectedVariableValues) =>
deps.variables.values.executeExpression(expression, null, requiredType, injectedVariableValues),
})

this.#eventBus = eventBus
Expand Down
9 changes: 8 additions & 1 deletion companion/lib/Controls/Entities/EntityListPoolBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import type { ModuleHost } from '../../Instance/Host.js'
import type { InternalController } from '../../Internal/Controller.js'
import { isEqual } from 'lodash-es'
import type { InstanceDefinitionsForEntity } from './Types.js'
import { ButtonStyleProperties } from '@companion-app/shared/Model/StyleModel.js'
import type { ButtonStyleProperties } from '@companion-app/shared/Model/StyleModel.js'
import type { CompanionVariableValues } from '@companion-module/base'
import type { ExecuteExpressionResult } from '../../Variables/Util.js'

export interface ControlEntityListPoolProps {
instanceDefinitions: InstanceDefinitionsForEntity
Expand All @@ -21,6 +23,11 @@ export interface ControlEntityListPoolProps {
controlId: string
commitChange: (redraw?: boolean) => void
triggerRedraw: () => void
executeExpressionInControl: (
expression: string,
requiredType?: string,
injectedVariableValues?: CompanionVariableValues
) => ExecuteExpressionResult
}

export abstract class ControlEntityListPoolBase {
Expand Down
Loading
Loading