VulcanBoard.py 3.6 KB

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