Skip to content

Commit

Permalink
Merge pull request desktop#14744 from desktop/remember-zoom-level-on-…
Browse files Browse the repository at this point in the history
…windows

Remember zoom level on update
  • Loading branch information
tidy-dev authored Jun 8, 2022
2 parents 0875026 + 43683c3 commit f36a893
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/lib/ipc-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type RequestChannels = {
'set-native-theme-source': (themeName: ThemeSource) => void
'focus-window': () => void
'notification-event': NotificationCallback<DesktopAliveEvent>
'set-window-zoom-factor': (zoomFactor: number) => void
}

/**
Expand Down
30 changes: 29 additions & 1 deletion app/src/lib/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function setBoolean(key: string, value: boolean) {
}

/**
* Retrieve a `number` value from a given local storage entry if found, or the
* Retrieve a integer number value from a given local storage entry if found, or the
* provided `defaultValue` if the key doesn't exist or if the value cannot be
* converted into a number
*
Expand All @@ -77,6 +77,34 @@ export function getNumber(
return value
}

/**
* Retrieve a floating point number value from a given local storage entry if
* found, or the provided `defaultValue` if the key doesn't exist or if the
* value cannot be converted into a number
*
* @param key local storage entry to read
* @param defaultValue fallback value if unable to find key or valid value
*/
export function getFloatNumber(key: string): number | undefined
export function getFloatNumber(key: string, defaultValue: number): number
export function getFloatNumber(
key: string,
defaultValue?: number
): number | undefined {
const numberAsText = localStorage.getItem(key)

if (numberAsText === null || numberAsText.length === 0) {
return defaultValue
}

const value = parseFloat(numberAsText)
if (isNaN(value)) {
return defaultValue
}

return value
}

/**
* Set the provided key in local storage to a numeric value, or update the
* existing value if a key is already defined.
Expand Down
33 changes: 32 additions & 1 deletion app/src/lib/stores/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
getCurrentWindowZoomFactor,
updatePreferredAppMenuItemLabels,
updateAccounts,
setWindowZoomFactor,
} from '../../ui/main-process-proxy'
import {
API,
Expand Down Expand Up @@ -205,6 +206,7 @@ import {
getEnum,
getObject,
setObject,
getFloatNumber,
} from '../local-storage'
import { ExternalEditorError, suggestedExternalEditor } from '../editors/shared'
import { ApiRepositoriesStore } from './api-repositories-store'
Expand Down Expand Up @@ -575,13 +577,41 @@ export class AppStore extends TypedBaseStore<IAppState> {
}

private initializeZoomFactor = async () => {
const zoomFactor = await getCurrentWindowZoomFactor()
const zoomFactor = await this.getWindowZoomFactor()
if (zoomFactor === undefined) {
return
}
this.onWindowZoomFactorChanged(zoomFactor)
}

/**
* On Windows OS, whenever a user toggles their zoom factor, chromium stores it
* in their `%AppData%/Roaming/GitHub Desktop/Preferences.js` denoted by the
* file path to the application. That file path contains the apps version.
* Thus, on every update, the users set zoom level gets reset as there is not
* defined value for the current app version.
* */
private async getWindowZoomFactor() {
const zoomFactor = await getCurrentWindowZoomFactor()
// One is the default value, we only care about checking the locally stored
// value if it is one because that is the default value after an
// update
if (zoomFactor !== 1 || !__WIN32__) {
return zoomFactor
}

const locallyStoredZoomFactor = getFloatNumber('zoom-factor')
if (
locallyStoredZoomFactor !== undefined &&
locallyStoredZoomFactor !== zoomFactor
) {
setWindowZoomFactor(locallyStoredZoomFactor)
return locallyStoredZoomFactor
}

return zoomFactor
}

private onTokenInvalidated = (endpoint: string) => {
const account = getAccountForEndpoint(this.accounts, endpoint)

Expand Down Expand Up @@ -804,6 +834,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
this.windowZoomFactor = zoomFactor

if (zoomFactor !== current) {
setNumber('zoom-factor', zoomFactor)
this.updateResizableConstraints()
this.emitUpdate()
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main-process/app-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ export class AppWindow {
return this.window.webContents.zoomFactor
}

public setWindowZoomFactor(zoomFactor: number) {
this.window.webContents.zoomFactor = zoomFactor
}

/**
* Method to show the save dialog and return the first file path it returns.
*/
Expand Down
4 changes: 4 additions & 0 deletions app/src/main-process/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ app.on('ready', () => {
mainWindow?.getCurrentWindowZoomFactor()
)

ipcMain.on('set-window-zoom-factor', (_, zoomFactor: number) =>
mainWindow?.setWindowZoomFactor(zoomFactor)
)

/**
* An event sent by the renderer asking for a copy of the current
* application menu.
Expand Down
3 changes: 3 additions & 0 deletions app/src/ui/main-process-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export const getCurrentWindowZoomFactor = invokeProxy(
0
)

/** Tell the main process to set the current window's zoom factor */
export const setWindowZoomFactor = sendProxy('set-window-zoom-factor', 1)

/** Tell the main process to check for app updates */
export const checkForUpdates = invokeProxy('check-for-updates', 1)

Expand Down

0 comments on commit f36a893

Please sign in to comment.