step 4: add digital bar level and status gauges

This commit is contained in:
2026-07-02 14:35:13 +02:00
parent 41e44a23bc
commit d638c512e9
11 changed files with 516 additions and 21 deletions

View File

@@ -0,0 +1,88 @@
# pyright: reportGeneralTypeIssues=false
from collections.abc import Generator
import dearpygui.dearpygui as dpg
import pytest
import dpg_gauges as dpgg
from dpg_gauges.gauges import LevelGaugeConfig, StatusLightConfig
from dpg_gauges.scales import fill_fraction, fill_span, normalized_span
from dpg_gauges.state import clear_registry, get_gauge_state
@pytest.fixture
def dpg_context() -> Generator[None]:
clear_registry()
dpgg.configure()
dpg.create_context()
try:
yield
finally:
clear_registry()
dpg.destroy_context()
def test_bar_geometry_helpers_clamp_and_center() -> None:
assert fill_fraction(150, 0, 100, clamp=True) == 1.0
assert fill_span(25, 0, 100) == (0.0, 0.25)
assert fill_span(-25, -100, 100, fill_mode="centered") == (0.375, 0.5)
assert fill_span(25, -100, 100, fill_mode="centered") == (0.5, 0.625)
assert normalized_span(90, 10, 0, 100) == (0.1, 0.9)
def test_bar_runtime_renders_fill_thresholds_and_markers(dpg_context: None) -> None:
threshold = dpgg.ThresholdBand(35, 50, (255, 120, 0, 120))
marker = dpgg.GaugeMarker(50, (255, 255, 255, 255))
with dpg.window(label="Bar"):
dpgg.bar_gauge(
tag="boost",
label="Boost",
value=25,
min_value=0,
max_value=50,
width=240,
height=90,
thresholds=[threshold],
markers=[marker],
)
debug = dpgg.get_gauge_debug_state("boost")
assert "boost__bar_fill" in debug["renderer"]["layers"]["dynamic"]
assert "boost__threshold_0" in debug["renderer"]["layers"]["thresholds"]
assert "boost__marker_0" in debug["renderer"]["layers"]["markers"]
dpgg.set_value("boost", 100)
state = get_gauge_state("boost")
assert state.clamped_value == 50
assert state.displayed_value == 50
def test_level_runtime_uses_vertical_fill(dpg_context: None) -> None:
with dpg.window(label="Level"):
dpgg.level_gauge(tag="fuel", label="Fuel", value=75, width=120, height=180)
debug = dpgg.get_gauge_debug_state("fuel")
config = get_gauge_state("fuel").config
assert isinstance(config, LevelGaugeConfig)
assert config.orientation == "vertical"
assert "fuel__bar_fill" in debug["renderer"]["layers"]["dynamic"]
def test_status_light_uses_configured_state_text_and_color(dpg_context: None) -> None:
states = {
"ok": dpgg.StatusState((0, 220, 120, 255), "OK"),
"fault": dpgg.StatusState((255, 45, 45, 255), "FAULT"),
}
with dpg.window(label="Status"):
dpgg.status_light(tag="ecu", label="ECU", state="ok", states=states, width=180, height=70)
debug = dpgg.get_gauge_debug_state("ecu")
assert "ecu__status_light" in debug["renderer"]["layers"]["dynamic"]
assert "ecu__status_text" in debug["renderer"]["layers"]["text"]
dpgg.configure_gauge("ecu", state="fault")
config = get_gauge_state("ecu").config
assert isinstance(config, StatusLightConfig)
assert config.state == "fault"
assert get_gauge_state("ecu").dirty == 0