Skip to content
Merged
4 changes: 4 additions & 0 deletions app/src/lib/app-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { BranchesTab } from '../models/branches-tab'
import { PullRequest } from '../models/pull-request'
import { IAuthor } from '../models/author'
import { ComparisonCache } from './comparison-cache'
import { ApplicationTheme } from '../ui/lib/application-theme'

export { ICommitMessage }

Expand Down Expand Up @@ -186,6 +187,9 @@ export interface IAppState {

/** The currently selected tab for the Branches foldout. */
readonly selectedBranchesTab: BranchesTab

/** The currently selected appearance (aka theme) */
readonly selectedTheme: ApplicationTheme
}

export enum PopupType {
Expand Down
8 changes: 8 additions & 0 deletions app/src/lib/dispatcher/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { PullRequest } from '../../models/pull-request'
import { IAuthor } from '../../models/author'
import { ITrailer } from '../git/interpret-trailers'
import { isGitRepository } from '../git'
import { ApplicationTheme } from '../../ui/lib/application-theme'

/**
* An error handler function.
Expand Down Expand Up @@ -1213,4 +1214,11 @@ export class Dispatcher {
public recordCompareInitiatedMerge() {
return this.appStore._recordCompareInitiatedMerge()
}

/**
* Set the application-wide theme
*/
public setSelectedTheme(theme: ApplicationTheme) {
return this.appStore._setSelectedTheme(theme)
}
}
21 changes: 21 additions & 0 deletions app/src/lib/stores/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ import { IAuthor } from '../../models/author'
import { ComparisonCache } from '../comparison-cache'
import { AheadBehindUpdater } from './helpers/ahead-behind-updater'
import { enableCompareSidebar } from '../feature-flag'
import { ApplicationTheme, getThemeName } from '../../ui/lib/application-theme'

/**
* Enum used by fetch to determine if
Expand Down Expand Up @@ -168,6 +169,8 @@ const imageDiffTypeKey = 'image-diff-type'

const shellKey = 'shell'

const applicationThemeKey = 'theme'

// background fetching should not occur more than once every two minutes
const BackgroundFetchMinimumInterval = 2 * 60 * 1000

Expand Down Expand Up @@ -263,6 +266,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
private selectedCloneRepositoryTab = CloneRepositoryTab.DotCom

private selectedBranchesTab = BranchesTab.Branches
private selectedTheme = ApplicationTheme.Light

public constructor(
gitHubUserStore: GitHubUserStore,
Expand Down Expand Up @@ -598,6 +602,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
repositoryFilterText: this.repositoryFilterText,
selectedCloneRepositoryTab: this.selectedCloneRepositoryTab,
selectedBranchesTab: this.selectedBranchesTab,
selectedTheme: this.selectedTheme,
}
}

Expand Down Expand Up @@ -1310,6 +1315,11 @@ export class AppStore extends TypedBaseStore<IAppState> {
? imageDiffTypeDefault
: parseInt(imageDiffTypeValue)

this.selectedTheme =
localStorage.getItem(applicationThemeKey) === 'dark'
? ApplicationTheme.Dark
: ApplicationTheme.Light

this.emitUpdateNow()

this.accountsStore.refresh()
Expand Down Expand Up @@ -3665,6 +3675,17 @@ export class AppStore extends TypedBaseStore<IAppState> {
public _recordCompareInitiatedMerge() {
this.statsStore.recordCompareInitiatedMerge()
}

/**
* Set the application-wide theme
*/
public _setSelectedTheme(theme: ApplicationTheme) {
localStorage.setItem(applicationThemeKey, getThemeName(theme))
this.selectedTheme = theme
this.emitUpdate()

return Promise.resolve()
}
}

