How to unify the theme style

This article applies to webview, custom editor

HBuilderX supports creating views and custom editors to build user interfaces.

How to create a perfect theme?

Get theme data of HBuilderX

The following javascript code (can be saved as a js file) is used to obtain the following data:

  • Themes in settings and user-defined theme colors
  • Editor font
  • Editor font size
  1. const hx = require('hbuilderx');
  2. /**
  3. * @description Is an 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 Get the color matching the theme
  11. * - fontFamily
  12. * - fontSize
  13. * - background
  14. * - fontColor
  15. * - liHoverBackground
  16. * - inputBgColor
  17. * - inputLineColor
  18. * - lineColor
  19. * - scrollbarColor
  20. * @param {String} area - In the HBuilderX, when area=undefinded, it returns the background color of the editor area; when area=siderBar, it returns the background color of the project manager
  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. // Get editor font size
  35. let fontSize = config.get('editor.fontSize');
  36. if (fontSize == undefined) {
  37. fontSize = 14;
  38. };
  39. // Get editor font family
  40. let fontFamily = config.get("editor.fontFamily");
  41. if (fontFamily) {
  42. fontFamily = "Monaco"
  43. };
  44. // Get current color theme
  45. if (colorScheme == undefined) {
  46. colorScheme = 'Default';
  47. };
  48. // Get custom color
  49. let customColors = {};
  50. try{
  51. customColors = colorcustomColorsizations[`[${colorScheme}]`];
  52. if (!isObj(customColors)) {
  53. customColors = {};
  54. };
  55. }catch(e){};
  56. // According to the parameters, return to the editor or project manager background color
  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. };

Monitor theme setting

HBuilder will notify the view when it finds that the values of editor.colorScheme, editor.fontSize, and editor.fontFamily have changed.

  1. //Call above javascript code
  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. });