Skip to content

Commit

Permalink
Merge branch 'main' into fix-typing
Browse files Browse the repository at this point in the history
  • Loading branch information
hchargois committed Dec 22, 2024
2 parents 93d93dc + 54c6763 commit b6118e6
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 127 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/themes-screenshot-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ jobs:
do
if test -f "$dir/theme.yaml"; then
# Keep only folder name
theme=`basename ${dir%*/}`
theme=`basename "${dir%*/}"`
# Setup selected theme in config.yaml
echo "Using theme $theme"
sed -i "/THEME:/c\ THEME: $theme" config.yaml
sed -i '/THEME:/c\ THEME: "'"$theme"'"' config.yaml
# For tests there is no real HW: use simulated LCD mode
# Check if theme is for 5"
orientation=$(grep 'DISPLAY_SIZE' $dir/theme.yaml | sed 's/ //g')
orientation=$(grep 'DISPLAY_SIZE' "$dir/theme.yaml" | sed 's/ //g')
if [ "$orientation" == "DISPLAY_SIZE:5\"" ]; then
sed -i "/REVISION:/c\ REVISION: SIMU5" config.yaml
else
Expand Down Expand Up @@ -70,4 +70,4 @@ jobs:
name: themes-screenshots
path: |
screenshot-*.png
diff-*.png
diff-*.png
8 changes: 4 additions & 4 deletions .github/workflows/themes-screenshot-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ jobs:
do
if test -f "$dir/theme.yaml"; then
# Keep only folder name
theme=`basename ${dir%*/}`
theme=`basename "${dir%*/}"`
# Setup selected theme in config.yaml
echo "Using theme $theme"
sed -i "/THEME:/c\ THEME: $theme" config.yaml
sed -i '/THEME:/c\ THEME: "'"$theme"'"' config.yaml
# For tests there is no real HW: use simulated LCD mode
# Check if theme is for 5"
orientation=$(grep 'DISPLAY_SIZE' $dir/theme.yaml | sed 's/ //g')
orientation=$(grep 'DISPLAY_SIZE' "$dir/theme.yaml" | sed 's/ //g')
if [ "$orientation" == "DISPLAY_SIZE:5\"" ]; then
sed -i "/REVISION:/c\ REVISION: SIMU5" config.yaml
else
Expand Down
20 changes: 11 additions & 9 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import psutil
import ruamel.yaml
import sv_ttk
from pathlib import Path
from PIL import Image
from serial.tools.list_ports import comports
from tktooltip import ToolTip
Expand Down Expand Up @@ -92,11 +93,12 @@
"STUB": "Fake random data", "STATIC": "Fake static data"}
reverse_map = {False: "classic", True: "reverse"}

themes_dir = 'res/themes'
MAIN_DIRECTORY = str(Path(__file__).parent.resolve()) + "/"
THEMES_DIR = MAIN_DIRECTORY + 'res/themes'


def get_theme_data(name: str):
dir = os.path.join(themes_dir, name)
dir = os.path.join(THEMES_DIR, name)
# checking if it is a directory
if os.path.isdir(dir):
# Check if a theme.yaml file exists
Expand All @@ -111,7 +113,7 @@ def get_theme_data(name: str):

def get_themes(size: str):
themes = []
for filename in os.listdir(themes_dir):
for filename in os.listdir(THEMES_DIR):
theme_data = get_theme_data(filename)
if theme_data and theme_data['display'].get("DISPLAY_SIZE", '3.5"') == size:
themes.append(filename)
Expand Down Expand Up @@ -155,7 +157,7 @@ def __init__(self):
self.window = Tk()
self.window.title('Turing System Monitor configuration')
self.window.geometry("770x570")
self.window.iconphoto(True, PhotoImage(file="res/icons/monitor-icon-17865/64.png"))
self.window.iconphoto(True, PhotoImage(file=MAIN_DIRECTORY + "res/icons/monitor-icon-17865/64.png"))
# When window gets focus again, reload theme preview in case it has been updated by theme editor
self.window.bind("<FocusIn>", self.on_theme_change)
self.window.after(0, self.on_fan_speed_update)
Expand Down Expand Up @@ -266,9 +268,9 @@ def run(self):

