Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
style: prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Apr 22, 2022
1 parent 89fa631 commit 0e58909
Show file tree
Hide file tree
Showing 31 changed files with 1,493 additions and 1,638 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Expand Up @@ -17,7 +17,6 @@
"ignorePatterns": ["test", "scripts", ".eslintrc", "tsconfig.json", "node_modules"],
"rules": {
/** Style */
"semi": 1,
"quotes": ["error", "single", { "avoidEscape": true }],

/** Preferences */
Expand Down
4 changes: 3 additions & 1 deletion .prettierrc
@@ -1,4 +1,6 @@
{
"singleQuote": true,
"printWidth": 100
"trailingComma": "all",
"printWidth": 120,
"semi": false
}
206 changes: 101 additions & 105 deletions app/application.ts
@@ -1,151 +1,147 @@
import { App, IpcMain, shell, Shell } from 'electron';
import { BackupsManager } from './javascripts/main/backupsManager';
import { createExtensionsServer } from './javascripts/main/extServer';
import { MenuManager } from './javascripts/main/menus';
import { isLinux, isMac, isWindows } from './javascripts/main/platforms';
import { Store, StoreKeys } from './javascripts/main/store';
import { AppName, initializeStrings } from './javascripts/main/strings';
import { createWindowState, WindowState } from './javascripts/main/window';
import { App, IpcMain, shell, Shell } from 'electron'
import { BackupsManager } from './javascripts/main/backupsManager'
import { createExtensionsServer } from './javascripts/main/extServer'
import { MenuManager } from './javascripts/main/menus'
import { isLinux, isMac, isWindows } from './javascripts/main/platforms'
import { Store, StoreKeys } from './javascripts/main/store'
import { AppName, initializeStrings } from './javascripts/main/strings'
import { createWindowState, WindowState } from './javascripts/main/window'
import {
getKeychainValue,
setKeychainValue,
clearKeychainValue,
ensureKeychainAccess,
} from './javascripts/main/keychain';
import { IpcMessages } from './javascripts/shared/ipcMessages';
import { isDev, isTesting } from './javascripts/main/utils';
import { Urls, Paths } from './javascripts/main/paths';
import { action, makeObservable, observable } from 'mobx';
import { UpdateState } from './javascripts/main/updateManager';
import { handleTestMessage } from './javascripts/main/testing';
import { MessageType } from '../test/TestIpcMessage';
} from './javascripts/main/keychain'
import { IpcMessages } from './javascripts/shared/ipcMessages'
import { isDev, isTesting } from './javascripts/main/utils'
import { Urls, Paths } from './javascripts/main/paths'
import { action, makeObservable, observable } from 'mobx'
import { UpdateState } from './javascripts/main/updateManager'
import { handleTestMessage } from './javascripts/main/testing'
import { MessageType } from '../test/TestIpcMessage'

const deepLinkScheme = 'standardnotes';
const deepLinkScheme = 'standardnotes'

export class AppState {
readonly version: string;
readonly store: Store;
readonly startUrl = Urls.indexHtml;
readonly isPrimaryInstance: boolean;
public willQuitApp = false;
public lastBackupDate: number | null = null;
public windowState?: WindowState;
public deepLinkUrl?: string;
public readonly updates: UpdateState;
readonly version: string
readonly store: Store
readonly startUrl = Urls.indexHtml
readonly isPrimaryInstance: boolean
public willQuitApp = false
public lastBackupDate: number | null = null
public windowState?: WindowState
public deepLinkUrl?: string
public readonly updates: UpdateState

constructor(app: Electron.App) {
this.version = app.getVersion();
this.store = new Store(Paths.userDataDir);
this.isPrimaryInstance = app.requestSingleInstanceLock();
this.version = app.getVersion()
this.store = new Store(Paths.userDataDir)
this.isPrimaryInstance = app.requestSingleInstanceLock()
makeObservable(this, {
lastBackupDate: observable,
setBackupCreationDate: action,
});
this.updates = new UpdateState(this);
})
this.updates = new UpdateState(this)

if (isTesting()) {
handleTestMessage(MessageType.AppStateCall, (method, ...args) => {
(this as any)[method](...args);
});
;(this as any)[method](...args)
})
}
}

setBackupCreationDate(date: number | null): void {
this.lastBackupDate = date;
this.lastBackupDate = date
}
}

export function initializeApplication(args: {
app: Electron.App;
ipcMain: Electron.IpcMain;
shell: Shell;
}): void {
const { app } = args;
export function initializeApplication(args: { app: Electron.App; ipcMain: Electron.IpcMain; shell: Shell }): void {
const { app } = args

app.name = AppName;
app.name = AppName

const state = new AppState(app);
setupDeepLinking(app);
registerSingleInstanceHandler(app, state);
const state = new AppState(app)
setupDeepLinking(app)
registerSingleInstanceHandler(app, state)
registerAppEventListeners({
...args,
state,
});
})

if (isDev()) {
/** Expose the app's state as a global variable. Useful for debugging */
(global as any).appState = state;
;(global as any).appState = state
}
}

function focusWindow(appState: AppState) {
const window = appState.windowState?.window;
const window = appState.windowState?.window

if (window) {
if (!window.isVisible()) {
window.show();
window.show()
}
if (window.isMinimized()) {
window.restore();
window.restore()
}
window.focus();
window.focus()
}
}

