Skip to content

Commit

Permalink
rework updates
Browse files Browse the repository at this point in the history
  • Loading branch information
timche committed Mar 20, 2021
1 parent 884b5ea commit 88f3647
Show file tree
Hide file tree
Showing 16 changed files with 699 additions and 126 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"storybook": "start-storybook"
},
"dependencies": {
"builder-util-runtime": "^8.7.3",
"darkreader": "^4.9.27",
"electron-context-menu": "^2.5.0",
"electron-dl": "^3.2.0",
Expand All @@ -57,11 +58,13 @@
"babel-loader": "^8.2.2",
"cross-env": "^7.0.3",
"del-cli": "^3.0.1",
"domhandler": "^4.0.0",
"electron": "^12.0.0",
"electron-builder": "^22.10.5",
"electron-notarize": "^1.0.0",
"esbuild": "^0.9.2",
"framer-motion": "^3.10.3",
"html-react-parser": "^1.2.4",
"husky": "^5.1.3",
"lint-staged": "^10.5.4",
"nodemon": "^2.0.7",
Expand Down
63 changes: 18 additions & 45 deletions src-main/account-views/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import * as path from 'path'
import {
BrowserView,
BrowserWindow,
app,
dialog,
shell,
session
} from 'electron'
import config, { ConfigKey } from '../config'
import { BrowserView, BrowserWindow, app, dialog, session } from 'electron'
import { addCustomCSS, initCustomStyles } from './custom-styles'
import { enableAutoFixUserAgent } from '../user-agent'
import { cleanURLFromGoogle } from '../utils'
import { getMainWindow, sendToMainWindow } from '../main-window'
import { getSelectedAccount, selectAccount } from '../accounts'
import { ACCOUNTS_TAB_HEIGHT, GMAIL_URL } from '../constants'
import { ACCOUNTS_TAB_HEIGHT, GMAIL_URL, BANNER_HEIGHT } from '../constants'
import { is } from 'electron-util'
import { addContextMenu } from './context-menu'
import { getSessionPartitionKey } from './helpers'
import { getIsUpdateAvailable } from '../updates'
import { openExternalUrl } from '../helpers'

const accountViews = new Map<string, BrowserView>()

Expand Down Expand Up @@ -75,14 +68,25 @@ export function updateAllAccountViewBounds() {
}

export function updateAccountViewBounds(accountView: BrowserView) {
const shouldAccountViewOffset = getShouldAccountViewOffset()
const hasMultipleAccounts = accountViews.size > 1
const isUpdateAvailable = getIsUpdateAvailable()
const { width, height } = getMainWindow().getBounds()

let offset = 0

if (hasMultipleAccounts) {
offset += ACCOUNTS_TAB_HEIGHT
}

if (isUpdateAvailable) {
offset += BANNER_HEIGHT
}

accountView.setBounds({
x: 0,
y: shouldAccountViewOffset ? ACCOUNTS_TAB_HEIGHT : 0,
y: hasMultipleAccounts ? offset : 0,
width,
height: shouldAccountViewOffset ? height - ACCOUNTS_TAB_HEIGHT : height
height: hasMultipleAccounts ? height - offset : height
})
}

Expand Down Expand Up @@ -125,37 +129,6 @@ export function showAccountViews() {
}
}

export function getShouldAccountViewOffset() {
return accountViews.size > 1
}

async function openExternalUrl(url: string): Promise<void> {
const cleanURL = cleanURLFromGoogle(url)

if (config.get(ConfigKey.ConfirmExternalLinks)) {
const { origin } = new URL(cleanURL)
const trustedHosts = config.get(ConfigKey.TrustedHosts)

if (!trustedHosts.includes(origin)) {
const { response, checkboxChecked } = await dialog.showMessageBox({
type: 'info',
buttons: ['Open Link', 'Cancel'],
message: `Do you want to open this external link in your default browser?`,
checkboxLabel: `Trust all links on ${origin}`,
detail: cleanURL
})

if (response !== 0) return

if (checkboxChecked) {
config.set(ConfigKey.TrustedHosts, [...trustedHosts, origin])
}
}
}

shell.openExternal(cleanURL)
}

