add ability to style and template menu buttons (#866)

* add ability to select button style for menu items

* allow for jinja template rendering for (nearly) all menu options
this shuffles params json checks to display time instead of config time
This commit is contained in:
Geoffrey Young 2023-02-13 22:06:33 -05:00 committed by GitHub
parent fc7b8c5304
commit 3d7e92f507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 13 deletions

View File

@ -133,6 +133,8 @@ A menu item is configured as follows:
name: Item Name
icon: home
# Optional Parameters
# Icon style, defined as "button.mycolor4" (for example) in the theme css
style: mycolor4
# Panel from the panels listed below
panel: preheat
# Moonraker method to call when the item is selected

View File

@ -175,7 +175,7 @@ class KlipperScreenConfig:
strs = ('gcode', '')
numbers = [f'{option}' for option in self.config[section] if option != 'gcode']
elif section.startswith('menu '):
strs = ('name', 'icon', 'panel', 'method', 'params', 'enable', 'confirm')
strs = ('name', 'icon', 'panel', 'method', 'params', 'enable', 'confirm', 'style')
elif section == 'bed_screws':
# This section may be deprecated in favor of moving this options under the printer section
numbers = ('rotation', '')
@ -549,13 +549,9 @@ class KlipperScreenConfig:
"panel": cfg.get("panel", None),
"method": cfg.get("method", None),
"confirm": cfg.get("confirm", None),
"enable": cfg.get("enable", "True")
"enable": cfg.get("enable", "True"),
"params": cfg.get("params", "{}"),
"style": cfg.get("style", None)
}
try:
item["params"] = json.loads(cfg.get("params", "{}"))
except Exception as e:
logging.exception(f"Unable to parse parameters for [{name}]:\n{e}")
item["params"] = {}
return {name[(len(menu) + 6):]: item}

View File

@ -2,6 +2,8 @@ import logging
import gi
import json
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from jinja2 import Environment, Template
@ -75,14 +77,29 @@ class MenuPanel(ScreenPanel):
env = Environment(extensions=["jinja2.ext.i18n"], autoescape=True)
env.install_gettext_translations(self._config.get_lang())
j2_temp = env.from_string(item['name'])
parsed_name = j2_temp.render()
b = self._gtk.Button(item['icon'], parsed_name, f"color{(i % 4) + 1}")
printer = self._printer.get_printer_status_data()
name = env.from_string(item['name']).render(printer)
icon = env.from_string(item['icon']).render(printer)
style = env.from_string(item['style']).render(printer) if item['style'] else None
b = self._gtk.Button(icon, name, (style if style else f"color{(i % 4) + 1}"))
if item['panel'] is not None:
b.connect("clicked", self.menu_item_clicked, item['panel'], item)
panel = env.from_string(item['panel']).render(printer)
b.connect("clicked", self.menu_item_clicked, panel, item)
elif item['method'] is not None:
params = item['params'] if item['params'] is not False else {}
params = {}
if item['params'] is not False:
try:
p = env.from_string(item['params']).render(printer)
params = json.loads(p)
except Exception as e:
logging.exception(f"Unable to parse parameters for [{name}]:\n{e}")
params = {}
if item['confirm'] is not None:
b.connect("clicked", self._screen._confirm_send_action, item['confirm'], item['method'], params)
else: