VulcanBoard.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. # pylint: disable=invalid-name
  14. import subprocess
  15. import sys
  16. import colorama
  17. from kivy.app import App
  18. from kivy.uix.gridlayout import GridLayout
  19. from kivy.utils import get_color_from_hex
  20. from kivy.config import Config as kvConfig
  21. from util import log, error_exit_gui
  22. from config import get_config_path, ConfigLoader, Config
  23. from ui import AutoResizeButton
  24. class VulcanBoardApp(App):
  25. def build(self):
  26. self.icon = "icon.jpg"
  27. config_loader = ConfigLoader(get_config_path())
  28. config = config_loader.get_config() # pyright: ignore
  29. if isinstance(config, str):
  30. error_exit_gui(config)
  31. else:
  32. config: Config = config
  33. self.Borderless = config.borderless
  34. kvConfig.set("kivy", "window_icon", "icon.ico")
  35. button_map = {
  36. (btn["position"][0], btn["position"][1]): btn
  37. for btn in config.buttons
  38. }
  39. layout = GridLayout(
  40. cols=config.columns,
  41. rows=config.rows,
  42. spacing=config.spacing,
  43. padding=config.padding,
  44. )
  45. # Populate grid with buttons and placeholders
  46. for row in range(config.rows):
  47. for col in range(config.columns):
  48. defined_button = button_map.get((row, col))
  49. if defined_button:
  50. btn = AutoResizeButton(
  51. text=defined_button.get("txt", ""),
  52. background_color=get_color_from_hex(
  53. defined_button.get("bg_color", "aaaaff")
  54. ),
  55. color=get_color_from_hex(
  56. defined_button.get("fg_color", "ffffff")
  57. ),
  58. halign="center",
  59. valign="middle",
  60. background_normal="",
  61. )
  62. cmd = defined_button.get("cmd", "")
  63. # pylint: disable=no-member
  64. btn.bind( # pyright: ignore
  65. on_release=lambda _, cmd=cmd: self.execute_command_async(
  66. cmd
  67. )
  68. )
  69. else:
  70. btn = AutoResizeButton(
  71. background_color=get_color_from_hex("cccccc"),
  72. )
  73. layout.add_widget(btn)
  74. return layout
  75. def config_error_exit(self, popup):
  76. popup.dismiss()
  77. sys.exit(1)
  78. def execute_command_async(self, cmd):
  79. if cmd:
  80. try:
  81. subprocess.Popen( # pylint: disable=consider-using-with
  82. cmd, shell=True
  83. )
  84. log(f"Executed command: {cmd}")
  85. except Exception as e:
  86. log(f"Error executing command: {e}", color="yellow")
  87. if __name__ == "__main__":
  88. colorama.init()
  89. VulcanBoardApp().run()