def load_theme_preview(self):
try:
theme_preview = Image.open("res/themes/" + self.theme_cb.get() + "/preview.png")
theme_preview = Image.open(MAIN_DIRECTORY + "res/themes/" + self.theme_cb.get() + "/preview.png")
except:
theme_preview = Image.open("res/docs/no-preview.png")
theme_preview = Image.open(MAIN_DIRECTORY + "res/docs/no-preview.png")
finally:
if theme_preview.width > theme_preview.height:
theme_preview = theme_preview.resize((300, 200), Image.Resampling.LANCZOS)
Expand All @@ -290,7 +292,7 @@ def load_theme_preview(self):
self.theme_author.place(x=10, y=self.theme_preview_img.height() + 15)

def load_config_values(self):
with open("config.yaml", "rt", encoding='utf8') as stream:
with open(MAIN_DIRECTORY + "config.yaml", "rt", encoding='utf8') as stream:
self.config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(stream)

# Check if theme is valid
Expand Down Expand Up @@ -403,14 +405,14 @@ def on_theme_change(self, e=None):
self.load_theme_preview()

def on_theme_editor_click(self):
subprocess.Popen(os.path.join(os.getcwd(), "theme-editor.py") + " \"" + self.theme_cb.get() + "\"", shell=True)
subprocess.Popen(MAIN_DIRECTORY + "theme-editor.py" + " \"" + self.theme_cb.get() + "\"", shell=True)

def on_save_click(self):
self.save_config_values()

def on_saverun_click(self):
self.save_config_values()
subprocess.Popen(os.path.join(os.getcwd(), "main.py"), shell=True)
subprocess.Popen(MAIN_DIRECTORY + "main.py", shell=True)
self.window.destroy()

def on_brightness_change(self, e=None):
Expand Down
16 changes: 9 additions & 7 deletions library/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import os
import queue
import sys

from pathlib import Path
import yaml

from library.log import logger
Expand All @@ -34,8 +34,10 @@ def load_yaml(configfile):


PATH = sys.path[0]
CONFIG_DATA = load_yaml("config.yaml")
THEME_DEFAULT = load_yaml("res/themes/default.yaml")
MAIN_DIRECTORY = Path(__file__).parent.parent.resolve()
FONTS_DIR = str(MAIN_DIRECTORY / "res" / "fonts") + "/"
CONFIG_DATA = load_yaml(MAIN_DIRECTORY / "config.yaml")
THEME_DEFAULT = load_yaml(MAIN_DIRECTORY / "res/themes/default.yaml")
THEME_DATA = None


