mirror of
https://github.com/haexhub/haex-hub.git
synced 2025-12-18 23:10:51 +01:00
polyfill for spa added. works now on android
This commit is contained in:
60
src/composables/useAndroidBackButton.ts
Normal file
60
src/composables/useAndroidBackButton.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { onMounted, onUnmounted, ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { platform } from '@tauri-apps/plugin-os'
|
||||
import { getCurrentWindow } from '@tauri-apps/api/window'
|
||||
|
||||
/**
|
||||
* Handles Android back button to navigate within the app instead of closing it
|
||||
* Mimics browser behavior: navigate back if possible, close app if on first page
|
||||
*/
|
||||
export function useAndroidBackButton() {
|
||||
const router = useRouter()
|
||||
const historyStack = ref<string[]>([])
|
||||
let unlisten: (() => void) | null = null
|
||||
|
||||
// Track navigation history manually
|
||||
router.afterEach((to, from) => {
|
||||
console.log('[AndroidBack] Navigation:', { to: to.path, from: from.path, stackSize: historyStack.value.length })
|
||||
|
||||
// If navigating forward (new page)
|
||||
if (from.path && to.path !== from.path && !historyStack.value.includes(to.path)) {
|
||||
historyStack.value.push(from.path)
|
||||
console.log('[AndroidBack] Added to stack:', from.path, 'Stack:', historyStack.value)
|
||||
}
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
const os = platform()
|
||||
|
||||
if (os === 'android') {
|
||||
const appWindow = getCurrentWindow()
|
||||
|
||||
// Listen to close requested event (triggered by Android back button)
|
||||
unlisten = await appWindow.onCloseRequested(async (event) => {
|
||||
console.log('[AndroidBack] Back button pressed, stack size:', historyStack.value.length)
|
||||
|
||||
// Check if we have history
|
||||
if (historyStack.value.length > 0) {
|
||||
// Prevent window from closing
|
||||
event.preventDefault()
|
||||
|
||||
// Remove current page from stack
|
||||
historyStack.value.pop()
|
||||
console.log('[AndroidBack] Going back, new stack size:', historyStack.value.length)
|
||||
|
||||
// Navigate back in router
|
||||
router.back()
|
||||
} else {
|
||||
console.log('[AndroidBack] No history, allowing app to close')
|
||||
}
|
||||
// If no history, allow default behavior (app closes)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (unlisten) {
|
||||
unlisten()
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user