实现自动切换主题的 VSCode 扩展
在白天,我常常需要浅色的 VSCode 主题;在夜间,我常常需要深色的 VSCode 主题。我不希望每天手动切换两次 VSCode 主题,所以我开发了这个可以自动切换主题的 VSCode 扩展 —— Dynamic Theme。

特性
- 根据时间点自动切换主题
- 自定义要切换的深色或浅色主题
- 自定义深色或浅色主题的开始时间
- 通过 VSCode 命令更新扩展设置
扩展的设置项
在项目的 package.json 中声明扩展的设置项。常常将扩展名作为设置项的前缀,设置项的默认值也在此处进行声明,在扩展代码中通过 VSCode Extension API 进行获取。
"configuration": [
{
"type": "object",
"title": "Dynamic Theme configuration",
"properties": {
"dynamic-theme.dark": {
"type": "string",
"default": "Default Dark+",
"description": "Dark Theme"
},
"dynamic-theme.light": {
"type": "string",
"default": "Default Light+",
"description": "Light theme"
},
"dynamic-theme.darkTime": {
"type": "string",
"default": "20:00",
"description": "Hour(24h) of the start of the dark theme. Format HH:MM"
},
"dynamic-theme.lightTime": {
"type": "string",
"default": "7:00",
"description": "Hour(24h) of the start of the light theme. Format HH:MM"
}
}
}
]
- dynamic-theme.dark 深色主题
- dynamic-theme.light 浅色主题
- dynamic-theme.darkTime 深色主题开始时间
- dynamic-theme.darkTime 浅色主题开始时间
扩展的命令
在项目的 package.json 文件中声明扩展要注册的命令。常常将扩展名作为命令的前缀,在扩展代码中通过 VSCode Extension API 获取命令并进行注册。
"commands": [
{
"command": "dynamic-theme.dark",
"title": "Dynamic Theme: Set dark theme"
},
{
"command": "dynamic-theme.light",
"title": "Dynamic Theme: Set light theme"
},
{
"command": "dynamic-theme.darkTime",
"title": "Dynamic Theme: Set dark theme time. Format HH:MM"
},
{
"command": "dynamic-theme.lightTime",
"title": "Dynamic Theme: Set light theme time. Format HH:MM"
}
]
- dynamic-theme.dark 设置深色主题
- dynamic-theme.light 设置浅色主题
- dynamic-theme.darkTime 设置深色主题开始时间
- dynamic-theme.darkTime 设置浅色主题开始时间
编写扩展的入口
import { updateTheme } from './update'
import { registerCommands } from './commands'
import { workspace } from 'vscode'
import type { ExtensionContext, ConfigurationChangeEvent, Event } from 'vscode'
export function activate({ subscriptions }: ExtensionContext) {
const { onDidChangeConfiguration } = workspace
updateTheme()
subscriptions.push(
onDidChangeConfiguration(() => updateTheme()),
...registerCommands()
)
}
在扩展激活时将执行 activate 函数。update 函数用于检查设置并更新 VSCode 主题。
在 subscriptions 中可添加一次性用品(disposables)。当配置发生更改时发出 onDidChangeConfiguration 事件,同样检查设置并更新 VSCode 主题。在 subscriptions 还添加了注册命令。
更新 VSCode 主题
update.ts 就做了一件事,获取配置,更新主题。
import type { IOptions } from './types'
import {
parseTime,
updateEditorTheme,
getExtensionConfiguration,
} from './helpers'
export function updateTheme() {
const { dark, light, darkTime, lightTime } = getExtensionConfiguration()
const date = new Date()
const hours = date.getHours() + date.getMinutes() / 60
parseTime(lightTime) <= hours && hours < parseTime(darkTime)
? updateEditorTheme(light)
: updateEditorTheme(dark)
}
注册 VSCode 命令
在 package.json 中声明了四个扩展的命令:
- dynamic-theme.dark 设置深色主题
- dynamic-theme.light 设置浅色主题
- dynamic-theme.darkTime 设置深色主题开始时间
- dynamic-theme.darkTime 设置浅色主题开始时间
设置时间时通过 VSCode Extension API 调出一个输入框即可。设置主题时期望打开选择主题 picker,但是貌似 VSCode 貌似并没有提供这个 API。只好通过读取所有主题,并生成一个 picker 的方式展示,缺点是无法像设置 VSCode 主题那样实时预览主题。

