2024-05-10 05:24:24 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
2024-09-12 03:03:55 +02:00
import { App } from "@/app/app" ;
2024-08-30 01:06:15 +02:00
import {
registerControlShiftStateUpdateHandler ,
registerElectronReinjectKeyHandler ,
registerGlobalKeys ,
} from "@/app/store/keymodel" ;
2024-09-17 08:45:47 +02:00
import { modalsModel } from "@/app/store/modalmodel" ;
2024-10-17 23:34:02 +02:00
import { FileService } from "@/app/store/services" ;
2024-09-16 20:59:39 +02:00
import { RpcApi } from "@/app/store/wshclientapi" ;
2024-10-17 23:34:02 +02:00
import { initWshrpc , TabRpcClient } from "@/app/store/wshrpcutil" ;
2024-09-03 01:48:10 +02:00
import { loadMonaco } from "@/app/view/codeeditor/codeeditor" ;
2024-10-17 23:34:02 +02:00
import { getLayoutModelForStaticTab } from "@/layout/index" ;
2024-08-24 03:12:40 +02:00
import {
atoms ,
countersClear ,
countersPrint ,
getApi ,
globalStore ,
initGlobal ,
2024-09-12 03:03:55 +02:00
initGlobalWaveEventSubs ,
2024-08-24 03:12:40 +02:00
loadConnStatus ,
2024-12-05 02:20:06 +01:00
overrideStaticTabAtom ,
2024-09-13 01:02:18 +02:00
pushFlashError ,
2024-11-16 06:26:16 +01:00
pushNotification ,
removeNotificationById ,
2024-08-24 03:12:40 +02:00
subscribeToConnEvents ,
} from "@/store/global" ;
2024-05-28 21:12:28 +02:00
import * as WOS from "@/store/wos" ;
2024-09-12 03:03:55 +02:00
import { loadFonts } from "@/util/fontutil" ;
import { setKeyUtilPlatform } from "@/util/keyutil" ;
import { createElement } from "react" ;
2024-05-10 05:24:24 +02:00
import { createRoot } from "react-dom/client" ;
2024-05-24 23:08:24 +02:00
2024-07-19 22:44:32 +02:00
const platform = getApi ( ) . getPlatform ( ) ;
2024-10-17 23:34:02 +02:00
document . title = ` Wave Terminal ` ;
let savedInitOpts : WaveInitOpts = null ;
2024-05-10 05:24:24 +02:00
2024-05-28 21:18:26 +02:00
( window as any ) . WOS = WOS ;
2024-06-04 22:05:44 +02:00
( window as any ) . globalStore = globalStore ;
2024-07-26 22:30:11 +02:00
( window as any ) . globalAtoms = atoms ;
2024-09-16 20:59:39 +02:00
( window as any ) . RpcApi = RpcApi ;
2024-07-22 22:33:10 +02:00
( window as any ) . isFullScreen = false ;
2024-08-22 00:49:23 +02:00
( window as any ) . countersPrint = countersPrint ;
( window as any ) . countersClear = countersClear ;
2024-10-17 23:34:02 +02:00
( window as any ) . getLayoutModelForStaticTab = getLayoutModelForStaticTab ;
2024-09-13 01:02:18 +02:00
( window as any ) . pushFlashError = pushFlashError ;
2024-11-16 06:26:16 +01:00
( window as any ) . pushNotification = pushNotification ;
( window as any ) . removeNotificationById = removeNotificationById ;
2024-09-17 08:45:47 +02:00
( window as any ) . modalsModel = modalsModel ;
2024-05-28 21:18:26 +02:00
2024-10-17 23:34:02 +02:00
async function initBare() {
getApi ( ) . sendLog ( "Init Bare" ) ;
document . body . style . visibility = "hidden" ;
document . body . style . opacity = "0" ;
document . body . classList . add ( "is-transparent" ) ;
getApi ( ) . onWaveInit ( initWaveWrap ) ;
setKeyUtilPlatform ( platform ) ;
loadFonts ( ) ;
document . fonts . ready . then ( ( ) = > {
console . log ( "Init Bare Done" ) ;
getApi ( ) . setWindowInitStatus ( "ready" ) ;
} ) ;
}
document . addEventListener ( "DOMContentLoaded" , initBare ) ;
async function initWaveWrap ( initOpts : WaveInitOpts ) {
try {
if ( savedInitOpts ) {
await reinitWave ( ) ;
return ;
}
savedInitOpts = initOpts ;
await initWave ( initOpts ) ;
} catch ( e ) {
getApi ( ) . sendLog ( "Error in initWave " + e . message ) ;
console . error ( "Error in initWave" , e ) ;
} finally {
document . body . style . visibility = null ;
document . body . style . opacity = null ;
document . body . classList . remove ( "is-transparent" ) ;
}
}
2024-06-20 04:10:53 +02:00
2024-10-17 23:34:02 +02:00
async function reinitWave() {
console . log ( "Reinit Wave" ) ;
getApi ( ) . sendLog ( "Reinit Wave" ) ;
2024-12-04 22:34:22 +01:00
2024-12-05 02:20:06 +01:00
// We use this hack to prevent a flicker of the previously-hovered tab when this view was last active. This class is set in setActiveTab in global.ts. See tab.scss for where this class is used.
// Also overrides the staticTabAtom to the new tab id so that the active tab is set correctly.
globalStore . set ( overrideStaticTabAtom , savedInitOpts . tabId ) ;
2024-12-05 23:25:02 +01:00
requestAnimationFrame ( ( ) = >
setTimeout ( ( ) = > {
document . body . classList . remove ( "nohover" ) ;
} , 100 )
) ;
2024-12-04 22:34:22 +01:00
2024-10-17 23:34:02 +02:00
const client = await WOS . reloadWaveObject < Client > ( WOS . makeORef ( "client" , savedInitOpts . clientId ) ) ;
const waveWindow = await WOS . reloadWaveObject < WaveWindow > ( WOS . makeORef ( "window" , savedInitOpts . windowId ) ) ;
2024-10-28 07:01:47 +01:00
const ws = await WOS . reloadWaveObject < Workspace > ( WOS . makeORef ( "workspace" , waveWindow . workspaceid ) ) ;
2024-10-17 23:34:02 +02:00
const initialTab = await WOS . reloadWaveObject < Tab > ( WOS . makeORef ( "tab" , savedInitOpts . tabId ) ) ;
await WOS . reloadWaveObject < LayoutState > ( WOS . makeORef ( "layout" , initialTab . layoutstate ) ) ;
2024-10-28 07:01:47 +01:00
reloadAllWorkspaceTabs ( ws ) ;
2024-10-17 23:34:02 +02:00
document . title = ` Wave Terminal - ${ initialTab . name } ` ; // TODO update with tab name change
getApi ( ) . setWindowInitStatus ( "wave-ready" ) ;
2024-10-28 07:01:47 +01:00
globalStore . set ( atoms . reinitVersion , globalStore . get ( atoms . reinitVersion ) + 1 ) ;
2024-10-30 22:20:19 +01:00
globalStore . set ( atoms . updaterStatusAtom , getApi ( ) . getUpdaterStatus ( ) ) ;
2024-10-28 07:01:47 +01:00
}
function reloadAllWorkspaceTabs ( ws : Workspace ) {
if ( ws == null || ws . tabids == null ) {
return ;
}
ws . tabids . forEach ( ( tabid ) = > {
WOS . reloadWaveObject < Tab > ( WOS . makeORef ( "tab" , tabid ) ) ;
} ) ;
2024-10-17 23:34:02 +02:00
}
function loadAllWorkspaceTabs ( ws : Workspace ) {
if ( ws == null || ws . tabids == null ) {
return ;
}
ws . tabids . forEach ( ( tabid ) = > {
WOS . getObjectValue < Tab > ( WOS . makeORef ( "tab" , tabid ) ) ;
} ) ;
}
async function initWave ( initOpts : WaveInitOpts ) {
getApi ( ) . sendLog ( "Init Wave " + JSON . stringify ( initOpts ) ) ;
console . log (
"Wave Init" ,
"tabid" ,
initOpts . tabId ,
"clientid" ,
initOpts . clientId ,
"windowid" ,
initOpts . windowId ,
"platform" ,
platform
) ;
initGlobal ( {
tabId : initOpts.tabId ,
clientId : initOpts.clientId ,
windowId : initOpts.windowId ,
platform ,
environment : "renderer" ,
} ) ;
( window as any ) . globalAtoms = atoms ;
2024-09-12 03:03:55 +02:00
// Init WPS event handlers
2024-10-17 23:34:02 +02:00
const globalWS = initWshrpc ( initOpts . tabId ) ;
2024-09-17 04:35:36 +02:00
( window as any ) . globalWS = globalWS ;
2024-10-17 23:34:02 +02:00
( window as any ) . TabRpcClient = TabRpcClient ;
2024-09-12 03:03:55 +02:00
await loadConnStatus ( ) ;
initGlobalWaveEventSubs ( ) ;
subscribeToConnEvents ( ) ;
2024-06-03 22:43:50 +02:00
// ensures client/window/workspace are loaded into the cache before rendering
2024-10-17 23:34:02 +02:00
const [ client , waveWindow , initialTab ] = await Promise . all ( [
WOS . loadAndPinWaveObject < Client > ( WOS . makeORef ( "client" , initOpts . clientId ) ) ,
WOS . loadAndPinWaveObject < WaveWindow > ( WOS . makeORef ( "window" , initOpts . windowId ) ) ,
WOS . loadAndPinWaveObject < Tab > ( WOS . makeORef ( "tab" , initOpts . tabId ) ) ,
] ) ;
const [ ws , layoutState ] = await Promise . all ( [
WOS . loadAndPinWaveObject < Workspace > ( WOS . makeORef ( "workspace" , waveWindow . workspaceid ) ) ,
WOS . reloadWaveObject < LayoutState > ( WOS . makeORef ( "layout" , initialTab . layoutstate ) ) ,
] ) ;
loadAllWorkspaceTabs ( ws ) ;
WOS . wpsSubscribeToObject ( WOS . makeORef ( "workspace" , waveWindow . workspaceid ) ) ;
document . title = ` Wave Terminal - ${ initialTab . name } ` ; // TODO update with tab name change
2024-09-12 03:03:55 +02:00
2024-08-30 01:06:15 +02:00
registerGlobalKeys ( ) ;
registerElectronReinjectKeyHandler ( ) ;
registerControlShiftStateUpdateHandler ( ) ;
2024-09-03 01:48:10 +02:00
setTimeout ( loadMonaco , 30 ) ;
2024-09-12 03:03:55 +02:00
const fullConfig = await FileService . GetFullConfig ( ) ;
2024-08-28 03:49:49 +02:00
console . log ( "fullconfig" , fullConfig ) ;
globalStore . set ( atoms . fullConfigAtom , fullConfig ) ;
2024-10-17 23:34:02 +02:00
console . log ( "Wave First Render" ) ;
let firstRenderResolveFn : ( ) = > void = null ;
let firstRenderPromise = new Promise < void > ( ( resolve ) = > {
firstRenderResolveFn = resolve ;
2024-09-05 09:21:08 +02:00
} ) ;
2024-10-17 23:34:02 +02:00
const reactElem = createElement ( App , { onFirstRender : firstRenderResolveFn } , null ) ;
2024-06-11 22:19:29 +02:00
const elem = document . getElementById ( "main" ) ;
const root = createRoot ( elem ) ;
2024-10-17 23:34:02 +02:00
root . render ( reactElem ) ;
await firstRenderPromise ;
console . log ( "Wave First Render Done" ) ;
getApi ( ) . setWindowInitStatus ( "wave-ready" ) ;
}