function registerSingleInstanceHandler(app: Electron.App, appState: AppState) {
app.on('second-instance', (_event: Event, argv: string[]) => {
if (isWindows()) {
appState.deepLinkUrl = argv.find((arg) => arg.startsWith(deepLinkScheme));
appState.deepLinkUrl = argv.find((arg) => arg.startsWith(deepLinkScheme))
}

/* Someone tried to run a second instance, we should focus our window. */
focusWindow(appState);
});
focusWindow(appState)
})
}

function registerAppEventListeners(args: {
app: Electron.App;
ipcMain: Electron.IpcMain;
shell: Shell;
state: AppState;
app: Electron.App
ipcMain: Electron.IpcMain
shell: Shell
state: AppState
}) {
const { app, state } = args;
const { app, state } = args

app.on('window-all-closed', () => {
if (!isMac()) {
app.quit();
app.quit()
}
});
})

app.on('before-quit', () => {
state.willQuitApp = true;
});
state.willQuitApp = true
})

app.on('activate', () => {
const windowState = state.windowState;
if (!windowState) return;
windowState.window.show();
});
const windowState = state.windowState
if (!windowState) return
windowState.window.show()
})

app.on('open-url', (_event, url) => {
state.deepLinkUrl = url;
focusWindow(state);
});
state.deepLinkUrl = url
focusWindow(state)
})

app.on('ready', () => {
if (!state.isPrimaryInstance) {
console.warn('Quiting app and focusing existing instance.');
app.quit();
return;
console.warn('Quiting app and focusing existing instance.')
app.quit()
return
}

finishApplicationInitialization(args);
});
finishApplicationInitialization(args)
})
}

async function setupDeepLinking(app: Electron.App) {
if (!app.isDefaultProtocolClient(deepLinkScheme)) {
app.setAsDefaultProtocolClient(deepLinkScheme);
app.setAsDefaultProtocolClient(deepLinkScheme)
}
}

Expand All @@ -155,68 +151,68 @@ async function finishApplicationInitialization({
shell,
state,
}: {
app: App;
ipcMain: IpcMain;
shell: Shell;
state: AppState;
app: App
ipcMain: IpcMain
shell: Shell
state: AppState
}) {
const keychainWindow = await ensureKeychainAccess(state.store);
const keychainWindow = await ensureKeychainAccess(state.store)

initializeStrings(app.getLocale());
initializeExtensionsServer(state.store);
initializeStrings(app.getLocale())
initializeExtensionsServer(state.store)
const windowState = await createWindowState({
shell,
appState: state,
appLocale: app.getLocale(),
teardown() {
state.windowState = undefined;
state.windowState = undefined
},
});
})

/**
* Close the keychain window after the main window is created, otherwise the
* app will quit automatically
*/
keychainWindow?.close();
keychainWindow?.close()

state.windowState = windowState;
registerIpcEventListeners(ipcMain, windowState.menuManager, windowState.backupsManager);
state.windowState = windowState
registerIpcEventListeners(ipcMain, windowState.menuManager, windowState.backupsManager)

if ((isWindows() || isLinux()) && state.windowState.trayManager.shouldMinimizeToTray()) {
state.windowState.trayManager.createTrayIcon();
state.windowState.trayManager.createTrayIcon()
}

windowState.window.loadURL(state.startUrl);
windowState.window.loadURL(state.startUrl)
}

function initializeExtensionsServer(store: Store) {
const host = createExtensionsServer();
store.set(StoreKeys.ExtServerHost, host);
const host = createExtensionsServer()
store.set(StoreKeys.ExtServerHost, host)
}

function registerIpcEventListeners(
ipcMain: Electron.IpcMain,
menuManager: MenuManager,
backupsManager: BackupsManager
backupsManager: BackupsManager,
) {
ipcMain.on(IpcMessages.DisplayAppMenu, () => {
menuManager.popupMenu();
});
menuManager.popupMenu()
})

ipcMain.on(IpcMessages.InitialDataLoaded, () => {
backupsManager.beginBackups();
});
backupsManager.beginBackups()
})

ipcMain.on(IpcMessages.MajorDataChange, () => {
backupsManager.performBackup();
});
backupsManager.performBackup()
})

ipcMain.handle(IpcMessages.GetKeychainValue, getKeychainValue);
ipcMain.handle(IpcMessages.SetKeychainValue, (_event, value) => setKeychainValue(value));
ipcMain.handle(IpcMessages.ClearKeychainValue, clearKeychainValue);
ipcMain.handle(IpcMessages.LocalBackupsCount, () => backupsManager.backupsCount());
ipcMain.handle(IpcMessages.GetKeychainValue, getKeychainValue)
ipcMain.handle(IpcMessages.SetKeychainValue, (_event, value) => setKeychainValue(value))
ipcMain.handle(IpcMessages.ClearKeychainValue, clearKeychainValue)
ipcMain.handle(IpcMessages.LocalBackupsCount, () => backupsManager.backupsCount())
ipcMain.on(IpcMessages.ViewLocalBackups, () => {
shell.openPath(backupsManager.backupsLocation);
});
ipcMain.handle(IpcMessages.DeleteLocalBackups, () => backupsManager.deleteBackups());
shell.openPath(backupsManager.backupsLocation)
})
ipcMain.handle(IpcMessages.DeleteLocalBackups, () => backupsManager.deleteBackups())
}

0 comments on commit 0e58909

Please sign in to comment.