Skip to content
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

views: Consider visibility state of when selecting previous/next view #722

Merged
Merged
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
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
Loading