<commit_before>/*
 * Copyright 2019 <NAME> <<EMAIL>>
 *
 * This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
 */
import { Event, CustomizedCommand, SessionConfiguration} from 'extraterm-extension-api';
import * as Electron from 'electron';
import * as _ from 'lodash';
import * as SourceMapSupport from 'source-map-support';

const ElectronMenu = Electron.remote.Menu;

import {AboutTab} from './AboutTab';
import './gui/All'; // Need to load all of the GUI web components into the browser engine
import {CheckboxMenuItem} from './gui/CheckboxMenuItem';
import { CommandPalette } from "./command/CommandPalette";
import { EVENT_CONTEXT_MENU_REQUEST } from './command/CommandUtils';

import {ConfigDatabase, injectConfigDatabase, ConfigKey, SESSION_CONFIG, SystemConfig, GENERAL_CONFIG, SYSTEM_CONFIG, GeneralConfig, ConfigChangeEvent} from '../Config';
import {ContextMenu} from './gui/ContextMenu';
import * as DomUtils from './DomUtils';
import {DropDown} from './gui/DropDown';
import {EmbeddedViewer} from './viewers/EmbeddedViewer';
import {ExtensionManagerImpl} from './extension/ExtensionManager';
import {ExtensionManager} from './extension/InternalTypes';
import {EVENT_DRAG_STARTED, EVENT_DRAG_ENDED} from './GeneralEvents';
import {Logger, getLogger} from "extraterm-logging";
import {MainWebUi} from './MainWebUi';
import {MenuItem} from './gui/MenuItem';
import {PopDownListPicker} from './gui/PopDownListPicker';
import {ResizeCanary} from './ResizeCanary';
import {SettingsTab} from './settings/SettingsTab';
import {TabWidget} from './gui/TabWidget';
import {EtTerminal} from './Terminal';
import {TerminalViewer} from './viewers/TerminalAceViewer';
import {TextViewer} from'./viewers/TextAceViewer';
import * as ThemeTypes from '../theme/Theme';
import * as ThemeConsumer from '../theme/ThemeConsumer';
import * as WebIpc from './WebIpc';
import * as Messages from '../WindowMessages';
import { EventEmitter } from '../utils/EventEmitter';
import { freezeDeep } from 'extraterm-readonly-toolbox';
import { log } from "extraterm-logging";
import { KeybindingsManager, injectKeybindingsManager, loadKeybindingsFromObject, TermKeybindingsMapping } from './keybindings/KeyBindingsManager';
import { trimBetweenTags } from 'extraterm-trim-between-tags';
import { ApplicationContextMenu } from "./command/ApplicationContextMenu";
import { registerCommands as TextCommandsRegisterCommands } from "./viewers/TextCommands";
import { DisposableHolder } from '../utils/DisposableUtils';
import { ExtensionCommandContribution, Category } from '../ExtensionMetadata';
import { EtViewerTab } from './ViewerTab';
import { isSupportsDialogStack } from './SupportsDialogStack';

type ThemeInfo = ThemeTypes.ThemeInfo;

SourceMapSupport.install();

const MENU_ITEM_DEVELOPER_TOOLS = 'developer_tools';
const ID_MAIN_MENU = "ID_MAIN_MENU";
const ID_MENU_BUTTON = "ID_MENU_BUTTON";
const CLASS_MAIN_DRAGGING = "CLASS_MAIN_DRAGGING";
const CLASS_MAIN_NOT_DRAGGING = "CLASS_MAIN_NOT_DRAGGING";

const _log = getLogger("mainweb");

/**
 * This module has control of the window and is responsible for
 * starting up the main component and handling the window directly.
 */

let keybindingsManager: KeybindingsManager = null;
let themes: ThemeInfo[];
let mainWebUi: MainWebUi = null;
let configDatabase: ConfigDatabaseImpl = null;
let extensionManager: ExtensionManager = null;
let commandPalette: CommandPalette = null;
let applicationContextMenu: ApplicationContextMenu = null;