function forkPullRequestRemoteName(remoteName: string) {
Expand Down
5 changes: 3 additions & 2 deletions app/src/models/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum PreferencesTab {
Accounts = 0,
Git,
Advanced,
Git = 1,
Appearance = 2,
Advanced = 3,
}
59 changes: 59 additions & 0 deletions app/src/ui/app-theme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as React from 'react'
import { ApplicationTheme, getThemeName } from './lib/application-theme'

interface IAppThemeProps {
readonly theme: ApplicationTheme
}

/**
* A pseudo-component responsible for adding the applicable CSS
* class names to the body tag in order to apply the currently
* selected theme.
*
* This component is a PureComponent, meaning that it'll only
* render when its props changes (shallow comparison).
*
* This component does not render anything into the DOM, it's
* purely (a)busing the component lifecycle to manipulate the
* body class list.
*/
export class AppTheme extends React.PureComponent<IAppThemeProps> {
public componentDidMount() {
this.ensureTheme()
}

public componentDidUpdate() {
this.ensureTheme()
}

public componentWillUnmount() {
this.clearThemes()
}

private ensureTheme() {
const newThemeClassName = `theme-${getThemeName(this.props.theme)}`
const body = document.body

if (body.classList.contains(newThemeClassName)) {
return
}

this.clearThemes()

body.classList.add(newThemeClassName)
}

private clearThemes() {
const body = document.body

for (const className of body.classList) {
if (className.startsWith('theme-')) {
body.classList.remove(className)
}
}
}

public render() {
return null
}
}
8 changes: 8 additions & 0 deletions app/src/ui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ import { InitializeLFS, AttributeMismatch } from './lfs'
import { UpstreamAlreadyExists } from './upstream-already-exists'
import { DeletePullRequest } from './delete-branch/delete-pull-request-dialog'
import { MergeConflictsWarning } from './merge-conflicts'
import { AppTheme } from './app-theme'
import { ApplicationTheme } from './lib/application-theme'

/** The interval at which we should check for updates. */
const UpdateCheckInterval = 1000 * 60 * 60 * 4
Expand Down Expand Up @@ -1010,6 +1012,7 @@ export class App extends React.Component<IAppProps, IAppState> {
enterpriseAccount={this.getEnterpriseAccount()}
onDismissed={this.onPopupDismissed}
selectedShell={this.state.selectedShell}
selectedTheme={this.state.selectedTheme}
/>
)
case PopupType.MergeBranch: {
Expand Down Expand Up @@ -1706,8 +1709,13 @@ export class App extends React.Component<IAppProps, IAppState> {

const className = this.state.appIsFocused ? 'focused' : 'blurred'

const currentTheme = this.state.showWelcomeFlow
? ApplicationTheme.Light
: this.state.selectedTheme

return (
<div id="desktop-app-chrome" className={className}>
<AppTheme theme={currentTheme} />
{this.renderTitlebar()}
{this.state.showWelcomeFlow
? this.renderWelcomeFlow()
Expand Down
4 changes: 0 additions & 4 deletions app/src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ dispatcher.registerErrorHandler(missingRepositoryHandler)

document.body.classList.add(`platform-${process.platform}`)

if (localStorage.getItem('theme') === 'dark') {
document.body.classList.add(`theme-dark`)
}

dispatcher.setAppFocusState(remote.getCurrentWindow().isFocused())

ipcRenderer.on('focus', () => {
Expand Down
25 changes: 25 additions & 0 deletions app/src/ui/lib/application-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assertNever } from '../../lib/fatal-error'

/**
* A set of the user-selectable appearances (aka themes)
*/
export enum ApplicationTheme {
Light,
Dark,
}

/**
* Gets the friendly name of an application theme for use
* in persisting to storage and/or calculating the required
* body class name to set in order to apply the theme.
*/
export function getThemeName(theme: ApplicationTheme): string {
switch (theme) {
case ApplicationTheme.Light:
return 'light'
case ApplicationTheme.Dark:
return 'dark'
default:
return assertNever(theme, `Unknown theme ${theme}`)
}
}
49 changes: 49 additions & 0 deletions app/src/ui/preferences/appearance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react'
import { DialogContent } from '../dialog'
import {
VerticalSegmentedControl,
ISegmentedItem,
} from '../lib/vertical-segmented-control'
import { ApplicationTheme } from '../lib/application-theme'
import { fatalError } from '../../lib/fatal-error'

interface IAppearanceProps {
readonly selectedTheme: ApplicationTheme
readonly onSelectedThemeChanged: (theme: ApplicationTheme) => void
}

const themes: ReadonlyArray<ISegmentedItem> = [
{ title: 'Light', description: 'The default theme of GitHub Desktop' },
{
title: 'Dark (beta)',
description:
'A beta version of our dark theme. Still under development. Please report any issues you may find to our issue tracker.',
},
]

export class Appearance extends React.Component<IAppearanceProps, {}> {
private onSelectedThemeChanged = (index: number) => {
if (index === 0) {
this.props.onSelectedThemeChanged(ApplicationTheme.Light)
} else if (index === 1) {
this.props.onSelectedThemeChanged(ApplicationTheme.Dark)
} else {
fatalError(`Unknown theme index ${index}`)
}
}

public render() {
const selectedIndex =
this.props.selectedTheme === ApplicationTheme.Dark ? 1 : 0

return (
<DialogContent>
<VerticalSegmentedControl

This comment was marked as spam.

This comment was marked as spam.

items={themes}
selectedIndex={selectedIndex}
onSelectionChanged={this.onSelectedThemeChanged}
/>
</DialogContent>
)
}
}
16 changes: 16 additions & 0 deletions app/src/ui/preferences/preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { lookupPreferredEmail } from '../../lib/email'
import { Shell, getAvailableShells } from '../../lib/shells'
import { getAvailableEditors } from '../../lib/editors/lookup'
import { disallowedCharacters } from './identifier-rules'
import { Appearance } from './appearance'
import { ApplicationTheme } from '../lib/application-theme'

interface IPreferencesProps {
readonly dispatcher: Dispatcher
Expand All @@ -33,6 +35,7 @@ interface IPreferencesProps {
readonly confirmDiscardChanges: boolean
readonly selectedExternalEditor?: ExternalEditor
readonly selectedShell: Shell
readonly selectedTheme: ApplicationTheme
}

interface IPreferencesState {
Expand Down Expand Up @@ -134,6 +137,7 @@ export class Preferences extends React.Component<
>
<span>Accounts</span>
<span>Git</span>
<span>Appearance</span>
<span>Advanced</span>
</TabBar>

Expand Down Expand Up @@ -203,6 +207,13 @@ export class Preferences extends React.Component<
/>
)
}
case PreferencesTab.Appearance:
return (
<Appearance
selectedTheme={this.props.selectedTheme}
onSelectedThemeChanged={this.onSelectedThemeChanged}
/>
)
case PreferencesTab.Advanced: {
return (
<Advanced
Expand Down Expand Up @@ -269,12 +280,17 @@ export class Preferences extends React.Component<
this.setState({ selectedShell: shell })
}

private onSelectedThemeChanged = (theme: ApplicationTheme) => {
this.props.dispatcher.setSelectedTheme(theme)
}

private renderFooter() {
const hasDisabledError = this.state.disallowedCharactersMessage != null

const index = this.state.selectedIndex
switch (index) {
case PreferencesTab.Accounts:
case PreferencesTab.Appearance:
return null
case PreferencesTab.Advanced:
case PreferencesTab.Git: {
Expand Down