11import { FRAME_DURATION } from './constants' ;
2- import { isConsolePoweredOn , state } from './state' ;
2+ import { isConsoleInteractive , state } from './state' ;
33import { SoundEngine } from './sound-engine' ;
44import { handleThemeSwitch } from './theme-manager' ;
55import { getNextIndex , getNextTab , isTypingTarget , type NavigationDirection , type TabDirection } from './navigation-core' ;
@@ -22,7 +22,7 @@ import {
2222export { getNextIndex , getNextTab , updateActiveLink , updateTabUI } ;
2323
2424function handleNavigation ( direction : NavigationDirection ) {
25- if ( ! isConsolePoweredOn ( ) ) return ;
25+ if ( ! isConsoleInteractive ( ) ) return ;
2626
2727 if ( state . currentTab === 0 ) {
2828 const links = getLinks ( ) ;
@@ -40,12 +40,12 @@ function handleNavigation(direction: NavigationDirection) {
4040}
4141
4242function handleSelection ( ) {
43- if ( ! isConsolePoweredOn ( ) ) return ;
43+ if ( ! isConsoleInteractive ( ) ) return ;
4444 activateSelectedLink ( ) ;
4545}
4646
4747export function switchTab ( direction : TabDirection ) {
48- if ( ! isConsolePoweredOn ( ) ) return ;
48+ if ( ! isConsoleInteractive ( ) ) return ;
4949
5050 state . currentTab = getNextTab ( state . currentTab , state . numTabs , direction ) ;
5151
@@ -59,15 +59,25 @@ export function switchTab(direction: TabDirection) {
5959}
6060
6161export function switchToTab ( index : number ) {
62- if ( ! isConsolePoweredOn ( ) ) return ;
62+ if ( ! isConsoleInteractive ( ) ) return ;
6363 if ( index < 0 || index >= state . numTabs || index === state . currentTab ) return ;
6464
6565 state . currentTab = index ;
6666 SoundEngine . switch ( ) ;
67+ resetScreenScroll ( ) ;
6768 updateTabUI ( ) ;
69+
70+ if ( state . currentTab === 0 ) {
71+ setTimeout ( ( ) => updateActiveLink ( state . currentIndex ) , FRAME_DURATION * 3 ) ;
72+ }
6873}
6974
7075export function initNavigation ( ) {
76+ const whenInteractive = ( action : ( ) => void ) => ( ) => {
77+ if ( ! isConsoleInteractive ( ) ) return ;
78+ action ( ) ;
79+ } ;
80+
7181 document . addEventListener ( 'keyup' , ( e ) => {
7282 toggleButtonState ( e . key , false ) ;
7383 } ) ;
@@ -78,21 +88,21 @@ export function initNavigation() {
7888 } ) ;
7989
8090 document . querySelectorAll ( '.tab-indicator' ) . forEach ( ( tab , index ) => {
81- tab . addEventListener ( 'click' , ( ) => switchToTab ( index ) ) ;
91+ tab . addEventListener ( 'click' , whenInteractive ( ( ) => switchToTab ( index ) ) ) ;
8292 } ) ;
8393
8494 document . addEventListener ( 'keydown' , ( e ) => {
8595 const target = e . target as HTMLElement ;
8696 if ( isTypingTarget ( target ) || e . isComposing || e . metaKey ) return ;
8797
8898 if ( ( e . key === 'h' || e . key === 'H' ) && ! e . ctrlKey && ! e . altKey && ! e . metaKey ) {
89- if ( ! isConsolePoweredOn ( ) ) return ;
99+ if ( ! isConsoleInteractive ( ) ) return ;
90100 e . preventDefault ( ) ;
91101 switchToTab ( 2 ) ;
92102 return ;
93103 }
94104
95- if ( ! e . repeat && isConsolePoweredOn ( ) ) toggleButtonState ( e . key , true ) ;
105+ if ( ! e . repeat && isConsoleInteractive ( ) ) toggleButtonState ( e . key , true ) ;
96106
97107 switch ( e . key ) {
98108 case 'ArrowUp' : case 'w' : case 'W' :
@@ -117,7 +127,7 @@ export function initNavigation() {
117127 break ;
118128 case 'x' : case 'X' :
119129 case 'q' : case 'Q' :
120- if ( ! e . ctrlKey && ! e . altKey && ! e . metaKey && isConsolePoweredOn ( ) ) {
130+ if ( ! e . ctrlKey && ! e . altKey && ! e . metaKey && isConsoleInteractive ( ) ) {
121131 e . preventDefault ( ) ;
122132 handleThemeSwitch ( ) ;
123133 }
@@ -131,21 +141,21 @@ export function initNavigation() {
131141 }
132142 } ) ;
133143
134- bindPressAction ( document . getElementById ( 'up' ) , ( ) => handleNavigation ( 'up' ) ) ;
135- bindPressAction ( document . getElementById ( 'down' ) , ( ) => handleNavigation ( 'down' ) ) ;
136- bindPressAction ( document . getElementById ( 'left' ) , ( ) => switchTab ( 'left' ) ) ;
137- bindPressAction ( document . getElementById ( 'right' ) , ( ) => switchTab ( 'right' ) ) ;
138- bindPressAction ( document . getElementById ( 'btn-a' ) , handleSelection ) ;
139- bindPressAction ( document . getElementById ( 'btn-b' ) , handleThemeSwitch ) ;
140- bindPressAction ( document . getElementById ( 'btn-select' ) , handleThemeSwitch ) ;
141- bindPressAction ( document . getElementById ( 'btn-start' ) , ( ) => switchToTab ( 2 ) ) ;
144+ bindPressAction ( document . getElementById ( 'up' ) , whenInteractive ( ( ) => handleNavigation ( 'up' ) ) ) ;
145+ bindPressAction ( document . getElementById ( 'down' ) , whenInteractive ( ( ) => handleNavigation ( 'down' ) ) ) ;
146+ bindPressAction ( document . getElementById ( 'left' ) , whenInteractive ( ( ) => switchTab ( 'left' ) ) ) ;
147+ bindPressAction ( document . getElementById ( 'right' ) , whenInteractive ( ( ) => switchTab ( 'right' ) ) ) ;
148+ bindPressAction ( document . getElementById ( 'btn-a' ) , whenInteractive ( handleSelection ) ) ;
149+ bindPressAction ( document . getElementById ( 'btn-b' ) , whenInteractive ( handleThemeSwitch ) ) ;
150+ bindPressAction ( document . getElementById ( 'btn-select' ) , whenInteractive ( handleThemeSwitch ) ) ;
151+ bindPressAction ( document . getElementById ( 'btn-start' ) , whenInteractive ( ( ) => switchToTab ( 2 ) ) ) ;
142152 bindTouchAction ( document . getElementById ( 'btn-turn' ) , toggleTurnLayout ) ;
143153
144154 const linksContainer = document . querySelector ( '.links' ) ;
145155 if ( linksContainer ) {
146156 linksContainer . addEventListener ( 'mouseover' , ( e ) => {
147157 const index = getHoveredLinkIndex ( e . target ) ;
148- if ( index !== null && state . currentTab === 0 && isConsolePoweredOn ( ) ) {
158+ if ( index !== null && state . currentTab === 0 && isConsoleInteractive ( ) ) {
149159 updateActiveLink ( index , false , true ) ;
150160 }
151161 } ) ;
@@ -156,7 +166,7 @@ export function initNavigation() {
156166 const LINK_SCROLL_THRESHOLD = 30 ;
157167
158168 document . addEventListener ( 'wheel' , ( e ) => {
159- if ( e . ctrlKey || ! isConsolePoweredOn ( ) ) return ;
169+ if ( e . ctrlKey || ! isConsoleInteractive ( ) ) return ;
160170 e . preventDefault ( ) ;
161171
162172 if ( state . currentTab === 1 || state . currentTab === 2 ) {
0 commit comments