Skip to content

Commit

Permalink
views: Consider visibility state of when selecting previous/next view
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Feb 6, 2024
1 parent 2b1a21b commit 68e7a9c
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/stores/widgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
registerActionCallback,
unregisterActionCallback,
} from '@/libs/joystick/protocols/cockpit-actions'
import { isEqual } from '@/libs/utils'
import { isEqual, sequentialArray } from '@/libs/utils'
import type { Point2D, SizeRect2D } from '@/types/general'
import type { MiniWidget, MiniWidgetContainer } from '@/types/miniWidgets'
import { type Profile, type View, type Widget, isProfile, isView, WidgetType } from '@/types/widgets'
Expand Down Expand Up @@ -473,21 +473,40 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => {
return isEqual(widget.position, fullScreenPosition) && isEqual(widget.size, fullScreenSize)
}

const selectNextView = (): void => {
const newIndex = currentViewIndex.value === currentProfile.value.views.length - 1 ? 0 : currentViewIndex.value + 1
selectView(currentProfile.value.views[newIndex])
const selectNextView = (direction: 'forward' | 'backward' = 'forward'): void => {
const currentViews = currentProfile.value.views
const indexesOfVisibleViews = sequentialArray(currentViews.length).filter((i) => currentViews[i].visible)

const numberOfVisibleViews = indexesOfVisibleViews.length
if (numberOfVisibleViews === 1) {
Swal.fire({ icon: 'error', text: 'No visible views other the current one.', timer: 2500, timerProgressBar: true })
return
}

const increment = direction === 'forward' ? 1 : -1
let indexOfNewIndex = indexesOfVisibleViews.indexOf(currentViewIndex.value) + increment

// Rotate indexes if out of bounds
if (increment === 1 && indexOfNewIndex >= numberOfVisibleViews) {
indexOfNewIndex = 0
}
if (increment === -1 && indexOfNewIndex < 0) {
indexOfNewIndex = numberOfVisibleViews - 1
}

const realIndex = indexesOfVisibleViews[indexOfNewIndex]
selectView(currentProfile.value.views[realIndex])
}

const selectPreviousView = (): void => selectNextView('backward')

const debouncedSelectNextView = useDebounceFn(() => selectNextView(), 10)
const selectNextViewCallbackId = registerActionCallback(
availableCockpitActions.go_to_next_view,
debouncedSelectNextView
)
onBeforeUnmount(() => unregisterActionCallback(selectNextViewCallbackId))

const selectPreviousView = (): void => {
const newIndex = currentViewIndex.value === 0 ? currentProfile.value.views.length - 1 : currentViewIndex.value - 1
selectView(currentProfile.value.views[newIndex])
}
const debouncedSelectPreviousView = useDebounceFn(() => selectPreviousView(), 10)
const selectPrevViewCBId = registerActionCallback(
availableCockpitActions.go_to_previous_view,
Expand Down

0 comments on commit 68e7a9c

Please sign in to comment.