Skip to content

Commit

Permalink
Merge pull request #624 from tuanchauict/browser-js
Browse files Browse the repository at this point in the history
Integrate BrowserManager into AppContext
  • Loading branch information
tuanchauict authored Dec 13, 2024
2 parents 75dad5a + 3ff8214 commit d220596
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
25 changes: 25 additions & 0 deletions monosketch-svelte/src/app/app-context.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import { TODO } from "$libs/todo";
import { ShapeManager } from "$mono/shape/shape-manager";
import { WorkspaceDao } from "$mono/store-manager/dao/workspace-dao";
import { BrowserManager } from "$mono/window/browser-manager";
import { LifecycleOwner } from 'lib/libs/flow';
import { AppUiStateManager } from '$mono/ui-state-manager';

/**
* Main class of the app to handle all kinds of events, UI, actions, etc.
*/
export class AppContext {
appLifecycleOwner = new LifecycleOwner();

appUiStateManager = new AppUiStateManager(this.appLifecycleOwner);

shapeManager = new ShapeManager();

onStart = (): void => {
this.appLifecycleOwner.onStart();

this.init();

this.appUiStateManager.observeTheme(() => {
console.log('Theme changed');
// TODO: Update theme in the workspace
});
};

private init() {
const browserManager = new BrowserManager((url: string) => {
console.log('URL updated:', url);
// TODO: Update the workspace based on the URL
TODO("Update the workspace based on the URL");
});
browserManager.startObserveStateChange(
this.shapeManager.rootIdFlow,
this.appLifecycleOwner,
WorkspaceDao.instance);

// TODO: Replicate from MonoSketchApplication
}
}
2 changes: 2 additions & 0 deletions monosketch-svelte/src/lib/mono/window/window-viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import { Flow } from '$libs/flow';

export namespace WindowViewModel {
export const windowSizeUpdateEventFlow = new Flow<boolean>(true);

export const applicationActiveStateFlow = new Flow<boolean>(true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProjectAction, type ProjectItem } from '../model';
import { onDestroy, onMount } from 'svelte';
import { LifecycleOwner } from '$libs/flow';
import { projectDataViewModel } from '../viewmodel';
import { BrowserManager } from "$mono/window/browser-manager";
export let dismiss: () => void;
Expand Down Expand Up @@ -41,7 +42,8 @@ function onAction(item: ProjectItem, action: ProjectAction) {
dismiss();
break;
case ProjectAction.OpenInNewTab:
console.log('open in new tab');
// Need to delay for a short time the action to ensure the modal is closed before opening the new tab
setTimeout(() => BrowserManager.openInNewTab(item.id), 10);
dismiss();
break;
case ProjectAction.Remove:
Expand Down
14 changes: 13 additions & 1 deletion monosketch-svelte/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import '$assets/fonts/stylesheet.css';
import '$style/main.scss';
import App from './App.svelte';
import { AppContext } from '$app/app-context';
import { APP_CONTEXT } from '$mono/common/constant';
import { WindowViewModel } from '$mono/window/window-viewmodel';
import App from './App.svelte';

const appContext = new AppContext();
const context = new Map();
Expand All @@ -13,6 +13,18 @@ window.onresize = () => {
WindowViewModel.windowSizeUpdateEventFlow.value = true;
};

window.onfocus = () => {
WindowViewModel.applicationActiveStateFlow.value = true;
};

window.onblur = () => {
WindowViewModel.applicationActiveStateFlow.value = false;
};

document.addEventListener('visibilitychange', () => {
WindowViewModel.applicationActiveStateFlow.value = document['visibilityState'] === 'visible';
});

const app = new App({
// @ts-expect-error - Safe to ignore
target: document.getElementById('app'),
Expand Down

0 comments on commit d220596

Please sign in to comment.