VulcanBoard.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 kivy.core.window import Window
  22. from util import log, error_exit_gui
  23. from config import get_config_path, ConfigLoader, Config
  24. from ui import AutoResizeButton
  25. class VulcanBoardApp(App):
  26. def build(self):
  27. self.icon = "icon.jpg"
  28. config_loader = ConfigLoader(get_config_path())
  29. config = config_loader.get_config() # pyright: ignore
  30. if isinstance(config, str):
  31. error_exit_gui(config)
  32. else:
  33. config: Config = config
  34. Window.borderless = config.borderless
  35. kvConfig.set("kivy", "window_icon", "icon.ico")
  36. kvConfig.set("kivy", "exit_on_escape", "0")
  37. button_map = {
  38. (btn["position"][0], btn["position"][1]): btn
  39. for btn in config.buttons
  40. }
  41. layout = GridLayout(
  42. cols=config.columns,
  43. rows=config.rows,
  44. spacing=config.spacing,
  45. padding=config.padding,
  46. )
  47. # Populate grid with buttons and placeholders
  48. for row in range(config.rows):
  49. for col in range(config.columns):
  50. defined_button = button_map.get((row, col))
  51. if defined_button:
  52. btn = AutoResizeButton(
  53. text=defined_button.get("txt", ""),
  54. background_color=get_color_from_hex(
  55. defined_button.get("bg_color", "aaaaff")
  56. ),
  57. color=get_color_from_hex(
  58. defined_button.get("fg_color", "ffffff")
  59. ),
  60. halign="center",
  61. valign="middle",
  62. background_normal="",
  63. )
  64. cmd = defined_button.get("cmd", "")
  65. # pylint: disable=no-member
  66. btn.bind( # pyright: ignore
  67. on_release=lambda _, cmd=cmd: self.execute_command_async(
  68. cmd
  69. )
  70. )
  71. else:
  72. btn = AutoResizeButton(
  73. background_color=get_color_from_hex("cccccc"),
  74. )
  75. layout.add_widget(btn)
  76. return layout
  77. def config_error_exit(self, popup):
  78. popup.dismiss()
  79. sys.exit(1)
  80. def execute_command_async(self, cmd):
  81. if cmd:
  82. try:
  83. subprocess.Popen( # pylint: disable=consider-using-with
  84. cmd, shell=True
  85. )
  86. log(f"Executed command: {cmd}")
  87. except Exception as e:
  88. log(f"Error executing command: {e}", color="yellow")
  89. if __name__ == "__main__":
  90. colorama.init()
  91. VulcanBoardApp().run()