VulcanBoard.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. button_map = {
  37. (btn["position"][0], btn["position"][1]): btn
  38. for btn in config.buttons
  39. }
  40. layout = GridLayout(
  41. cols=config.columns,
  42. rows=config.rows,
  43. spacing=config.spacing,
  44. padding=config.padding,
  45. )
  46. # Populate grid with buttons and placeholders
  47. for row in range(config.rows):
  48. for col in range(config.columns):
  49. defined_button = button_map.get((row, col))
  50. if defined_button:
  51. btn = AutoResizeButton(
  52. text=defined_button.get("txt", ""),
  53. background_color=get_color_from_hex(
  54. defined_button.get("bg_color", "aaaaff")
  55. ),
  56. color=get_color_from_hex(
  57. defined_button.get("fg_color", "ffffff")
  58. ),
  59. halign="center",
  60. valign="middle",
  61. background_normal="",
  62. )
  63. cmd = defined_button.get("cmd", "")
  64. # pylint: disable=no-member
  65. btn.bind( # pyright: ignore
  66. on_release=lambda _, cmd=cmd: self.execute_command_async(
  67. cmd
  68. )
  69. )
  70. else:
  71. btn = AutoResizeButton(
  72. background_color=get_color_from_hex("cccccc"),
  73. )
  74. layout.add_widget(btn)
  75. return layout
  76. def config_error_exit(self, popup):
  77. popup.dismiss()
  78. sys.exit(1)
  79. def execute_command_async(self, cmd):
  80. if cmd:
  81. try:
  82. subprocess.Popen( # pylint: disable=consider-using-with
  83. cmd, shell=True
  84. )
  85. log(f"Executed command: {cmd}")
  86. except Exception as e:
  87. log(f"Error executing command: {e}", color="yellow")
  88. if __name__ == "__main__":
  89. colorama.init()
  90. VulcanBoardApp().run()