如何适配主题

本文适用于webview、自定义编辑器

HBuilderX支持创建视图、自定义编辑器,来构建用户界面。

如何创建一个好看风格统一的UI?

获取HBuilderX主题数据

下面的js代码(可以保存为js文件),主要用来获取如下数据:

  • 设置中的主题、以及用户自定义的主题颜色
  • 编辑器字体
  • 编辑器字体大小
  1. const hx = require('hbuilderx');
  2. /**
  3. * @description 判断是否是object
  4. * @param {Object} object
  5. */
  6. function isObj(object){
  7. return object && typeof (object) == 'object' && Object.prototype.toString.call(object).toLowerCase() == "[object object]";
  8. };
  9. /**
  10. * @description 获取跟主题相匹配的颜色
  11. * - fontFamily 字体
  12. * - fontSize 字号
  13. * - background 背景色
  14. * - fontColor 字体颜色
  15. * - liHoverBackground li类元素,悬停背景色
  16. * - inputBgColor 输入框背景色
  17. * - inputLineColor 输入框线条颜色
  18. * - lineColor 其它线条颜色
  19. * - scrollbarColor 滚动条颜色
  20. * @param {String} area - HBuilderX区域,当area=undefinded,返回编辑器区域的背景色;当area=siderBar时,返回项目管理器背景色
  21. * @return {Object}
  22. */
  23. function getHBuilderXThemeData(area) {
  24. let background;
  25. let fontColor;
  26. let liHoverBackground;
  27. let inputBgColor;
  28. let inputLineColor;
  29. let lineColor;
  30. let scrollbarColor;
  31. let config = hx.workspace.getConfiguration();
  32. let colorScheme = config.get('editor.colorScheme');
  33. let colorcustomColorsizations = config.get('workbench.colorCustomizations');
  34. // 获取HBuilderX编辑器字体大小
  35. let fontSize = config.get('editor.fontSize');
  36. if (fontSize == undefined) {
  37. fontSize = 14;
  38. };
  39. // 获取HBuilderX编辑器字体
  40. let fontFamily = config.get("editor.fontFamily");
  41. if (fontFamily) {
  42. fontFamily = "Monaco"
  43. };
  44. // 获取当前主题
  45. if (colorScheme == undefined) {
  46. colorScheme = 'Default';
  47. };
  48. // 处理用户自定义的颜色
  49. let customColors = {};
  50. try{
  51. customColors = colorcustomColorsizations[`[${colorScheme}]`];
  52. if (!isObj(customColors)) {
  53. customColors = {};
  54. };
  55. }catch(e){};
  56. // 根据参数,返回编辑器、或项目管理器背景色
  57. let viewBackgroundOptionName = area == 'siderBar' ? 'sideBar.background' : 'editor.background';
  58. let viewFontOptionName = area == 'siderBar' ? 'list.foreground' : undefined;
  59. let viewLiHoverBgOptionName = area == 'siderBar' ? 'list.hoverBackground' : 'list.hoverBackground';
  60. switch (colorScheme){
  61. case "Monokai":
  62. background = 'rgb(39,40,34)';
  63. fontColor = 'rgb(179,182,166)';
  64. liHoverBackground = 'rgb(78,80,73)';
  65. inputBgColor = '#2E2E2E';
  66. inputLineColor = 'rgb(81,140,255)';
  67. lineColor = 'rgb(23,23,23)';
  68. scrollbarColor = '#6F6F6F';
  69. break;
  70. case "Atom One Dark":
  71. background = 'rgb(40,44,53)';
  72. fontColor = 'rgb(171,178,191)';
  73. liHoverBackground = 'rgb(44,47,55)';
  74. inputBgColor = '#2E2E2E';
  75. inputLineColor = 'rgb(81,140,255)';
  76. lineColor = 'rgb(33,37,43)';
  77. scrollbarColor = '#6F6F6F';
  78. break;
  79. default:
  80. background = 'rgb(255,250,232)';
  81. fontColor = '#243237';
  82. liHoverBackground = 'rgb(224,237,211)';
  83. inputBgColor = 'rgb(255,254,250)';
  84. inputLineColor = 'rgb(65,168,99)';
  85. lineColor = 'rgb(225,212,178)';
  86. scrollbarColor = 'rgb(207,181,106)';
  87. break;
  88. };
  89. if (customColors != undefined) {
  90. if (customColors[viewBackgroundOptionName] && viewBackgroundOptionName in customColors) {
  91. background = customColors[viewBackgroundOptionName];
  92. };
  93. if (customColors[viewFontOptionName] && viewFontOptionName in customColors) {
  94. fontColor = customColors[viewFontOptionName];
  95. };
  96. if (customColors[viewLiHoverBgOptionName] && viewLiHoverBgOptionName in customColors) {
  97. liHoverBackground = customColors[viewLiHoverBgOptionName];
  98. };
  99. };
  100. return {
  101. fontFamily,
  102. fontSize,
  103. background,
  104. fontColor,
  105. liHoverBackground,
  106. inputLineColor,
  107. inputBgColor,
  108. lineColor,
  109. scrollbarColor
  110. };
  111. };
  112. module.exports = {
  113. getHBuilderXThemeData
  114. };

监听主题切换

监听设置项editor.colorSchemeeditor.fontSizeeditor.fontFamily,当值改变后,发送消息(含主题数据)到视图。

  1. // 引用上面的js代码
  2. let configurationChangeDisplose = hx.workspace.onDidChangeConfiguration(function(event){
  3. if(event.affectsConfiguration("editor.colorScheme") || event.affectsConfiguration("editor.fontSize") || event.affectsConfiguration("editor.fontFamily") ){
  4. let ThemeColorData = getHBuilderXThemeData();
  5. webView.postMessage({
  6. "command": "themeColor",
  7. "data": ThemeColorData
  8. });
  9. };
  10. });