Files

107 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Single shared derivation of the XFrame widget count.
The canonical widget list is widgets/common/registry.h, GENERATED by
tools/gen_registry.py from the widgets/components/ directories. Every
driver, composite tool, palette/layout check and test in this tree must
derive the count from that registry -- never hard-code it. The Stock
Market section is adding hundreds of widgets; a baked-in number (the
old 194) breaks the composite grid, palette counts and PLACE_ALL the
moment the registry is regenerated.
Two views of the same truth:
- registry_names() / widget_count(): the integrated set -- what the
C++ palette/layout apps actually build from (kWidgetCount). This is
what drivers and tests must use.
- component_names() / component_count(): every component directory on
disk that holds a real widget (<name>/<name>.h). This is what
gen_registry.py feeds on, and the cross-check that the registry is
current. It deliberately excludes stray files in components/
(e.g. the shared StockData.h header, which is not a widget).
During a landing the component count runs ahead of the registry
count; that gap closes when the registry is regenerated.
Paths resolve relative to this file, so callers work from any cwd.
"""
import math
import os
import re
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
WIDGETS_DIR = os.path.dirname(TOOLS_DIR)
REPO_DIR = os.path.dirname(WIDGETS_DIR)
REGISTRY_H = os.path.join(WIDGETS_DIR, "common", "registry.h")
COMPONENTS_DIR = os.path.join(WIDGETS_DIR, "components")
def registry_names(registry_h=REGISTRY_H):
"""Widget names in registry order, parsed from registry.h."""
with open(registry_h) as f:
text = f.read()
m = re.search(r"kWidgetNames\[kWidgetCount\] = \{(.*?)\};", text, re.S)
if m:
names = re.findall(r'"([^"]+)"', m.group(1))
if names:
return names
# Fallback: the #include lines list the same widgets in the same order.
names = re.findall(r'#include "components/([^/"]+)/[^"]+\.h"', text)
if names:
return names
m = re.search(r"kWidgetCount\s*=\s*(\d+)", text)
raise ValueError("cannot derive widget names from %s%s"
% (registry_h,
(" (kWidgetCount=%s)" % m.group(1)) if m else ""))
def widget_count(registry_h=REGISTRY_H):
"""Number of widgets in the registry -- the derived count."""
return len(registry_names(registry_h))
def is_widget_dir(name, components_dir=COMPONENTS_DIR):
"""True if components/<name>/ holds a real widget (<name>/<name>.h)."""
return (os.path.isdir(os.path.join(components_dir, name))
and os.path.isfile(os.path.join(components_dir, name,
name + ".h")))
def component_names(components_dir=COMPONENTS_DIR):
"""Every real widget directory on disk, sorted (gen_registry order)."""
if not os.path.isdir(components_dir):
return []
return sorted(n for n in os.listdir(components_dir)
if is_widget_dir(n, components_dir))
def component_count(components_dir=COMPONENTS_DIR):
return len(component_names(components_dir))
def composite_cols(n):
"""Grid columns for n tiles: near-square, so the sheet grows evenly."""
if n <= 0:
return 0
return int(math.ceil(math.sqrt(n)))
def composite_rows(n, cols=None):
if n <= 0:
return 0
if cols is None:
cols = composite_cols(n)
return (n + cols - 1) // cols
def composite_tiles(shot_dir):
"""Screenshot tiles: PNGs whose stem is a real widget directory.
Excludes the composite sheet itself and the palette/layout
screenshots (all_widgets/palette/layout*.png), which are not tiles.
"""
widgets = set(component_names())
out = []
for fn in sorted(os.listdir(shot_dir)):
if fn.endswith(".png") and fn[:-4] in widgets:
out.append(os.path.join(shot_dir, fn))
return out