export function getSelectedAccountView() {
const selectedAccount = getSelectedAccount()
if (!selectedAccount) {
Expand Down
24 changes: 18 additions & 6 deletions src-main/app-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from 'electron'
import * as fs from 'fs'
import { is } from 'electron-util'
import { checkForUpdates, changeReleaseChannel } from './updates'
import {
manuallyCheckForUpdates,
changeReleaseChannel,
setAutoUpdateCheck
} from './updates'
import config, { ConfigKey } from './config'
import {
setCustomStyle,
Expand Down Expand Up @@ -100,7 +104,7 @@ export function initOrUpdateAppMenu() {
{
label: 'Check for Updates...',
click() {
checkForUpdates()
manuallyCheckForUpdates()
}
},
{
Expand Down Expand Up @@ -338,12 +342,20 @@ export function initOrUpdateAppMenu() {
label: 'Updates',
submenu: [
{
label: 'Auto Update',
label: 'Check For Updates Automatically',
type: 'checkbox',
checked: config.get(ConfigKey.AutoUpdate),
checked: config.get(ConfigKey.AutoUpdateCheck),
click({ checked }: { checked: boolean }) {
config.set(ConfigKey.AutoUpdate, checked)
showRestartDialog()
setAutoUpdateCheck(checked)
config.set(ConfigKey.AutoUpdateCheck, checked)
}
},
{
label: 'Notify When Update Downloaded',
type: 'checkbox',
checked: config.get(ConfigKey.NotifyUpdateDownloaded),
click({ checked }: { checked: boolean }) {
config.set(ConfigKey.NotifyUpdateDownloaded, checked)
}
},
{
Expand Down
4 changes: 4 additions & 0 deletions src-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function getIsQuitting() {
return isQuitting
}

export function setIsQuitting(quit: boolean) {
isQuitting = quit
}

if (!app.requestSingleInstanceLock()) {
app.quit()
}
Expand Down
9 changes: 6 additions & 3 deletions src-main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export interface Account {
}

export enum ConfigKey {
AutoUpdate = 'autoUpdate',
AutoUpdateCheck = 'autoUpdateCheck',
NotifyUpdateDownloaded = 'notifyUpdateDownloaded',
CompactHeader = 'compactHeader',
HideFooter = 'hideFooter',
HideSupport = 'hideSupport',
Expand All @@ -47,7 +48,8 @@ export enum ConfigKey {
}

type TypedStore = {
[ConfigKey.AutoUpdate]: boolean
[ConfigKey.AutoUpdateCheck]: boolean
[ConfigKey.NotifyUpdateDownloaded]: boolean
[ConfigKey.LastWindowState]: LastWindowState
[ConfigKey.CompactHeader]: boolean
[ConfigKey.HideFooter]: boolean
Expand All @@ -72,7 +74,8 @@ type TypedStore = {
}

const defaults: TypedStore = {
[ConfigKey.AutoUpdate]: true,
[ConfigKey.AutoUpdateCheck]: true,
[ConfigKey.NotifyUpdateDownloaded]: true,
[ConfigKey.LastWindowState]: {
bounds: {
width: 860,
Expand Down
2 changes: 2 additions & 0 deletions src-main/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const ACCOUNTS_TAB_HEIGHT = 40

export const BANNER_HEIGHT = 32

export const GMAIL_URL = 'https://mail.google.com'

export const DEFAULT_ACCOUNT_ID = 'default'
30 changes: 29 additions & 1 deletion src-main/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { app } from 'electron'
import { app, dialog, shell } from 'electron'
import { platform as selectPlatform } from 'electron-util'
import config, { ConfigKey } from './config'
import { cleanURLFromGoogle } from './utils'

export const platform: 'macos' | 'linux' | 'windows' = selectPlatform({
macos: 'macos',
Expand All @@ -11,3 +12,30 @@ export const platform: 'macos' | 'linux' | 'windows' = selectPlatform({
export const shouldStartMinimized = () =>
app.commandLine.hasSwitch('launch-minimized') ||
config.get(ConfigKey.LaunchMinimized)

export async function openExternalUrl(url: string): Promise<void> {
const cleanURL = cleanURLFromGoogle(url)

if (config.get(ConfigKey.ConfirmExternalLinks)) {
const { origin } = new URL(cleanURL)
const trustedHosts = config.get(ConfigKey.TrustedHosts)

if (!trustedHosts.includes(origin)) {
const { response, checkboxChecked } = await dialog.showMessageBox({
type: 'info',
buttons: ['Open Link', 'Cancel'],
message: `Do you want to open this external link in your default browser?`,
checkboxLabel: `Trust all links on ${origin}`,
detail: cleanURL
})

if (response !== 0) return

if (checkboxChecked) {
config.set(ConfigKey.TrustedHosts, [...trustedHosts, origin])
}
}
}

shell.openExternal(cleanURL)
}
7 changes: 6 additions & 1 deletion src-main/main-window/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { toggleAppVisiblityTrayItem } from '../tray'
import { setAppMenuBarVisibility } from '../utils'
import { getAccountView } from '../account-views'
import { getIsQuitting } from '../app'
import { shouldStartMinimized } from '../helpers'
import { openExternalUrl, shouldStartMinimized } from '../helpers'

let mainWindow: BrowserWindow

Expand Down Expand Up @@ -106,4 +106,9 @@ export function createMainWindow(): void {
mainWindow.show()
}
})

mainWindow.webContents.on('will-navigate', (event, url) => {
event.preventDefault()
openExternalUrl(url)
})
}
Loading

0 comments on commit 88f3647

Please sign in to comment.