VulcanBoard.py 3.6 KB

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