Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
Fix UI loading bug on window reload
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmandKrynauw committed Jul 26, 2023
1 parent 1317560 commit 0701825
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/electron/lib/Blix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Blix {
private _pluginManager!: PluginManager;
private _mainWindow!: MainWindow;
private _aiManager!: AiManager;
private _isReady = false;

// private startTime: Date;

Expand Down Expand Up @@ -61,7 +62,7 @@ export class Blix {
this._projectManager = new ProjectManager(mainWindow);

this.initSubscribers();
mainWindow.apis.utilClientApi.onBlixReady();
this._isReady = true;

// testStuffies(this);

Expand Down Expand Up @@ -132,4 +133,8 @@ export class Blix {
get mainWindow(): MainWindow | null {
return this._mainWindow;
}

get isReady() {
return this._isReady;
}
}
2 changes: 1 addition & 1 deletion src/frontend/lib/api/apiInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function initAPIs() {
* Bind the main process APIs to the window.
* If a new main process API is created then add it to this method.
*/
async function bindMainApis() {
export async function bindMainApis() {
return {
utilApi: await bindMainApi<UtilApi>("UtilApi"),
projectApi: await bindMainApi<ProjectApi>("ProjectApi"),
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/lib/api/apis/UtilClientApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import type { ElectronWindowApi } from "electron-affinity/window";
import { toastStore, type ToastOptions } from "../../stores/ToastStore";
import { blixStore, setInitialStores } from "../../../lib/stores/BlixStore";
import { bindMainApis } from "../apiInitializer";

export class UtilClientApi implements ElectronWindowApi<UtilClientApi> {
showToast(options: Partial<ToastOptions>) {
toastStore.trigger(options);
}

/**
* Will be called when Blix first initializes and when window is reloaded.
*/
async onBlixReady() {
if (!window.apis) {
window.apis = await bindMainApis();
}
await setInitialStores();
blixStore.update((state) => ({ ...state, blixReady: true }));
}
Expand Down
4 changes: 0 additions & 4 deletions src/frontend/ui/base/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
<Toasts />
<ContextMenu />

<!-- <div class="flex h-screen w-screen items-center justify-center bg-zinc-700">
<ContextMenu />
</div> -->

<style lang="postcss" global>
@tailwind base;
@tailwind components;
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protocol.registerSchemesAsPrivileged([

let mainWindow: MainWindow | null = null;
let notification: Notification | null = null;
let blix: Blix;
let blix: Blix | null = null;

/**
* Will run when Electron has finished initializing. 1. Blix is instantiated
Expand All @@ -62,8 +62,11 @@ app.on("ready", async () => {
exposeMainApis(blix);

createMainWindow().then(async () => {
if (mainWindow) {
if (mainWindow && blix) {
await blix.init(mainWindow);
if (blix.isReady) {
mainWindow.apis.utilClientApi.onBlixReady();
}
} else {
app.quit();
}
Expand Down Expand Up @@ -107,6 +110,12 @@ async function createMainWindow() {
mainWindow.on("closed", () => {
mainWindow = null;
});

mainWindow.on("ready-to-show", () => {
if (blix?.isReady) {
mainWindow?.apis.utilClientApi.onBlixReady();
}
});
}

// those two events are completely optional to subscrbe to, but that's a common way to get the
Expand Down

0 comments on commit 0701825

Please sign in to comment.