commands.ts
import { commands, window } from 'vscode'
import {
updateExtensionConfiguration,
getThemeList,
parseTime,
} from './helpers'
export enum Commands {
hello = 'helloWorld',
dark = 'dark',
light = 'light',
darkTime = 'darkTime',
lightTime = 'lightTime',
}
const commandPrefix = 'dynamic-theme.'
export function registerCommands() {
const light = commands.registerCommand(commandPrefix + Commands.light, () => {
const themeList = getThemeList()
window.showQuickPick(themeList).then(res => {
if (res !== undefined) {
updateExtensionConfiguration(Commands.light, res)
}
})
})
const dark = commands.registerCommand(commandPrefix + Commands.dark, () => {
const themeList = getThemeList()
window.showQuickPick(themeList).then(res => {
if (res !== undefined) {
updateExtensionConfiguration(Commands.dark, res)
}
})
})
const lightTime = commands.registerCommand(
commandPrefix + Commands.lightTime,
() => {
window.showInputBox().then(res => {
if (res !== undefined) {
updateExtensionConfiguration(Commands.lightTime, res)
}
})
}
)
const darkTime = commands.registerCommand(
commandPrefix + Commands.darkTime,
() => {
window.showInputBox().then((res: any) => {
if (res !== undefined) {
updateExtensionConfiguration(Commands.darkTime, res)
}
})
}
)
return [dark, light, darkTime, lightTime]
}
链接
实现自动切换主题的 VSCode 扩展的更多相关文章
- ThinkPHP实现移动端访问自动切换主题模板
ThinkPHP的模板主题机制,如果只是在PC,只要需修改 DEFAULT_THEME (新版模板主题默认是空,表示不启用模板主题功能)配置项就可以方便的实现多模板主题切换. 但对于移动端和PC端,也 ...
- 我编写 33 个 VSCode 扩展的原因以及管理扩展的经验
简评:使用工具的同时自己创造一些工具或扩展,是一件很棒的事情. 以下"我"指原作者 Fabio 大家好,我叫 Fabio,是一位自学成才的开发人员,热衷于开源和授权.我也喜欢自己制 ...
- 使用顶级 VSCode 扩展来加快开发 JavaScript
使用顶级 VSCode 扩展来加快开发 JavaScript 发表于 2018年08月24日 by 愚人码头 被浏览 3,942 次 分享到: 小编推荐:掘金是一个面向程序员的高质量技术社区,从 ...
- 提高 JavaScript 开发效率的高级 VSCode 扩展!
原文:提高 JavaScript 开发效率的高级 VSCode 扩展! 作者:前端小智 Fundebug经授权转载,版权归原作者所有. Quokka.js Quokka.js 是一个用于 JavaSc ...
- 使用 AHK 在 VS Code 中根据上下文自动切换输入法状态
平常在VS Code打公式,中英文切换一直狂点 Shift 手都快按断了,于是试图用 AutoHotKey 搞一些自动切换输入法程序,让它根据当前输入环境自动切输入法. 之前在网上搜到的是切换键盘的( ...
- iOS 实现快速切换主题详细教程(附上源码)
前言 iOS 实现主题切换,相信在未来的app里也是会频繁出现的,尽管现在只是出现在主流的APP,如(QQ.新浪微博.酷狗音乐.网易云音乐等),但是现在是看颜值.追求个性的年代,所以根据用户喜好自定义 ...
- Nginx+Keepalived主从双机热备+自动切换
1 安装配置nginx 参考: http://www.cnblogs.com/jager/p/4388202.html 2 安装配置keepalived tar xvf keepalived-1.2. ...
- nginx+keepalive主从双机热备+自动切换解决方案
环境采集cenots 6.3 64位迷你安装,因为安装前,你需要做一些工作 yum install -y make wget 如果你愿意可以更新下系统,更换下yum源. 1.安装keepalive 官 ...
- html+css+jQuery+JavaScript实现tab自动切换功能
tab1.html内容 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
随机推荐
- Unity 按空格一直触发Button点击事件的问题
#解决 这是由于Button中Navigation(导航)功能导致的. 将导航设置为None即可. 真是气死我了,我说为什么点击完按钮界面,按空格就一直触发界面,难搞
- Binding(四):数据校验
除了上一节讲的类型转换器,Binding还自带数据校验功能,这节主要来讲一下. 跟类型转换器一样,数据校验需要我们继承ValidationRule类,实现其中的Validate方法,并写入我 ...
- [HNOI2006]公路修建问题题解
题目 题目描述 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织成立了, ...
- SQL 小知识笔记
1.自动生成序列号 select row_number() over(order by field1) as row_number,* from t_table
- Spring:Spring-IOC简介
想要了解控制反转( Inversion of Control ), 我觉得有必要先了解软件设计的一个重要思想:依赖倒置原则(Dependency Inversion Principle ). 什么是依 ...
- 解决pip安装时出现报错TypeError unsupported operand type(s) for -= 'Retry' and 'int'
1.参考 https://stackoverflow.com/questions/42610545/typeerror-unsupported-operand-types-for-retry-and- ...
- linux之软连接 硬链接 link ln
p.p1 { margin: 0; font: 12px "Helvetica Neue"; color: rgba(220, 161, 13, 1) } p.p2 { margi ...
- chrome 屏蔽广告的利器
Adblock Plus https://chrome.google.com/webstore/detail/adblock-plus/cfhdojbkjhnklbpkdaibdccddilifddb ...
- NSURLSession的简单使用
NSURLSession的简单使用(不同于NSURLConnection,仅仅支持异步请求) dataTask,简单请求直接block里面执行,不走代理 NSURLSessionDataTaskDel ...
- vim程序编辑器---常用操作整理
vim程序编辑器---常用操作整理 移动光标方法 o 在光标行的下一行,进入编辑模式 $ 移动到光标这行,最末尾的地方 G(大写) 移动到文件最末行 :set nu 文件显示行数 :set non ...