Skip to content

Commit

Permalink
feat: add total restart prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Oct 30, 2024
1 parent ce024cd commit bd8ee9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfigObject } from 'reactive-vscode'
import * as Meta from './generated/meta'
import { showMessage } from './utils'

export const config = defineConfigObject<Meta.ScopedConfigKeyTypeMap>(
Meta.scopedConfigs.scope,
Expand All @@ -16,3 +17,17 @@ export function getFamilies() {
sansSerif: config['font.sansSerif'],
}
}

let last = hasElectronWindowOptions()
function hasElectronWindowOptions(): string {
return JSON.stringify(config.electron)
}

export function logWindowOptionsChanged() {
const current = hasElectronWindowOptions()
if (last !== current) {
const method = process.platform === 'darwin' ? 'Press "Command + Q"' : 'Close all windows'
showMessage(`Note: Please TOTALLY restart VSCode (${method}) to take effect, "custom-ui-style.electron" is changed`)
}
last = current
}
23 changes: 13 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ import * as Meta from './generated/meta'
import { createFileManagers } from './manager'
import { debounce, showMessage } from './utils'

const changedMsg = 'UI Style changed.'
const rollbackMsg = 'UI Style rollback.'

const { activate, deactivate } = defineExtension(() => {
const { hasBakFile, reload, rollback } = createFileManagers()

if (!hasBakFile()) {
showMessage(
'Seems like first time use or new version is installed, reload the configuration now?',
'Seems like first time use or new version is installed, reload now?',
'Reload',
'Cancel',
)
.then<any>(item => item === 'Reload' && reload('UI style changed'))
.then<any>(item => item === 'Reload' && reload(changedMsg))
}

useCommand(Meta.commands.reload, () => reload('UI style changed'))
useCommand(Meta.commands.rollback, () => rollback('UI style rollback'))
useCommand(Meta.commands.reload, () => reload(changedMsg))
useCommand(Meta.commands.rollback, () => rollback(rollbackMsg))

const watchAndReload = debounce(
() => showMessage('Configuration changed, apply?', 'Apply', 'Cancel')
.then<any>(item => item === 'Apply' && reload('UI style changed')),
1500,
)
const startWatch = () => {
const watchAndReload = debounce(
() => showMessage('Configuration changed, apply?', 'Apply', 'Cancel')
.then<any>(item => item === 'Apply' && reload(changedMsg)),
1500,
)
const cleanup1 = watch(
() => editorConfig.fontFamily,
() => !config['font.monospace'] && watchAndReload(),
Expand All @@ -47,7 +50,7 @@ const { activate, deactivate } = defineExtension(() => {
} else {
cleanup()
}
})
}, { immediate: true })
})

export { activate, deactivate }
23 changes: 15 additions & 8 deletions src/manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FileManager } from './base'
import { logWindowOptionsChanged } from '../config'
import { runAndRestart } from '../utils'
import { CssFileManager } from './css'
import { MainFileManager } from './main'
Expand All @@ -14,13 +15,19 @@ export function createFileManagers() {
]
return {
hasBakFile: () => managers.every(m => m.hasBakFile),
reload: (text: string) => runAndRestart(
text,
() => Promise.all(managers.map(m => m.reload())),
),
rollback: (text: string) => runAndRestart(
text,
() => Promise.all(managers.map(m => m.rollback())),
),
reload: (text: string) => {
logWindowOptionsChanged()
runAndRestart(
text,
() => Promise.all(managers.map(m => m.reload())),
)
},
rollback: (text: string) => {
logWindowOptionsChanged()
runAndRestart(
text,
() => Promise.all(managers.map(m => m.rollback())),
)
},
}
}
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export async function runAndRestart(message: string, action: () => Promise<any>)
try {
writeFileSync(lockFile, String(Date.now()))
await action()
const item = await window.showInformationMessage(message, { title: 'Restart vscode' })
if (item) {
const item = await showMessage(message, 'Reload Window', 'Cancel')
if (item === 'Reload Window') {
commands.executeCommand('workbench.action.reloadWindow')
}
} catch (e) {
Expand Down

0 comments on commit bd8ee9e

Please sign in to comment.