Skip to content

Commit

Permalink
feat: add status bar icon
Browse files Browse the repository at this point in the history
  • Loading branch information
muuvmuuv committed Jul 7, 2023
1 parent ac10083 commit 0132ede
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 25 deletions.
13 changes: 3 additions & 10 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ const log = getLogger('editor')

dayjs.extend(customParseFormat)

export interface EditorConfig {
sundial: SundialConfiguration
workbench: WorkspaceConfiguration
}

export function getConfig(): EditorConfig {
const sundial = workspace.getConfiguration('sundial') as SundialConfiguration
const workbench = workspace.getConfiguration('workbench')
export function getConfig() {
return {
sundial,
workbench,
sundial: workspace.getConfiguration('sundial') as SundialConfiguration,
workbench: workspace.getConfiguration('workbench'),
}
}

Expand Down
25 changes: 11 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
workspace,
} from 'vscode'

import { TimeNames, toggleTheme as editorToggleTheme } from './editor'
import { TimeNames } from './editor'
import { outputChannel } from './logger'
import Sundial from './sundial'

Expand All @@ -16,17 +16,12 @@ function check() {
sundial.check()
}

function toggleTheme(time?: TimeNames) {
sundial.disableExtension()
editorToggleTheme(time)
}

function configChanged(event: ConfigurationChangeEvent) {
const sundialConfig = event.affectsConfiguration('sundial')
const darkColorTheme = event.affectsConfiguration('workbench.preferredDarkColorTheme')
const lightColorTheme = event.affectsConfiguration('workbench.preferredDarkColorTheme')

if (sundialConfig || darkColorTheme || lightColorTheme) {
if (
event.affectsConfiguration('sundial') ||
event.affectsConfiguration('workbench.preferredDarkColorTheme') ||
event.affectsConfiguration('workbench.preferredDarkColorTheme')
) {
check()
}
}
Expand All @@ -48,10 +43,12 @@ export function activate(context: ExtensionContext): void {
)

commands.registerCommand('sundial.switchToNightTheme', () =>
toggleTheme(TimeNames.NIGHT),
sundial.toggleTheme(TimeNames.NIGHT),
)
commands.registerCommand('sundial.switchToDayTheme', () =>
sundial.toggleTheme(TimeNames.DAY),
)
commands.registerCommand('sundial.switchToDayTheme', () => toggleTheme(TimeNames.DAY))
commands.registerCommand('sundial.toggleDayNightTheme', () => toggleTheme())
commands.registerCommand('sundial.toggleDayNightTheme', () => sundial.toggleTheme())

commands.registerCommand('sundial.enableExtension', () => sundial.enableExtension())
commands.registerCommand('sundial.disableExtension', () => sundial.disableExtension())
Expand Down
79 changes: 78 additions & 1 deletion src/sundial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import dayjs from 'dayjs'
import { ExtensionContext, WorkspaceConfiguration } from 'vscode'
import {
ExtensionContext,
StatusBarAlignment,
StatusBarItem,
window,
WorkspaceConfiguration,
} from 'vscode'

import * as editor from './editor'
import { getLogger, LogLevel, setLogLevelAll } from './logger'
Expand All @@ -23,23 +29,29 @@ export interface SundialConfiguration extends WorkspaceConfiguration {
nightVariable: number
daySettings: WorkspaceConfiguration
nightSettings: WorkspaceConfiguration
statusBarItemPriority: number
interval: number
debug: LogLevel
}

export default class Sundial {
static readonly extensionName = 'Sundial'
static readonly extensionAlias = 'sundial'

static extensionContext: ExtensionContext

private isRunning = false
private nextCircle?: editor.TimeNames
private checkInterval!: NodeJS.Timer
private statusBarItem?: StatusBarItem

get enabled(): boolean {
return Sundial.extensionContext.globalState.get(STATE_ENABLED, true)
}

/**
* Enable the automation and checks.
*/
enableExtension(): void {
const log = getLogger('enableExtension')
log.info('Enabling Sundial')
Expand All @@ -49,13 +61,20 @@ export default class Sundial {
this.check()
}

/**
* Disable the extension automation and checks.
*/
disableExtension(): void {
const log = getLogger('disableExtension')
log.info('Disabling Sundial')
Sundial.extensionContext.globalState.update(STATE_ENABLED, false)
this.killAutomator()
this.statusBarItem?.dispose()
}

/**
* Pause automated checks until next time circle.
*/
async pauseUntilNextCircle(): Promise<void> {
const log = getLogger('pauseUntilNextCircle')
const currentTime = await this.getCurrentTime()
Expand All @@ -64,6 +83,9 @@ export default class Sundial {
log.info(`Waiting until it becomes ${this.nextCircle} again...`)
}

/**
* Create and start the automator interval.
*/
automator(): void {
if (!this.enabled) {
this.killAutomator()
Expand All @@ -83,10 +105,16 @@ export default class Sundial {
}, interval)
}

/**
* Kill the automator interval.
*/
killAutomator(): void {
clearInterval(this.checkInterval)
}

/**
* Main check. Will change theme if needed.
*/
async check(): Promise<void> {
if (!this.enabled || this.isRunning) {
return // disabled or already running
Expand Down Expand Up @@ -120,11 +148,51 @@ export default class Sundial {
}
}

this.setStatusIconToggle()

await sleep(400) // Short nap 😴

this.isRunning = false
this.automator()
}

/**
* Toggle the theme and disable the extension. So no automation
* will be done until you enable it again.
*
* @see editor.toggleTheme
*/
toggleTheme(time?: editor.TimeNames): void {
this.disableExtension()
editor.toggleTheme(time)
}

/**
* Set the status bar icon to toggle the theme.
*/
private setStatusIconToggle(): void {
this.statusBarItem?.dispose()

if (!this.statusBarItem) {
const { sundial } = editor.getConfig()

this.statusBarItem = window.createStatusBarItem(
StatusBarAlignment.Right,
sundial.statusBarItemPriority,
)
this.statusBarItem.command = 'sundial.toggleDayNightTheme'
this.statusBarItem.text = '$(color-mode)'
this.statusBarItem.tooltip = 'Toggle day/night theme'

Sundial.extensionContext.subscriptions.push(this.statusBarItem)
}

this.statusBarItem.show()
}

/**
* Get current time name based on sunrise and sunset.
*/
private async getCurrentTime(): Promise<editor.TimeNames> {
const log = getLogger('getCurrentTime')
const tides = await this.getTides()
Expand All @@ -143,6 +211,9 @@ export default class Sundial {
return editor.TimeNames.NIGHT // always return something
}

/**
* Get sunrise and sunset based on user settings.
*/
private async getTides() {
const log = getLogger('getTides')
const { sundial } = editor.getConfig()
Expand All @@ -162,6 +233,9 @@ export default class Sundial {
}
}

/**
* Set the time variables based on tides.
*/
private evaluateTides(tides: Tides) {
const log = getLogger('evaluateTides')
const { sundial } = editor.getConfig()
Expand Down Expand Up @@ -194,6 +268,9 @@ export default class Sundial {
}
}

/**
* Set the time variables based on user settings.
*/
private setTimeVariables(tides: Tides) {
const log = getLogger('setTimeVariables')
const { sundial } = editor.getConfig()
Expand Down

0 comments on commit 0132ede

Please sign in to comment.