|
1 | | -import { getMode, setMode, setPlatformHelpers, getElement } from '@stencil/core'; |
| 1 | +import { Build, getMode, setMode, setPlatformHelpers, getElement } from '@stencil/core'; |
2 | 2 | import { printIonWarning } from '@utils/logging'; |
3 | 3 |
|
4 | 4 | import type { IonicConfig, Mode, Theme } from '../interface'; |
| 5 | +import { shouldUseCloseWatcher } from '../utils/hardware-back-button'; |
5 | 6 | import { isPlatform, setupPlatforms } from '../utils/platform'; |
6 | 7 |
|
7 | 8 | import { config, configFromSession, configFromURL, saveConfig } from './config'; |
@@ -131,6 +132,36 @@ export const getIonTheme = (ref?: any): Theme => { |
131 | 132 | return defaultTheme; |
132 | 133 | }; |
133 | 134 |
|
| 135 | +export const rIC = (callback: () => void) => { |
| 136 | + if ('requestIdleCallback' in window) { |
| 137 | + (window as any).requestIdleCallback(callback); |
| 138 | + } else { |
| 139 | + setTimeout(callback, 32); |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +export const needInputShims = () => { |
| 144 | + /** |
| 145 | + * iOS always needs input shims |
| 146 | + */ |
| 147 | + const needsShimsIOS = isPlatform(window, 'ios') && isPlatform(window, 'mobile'); |
| 148 | + if (needsShimsIOS) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Android only needs input shims when running |
| 154 | + * in the browser and only if the browser is using the |
| 155 | + * new Chrome 108+ resize behavior: https://developer.chrome.com/blog/viewport-resize-behavior/ |
| 156 | + */ |
| 157 | + const isAndroidMobileWeb = isPlatform(window, 'android') && isPlatform(window, 'mobileweb'); |
| 158 | + if (isAndroidMobileWeb) { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + return false; |
| 163 | +}; |
| 164 | + |
134 | 165 | export const initialize = (userConfig: IonicConfig = {}) => { |
135 | 166 | if (typeof (window as any) === 'undefined') { |
136 | 167 | return; |
@@ -255,6 +286,51 @@ export const initialize = (userConfig: IonicConfig = {}) => { |
255 | 286 | } |
256 | 287 | return defaultTheme; |
257 | 288 | }); |
| 289 | + |
| 290 | + // `IonApp` code |
| 291 | + // ---------------------------------------------- |
| 292 | + |
| 293 | + if (Build.isBrowser) { |
| 294 | + rIC(async () => { |
| 295 | + const isHybrid = isPlatform(window, 'hybrid'); |
| 296 | + if (!config.getBoolean('_testing')) { |
| 297 | + import('../utils/tap-click').then((module) => module.startTapClick(config)); |
| 298 | + } |
| 299 | + if (config.getBoolean('statusTap', isHybrid)) { |
| 300 | + import('../utils/status-tap').then((module) => module.startStatusTap()); |
| 301 | + } |
| 302 | + if (config.getBoolean('inputShims', needInputShims())) { |
| 303 | + /** |
| 304 | + * needInputShims() ensures that only iOS and Android |
| 305 | + * platforms proceed into this block. |
| 306 | + */ |
| 307 | + const platform = isPlatform(window, 'ios') ? 'ios' : 'android'; |
| 308 | + import('../utils/input-shims/input-shims').then((module) => module.startInputShims(config, platform)); |
| 309 | + } |
| 310 | + const hardwareBackButtonModule = await import('../utils/hardware-back-button'); |
| 311 | + const supportsHardwareBackButtonEvents = isHybrid || shouldUseCloseWatcher(); |
| 312 | + if (config.getBoolean('hardwareBackButton', supportsHardwareBackButtonEvents)) { |
| 313 | + hardwareBackButtonModule.startHardwareBackButton(); |
| 314 | + } else { |
| 315 | + /** |
| 316 | + * If an app sets hardwareBackButton: false and experimentalCloseWatcher: true |
| 317 | + * then the close watcher will not be used. |
| 318 | + */ |
| 319 | + if (shouldUseCloseWatcher()) { |
| 320 | + printIonWarning( |
| 321 | + 'experimentalCloseWatcher was set to `true`, but hardwareBackButton was set to `false`. Both config options must be `true` for the Close Watcher API to be used.' |
| 322 | + ); |
| 323 | + } |
| 324 | + |
| 325 | + hardwareBackButtonModule.blockHardwareBackButton(); |
| 326 | + } |
| 327 | + if (typeof (window as any) !== 'undefined') { |
| 328 | + import('../utils/keyboard/keyboard').then((module) => module.startKeyboardAssist(window)); |
| 329 | + } |
| 330 | + // TODO |
| 331 | + // import('../../utils/focus-visible').then((module) => (this.focusVisible = module.startFocusVisible())); |
| 332 | + }); |
| 333 | + } |
258 | 334 | }; |
259 | 335 |
|
260 | 336 | export default initialize; |
0 commit comments