import { EventData , EventDataValue } from '../data/observable' ; import type { View } from '../ui/core/view' ; import type { Page } from '../ui/page' ; const lastFocusedViewOnPageKeyName = '__lastFocusedViewOnPage' ; export const accessibilityBlurEvent = 'accessibilityBlur' ; export const accessibilityFocusEvent = 'accessibilityFocus' ; export const accessibilityFocusChangedEvent = 'accessibilityFocusChanged' ; export const accessibilityPerformEscapeEvent = 'accessibilityPerformEscape' ; param { View } view param { boolean } receivedFocus param { boolean } lostFocus export function notifyAccessibilityFocusState ( view , receivedFocus , lostFocus ) { if ( ! receivedFocus && ! lostFocus ) { return ; } view . notify ( { eventName : accessibilityFocusChangedEvent , object : view , value : ! ! receivedFocus , } as EventDataValue ) ; if ( receivedFocus ) { if ( view . page ) { view . page [ lastFocusedViewOnPageKeyName ] = new WeakRef ( view ) ; } view . notify ( { eventName : accessibilityFocusEvent , object : view , } as EventData ) ; } else if ( lostFocus ) { view . notify ( { eventName : accessibilityBlurEvent , object : view , } as EventData ) ; } } export function getLastFocusedViewOnPage ( page ) { try { const lastFocusedViewRef = page [ lastFocusedViewOnPageKeyName ] as WeakRef < View > ; if ( ! lastFocusedViewRef ) { return null ; } const lastFocusedView = lastFocusedViewRef . get ( ) ; if ( ! lastFocusedView ) { return null ; } if ( ! lastFocusedView . parent || lastFocusedView . page !== page ) { return null ; } return lastFocusedView ; } catch { } finally { delete page [ lastFocusedViewOnPageKeyName ] ; } return null ; }