SettingsUiScreen.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {FsUtils} from "../lib/FsUtils";
  2. import {t} from "../lib/i18n";
  3. import {TouchEventManager} from "../lib/TouchEventManager";
  4. import {QS_BUTTONS, DEFAULT_SETTINGS} from "../utils/data";
  5. class SettingsUiScreen {
  6. userTiels = null;
  7. settings = null;
  8. _load() {
  9. try {
  10. this.settings = FsUtils.fetchJSON("/storage/mmk_tb_layout.json");
  11. } catch(e) {
  12. console.log(e);
  13. this.settings = DEFAULT_SETTINGS;
  14. }
  15. }
  16. start () {
  17. this._load();
  18. // Battety
  19. const batteryToggle = hmUI.createWidget(hmUI.widget.IMG, {
  20. x: 60,
  21. y: 28,
  22. src: 'edit/battery_pv.png',
  23. alpha: this.settings.withBattery ? 255 : 100
  24. });
  25. const batteryToggleEvents = new TouchEventManager(batteryToggle);
  26. batteryToggleEvents.ontouch = () => {
  27. this.settings.withBattery = !this.settings.withBattery;
  28. batteryToggle.setProperty(hmUI.prop.MORE, {
  29. alpha: this.settings.withBattery ? 255 : 100
  30. })
  31. };
  32. // Brightness
  33. const brightnessToggle = hmUI.createWidget(hmUI.widget.IMG, {
  34. x: 12,
  35. y: 72,
  36. src: "edit/brightness_cfg.png",
  37. alpha: this.settings.withBrightness ? 255 : 100
  38. });
  39. const brightnessToggleEvents = new TouchEventManager(brightnessToggle);
  40. brightnessToggleEvents.ontouch = () => {
  41. this.settings.withBrightness = !this.settings.withBrightness;
  42. brightnessToggle.setProperty(hmUI.prop.MORE, {
  43. alpha: this.settings.withBrightness ? 255 : 100
  44. })
  45. };
  46. Object.keys(QS_BUTTONS).forEach((id, i) => {
  47. const config = QS_BUTTONS[id];
  48. if(!config) return;
  49. const x = 12 + (i % 2) * 90;
  50. const y = 164 + Math.floor(i / 2) * 90;
  51. const btn = hmUI.createWidget(hmUI.widget.IMG, {
  52. x,
  53. y,
  54. w: 78,
  55. h: 78,
  56. alpha: this.settings.tiles.indexOf(id) > -1 ? 255 : 100,
  57. src: "qs/" + id + ".png",
  58. });
  59. const events = new TouchEventManager(btn);
  60. events.ontouch = () => {
  61. hmUI.showToast({text: t("qs_" + id)})
  62. this._toggleTile(id, btn);
  63. };
  64. });
  65. // Screen overflow
  66. const end_y = 176 + Math.ceil(Object.keys(QS_BUTTONS).length / 2) * 90;
  67. hmUI.createWidget(hmUI.widget.TEXT, {
  68. x: 0,
  69. y: end_y,
  70. w: 192,
  71. h: 72,
  72. text: ""
  73. });
  74. }
  75. finish() {
  76. FsUtils.writeText("/storage/mmk_tb_layout.json", JSON.stringify(this.settings));
  77. }
  78. _toggleTile(id, btn) {
  79. const ind = this.settings.tiles.indexOf(id);
  80. if(ind < 0) {
  81. this.settings.tiles.push(id);
  82. btn.setProperty(hmUI.prop.MORE, {alpha: 255})
  83. } else {
  84. this.settings.tiles = this.settings.tiles.filter((i) => i !== id);
  85. console.log(this.settings.tiles);
  86. btn.setProperty(hmUI.prop.MORE, {alpha: 100})
  87. }
  88. }
  89. }
  90. let screen;
  91. let __$$app$$__ = __$$hmAppManager$$__.currentApp;
  92. let __$$module$$__ = __$$app$$__.current;
  93. __$$module$$__.module = DeviceRuntimeCore.Page({
  94. onInit(p) {
  95. screen = new SettingsUiScreen();
  96. screen.start();
  97. },
  98. onDestroy: () => {
  99. screen.finish();
  100. }
  101. });