Skip to content

Commit

Permalink
widget-manager: Fix acessing of unreal mini-widgets manager vars
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Sep 30, 2024
1 parent f37bb6b commit 1564475
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
48 changes: 43 additions & 5 deletions src/components/mini-widgets/BatteryIndicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,29 @@
<v-card class="pa-4 text-white" style="border-radius: 15px" :style="interfaceStore.globalGlassMenuStyles">
<v-card-title class="text-center">Battery Indicator Config</v-card-title>
<v-card-text class="flex flex-col gap-y-4">
<v-checkbox v-model="miniWidget.options.showVoltageAndCurrent" label="Show Voltage and Current" hide-details />
<v-checkbox
v-model="miniWidget.options.showVoltageAndCurrent"
label="Show Voltage and Current"
hide-details
@update:model-value="validateShowOptions"
/>
<v-checkbox
v-model="miniWidget.options.showPowerAndConsumption"
label="Show Power and Consumption"
hide-details
@update:model-value="validateShowOptions"
/>
<v-text-field
v-if="miniWidget.options.showVoltageAndCurrent && miniWidget.options.showPowerAndConsumption"
v-model.number="miniWidget.options.toggleInterval"
v-model.number="toggleInterval"
label="Toggle Interval (ms)"
type="number"
min="1000"
:min="minInterval"
step="100"
density="compact"
variant="outlined"
hide-details
:error-messages="intervalErrorMessage"
@update:model-value="(v) => validateToggleInterval(Number(v))"
/>
</v-card-text>
</v-card>
Expand Down Expand Up @@ -78,6 +85,8 @@ const interfaceStore = useAppInterfaceStore()
const showVoltageAndCurrent = ref(true)
const toggleIntervaler = ref<ReturnType<typeof setInterval> | undefined>(undefined)
const minInterval = 500
const toggleInterval = ref(miniWidget.value.options.toggleInterval)
const voltageDisplayValue = computed(() => {
if (store?.powerSupply?.voltage === undefined) return NaN
Expand Down Expand Up @@ -110,6 +119,8 @@ const setupToggleInterval = (): void => {
clearInterval(toggleIntervaler.value)
}
toggleInterval.value = miniWidget.value.options.toggleInterval
if (miniWidget.value.options.showVoltageAndCurrent && miniWidget.value.options.showPowerAndConsumption) {
toggleIntervaler.value = setInterval(() => {
showVoltageAndCurrent.value = !showVoltageAndCurrent.value
Expand All @@ -121,14 +132,41 @@ const setupToggleInterval = (): void => {
watch(() => miniWidget.value.options, setupToggleInterval, { deep: true })
const validateShowOptions = (value: boolean): void => {
if (!miniWidget.value.options.showVoltageAndCurrent && !miniWidget.value.options.showPowerAndConsumption) {
// If both options are unchecked, force the current one to be checked
miniWidget.value.options[value ? 'showPowerAndConsumption' : 'showVoltageAndCurrent'] = true
}
}
const intervalErrorMessage = ref('')
const validateToggleInterval = (value: number): void => {
if (value < minInterval) {
intervalErrorMessage.value = `Interval must be at least ${minInterval}ms`
} else {
intervalErrorMessage.value = ''
miniWidget.value.options.toggleInterval = value
}
}
onBeforeMount(() => {
// Set default options if not already set
const defaultOptions = {
showVoltageAndCurrent: true,
showPowerAndConsumption: true,
toggleInterval: 3000,
}
miniWidget.value.options = Object.assign({}, defaultOptions, miniWidget.value.options)
// If both show options are disabled, use default options
if (!miniWidget.value.options.showVoltageAndCurrent && !miniWidget.value.options.showPowerAndConsumption) {
miniWidget.value.options = { ...defaultOptions }
} else {
miniWidget.value.options = Object.assign({}, defaultOptions, miniWidget.value.options)
}
// Ensure toggle interval is above the minimum
miniWidget.value.options.toggleInterval = Math.max(minInterval, miniWidget.value.options.toggleInterval)
setupToggleInterval()
Expand Down
2 changes: 1 addition & 1 deletion src/components/mini-widgets/MiniVideoRecorder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const updateCurrentStream = async (internalStreamName: string | undefined): Prom
let streamConnectionRoutine: ReturnType<typeof setInterval> | undefined = undefined
if (widgetStore.isRealMiniWidget(miniWidget.value)) {
if (widgetStore.isRealMiniWidget(miniWidget.value.hash)) {
streamConnectionRoutine = setInterval(() => {
// If the video recording widget is cold booted, assign the first stream to it
if (miniWidget.value.options.internalStreamName === undefined && !namesAvailableStreams.value.isEmpty()) {
Expand Down
20 changes: 11 additions & 9 deletions src/stores/widgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => {
}

const miniWidgetManagerVars = (miniWidgetHash: string): MiniWidgetManagerVars => {
if (!isRealMiniWidget(miniWidgetHash)) {
return { ...defaultMiniWidgetManagerVars }
}
if (!_miniWidgetManagerVars.value[miniWidgetHash]) {
_miniWidgetManagerVars.value[miniWidgetHash] = { ...defaultMiniWidgetManagerVars }
}
Expand Down Expand Up @@ -456,17 +459,16 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => {
/**
* States whether the given mini-widget is a real mini-widget
* Fake mini-widgets are those used as placeholders, in the edit-menu, for example
* @param { MiniWidget } miniWidget - Mini-widget
* @param { string } miniWidgetHash - Hash of the mini-widget
* @returns { boolean }
*/
function isRealMiniWidget(miniWidget: MiniWidget): boolean {
return savedProfiles.value.some((profile) =>
profile.views.some((view) =>
view.miniWidgetContainers.some((container) =>
container.widgets.some((widget) => widget.hash === miniWidget.hash)
)
)
)
function isRealMiniWidget(miniWidgetHash: string): boolean {
const allContainers = [
...savedProfiles.value.flatMap((profile) => profile.views.flatMap((view) => view.miniWidgetContainers)),
...currentMiniWidgetsProfile.value.containers,
]

return allContainers.some((container) => container.widgets.some((widget) => widget.hash === miniWidgetHash))
}

const fullScreenPosition = { x: 0, y: 0 }
Expand Down

0 comments on commit 1564475

Please sign in to comment.