Expand All @@ -51,10 +53,10 @@ def copy_default(default, theme):
def load_theme():
global THEME_DATA
try:
theme_path = "res/themes/" + CONFIG_DATA['config']['THEME'] + "/"
logger.info("Loading theme %s from %s" % (CONFIG_DATA['config']['THEME'], theme_path + "theme.yaml"))
THEME_DATA = load_yaml(theme_path + "theme.yaml")
THEME_DATA['PATH'] = theme_path
theme_path = Path("res/themes/" + CONFIG_DATA['config']['THEME'])
logger.info("Loading theme %s from %s" % (CONFIG_DATA['config']['THEME'], theme_path / "theme.yaml"))
THEME_DATA = load_yaml(MAIN_DIRECTORY / theme_path / "theme.yaml")
THEME_DATA['PATH'] = str(MAIN_DIRECTORY / theme_path) + "/"
except:
logger.error("Theme not found or contains errors!")
try:
Expand Down
2 changes: 1 addition & 1 deletion library/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def display_static_text(self):
y=config.THEME_DATA['static_text'][text].get("Y", 0),
width=config.THEME_DATA['static_text'][text].get("WIDTH", 0),
height=config.THEME_DATA['static_text'][text].get("HEIGHT", 0),
font=config.THEME_DATA['static_text'][text].get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
font=config.FONTS_DIR + config.THEME_DATA['static_text'][text].get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
font_size=config.THEME_DATA['static_text'][text].get("FONT_SIZE", 10),
font_color=config.THEME_DATA['static_text'][text].get("FONT_COLOR", (0, 0, 0)),
background_color=config.THEME_DATA['static_text'][text].get("BACKGROUND_COLOR", (255, 255, 255)),
Expand Down
30 changes: 16 additions & 14 deletions library/lcd/lcd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def DisplayText(
y: int = 0,
width: int = 0,
height: int = 0,
font: str = "roboto-mono/RobotoMono-Regular.ttf",
font: str = "./res/fonts/roboto-mono/RobotoMono-Regular.ttf",
font_size: int = 20,
font_color: Color = (0, 0, 0),
background_color: Color = (255, 255, 255),
Expand Down Expand Up @@ -262,11 +262,11 @@ def DisplayText(
text_image = self.open_image(background_image)

# Get text bounding box
fontdata = self.open_font(font, font_size)
ttfont = self.open_font(font, font_size)
d = ImageDraw.Draw(text_image)

if width == 0 or height == 0:
left, top, right, bottom = d.textbbox((x, y), text, font=fontdata, align=align, anchor=anchor)
left, top, right, bottom = d.textbbox((x, y), text, font=ttfont, align=align, anchor=anchor)

# textbbox may return float values, which is not good for the bitmap operations below.
# Let's extend the bounding box to the next whole pixel in all directions
Expand All @@ -290,7 +290,7 @@ def DisplayText(
y = top

# Draw text onto the background image with specified color & font
d.text((x, y), text, font=fontdata, fill=font_color, align=align, anchor=anchor)
d.text((x, y), text, font=ttfont, fill=font_color, align=align, anchor=anchor)

# Restrict the dimensions if they overflow the display size
left = max(left, 0)
Expand Down Expand Up @@ -360,6 +360,8 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
line_width: int = 2,
graph_axis: bool = True,
axis_color: Color = (0, 0, 0),
axis_font: str = "./res/fonts/roboto/Roboto-Black.ttf",
axis_font_size: int = 10,
background_color: Color = (255, 255, 255),
background_image: Optional[str] = None):
# Generate a plot graph and display it
Expand Down Expand Up @@ -433,15 +435,15 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
# Draw Legend
draw.line([0, 0, 1, 0], fill=axis_color)
text = f"{int(max_value)}"
fontdata = self.open_font("roboto/Roboto-Black.ttf", 10)
_, top, right, bottom = fontdata.getbbox(text)
ttfont = self.open_font(axis_font, axis_font_size)
_, top, right, bottom = ttfont.getbbox(text)
draw.text((2, 0 - top), text,
font=fontdata, fill=axis_color)
font=ttfont, fill=axis_color)

text = f"{int(min_value)}"
_, top, right, bottom = fontdata.getbbox(text)
_, top, right, bottom = ttfont.getbbox(text)
draw.text((width - 1 - right, height - 2 - bottom), text,
font=fontdata, fill=axis_color)
font=ttfont, fill=axis_color)

self.DisplayPILImage(graph_image, x, y)

