-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #627 from tuanchauict/app-ui-state
Improve UI state manager
- Loading branch information
Showing
16 changed files
with
194 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
monosketch-svelte/src/lib/mono/ui-state-manager/app-ui-state-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { DEBUG_MODE } from "$mono/build_environment"; | ||
import { StorageDocument, StoreKeys } from "$mono/store-manager"; | ||
import type { UiStatePayloadType } from "$mono/ui-state-manager/ui-state-payload"; | ||
import { Flow, LifecycleOwner } from 'lib/libs/flow'; | ||
import { ScrollMode, type ThemeColor, type ThemeMode } from '$mono/ui-state-manager/states'; | ||
import { AppThemeManager } from '$mono/ui-state-manager/theme-manager'; | ||
import { ScrollModeManager } from '$mono/ui-state-manager/scroll-mode-manager'; | ||
import { PanelVisibilityManager } from '$mono/ui-state-manager/panel-visibility-manager'; | ||
import { type KeyCommand, KeyCommandController } from '$mono/keycommand'; | ||
|
||
/** | ||
* A domain class for managing UI state of the app. | ||
* This does not include the state of the workspace. | ||
*/ | ||
export class AppUiStateManager { | ||
private appThemeManager = new AppThemeManager(); | ||
private scrollModeManager = new ScrollModeManager(); | ||
private panelVisibilityManager = new PanelVisibilityManager(); | ||
private keyCommandController = new KeyCommandController(document.body); | ||
private settingDocument: StorageDocument = StorageDocument.get(StoreKeys.SETTINGS); | ||
|
||
themeModeFlow: Flow<ThemeMode> = this.appThemeManager.themeModeFlow; | ||
scrollModeFlow: Flow<ScrollMode> = this.scrollModeManager.scrollModeFlow; | ||
shapeFormatPanelVisibilityFlow: Flow<boolean> = | ||
this.panelVisibilityManager.shapeFormatPanelVisibilityFlow; | ||
keyCommandFlow: Flow<KeyCommand> = this.keyCommandController.keyCommandFlow; | ||
|
||
private fontSizeMutableFlow = new Flow<number>(parseInt(this.settingDocument.get(StoreKeys.FONT_SIZE, "13")!)); | ||
fontSizeFlow: Flow<number> = this.fontSizeMutableFlow.distinctUntilChanged(); | ||
|
||
/** | ||
* A flow that emits true when the font is ready to use. | ||
*/ | ||
private fontReadyMutableFlow = new Flow<boolean>(false); | ||
fontReadyFlow: Flow<boolean> = this.fontReadyMutableFlow.immutable(); | ||
|
||
constructor( | ||
private appLifecycleOwner: LifecycleOwner, | ||
) { | ||
const workspaceFont = new FontFace( | ||
'JetBrainsMono-Regular', | ||
`url('/src/assets/fonts/JetBrainsMono-Regular.woff2')`, | ||
); | ||
workspaceFont.load().then(() => { | ||
document.fonts.add(workspaceFont); | ||
this.fontReadyMutableFlow.value = true; | ||
if (DEBUG_MODE) { | ||
console.log('Font loaded'); | ||
} | ||
}); | ||
} | ||
|
||
observeTheme = (): void => { | ||
this.appThemeManager.observeTheme(this.appLifecycleOwner); | ||
}; | ||
|
||
getThemedColorCode = (color: ThemeColor): string => { | ||
return this.appThemeManager.getThemedColorCode(color); | ||
}; | ||
|
||
updateUiState = (payload: UiStatePayloadType): void => { | ||
switch (payload.type) { | ||
case 'ShapeToolVisibility': | ||
this.panelVisibilityManager.setShapeFormatPanelVisibility(payload.isVisible); | ||
break; | ||
case 'ChangeScrollMode': | ||
this.scrollModeManager.setScrollMode(payload.scrollMode); | ||
break; | ||
case 'ChangeTheme': | ||
this.appThemeManager.setTheme(payload.themeMode); | ||
break; | ||
case 'ChangeFontSize': | ||
this.changeFontSize(payload.isIncreased); | ||
break; | ||
} | ||
}; | ||
|
||
private changeFontSize = (isIncreased: boolean): void => { | ||
const offset = isIncreased ? 2 : -2; | ||
const currentFontSize = this.fontSizeMutableFlow.value!; | ||
const newFontSize = currentFontSize + offset; | ||
const coercedFontSize = Math.max(13, Math.min(25, newFontSize)); | ||
this.fontSizeMutableFlow.value = coercedFontSize; | ||
|
||
this.settingDocument.set(StoreKeys.FONT_SIZE, coercedFontSize.toString()); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
8 changes: 4 additions & 4 deletions
8
monosketch-svelte/src/lib/mono/ui-state-manager/panel-visibility-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Flow } from 'lib/libs/flow'; | ||
|
||
export class PanelVisibilityManager { | ||
private _shapeFormatPanelVisibilityFlow = new Flow<boolean>(true); | ||
shapeFormatPanelVisibilityFlow = this._shapeFormatPanelVisibilityFlow.immutable(); | ||
private shapeFormatPanelVisibilityMutableFlow = new Flow<boolean>(true); | ||
shapeFormatPanelVisibilityFlow = this.shapeFormatPanelVisibilityMutableFlow.immutable(); | ||
|
||
toggleShapeFormatPanelVisibility(): void { | ||
this._shapeFormatPanelVisibilityFlow.value = !this._shapeFormatPanelVisibilityFlow.value; | ||
setShapeFormatPanelVisibility(isVisible: boolean): void { | ||
this.shapeFormatPanelVisibilityMutableFlow.value = isVisible; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
monosketch-svelte/src/lib/mono/ui-state-manager/ui-state-payload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2024, tuanchauict | ||
*/ | ||
|
||
import { type ScrollMode, ThemeMode } from "$mono/ui-state-manager/states"; | ||
|
||
export type UiStatePayloadType = | ||
| { type: "ShapeToolVisibility", isVisible: boolean } | ||
| { type: "ChangeScrollMode", scrollMode: ScrollMode } | ||
| { type: "ChangeTheme", themeMode: ThemeMode } | ||
| { type: "ChangeFontSize", isIncreased: boolean } | ||
|
||
export const UiStatePayload = { | ||
ShapeToolVisibility: (isVisible: boolean): UiStatePayloadType => ({ type: "ShapeToolVisibility", isVisible }), | ||
ChangeScrollMode: (scrollMode: ScrollMode): UiStatePayloadType => ({ type: "ChangeScrollMode", scrollMode }), | ||
ChangeTheme: (themeMode: ThemeMode): UiStatePayloadType => ({ type: "ChangeTheme", themeMode }), | ||
ChangeFontSize: (isIncreased: boolean): UiStatePayloadType => ({ type: "ChangeFontSize", isIncreased }), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.