export function startUp(closeSplash: () => void): void {
  ElectronMenu.setApplicationMenu(null);

  startUpTheming();
  startUpWebIpc();

  // Get the Config working.
  configDatabase = new ConfigDatabaseImpl();
  keybindingsManager = new KeybindingsManagerImpl();  // depends on the config.
  const configPromise = WebIpc.requestConfig("*").then( (msg: Messages.ConfigMessage) => {
    return handleConfigMessage(msg);
  });
  
  // Get the config and theme info in and then continue starting up.
  const allPromise = Promise.all<void>( [configPromise, WebIpc.requestThemeList().then(handleThemeListMessage)] );
  allPromise.then(loadFontFaces).then( () => {

    const doc = window.document;
    doc.body.classList.add(CLASS_MAIN_NOT_DRAGGING);

    startUpExtensions();
    startUpMainWebUi();
    registerCommands(extensionManager);
    startUpSessions(configDatabase, extensionManager);

    closeSplash();

    startUpMainMenu();
    startUpCommandPalette();
    startUpApplicationContextMenu();
    startUpWindowEvents();
    startUpMenus();

    if (configDatabase.getConfig(SESSION_CONFIG).length !== 0) {
      mainWebUi.commandNewTerminal({ sessionUuid: configDatabase.getConfig(SESSION_CONFIG)[0].uuid });
    } else {
      mainWebUi.commandOpenSettingsTab("session");
      Electron.remote.dialog.showErrorBox("No session types available",
        "Extraterm doesn't have any session types configured.");
    }
    mainWebUi.focus();
    window.focus();
  });
}

