实现自动切换主题的 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"> & ...
随机推荐
- String ,StringBuffer 与S tringBuilder的区别??
String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) ------------------------------------- ...
- Golang修改操作系统时间
Golang修改操作系统时间 需求 程序有时需要和服务器对时,发现延迟过高修改本地时间,这段代码网上抄的,实测可用,windows环境需要以管理员身份启动命令提示符调试 实现Demo package ...
- javascript的getTime函数
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 当VS和Flash builder同时运行时
1, 运行环境: windows 7, VS2010, Flash Builder 4.6 2, 在Flash Builder中运行工作空间和VS2010调试程序是同一个时,也就说在Flash ...
- 如何筛选CRM客户系统无效数据
企业将各个渠道获得的大量数据导入CRM系统之后,要如何筛选CRM客户系统无效数据?销售人员应该将更多的时间用于发掘潜在客户,而不是浪费时间来检索CRM数据.Zoho CRM能够呈现最有价值的客户,让销 ...
- AcWing 106. 动态中位数
依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数. #include<bits/stdc++.h> using namespace std; pri ...
- java设计模式(9):模板方法模式(TemplateMethod)
一,定义:模板方法模式定义了一个操作中的算法骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 二,类图: 三,通过小例子讲解: 这个模式一般用在 ...
- Game游戏分析
1.鲁棒图分析 2.系统上下文及交互方式 3.用例 4.逻辑拓扑图 5.物理拓扑图 6.时序图 7.状态图 8.物理数据模型 9.类图 10.技术选型 11.框架搭建 12.工具及通用服务 13.架构 ...
- php curl处理get和post请求
CURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 CURL 库.使用PHP的CURL 库可以简单和有效地去抓网页.你只 ...
- XCTF logmein
一.查壳 发现是64位的Linux文件(ELF可以看出是linux的文件) 二.拖入ida64,静态分析 注意这里两个坑: 1.strcpy是复制字符串的意思,前面定义的v8数组只有8个,但是后面的字 ...