import { globalState } from '../app/global-state'; export default class KeyboardOpenTracker { static bind() { if (globalState._keyboardOpenTrackerBound == true) { return; } globalState._keyboardOpenTrackerBound = true; const getOrientation = () => { if (!globalState.matchMedia) { return 'portrait'; } return globalState.matchMedia('(orientation: portrait)').matches ? 'portrait' : 'landscape'; }; const getScreenHeight = () => { if (!globalState.screen) { return 0; } return globalState.screen.height; // Constant physical screen height }; let lastOrientation = getOrientation(); const handleResize = () => { if (!globalState.windowExists) { return; } const currentOrientation = getOrientation(); const currentInnerHeight = globalState.visualViewport?.height ?? globalState.innerHeight; const screenHeight = getScreenHeight(); // Different thresholds for portrait/landscape const THRESHOLD_PORTRAIT = 300; const THRESHOLD_LANDSCAPE = 190; const isPortrait = currentOrientation === 'portrait'; const threshold = isPortrait ? THRESHOLD_PORTRAIT : THRESHOLD_LANDSCAPE; // Reset baseline when orientation changes if (currentOrientation !== lastOrientation) { lastOrientation = currentOrientation; return; } const heightDiff = screenHeight - currentInnerHeight; const isKeyboardOpen = heightDiff > threshold; document.body.classList.toggle('powerduck-keyboard-open', isKeyboardOpen); }; globalState.addEventListener('resize', handleResize); handleResize(); // Run initially } }