function startUpTheming(): void {
  // Theme control for the window level.
  const topThemeable: ThemeTypes.Themeable = {
    setThemeCssMap(cssMap: Map<ThemeTypes.CssFile, string>): void {      
      (<HTMLStyleElement> document.getElementById('THEME_STYLE')).textContent =
        cssMap.get(ThemeTypes.CssFile.GENERAL_GUI) + "\n" + 
        cssMap.get(ThemeTypes.CssFile.FONT_AWESOME) + "\n" + <commit_msg>Convert `MainWeb.startUp()` to async/await<commit_after>/*
 * Copyright 2019 <NAME> <<EMAIL>>
 *
 * This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
 */
import { Event, CustomizedCommand, SessionConfiguration} from 'extraterm-extension-api';
import * as Electron from 'electron';
import * as _ from 'lodash';
import * as SourceMapSupport from 'source-map-support';

const ElectronMenu = Electron.remote.Menu;

import {AboutTab} from './AboutTab';
import './gui/All'; // Need to load all of the GUI web components into the browser engine
import {CheckboxMenuItem} from './gui/CheckboxMenuItem';
import { CommandPalette } from "./command/CommandPalette";
import { EVENT_CONTEXT_MENU_REQUEST } from './command/CommandUtils';

import {ConfigDatabase, injectConfigDatabase, ConfigKey, SESSION_CONFIG, SystemConfig, GENERAL_CONFIG, SYSTEM_CONFIG, GeneralConfig, ConfigChangeEvent} from '../Config';
import {ContextMenu} from './gui/ContextMenu';
import * as DomUtils from './DomUtils';
import {DropDown} from './gui/DropDown';
import {EmbeddedViewer} from './viewers/EmbeddedViewer';
import {ExtensionManagerImpl} from './extension/ExtensionManager';
import {ExtensionManager} from './extension/InternalTypes';
import {EVENT_DRAG_STARTED, EVENT_DRAG_ENDED} from './GeneralEvents';
import {Logger, getLogger} from "extraterm-logging";
import {MainWebUi} from './MainWebUi';
import {MenuItem} from './gui/MenuItem';
import {PopDownListPicker} from './gui/PopDownListPicker';
import {ResizeCanary} from './ResizeCanary';
import {SettingsTab} from './settings/SettingsTab';
import {TabWidget} from './gui/TabWidget';
import {EtTerminal} from './Terminal';
import {TerminalViewer} from './viewers/TerminalAceViewer';
import {TextViewer} from'./viewers/TextAceViewer';
import * as ThemeTypes from '../theme/Theme';
import * as ThemeConsumer from '../theme/ThemeConsumer';
import * as WebIpc from './WebIpc';
import * as Messages from '../WindowMessages';
import { EventEmitter } from '../utils/EventEmitter';
import { freezeDeep } from 'extraterm-readonly-toolbox';
import { log } from "extraterm-logging";
import { KeybindingsManager, injectKeybindingsManager, loadKeybindingsFromObject, TermKeybindingsMapping } from './keybindings/KeyBindingsManager';
import { trimBetweenTags } from 'extraterm-trim-between-tags';
import { ApplicationContextMenu } from "./command/ApplicationContextMenu";
import { registerCommands as TextCommandsRegisterCommands } from "./viewers/TextCommands";
import { DisposableHolder } from '../utils/DisposableUtils';
import { ExtensionCommandContribution, Category } from '../ExtensionMetadata';
import { EtViewerTab } from './ViewerTab';
import { isSupportsDialogStack } from './SupportsDialogStack';

type ThemeInfo = ThemeTypes.ThemeInfo;

SourceMapSupport.install();

const MENU_ITEM_DEVELOPER_TOOLS = 'developer_tools';
const ID_MAIN_MENU = "ID_MAIN_MENU";
const ID_MENU_BUTTON = "ID_MENU_BUTTON";
const CLASS_MAIN_DRAGGING = "CLASS_MAIN_DRAGGING";
const CLASS_MAIN_NOT_DRAGGING = "CLASS_MAIN_NOT_DRAGGING";

const _log = getLogger("mainweb");

/**
 * This module has control of the window and is responsible for
 * starting up the main component and handling the window directly.
 */

let keybindingsManager: KeybindingsManager = null;
let themes: ThemeInfo[];
let mainWebUi: MainWebUi = null;
let configDatabase: ConfigDatabaseImpl = null;
let extensionManager: ExtensionManager = null;
let commandPalette: CommandPalette = null;
let applicationContextMenu: ApplicationContextMenu = null;


export async function startUp(closeSplash: () => void): Promise<void> {
  ElectronMenu.setApplicationMenu(null);

  startUpTheming();
  startUpWebIpc();

  // Get the Config working.
  configDatabase = new ConfigDatabaseImpl();
  keybindingsManager = new KeybindingsManagerImpl();  // depends on the config.

  const configMsg = await WebIpc.requestConfig("*");
  handleConfigMessage(configMsg);
  
  const themeListMsg = await WebIpc.requestThemeList()
  handleThemeListMessage(themeListMsg);

  await loadFontFaces();

  const doc = window.document;
  doc.body.classList.add(CLASS_MAIN_NOT_DRAGGING);

  startUpExtensions();
  startUpMainWebUi();
  registerCommands(extensionManager);
  startUpSessions(configDatabase, extensionManager);

  closeSplash();

  startUpMainMenu();
  startUpCommandPalette();
  startUpApplicationContextMenu();
  startUpWindowEvents();
  startUpMenus();

  if (configDatabase.getConfig(SESSION_CONFIG).length !== 0) {
    mainWebUi.commandNewTerminal({ sessionUuid: configDatabase.getConfig(SESSION_CONFIG)[0].uuid });
  } else {
    mainWebUi.commandOpenSettingsTab("session");
    Electron.remote.dialog.showErrorBox("No session types available",
      "Extraterm doesn't have any session types configured.");
  }
  mainWebUi.focus();
  window.focus();
}

function startUpTheming(): void {
  // Theme control for the window level.
  const topThemeable: ThemeTypes.Themeable = {
    setThemeCssMap(cssMap: Map<ThemeTypes.CssFile, string>): void {      
      (<HTMLStyleElement> document.getElementById('THEME_STYLE')).textContent =
        cssMap.get(ThemeTypes.CssFile.GENERAL_GUI) + "\n" + 
        cssMap.get(ThemeTypes.CssFile.FONT_AWESOME) + "\n" + 
{'fraction_non_alphanumeric': 0.11838152459631994, 'fraction_numerical': 0.0011265490048817123, 'mean_word_length': 5.4878197320341044, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3418689', 'n_tokens_mistral': 3112, 'n_tokens_neox': 3044, 'n_words': 795}