Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep panes settings after reload #2888

Merged
merged 8 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/services/src/Domain/Preferences/LocalPrefKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EditorFontSize, EditorLineHeight, EditorLineWidth } from '@standardnotes/models'
import { NativeFeatureIdentifier } from '@standardnotes/features'

export enum LocalPrefKey {
ListPaneCollapsed = 'listPaneCollapsed',
NavigationPaneCollapsed = 'navigationPaneCollapsed',
ActiveThemes = 'activeThemes',
UseSystemColorScheme = 'useSystemColorScheme',
UseTranslucentUI = 'useTranslucentUI',
Expand All @@ -14,6 +17,8 @@ export enum LocalPrefKey {
}

export type LocalPrefValue = {
[LocalPrefKey.ListPaneCollapsed]: boolean
[LocalPrefKey.NavigationPaneCollapsed]: boolean
[LocalPrefKey.ActiveThemes]: string[]
[LocalPrefKey.UseSystemColorScheme]: boolean
[LocalPrefKey.UseTranslucentUI]: boolean
Expand All @@ -25,3 +30,20 @@ export type LocalPrefValue = {
[LocalPrefKey.EditorLineWidth]: EditorLineWidth
[LocalPrefKey.EditorFontSize]: EditorFontSize
}

export const LocalPrefDefaults = {
[LocalPrefKey.ListPaneCollapsed]: false,
[LocalPrefKey.NavigationPaneCollapsed]: false,
[LocalPrefKey.ActiveThemes]: [],
[LocalPrefKey.UseSystemColorScheme]: false,
[LocalPrefKey.UseTranslucentUI]: true,
[LocalPrefKey.AutoLightThemeIdentifier]: 'Default',
[LocalPrefKey.AutoDarkThemeIdentifier]: NativeFeatureIdentifier.TYPES.DarkTheme,

[LocalPrefKey.EditorMonospaceEnabled]: false,
[LocalPrefKey.EditorLineHeight]: EditorLineHeight.Normal,
[LocalPrefKey.EditorLineWidth]: EditorLineWidth.FullWidth,
[LocalPrefKey.EditorFontSize]: EditorFontSize.Normal,
} satisfies {
[key in LocalPrefKey]: LocalPrefValue[key]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { PanesForLayout } from './../../Application/UseCase/PanesForLayout'
import {
InternalEventHandlerInterface,
InternalEventInterface,
LocalPrefDefaults,
LocalPrefKey,
PreferenceServiceInterface,
} from '@standardnotes/services'
import {
Expand Down Expand Up @@ -42,8 +44,14 @@ export class PaneController extends AbstractViewController implements InternalEv
currentItemsPanelWidth = 0
focusModeEnabled = false

listPaneExplicitelyCollapsed = false
navigationPaneExplicitelyCollapsed = false
listPaneExplicitelyCollapsed = this.preferences.getLocalValue(
LocalPrefKey.ListPaneCollapsed,
LocalPrefDefaults[LocalPrefKey.ListPaneCollapsed],
)
navigationPaneExplicitelyCollapsed = this.preferences.getLocalValue(
LocalPrefKey.NavigationPaneCollapsed,
LocalPrefDefaults[LocalPrefKey.NavigationPaneCollapsed],
)

constructor(
private preferences: PreferenceServiceInterface,
Expand Down Expand Up @@ -84,11 +92,6 @@ export class PaneController extends AbstractViewController implements InternalEv
this.setCurrentNavPanelWidth(preferences.getValue(PrefKey.TagsPanelWidth, MinimumNavPanelWidth))
this.setCurrentItemsPanelWidth(preferences.getValue(PrefKey.NotesPanelWidth, MinimumNotesPanelWidth))

const screen = this._isTabletOrMobileScreen.execute().getValue()

this.panes = screen.isTabletOrMobile
? [AppPaneId.Navigation, AppPaneId.Items]
: [AppPaneId.Navigation, AppPaneId.Items, AppPaneId.Editor]

const mediaQuery = window.matchMedia(MediaQueryBreakpoints.md)
if (mediaQuery?.addEventListener != undefined) {
Expand All @@ -98,6 +101,7 @@ export class PaneController extends AbstractViewController implements InternalEv
}

eventBus.addEventHandler(this, ApplicationEvent.PreferencesChanged)
eventBus.addEventHandler(this, ApplicationEvent.LocalPreferencesChanged)

this.disposers.push(
keyboardService.addCommandHandler({
Expand Down Expand Up @@ -136,6 +140,30 @@ export class PaneController extends AbstractViewController implements InternalEv
this.setCurrentNavPanelWidth(this.preferences.getValue(PrefKey.TagsPanelWidth, MinimumNavPanelWidth))
this.setCurrentItemsPanelWidth(this.preferences.getValue(PrefKey.NotesPanelWidth, MinimumNotesPanelWidth))
}
if (event.type === ApplicationEvent.LocalPreferencesChanged) {
this.listPaneExplicitelyCollapsed = this.preferences.getLocalValue(
LocalPrefKey.ListPaneCollapsed,
LocalPrefDefaults[LocalPrefKey.ListPaneCollapsed],
)
this.navigationPaneExplicitelyCollapsed = this.preferences.getLocalValue(
LocalPrefKey.NavigationPaneCollapsed,
LocalPrefDefaults[LocalPrefKey.NavigationPaneCollapsed],
)
const screen = this._isTabletOrMobileScreen.execute().getValue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will now run after every setLocalValue call, no? i think it makes more sense to have a boolean in the class to make sure this only runs once since its supposed to be initialization logic.

if (screen.isTabletOrMobile) {
this.panes = [AppPaneId.Navigation, AppPaneId.Items]
} else {
if (!this.listPaneExplicitelyCollapsed && !this.navigationPaneExplicitelyCollapsed) {
this.panes = [AppPaneId.Navigation, AppPaneId.Items, AppPaneId.Editor]
} else if (this.listPaneExplicitelyCollapsed && this.navigationPaneExplicitelyCollapsed) {
this.panes = [AppPaneId.Editor]
} else if (this.listPaneExplicitelyCollapsed) {
this.panes = [AppPaneId.Navigation, AppPaneId.Editor]
} else {
this.panes = [AppPaneId.Items, AppPaneId.Editor]
}
}
}
}

setCurrentNavPanelWidth(width: number) {
Expand Down Expand Up @@ -250,24 +278,24 @@ export class PaneController extends AbstractViewController implements InternalEv
toggleListPane = () => {
if (this.panes.includes(AppPaneId.Items)) {
this.removePane(AppPaneId.Items)
this.listPaneExplicitelyCollapsed = true
this.preferences.setLocalValue(LocalPrefKey.ListPaneCollapsed, true)
} else {
if (this.panes.includes(AppPaneId.Navigation)) {
this.insertPaneAtIndex(AppPaneId.Items, 1)
} else {
this.insertPaneAtIndex(AppPaneId.Items, 0)
}
this.listPaneExplicitelyCollapsed = false
this.preferences.setLocalValue(LocalPrefKey.ListPaneCollapsed, false)
}
}

toggleNavigationPane = () => {
if (this.panes.includes(AppPaneId.Navigation)) {
this.removePane(AppPaneId.Navigation)
this.navigationPaneExplicitelyCollapsed = true
this.preferences.setLocalValue(LocalPrefKey.NavigationPaneCollapsed, true)
} else {
this.insertPaneAtIndex(AppPaneId.Navigation, 0)
this.navigationPaneExplicitelyCollapsed = false
this.preferences.setLocalValue(LocalPrefKey.NavigationPaneCollapsed, false)
}
}

Expand Down
13 changes: 10 additions & 3 deletions packages/web/src/javascripts/Hooks/usePreference.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { useApplication } from '@/Components/ApplicationProvider'
import { ApplicationEvent, PrefKey, PrefDefaults, LocalPrefKey, LocalPrefValue } from '@standardnotes/snjs'
import {
ApplicationEvent,
PrefKey,
PrefDefaults,
LocalPrefKey,
LocalPrefValue,
LocalPrefDefaults,
} from '@standardnotes/snjs'
import { useCallback, useEffect, useState } from 'react'

export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {
const application = useApplication()

const [value, setValue] = useState(application.preferences.getLocalValue(preference, PrefDefaults[preference]))
const [value, setValue] = useState(application.preferences.getLocalValue(preference, LocalPrefDefaults[preference]))

const setNewValue = useCallback(
(newValue: LocalPrefValue[Key]) => {
Expand All @@ -16,7 +23,7 @@ export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {

useEffect(() => {
return application.addEventObserver(async () => {
const latestValue = application.preferences.getLocalValue(preference, PrefDefaults[preference])
const latestValue = application.preferences.getLocalValue(preference, LocalPrefDefaults[preference])

setValue(latestValue)
}, ApplicationEvent.LocalPreferencesChanged)
Expand Down