Expand Down Expand Up @@ -479,7 +481,7 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
value: int = 50,
text: Optional[str] = None,
with_text: bool = True,
font: str = "roboto/Roboto-Black.ttf",
font: str = "./res/fonts/roboto/Roboto-Black.ttf",
font_size: int = 20,
font_color: Color = (0, 0, 0),
bar_color: Color = (0, 0, 0),
Expand Down Expand Up @@ -650,11 +652,11 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
if with_text:
if text is None:
text = f"{int(pct * 100 + .5)}%"
fontdata = self.open_font(font, font_size)
left, top, right, bottom = fontdata.getbbox(text)
ttfont = self.open_font(font, font_size)
left, top, right, bottom = ttfont.getbbox(text)
w, h = right - left, bottom - top
draw.text((radius - w / 2 + text_offset[0], radius - top - h / 2 + text_offset[1]), text,
font=fontdata, fill=font_color)
font=ttfont, fill=font_color)

if custom_bbox[0] != 0 or custom_bbox[1] != 0 or custom_bbox[2] != 0 or custom_bbox[3] != 0:
bar_image = bar_image.crop(box=custom_bbox)
Expand All @@ -671,5 +673,5 @@ def open_image(self, bitmap_path: str) -> Image.Image:

def open_font(self, name: str, size: int) -> ImageFont.FreeTypeFont:
if (name, size) not in self.font_cache:
self.font_cache[(name, size)] = ImageFont.truetype("./res/fonts/" + name, size)
self.font_cache[(name, size)] = ImageFont.truetype(name, size)
return self.font_cache[(name, size)]
6 changes: 4 additions & 2 deletions library/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def display_themed_value(theme_data, value, min_size=0, unit=''):
y=theme_data.get("Y", 0),
width=theme_data.get("WIDTH", 0),
height=theme_data.get("HEIGHT", 0),
font=theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
font=config.FONTS_DIR + theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
font_size=theme_data.get("FONT_SIZE", 10),
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
background_color=theme_data.get("BACKGROUND_COLOR", (255, 255, 255)),
Expand Down Expand Up @@ -184,7 +184,7 @@ def display_themed_radial_bar(theme_data, value, min_size=0, unit='', custom_tex
value=value,
bar_color=theme_data.get("BAR_COLOR", (0, 0, 0)),
text=text,
font=theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
font=config.FONTS_DIR + theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
font_size=theme_data.get("FONT_SIZE", 10),
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
Expand Down Expand Up @@ -234,6 +234,8 @@ def display_themed_line_graph(theme_data, values):
line_width=theme_data.get("LINE_WIDTH", 2),
graph_axis=theme_data.get("AXIS", False),
axis_color=theme_data.get("AXIS_COLOR", line_color), # If no color specified, use line color for axis
axis_font=config.FONTS_DIR + theme_data.get("AXIS_FONT", "roboto/Roboto-Black.ttf"),
axis_font_size=theme_data.get("AXIS_FONT_SIZE", 10),
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None))
)
Expand Down
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import signal
import subprocess
import time
from pathlib import Path
from PIL import Image

if platform.system() == 'Windows':
Expand Down Expand Up @@ -68,6 +69,8 @@
# If pystray cannot be loaded do not stop the program, just ignore it. The tray icon will not be displayed.
pass

MAIN_DIRECTORY = str(Path(__file__).parent.resolve()) + "/"

if __name__ == "__main__":

# Apply system locale to this program
Expand Down Expand Up @@ -112,7 +115,7 @@ def on_signal_caught(signum, frame=None):

def on_configure_tray(tray_icon, item):
logger.info("Configure from tray icon")
subprocess.Popen(os.path.join(os.getcwd(), "configure.py"), shell=True)
subprocess.Popen(MAIN_DIRECTORY + "configure.py", shell=True)
clean_stop(tray_icon)


Expand Down Expand Up @@ -159,7 +162,7 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
tray_icon = pystray.Icon(
name='Turing System Monitor',
title='Turing System Monitor',
icon=Image.open("res/icons/monitor-icon-17865/64.png"),
icon=Image.open(MAIN_DIRECTORY + "res/icons/monitor-icon-17865/64.png"),
menu=pystray.Menu(
pystray.MenuItem(
text='Configure',
Expand Down
Binary file modified res/themes/Advanced Radials Test/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b6118e6

Please sign in to comment.