|
| 1 | +# 如何适配主题 |
| 2 | + |
| 3 | +> 本文适用于webview、自定义编辑器 |
| 4 | +
|
| 5 | +HBuilderX支持创建视图、自定义编辑器,来构建用户界面。 |
| 6 | + |
| 7 | +如何创建一个`好看`、`风格统一`的UI? |
| 8 | + |
| 9 | +## 获取HBuilderX主题数据 |
| 10 | + |
| 11 | +下面的js代码(可以保存为js文件),主要用来获取如下数据: |
| 12 | + |
| 13 | +- 设置中的主题、以及用户自定义的主题颜色 |
| 14 | +- 编辑器字体 |
| 15 | +- 编辑器字体大小 |
| 16 | + |
| 17 | +```js |
| 18 | +const hx = require('hbuilderx'); |
| 19 | + |
| 20 | +/** |
| 21 | + * @description 判断是否是object |
| 22 | + * @param {Object} object |
| 23 | + */ |
| 24 | +function isObj(object){ |
| 25 | + return object && typeof (object) == 'object' && Object.prototype.toString.call(object).toLowerCase() == "[object object]"; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * @description 获取跟主题相匹配的颜色 |
| 30 | + * - fontFamily 字体 |
| 31 | + * - fontSize 字号 |
| 32 | + * - background 背景色 |
| 33 | + * - fontColor 字体颜色 |
| 34 | + * - liHoverBackground li类元素,悬停背景色 |
| 35 | + * - inputBgColor 输入框背景色 |
| 36 | + * - inputLineColor 输入框线条颜色 |
| 37 | + * - lineColor 其它线条颜色 |
| 38 | + * - scrollbarColor 滚动条颜色 |
| 39 | + * @param {String} area - HBuilderX区域,当area=undefinded,返回编辑器区域的背景色;当area=siderBar时,返回项目管理器背景色 |
| 40 | + * @return {Object} |
| 41 | + */ |
| 42 | +function getHBuilderXThemeData(area) { |
| 43 | + let background; |
| 44 | + let fontColor; |
| 45 | + let liHoverBackground; |
| 46 | + let inputBgColor; |
| 47 | + let inputLineColor; |
| 48 | + let lineColor; |
| 49 | + let scrollbarColor; |
| 50 | + |
| 51 | + let config = hx.workspace.getConfiguration(); |
| 52 | + let colorScheme = config.get('editor.colorScheme'); |
| 53 | + let colorcustomColorsizations = config.get('workbench.colorCustomizations'); |
| 54 | + |
| 55 | + // 获取HBuilderX编辑器字体大小 |
| 56 | + let fontSize = config.get('editor.fontSize'); |
| 57 | + if (fontSize == undefined) { |
| 58 | + fontSize = 14; |
| 59 | + }; |
| 60 | + |
| 61 | + // 获取HBuilderX编辑器字体 |
| 62 | + let fontFamily = config.get("editor.fontFamily"); |
| 63 | + if (fontFamily) { |
| 64 | + fontFamily = "Monaco" |
| 65 | + }; |
| 66 | + |
| 67 | + // 获取当前主题 |
| 68 | + if (colorScheme == undefined) { |
| 69 | + colorScheme = 'Default'; |
| 70 | + }; |
| 71 | + |
| 72 | + // 处理用户自定义的颜色 |
| 73 | + let customColors = {}; |
| 74 | + try{ |
| 75 | + customColors = colorcustomColorsizations[`[${colorScheme}]`]; |
| 76 | + if (!isObj(customColors)) { |
| 77 | + customColors = {}; |
| 78 | + }; |
| 79 | + }catch(e){}; |
| 80 | + |
| 81 | + // 根据参数,返回编辑器、或项目管理器背景色 |
| 82 | + let viewBackgroundOptionName = area == 'siderBar' ? 'sideBar.background' : 'editor.background'; |
| 83 | + let viewFontOptionName = area == 'siderBar' ? 'list.foreground' : undefined; |
| 84 | + let viewLiHoverBgOptionName = area == 'siderBar' ? 'list.hoverBackground' : 'list.hoverBackground'; |
| 85 | + |
| 86 | + switch (colorScheme){ |
| 87 | + case "Monokai": |
| 88 | + background = 'rgb(39,40,34)'; |
| 89 | + fontColor = 'rgb(179,182,166)'; |
| 90 | + liHoverBackground = 'rgb(78,80,73)'; |
| 91 | + inputBgColor = '#2E2E2E'; |
| 92 | + inputLineColor = 'rgb(81,140,255)'; |
| 93 | + lineColor = 'rgb(23,23,23)'; |
| 94 | + scrollbarColor = '#6F6F6F'; |
| 95 | + break; |
| 96 | + case "Atom One Dark": |
| 97 | + background = 'rgb(40,44,53)'; |
| 98 | + fontColor = 'rgb(171,178,191)'; |
| 99 | + liHoverBackground = 'rgb(44,47,55)'; |
| 100 | + inputBgColor = '#2E2E2E'; |
| 101 | + inputLineColor = 'rgb(81,140,255)'; |
| 102 | + lineColor = 'rgb(33,37,43)'; |
| 103 | + scrollbarColor = '#6F6F6F'; |
| 104 | + break; |
| 105 | + default: |
| 106 | + background = 'rgb(255,250,232)'; |
| 107 | + fontColor = '#243237'; |
| 108 | + liHoverBackground = 'rgb(224,237,211)'; |
| 109 | + inputBgColor = 'rgb(255,254,250)'; |
| 110 | + inputLineColor = 'rgb(65,168,99)'; |
| 111 | + lineColor = 'rgb(225,212,178)'; |
| 112 | + scrollbarColor = 'rgb(207,181,106)'; |
| 113 | + break; |
| 114 | + }; |
| 115 | + |
| 116 | + if (customColors != undefined) { |
| 117 | + if (customColors[viewBackgroundOptionName] && viewBackgroundOptionName in customColors) { |
| 118 | + background = customColors[viewBackgroundOptionName]; |
| 119 | + }; |
| 120 | + if (customColors[viewFontOptionName] && viewFontOptionName in customColors) { |
| 121 | + fontColor = customColors[viewFontOptionName]; |
| 122 | + }; |
| 123 | + if (customColors[viewLiHoverBgOptionName] && viewLiHoverBgOptionName in customColors) { |
| 124 | + fontColor = customColors[viewLiHoverBgOptionName]; |
| 125 | + }; |
| 126 | + }; |
| 127 | + |
| 128 | + return { |
| 129 | + fontFamily, |
| 130 | + fontSize, |
| 131 | + background, |
| 132 | + fontColor, |
| 133 | + liHoverBackground, |
| 134 | + inputLineColor, |
| 135 | + inputBgColor, |
| 136 | + lineColor, |
| 137 | + scrollbarColor |
| 138 | + }; |
| 139 | +}; |
| 140 | + |
| 141 | +module.exports = { |
| 142 | + getHBuilderXThemeData |
| 143 | +}; |
| 144 | + |
| 145 | +``` |
| 146 | + |
| 147 | +## 监听主题切换 |
| 148 | + |
| 149 | +监听设置项`editor.colorScheme`、`editor.fontSize`、`editor.fontFamily`,当值改变后,发送消息(含主题数据)到视图。 |
| 150 | + |
| 151 | +```js |
| 152 | +// 引用上面的js代码 |
| 153 | +let configurationChangeDisplose = hx.workspace.onDidChangeConfiguration(function(event){ |
| 154 | + if(event.affectsConfiguration("editor.colorScheme") || event.affectsConfiguration("editor.fontSize") || event.affectsConfiguration("editor.fontFamily") ){ |
| 155 | + let ThemeColorData = getHBuilderXThemeData(); |
| 156 | + webView.postMessage({ |
| 157 | + "command": "themeColor", |
| 158 | + "data": ThemeColorData |
| 159 | + }); |
| 160 | + }; |
| 161 | +}); |
| 162 | +``` |
0 commit comments