Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(appshot): the area picker gets the full editor tools, grips and a background frame

rubenluetke10-beep30d1e9734401
diff --git a/jarvis/appshot/markup.py b/jarvis/appshot/markup.py
index 7cb0639f52..cdd3acb562 100644
--- a/jarvis/appshot/markup.py
+++ b/jarvis/appshot/markup.py
@@ -8,7 +8,9 @@ and all — and only the finished, privacy-filtered picture gets the markings:
1. blur and pixelate patches, on the capture's own pixels;
2. the overlay on top, stretched to the capture's size (the overlay is drawn
- at the overlay screen's pixel density, the capture may be downscaled).
+ at the overlay screen's pixel density, the capture may be downscaled);
+3. the background frame, when one was chosen — the full editor's gradient
+ presets, margin, rounded corners and shadow.
So the assistant sees exactly what the user pointed at. Pure Pillow, no GUI —
works on every OS and on a headless box.
@@ -42,16 +44,45 @@ class Hide:
rect: FRect
+@dataclass(frozen=True, slots=True)
+class Frame:
+ """A background frame around the picture (the full editor's ``Background``)."""
+
+ preset: str
+ padding: float = 0.08
+ radius: float = 12.0
+ shadow: bool = True
+
+
@dataclass(frozen=True, slots=True)
class Markup:
"""The user's markings on an area appshot."""
overlay_png: bytes = b""
hides: tuple[Hide, ...] = ()
+ frame: Frame | None = None
@property
def empty(self) -> bool:
- return not self.overlay_png and not self.hides
+ return not self.overlay_png and not self.hides and self.frame is None
+
+
+def _parse_frame(payload: Any) -> Frame | None:
+ from jarvis.appshot.picker.markup_model import BACKGROUND_PRESETS # noqa: PLC0415
+
+ if not isinstance(payload, dict):
+ return None
+ preset = payload.get("preset")
+ if preset not in dict(BACKGROUND_PRESETS):
+ return None
+ try:
+ padding = max(0.0, min(0.25, float(payload.get("padding", 0.08))))
+ radius = max(0.0, min(80.0, float(payload.get("radius", 12))))
+ except (TypeError, ValueError):
+ return None
+ return Frame(
+ preset=str(preset), padding=padding, radius=radius, shadow=bool(payload.get("shadow", True))
+ )
def parse_markup(payload: Any) -> Markup | None:
@@ -81,7 +112,9 @@ def parse_markup(payload: Any) -> Markup | None:
continue
if fw > 0 and fh > 0:
hides.append(Hide(kind=str(item["kind"]), rect=(fx, fy, fw, fh)))
- markup = Markup(overlay_png=overlay, hides=tuple(hides))
+ markup = Markup(
+ overlay_png=overlay, hides=tuple(hides), frame=_parse_frame(payload.get("background"))
+ )
return None if markup.empty else markup
@@ -125,9 +158,65 @@ def apply_to_image(image: Any, markup: Markup) -> Any:
base = out.convert("RGBA")
base.alpha_composite(layer)
out = base.convert("RGB")
+ if markup.frame is not None:
+ out = _framed(out, markup.frame)
return out
+def _gradient(size: tuple[int, int], stops: tuple[str, ...]) -> Any:
+ """A diagonal (top-left to bottom-right) gradient through ``stops``."""
+ from PIL import Image, ImageColor # noqa: PLC0415
+
+ colours = [ImageColor.getrgb(stop) for stop in stops]
+ if len(colours) == 1:
+ return Image.new("RGB", size, colours[0])
+ # Linear in x and y, so a small grid stretched up is exact enough.
+ grid = 64
+ small = Image.new("RGB", (grid, grid))
+ width, height = size
+ span = width * width + height * height
+ for gy in range(grid):
+ for gx in range(grid):
+ x, y = gx / (grid - 1) * width, gy / (grid - 1) * height
+ t = max(0.0, min(1.0, (x * width + y * height) / span))
+ pos = t * (len(colours) - 1)
+ i = min(int(pos), len(colours) - 2)
+ f = pos - i
+ a, b = colours[i], colours[i + 1]
+ small.putpixel((gx, gy), tuple(round(a[k] + (b[k] - a[k]) * f) for k in range(3)))
+ return small.resize(size, Image.Resampling.BILINEAR)
+
+
+def _framed(picture: Any, frame: Frame) -> Any:
+ """``picture`` on its background, as the full editor's ``renderResult`` frames it."""
+ from PIL import Image, ImageDraw, ImageFilter # noqa: PLC0415
+
+ from jarvis.appshot.picker.markup_model import BACKGROUND_PRESETS # noqa: PLC0415
+
+ width, height = picture.size
+ longer = max(width, height)
+ pad = round(longer * frame.padding)
+ unit = max(1.0, longer / 1400.0)
+ radius = round(frame.radius * unit)
+ stops = dict(BACKGROUND_PRESETS).get(frame.preset) or BACKGROUND_PRESETS[0][1]
+ canvas = _gradient((width + pad * 2, height + pad * 2), stops).convert("RGBA")
+ box = (pad, pad, pad + width, pad + height)
+ if frame.shadow and pad > 0:
+ blur = max(12.0, pad * 0.5)
+ offset = round(max(4.0, pad * 0.12))
+ shadow = Image.new("L", canvas.size, 0)
+ ImageDraw.Draw(shadow).rounded_rectangle(
+ (box[0], box[1] + offset, box[2], box[3] + offset), radius=radius, fill=90
+ )
+ shadow = shadow.filter(ImageFilter.GaussianBlur(blur / 2.0))
+ black = Image.new("RGBA", canvas.size, (0, 0, 0, 255))
+ canvas = Image.composite(black, canvas, shadow)
+ mask = Image.new("L", picture.size, 0)
+ ImageDraw.Draw(mask).rounded_rectangle((0, 0, width - 1, height - 1), radius=radius, fill=255)
+ canvas.paste(picture.convert("RGBA"), box[:2], mask)
+ return canvas.convert("RGB")
+
+
def apply_to_bytes(image: bytes, mime: str, markup: Markup) -> bytes:
"""``image`` (JPEG or PNG bytes) with the markings, in the same format."""
from PIL import Image # noqa: PLC0415
@@ -154,6 +243,7 @@ def apply_to_rgb(
__all__ = [
"HIDE_KINDS",
+ "Frame",
"Hide",
"Markup",
"apply_to_bytes",
diff --git a/jarvis/appshot/picker/annotate.py b/jarvis/appshot/picker/annotate.py
index 0685eef2a6..9f9c646940 100644
--- a/jarvis/appshot/picker/annotate.py
+++ b/jarvis/appshot/picker/annotate.py
@@ -2,16 +2,17 @@
Runs ONLY inside ``python -m jarvis.appshot.picker`` (PySide6, AP-26). Once
an area is chosen the overlay stays up: a toolbar docks under the selection
-and the user draws on the frozen frame right there — boxes, circles, arrows,
-lines, pen and highlighter strokes, text, numbered steps, blur and pixelate.
-Enter (or the check button) takes the appshot WITH the markings, so the
-assistant sees exactly what was pointed at.
+and the user draws on the frozen frame right there, with the same tools,
+looks and keys as the full appshot editor. Enter (or the check button) takes
+the appshot WITH the markings, so the assistant sees exactly what was pointed
+at.
The rules live in :mod:`jarvis.appshot.picker.markup_model` (plain data);
-this module paints them, draws the toolbar and turns input into model calls.
-The markings leave the process as a transparent PNG overlay plus the
-blur/pixelate rectangles; the main process applies both to the real,
-privacy-filtered capture (:mod:`jarvis.appshot.markup`).
+this module paints them like the full editor does, draws the toolbar and its
+option row, and measures text. The markings leave the process as a
+transparent PNG overlay plus the pixelate/blur rectangles and the background
+frame; the main process applies them to the real, privacy-filtered capture
+(:mod:`jarvis.appshot.markup`).
"""
from __future__ import annotations
@@ -28,6 +29,7 @@ from PySide6.QtGui import (
QGuiApplication,
QIcon,
QImage,
+ QLinearGradient,
QPainter,
QPainterPath,
QPen,
@@ -40,6 +42,7 @@ from PySide6.QtWidgets import (
QGraphicsDropShadowEffect,
QHBoxLayout,
QToolButton,
+ QVBoxLayout,
QWidget,
)
@@ -51,27 +54,40 @@ DIM_OUTSIDE = QColor(0, 0, 0, 120)
_HANDLE_FILL = QColor(255, 255, 255)
_HANDLE_EDGE = QColor(0, 0, 0, 110)
_HANDLE_R = 4.0
+_GRIP_R = 5.0
+_ACCENT = QColor(10, 132, 255)
#: Logical px per pixelate block, and the blur's downscale factor.
_PIXEL_BLOCK = 9.0
_BLUR_FACTOR = 12.0
-_HIGHLIGHT_ALPHA = 105
+_SPOTLIGHT_DIM = QColor(0, 0, 0, 140)
# Toolbar wording — app chrome, so it follows [ui].language like the card.
_LABELS: dict[str, dict[str, str]] = {
"en": {
mm.MOVE: "Select and move",
mm.RECT: "Rectangle",
+ mm.FILLED: "Filled rectangle",
mm.ELLIPSE: "Ellipse",
- mm.ARROW: "Arrow",
mm.LINE: "Line",
- mm.PEN: "Pen",
- mm.HIGHLIGHT: "Highlighter",
+ mm.ARROW: "Arrow",
mm.TEXT: "Text",
+ mm.REDACT: "Pixelate / blur",
+ mm.SPOTLIGHT: "Spotlight",
mm.COUNTER: "Numbered step",
- mm.BLUR: "Blur",
- mm.PIXELATE: "Pixelate",
+ mm.PEN: "Draw",
+ mm.HIGHLIGHT: "Highlighter",
+ mm.BACKGROUND: "Background",
+ "arrow_tapered": "Tapered",
+ "arrow_classic": "Classic",
+ "arrow_double": "Two heads",
+ "text_plain": "Plain",
+ "text_label": "Label",
+ "text_outline": "Outline",
+ "redact_pixelate": "Pixelate",
+ "redact_blur": "Blur",
+ "background_off": "No background",
"color": "Colour",
- "width": "Line width (mouse wheel)",
+ "width": "Size (1-5, mouse wheel)",
"undo": "Undo",
"redo": "Redo",
"copy": "Copy",
@@ -79,21 +95,33 @@ _LABELS: dict[str, dict[str, str]] = {
"edit": "Open in the editor",
"cancel": "Cancel",
"done": "Done — send to the assistant",
+ "placeholder": "Type text",
},
"de": {
mm.MOVE: "Auswählen und verschieben", # i18n-allow: product UI string
mm.RECT: "Rechteck", # i18n-allow: product UI string
+ mm.FILLED: "Gefülltes Rechteck", # i18n-allow: product UI string
mm.ELLIPSE: "Ellipse", # i18n-allow: product UI string
- mm.ARROW: "Pfeil", # i18n-allow: product UI string
mm.LINE: "Linie", # i18n-allow: product UI string
- mm.PEN: "Stift", # i18n-allow: product UI string
- mm.HIGHLIGHT: "Textmarker", # i18n-allow: product UI string
+ mm.ARROW: "Pfeil", # i18n-allow: product UI string
mm.TEXT: "Text", # i18n-allow: product UI string
+ mm.REDACT: "Verpixeln / Weichzeichnen", # i18n-allow: product UI string
+ mm.SPOTLIGHT: "Hervorheben", # i18n-allow: product UI string
mm.COUNTER: "Nummerierter Schritt", # i18n-allow: product UI string
- mm.BLUR: "Weichzeichnen", # i18n-allow: product UI string
- mm.PIXELATE: "Verpixeln", # i18n-allow: product UI string
+ mm.PEN: "Zeichnen", # i18n-allow: product UI string
+ mm.HIGHLIGHT: "Textmarker", # i18n-allow: product UI string
+ mm.BACKGROUND: "Hintergrund", # i18n-allow: product UI string
+ "arrow_tapered": "Spitz zulaufend", # i18n-allow: product UI string
+ "arrow_classic": "Klassisch", # i18n-allow: product UI string
+ "arrow_double": "Zwei Spitzen", # i18n-allow: product UI string
+ "text_plain": "Schlicht", # i18n-allow: product UI string
+ "text_label": "Etikett", # i18n-allow: product UI string
+ "text_outline": "Kontur", # i18n-allow: product UI string
+ "redact_pixelate": "Verpixeln", # i18n-allow: product UI string
+ "redact_blur": "Weichzeichnen", # i18n-allow: product UI string
+ "background_off": "Kein Hintergrund", # i18n-allow: product UI string
"color": "Farbe", # i18n-allow: product UI string
- "width": "Linienstärke (Mausrad)", # i18n-allow: product UI string
+ "width": "Größe (1-5, Mausrad)", # i18n-allow: product UI string
"undo": "Rückgängig", # i18n-allow: product UI string
"redo": "Wiederholen", # i18n-allow: product UI string
"copy": "Kopieren", # i18n-allow: product UI string
@@ -101,21 +129,33 @@ _LABELS: dict[str, dict[str, str]] = {
"edit": "Im Editor öffnen", # i18n-allow: product UI string
"cancel": "Abbrechen", # i18n-allow: product UI string
"done": "Fertig — an den Assistenten", # i18n-allow: product UI string
+ "placeholder": "Text eingeben", # i18n-allow: product UI string
},
"es": {
mm.MOVE: "Seleccionar y mover", # i18n-allow: product UI string
mm.RECT: "Rectángulo", # i18n-allow: product UI string
+ mm.FILLED: "Rectángulo relleno", # i18n-allow: product UI string
mm.ELLIPSE: "Elipse", # i18n-allow: product UI string
- mm.ARROW: "Flecha", # i18n-allow: product UI string
mm.LINE: "Línea", # i18n-allow: product UI string
- mm.PEN: "Lápiz", # i18n-allow: product UI string
- mm.HIGHLIGHT: "Resaltador", # i18n-allow: product UI string
+ mm.ARROW: "Flecha", # i18n-allow: product UI string
mm.TEXT: "Texto", # i18n-allow: product UI string
+ mm.REDACT: "Pixelar / desenfocar", # i18n-allow: product UI string
+ mm.SPOTLIGHT: "Resaltar zona", # i18n-allow: product UI string
mm.COUNTER: "Paso numerado", # i18n-allow: product UI string
- mm.BLUR: "Desenfocar", # i18n-allow: product UI string
- mm.PIXELATE: "Pixelar", # i18n-allow: product UI string
+ mm.PEN: "Dibujar", # i18n-allow: product UI string
+ mm.HIGHLIGHT: "Resaltador", # i18n-allow: product UI string
+ mm.BACKGROUND: "Fondo", # i18n-allow: product UI string
+ "arrow_tapered": "Afilada", # i18n-allow: product UI string
+ "arrow_classic": "Clásica", # i18n-allow: product UI string
+ "arrow_double": "Dos puntas", # i18n-allow: product UI string
+ "text_plain": "Simple", # i18n-allow: product UI string
+ "text_label": "Etiqueta", # i18n-allow: product UI string
+ "text_outline": "Contorno", # i18n-allow: product UI string
+ "redact_pixelate": "Pixelar", # i18n-allow: product UI string
+ "redact_blur": "Desenfocar", # i18n-allow: product UI string
+ "background_off": "Sin fondo", # i18n-allow: product UI string
"color": "Color", # i18n-allow: product UI string
- "width": "Grosor (rueda del ratón)", # i18n-allow: product UI string
+ "width": "Tamaño (1-5, rueda del ratón)", # i18n-allow: product UI string
"undo": "Deshacer", # i18n-allow: product UI string
"redo": "Rehacer", # i18n-allow: product UI string
"copy": "Copiar", # i18n-allow: product UI string
@@ -123,6 +163,7 @@ _LABELS: dict[str, dict[str, str]] = {
"edit": "Abrir en el editor", # i18n-allow: product UI string
"cancel": "Cancelar", # i18n-allow: product UI string
"done": "Listo — enviar al asistente", # i18n-allow: product UI string
+ "placeholder": "Escribe un texto", # i18n-allow: product UI string
},
}
@@ -137,6 +178,23 @@ _ACTION_KEYS = {
"done": "Enter",
}
+#: Toolbar order — the full editor's: selection, shapes, text, effects, ink.
+TOOL_ORDER = (
+ mm.MOVE,
+ mm.RECT,
+ mm.FILLED,
+ mm.ELLIPSE,
+ mm.LINE,
+ mm.ARROW,
+ mm.TEXT,
+ mm.REDACT,
+ mm.SPOTLIGHT,
+ mm.COUNTER,
+ mm.PEN,
+ mm.HIGHLIGHT,
+ mm.BACKGROUND,
+)
+
def labels_for(language: str) -> dict[str, str]:
return dict(_LABELS.get((language or "").lower()[:2], _LABELS["en"]))
@@ -148,70 +206,147 @@ def _qcolor(hex_color: str, alpha: int = 255) -> QColor:
return colour
-def _is_light(hex_color: str) -> bool:
+def contrast_on(hex_color: str) -> QColor:
+ """Black or white, whichever reads on ``hex_color`` (the full editor's rule)."""
c = QColor(hex_color)
- return (0.299 * c.red() + 0.587 * c.green() + 0.114 * c.blue()) > 160
+ luminance = (0.2126 * c.red() + 0.7152 * c.green() + 0.0722 * c.blue()) / 255
+ return QColor(17, 17, 17) if luminance > 0.6 else QColor(255, 255, 255)
def _qrect(box: mm.Box) -> QRectF:
return QRectF(box[0], box[1], box[2], box[3])
-def _text_font(shape: mm.Shape) -> QFont:
- # The editor's text face: Inter, else the system UI font, semibold.
+def text_font(size: float) -> QFont:
+ """The editors' text face: Inter, else the system UI font, semibold."""
font = QFont()
font.setFamilies(["Inter", "Segoe UI", "SF Pro Text", "Helvetica Neue", "Noto Sans"])
- font.setPixelSize(max(8, round(mm.text_size(shape.width))))
+ font.setPixelSize(max(6, round(size)))
font.setWeight(QFont.Weight.DemiBold)
font.setStyleHint(QFont.StyleHint.SansSerif)
return font
-def text_rect(shape: mm.Shape) -> QRectF:
- """A text marking's exact bounds, measured with its own font."""
- metrics = QFontMetricsF(_text_font(shape))
- lines = shape.text.split("\n") or [""]
- width = max(metrics.horizontalAdvance(line) for line in lines)
- x, y = shape.points[0]
- return QRectF(x, y, max(width, metrics.averageCharWidth()), metrics.lineSpacing() * len(lines))
+_metrics_cache: dict[int, QFontMetricsF] = {}
+
+
+def qt_measure(text: str, size: float) -> float:
+ """Width of ``text`` at ``size`` px in the editors' face."""
+ key = max(6, round(size))
+ metrics = _metrics_cache.get(key)
+ if metrics is None:
+ metrics = QFontMetricsF(text_font(key))
+ _metrics_cache[key] = metrics
+ return metrics.horizontalAdvance(text)
# --------------------------------------------------------------------------
-# Painting the markings
+# Painting the markings — ports of the full editor's renderer
# --------------------------------------------------------------------------
-def _arrow_polygon(start: mm.Point, end: mm.Point, width: float) -> QPolygonF | None:
- """A tapered arrow: thin tail, wide shaft, sharp head."""
+def tapered_arrow_outline(start: mm.Point, end: mm.Point, width: float) -> list[mm.Point]:
+ """The tapered arrow, tail to tip and back (``taperedArrowOutline`` in TS)."""
dx, dy = end[0] - start[0], end[1] - start[1]
length = math.hypot(dx, dy)
- if length < 1.0:
- return None
+ if length < 2:
+ return []
ux, uy = dx / length, dy / length
nx, ny = -uy, ux
- head = min(length * 0.6, max(12.0, width * 3.4 + 8.0))
- head_half = head * 0.62
- tail_half = max(0.6, width * 0.3)
- shaft_half = max(1.2, width * 0.75)
- bx, by = end[0] - ux * head, end[1] - uy * head
- points = [
- (start[0] + nx * tail_half, start[1] + ny * tail_half),
- (bx + nx * shaft_half, by + ny * shaft_half),
- (bx + nx * head_half, by + ny * head_half),
- end,
- (bx - nx * head_half, by - ny * head_half),
- (bx - nx * shaft_half, by - ny * shaft_half),
- (start[0] - nx * tail_half, start[1] - ny * tail_half),
+ head = min(max(12.0, width * 4.6 + 6.0), length * 0.62)
+ barb = head * 0.56
+ neck_at = length - head * 0.7
+ tail = max(0.6, width * 0.14)
+ neck = max(1.4, width * 0.78)
+
+ def at(along: float, across: float) -> mm.Point:
+ return (start[0] + ux * along + nx * across, start[1] + uy * along + ny * across)
+
+ return [
+ at(0, tail),
+ at(neck_at, neck),
+ at(length - head, barb),
+ at(length, 0),
+ at(length - head, -barb),
+ at(neck_at, -neck),
+ at(0, -tail),
]
+
+
+def _polygon(points: list[mm.Point]) -> QPolygonF:
return QPolygonF([QPointF(x, y) for x, y in points])
-def _stroke_path(points: list[mm.Point]) -> QPainterPath:
+def _soft_shadow(painter: QPainter, path: QPainterPath, blur: float, dy: float, alpha: int) -> None:
+ """A cheap soft lift: a few offset, fading copies of the shape's fill."""
+ steps = 3
+ for i in range(steps, 0, -1):
+ spread = blur * i / steps
+ shade = QColor(0, 0, 0, max(4, alpha // (steps + i)))
+ pen = QPen(shade, spread)
+ pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
+ painter.save()
+ painter.translate(0, dy)
+ painter.setPen(pen)
+ painter.setBrush(shade)
+ painter.drawPath(path)
+ painter.restore()
+
+
+def _tapered_arrow(painter: QPainter, shape: mm.Shape, colour: QColor) -> None:
+ outline = tapered_arrow_outline(shape.points[0], shape.points[-1], shape.width)
+ if not outline:
+ return
+ path = QPainterPath()
+ path.addPolygon(_polygon(outline))
+ path.closeSubpath()
+ _soft_shadow(painter, path, max(3.0, shape.width * 1.1), max(1.0, shape.width * 0.3), 80)
+ edge = QPen(colour, max(1.0, shape.width * 0.22))
+ edge.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
+ painter.setPen(edge)
+ painter.setBrush(colour)
+ painter.drawPath(path)
+
+
+def _classic_arrow(painter: QPainter, start: mm.Point, end: mm.Point, shape: mm.Shape) -> None:
+ colour = _qcolor(shape.color)
+ angle = math.atan2(end[1] - start[1], end[0] - start[0])
+ head = max(10.0, shape.width * 4.0)
+ back = (end[0] - math.cos(angle) * head * 0.8, end[1] - math.sin(angle) * head * 0.8)
+ pen = QPen(colour, shape.width)
+ pen.setCapStyle(Qt.PenCapStyle.RoundCap)
+ painter.setPen(pen)
+ painter.drawLine(QPointF(*start), QPointF(*back))
+ painter.setPen(Qt.PenStyle.NoPen)
+ painter.setBrush(colour)
+ painter.drawPolygon(
+ _polygon(
+ [
+ end,
+ (end[0] - math.cos(angle - 0.45) * head, end[1] - math.sin(angle - 0.45) * head),
+ (end[0] - math.cos(angle + 0.45) * head, end[1] - math.sin(angle + 0.45) * head),
+ ]
+ )
+ )
+
+
+def _smooth_path(points: list[mm.Point]) -> QPainterPath:
+ """A hand-drawn stroke smoothed through the midpoints of its samples."""
path = QPainterPath(QPointF(*points[0]))
if len(points) == 1:
- path.lineTo(QPointF(points[0][0] + 0.01, points[0][1]))
- for point in points[1:]:
- path.lineTo(QPointF(*point))
+ path.lineTo(QPointF(points[0][0] + 0.1, points[0][1]))
+ return path
+ for i in range(1, len(points) - 1):
+ mid = ((points[i][0] + points[i + 1][0]) / 2, (points[i][1] + points[i + 1][1]) / 2)
+ path.quadTo(QPointF(*points[i]), QPointF(*mid))
+ path.lineTo(QPointF(*points[-1]))
+ return path
+
+
+def _rounded(box: mm.Box, radius: float) -> QPainterPath:
+ path = QPainterPath()
+ r = max(0.0, min(radius, box[2] / 2, box[3] / 2))
+ path.addRoundedRect(_qrect(box), r, r)
return path
@@ -220,92 +355,171 @@ def paint_shape(
shape: mm.Shape,
*,
hide_source: Callable[[mm.Shape], QPixmap | None] | None = None,
- caret: bool = False,
) -> None:
- """Paint one marking in the painter's current (logical) coordinates."""
- if not shape.points:
+ """Paint one marking in the painter's current (logical) coordinates.
+
+ Spotlights are not painted here: they dim everything around them in one
+ layer (:func:`paint_spotlights`).
+ """
+ if not shape.points or shape.kind == mm.SPOTLIGHT:
return
painter.save()
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
colour = _qcolor(shape.color)
kind = shape.kind
- if kind in mm.HIDE_KINDS:
+ if kind == mm.REDACT:
box = _qrect(mm.normalized(shape.points[0], shape.points[-1]))
patch = hide_source(shape) if hide_source is not None else None
if patch is not None:
- painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, kind == mm.BLUR)
+ painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, shape.mode == "blur")
painter.drawPixmap(box, patch, QRectF(patch.rect()))
else:
painter.fillRect(box, QColor(128, 128, 132, 220))
- elif kind in (mm.RECT, mm.ELLIPSE):
- pen = QPen(colour, shape.width)
- pen.setJoinStyle(Qt.PenJoinStyle.MiterJoin)
- painter.setPen(pen)
- painter.setBrush(Qt.BrushStyle.NoBrush)
- box = _qrect(mm.normalized(shape.points[0], shape.points[-1]))
+ elif kind in (mm.RECT, mm.FILLED):
+ path = _rounded(mm.normalized(shape.points[0], shape.points[-1]), shape.width)
if kind == mm.RECT:
- painter.drawRect(box)
+ pen = QPen(colour, shape.width)
+ pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
+ painter.setPen(pen)
+ painter.setBrush(Qt.BrushStyle.NoBrush)
else:
- painter.drawEllipse(box)
+ painter.setPen(Qt.PenStyle.NoPen)
+ painter.setBrush(colour)
+ painter.drawPath(path)
+ elif kind == mm.ELLIPSE:
+ painter.setPen(QPen(colour, shape.width))
+ painter.setBrush(Qt.BrushStyle.NoBrush)
+ painter.drawEllipse(_qrect(mm.normalized(shape.points[0], shape.points[-1])))
elif kind == mm.LINE:
pen = QPen(colour, shape.width)
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
painter.setPen(pen)
painter.drawLine(QPointF(*shape.points[0]), QPointF(*shape.points[-1]))
elif kind == mm.ARROW:
- polygon = _arrow_polygon(shape.points[0], shape.points[-1], shape.width)
- if polygon is not None:
- edge = QPen(colour, 1.0)
- edge.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
- painter.setPen(edge)
- painter.setBrush(colour)
- painter.drawPolygon(polygon)
+ start, end = shape.points[0], shape.points[-1]
+ if shape.style == "classic":
+ _classic_arrow(painter, start, end, shape)
+ elif shape.style == "double":
+ _classic_arrow(painter, start, end, shape)
+ _classic_arrow(painter, end, start, shape)
+ else:
+ _tapered_arrow(painter, shape, colour)
elif kind in mm.STROKE_KINDS:
highlight = kind == mm.HIGHLIGHT
- width = shape.width * 4.0 + 8.0 if highlight else shape.width
- pen = QPen(_qcolor(shape.color, _HIGHLIGHT_ALPHA) if highlight else colour, width)
+ pen = QPen(_qcolor(shape.color, 102) if highlight else colour)
+ pen.setWidthF(shape.width * 4.0 if highlight else shape.width)
pen.setCapStyle(Qt.PenCapStyle.FlatCap if highlight else Qt.PenCapStyle.RoundCap)
pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
painter.setPen(pen)
painter.setBrush(Qt.BrushStyle.NoBrush)
- painter.drawPath(_stroke_path(shape.points))
+ painter.drawPath(_smooth_path(shape.points))
elif kind == mm.TEXT:
- _paint_text(painter, shape, caret=caret)
+ _paint_text(painter, shape)
elif kind == mm.COUNTER:
- r = mm.counter_radius(shape.width)
- centre = QPointF(*shape.points[0])
- painter.setPen(QPen(QColor(255, 255, 255, 235), 2.0))
- painter.setBrush(colour)
- painter.drawEllipse(centre, r, r)
- font = QFont()
- font.setPixelSize(max(8, round(r * 1.15)))
- font.setWeight(QFont.Weight.Bold)
- painter.setFont(font)
- painter.setPen(QColor(17, 17, 17) if _is_light(shape.color) else QColor(255, 255, 255))
- painter.drawText(
- QRectF(centre.x() - r, centre.y() - r, 2 * r, 2 * r),
- Qt.AlignmentFlag.AlignCenter,
- str(shape.number),
- )
+ _paint_counter(painter, shape, colour)
painter.restore()
-def _paint_text(painter: QPainter, shape: mm.Shape, *, caret: bool) -> None:
- font = _text_font(shape)
+def _paint_text(painter: QPainter, shape: mm.Shape) -> None:
+ font = text_font(shape.size)
metrics = QFontMetricsF(font)
x, y = shape.points[0]
- lines = shape.text.split("\n")
- # Plain text in the chosen colour — no outline, no halo.
+ lines = mm.text_lines(shape)
+ line_height = shape.size * 1.25
+ # Centre each line's glyphs in its 1.25 line box, like the canvas does
+ # with textBaseline "top" plus the line height.
+ lead = (line_height - metrics.height()) / 2.0 + metrics.ascent()
+ ink = _qcolor(shape.color)
+ if shape.style == "label":
+ pad = mm.label_pad(shape)
+ width = max(metrics.horizontalAdvance(line) for line in lines)
+ box = (x - pad, y - pad, width + pad * 2, len(lines) * line_height + pad * 2)
+ painter.setPen(Qt.PenStyle.NoPen)
+ painter.setBrush(ink)
+ painter.drawPath(_rounded(box, pad))
+ ink = contrast_on(shape.color)
+ if shape.style == "outline":
+ path = QPainterPath()
+ for i, line in enumerate(lines):
+ path.addText(QPointF(x, y + lead + i * line_height), font, line)
+ outline = QPen(contrast_on(shape.color), max(2.0, shape.size / 6.0))
+ outline.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
+ painter.strokePath(path, outline)
+ painter.fillPath(path, ink)
+ return
painter.setFont(font)
- painter.setPen(_qcolor(shape.color))
+ painter.setPen(ink)
for i, line in enumerate(lines):
- painter.drawText(QPointF(x, y + metrics.ascent() + i * metrics.lineSpacing()), line)
+ painter.drawText(QPointF(x, y + lead + i * line_height), line)
+
+
+def _paint_counter(painter: QPainter, shape: mm.Shape, colour: QColor) -> None:
+ r = shape.size
+ centre = QPointF(*shape.points[0])
+ disc = QPainterPath()
+ disc.addEllipse(centre, r, r)
+ _soft_shadow(painter, disc, max(2.0, r / 4.0), 1.0, 70)
+ painter.setPen(QPen(QColor(255, 255, 255, 230), max(1.5, r / 9.0)))
+ painter.setBrush(colour)
+ painter.drawEllipse(centre, r, r)
+ painter.setFont(text_font(r * (0.95 if shape.number > 9 else 1.15)))
+ painter.setPen(contrast_on(shape.color))
+ painter.drawText(
+ QRectF(centre.x() - r, centre.y() - r, 2 * r, 2 * r),
+ Qt.AlignmentFlag.AlignCenter,
+ str(shape.number),
+ )
+
+
+def paint_spotlights(painter: QPainter, shapes: list[mm.Shape], area: QRectF) -> None:
+ """Dim the area outside every spotlight, in one layer so they never stack."""
+ spots = [s for s in shapes if s.kind == mm.SPOTLIGHT and len(s.points) >= 2]
+ if not spots:
+ return
+ dim = QPainterPath()
+ dim.addRect(area)
+ for spot in spots:
+ box = mm.normalized(spot.points[0], spot.points[-1])
+ dim = dim.subtracted(_rounded(box, min(box[2], box[3]) * 0.06))
+ painter.save()
+ painter.setRenderHint(QPainter.RenderHint.Antialiasing)
+ painter.fillPath(dim, _SPOTLIGHT_DIM)
+ painter.restore()
+
+
+def paint_typing(painter: QPainter, shape: mm.Shape, placeholder: str, caret: bool) -> None:
+ """The text box being typed in: a visible field, the text, a caret."""
+ font = text_font(shape.size)
+ metrics = QFontMetricsF(font)
+ x, y = shape.points[0]
+ lines = mm.text_lines(shape)
+ line_height = shape.size * 1.25
+ shown = shape.text or placeholder
+ width = max(metrics.horizontalAdvance(line) for line in shown.split("\n"))
+ box = QRectF(x - 6, y - 4, max(width, shape.size * 4) + 12, len(lines) * line_height + 8)
+ painter.save()
+ painter.setRenderHint(QPainter.RenderHint.Antialiasing)
+ painter.setPen(QPen(QColor(255, 255, 255, 220), 1.2, Qt.PenStyle.DashLine))
+ painter.setBrush(QColor(0, 0, 0, 70))
+ painter.drawRoundedRect(box, 5, 5)
+ painter.restore()
+ if shape.text:
+ paint_shape(painter, shape)
+ else:
+ lead = (line_height - metrics.height()) / 2.0 + metrics.ascent()
+ painter.save()
+ painter.setFont(font)
+ painter.setPen(QColor(255, 255, 255, 150))
+ painter.drawText(QPointF(x, y + lead), placeholder)
+ painter.restore()
if caret:
- last = lines[-1] if lines else ""
+ last = lines[-1] if shape.text else ""
cx = x + metrics.horizontalAdvance(last) + 1.5
- cy = y + (len(lines) - 1) * metrics.lineSpacing()
- painter.setPen(QPen(_qcolor(shape.color), 1.5))
- painter.drawLine(QPointF(cx, cy + 2), QPointF(cx, cy + metrics.height() - 2))
+ cy = y + (len(lines) - 1) * line_height
+ painter.save()
+ painter.setPen(QPen(_qcolor(shape.color), max(1.5, shape.size / 14.0)))
+ painter.drawLine(QPointF(cx, cy + line_height * 0.12), QPointF(cx, cy + line_height * 0.88))
+ painter.restore()
def paint_selection_frame(painter: QPainter, sel: QRectF) -> None:
@@ -326,24 +540,59 @@ def paint_selection_frame(painter: QPainter, sel: QRectF) -> None:
def paint_picked(painter: QPainter, shape: mm.Shape) -> None:
- """A dashed frame around the marking the move tool holds."""
- box = text_rect(shape) if shape.kind == mm.TEXT else _qrect(mm.bounds(shape))
- pad = shape.width / 2.0 + 4.0
+ """The selected marking: a dashed frame (not for lines) and its grips."""
painter.save()
- pen = QPen(QColor(255, 255, 255, 230), 1.0, Qt.PenStyle.DashLine)
- painter.setPen(pen)
- painter.setBrush(Qt.BrushStyle.NoBrush)
- painter.drawRect(box.adjusted(-pad, -pad, pad, pad))
+ painter.setRenderHint(QPainter.RenderHint.Antialiasing)
+ if shape.kind not in mm.SEGMENT_KINDS:
+ box = _qrect(mm.bounds(shape, qt_measure)).adjusted(-6, -6, 6, 6)
+ painter.setBrush(Qt.BrushStyle.NoBrush)
+ dark = QPen(QColor(0, 0, 0, 150), 1.5, Qt.PenStyle.DashLine)
+ painter.setPen(dark)
+ painter.drawRect(box)
+ light = QPen(QColor(255, 255, 255), 1.5, Qt.PenStyle.DashLine)
+ light.setDashOffset(3.0)
+ painter.setPen(light)
+ painter.drawRect(box)
+ for _name, (gx, gy) in mm.grips(shape, qt_measure):
+ painter.setPen(QPen(_ACCENT, 1.5))
+ painter.setBrush(QColor(255, 255, 255))
+ painter.drawEllipse(QPointF(gx, gy), _GRIP_R, _GRIP_R)
+ painter.restore()
+
+
+def background_brush(preset_id: str, rect: QRectF) -> QColor | QLinearGradient:
+ stops = dict(mm.BACKGROUND_PRESETS).get(preset_id) or mm.BACKGROUND_PRESETS[0][1]
+ if len(stops) == 1:
+ return QColor(stops[0])
+ gradient = QLinearGradient(rect.topLeft(), rect.bottomRight())
+ for i, stop in enumerate(stops):
+ gradient.setColorAt(i / (len(stops) - 1), QColor(stop))
+ return gradient
+
+
+def paint_background_frame(painter: QPainter, sel: QRectF, background: dict) -> None:
+ """Preview the background frame around the area, as the result will have it."""
+ pad = mm.frame_padding((sel.x(), sel.y(), sel.width(), sel.height()), background["padding"])
+ outer = sel.adjusted(-pad, -pad, pad, pad)
+ unit = max(1.0, max(sel.width(), sel.height()) / 1400.0)
+ radius = max(0.0, float(background["radius"])) * unit
+ ring = QPainterPath()
+ ring.addRoundedRect(outer, 6, 6)
+ hole = QPainterPath()
+ hole.addRoundedRect(sel, radius, radius)
+ painter.save()
+ painter.setRenderHint(QPainter.RenderHint.Antialiasing)
+ painter.fillPath(ring.subtracted(hole), background_brush(background["preset"], outer))
painter.restore()
# --------------------------------------------------------------------------
-# Hide patches from the frozen frame
+# Pixelate / blur previews from the frozen frame
# --------------------------------------------------------------------------
class HidePatches:
- """Blur/pixelate previews cut from the frozen frame, cached per rectangle."""
+ """Pixelate/blur previews cut from the frozen frame, cached per rectangle."""
def __init__(self, frozen: QPixmap | None, scale: Callable[[], float]) -> None:
self._frozen = frozen
@@ -357,7 +606,7 @@ class HidePatches:
if w < 1 or h < 1:
return None
scale = self._scale()
- key = (shape.kind, round(x), round(y), round(w), round(h))
+ key = (shape.mode, round(x), round(y), round(w), round(h))
cached = self._cache.get(key)
if cached is not None:
return cached
@@ -366,21 +615,13 @@ class HidePatches:
)
if source.isNull():
return None
- if shape.kind == mm.PIXELATE:
- block = max(2.0, _PIXEL_BLOCK * scale)
- small = source.scaled(
- max(1, round(source.width() / block)),
- max(1, round(source.height() / block)),
- Qt.AspectRatioMode.IgnoreAspectRatio,
- Qt.TransformationMode.SmoothTransformation,
- )
- else:
- small = source.scaled(
- max(1, round(source.width() / _BLUR_FACTOR)),
- max(1, round(source.height() / _BLUR_FACTOR)),
- Qt.AspectRatioMode.IgnoreAspectRatio,
- Qt.TransformationMode.SmoothTransformation,
- )
+ factor = _BLUR_FACTOR if shape.mode == "blur" else max(2.0, _PIXEL_BLOCK * scale)
+ small = source.scaled(
+ max(1, round(source.width() / factor)),
+ max(1, round(source.height() / factor)),
+ Qt.AspectRatioMode.IgnoreAspectRatio,
+ Qt.TransformationMode.SmoothTransformation,
+ )
if len(self._cache) > 64:
self._cache.clear()
self._cache[key] = small
@@ -393,10 +634,11 @@ class HidePatches:
def render_overlay(shapes: list[mm.Shape], sel: QRectF, scale: float) -> str:
- """Every non-hide marking on a transparent PNG of the selection, as base64.
+ """Every marking except redactions on a transparent PNG of the area, as base64.
``scale`` is device pixels per logical pixel, so the overlay is as sharp
- as the screen. The main process resizes it onto the real capture.
+ as the screen. The main process resizes it onto the real capture, after
+ it has pixelated or blurred the redactions on the capture's own pixels.
"""
width = max(1, round(sel.width() * scale))
height = max(1, round(sel.height() * scale))
@@ -407,8 +649,9 @@ def render_overlay(shapes: list[mm.Shape], sel: QRectF, scale: float) -> str:
painter.translate(-sel.x(), -sel.y())
painter.setClipRect(sel)
for shape in shapes:
- if shape.kind not in mm.HIDE_KINDS:
+ if shape.kind != mm.REDACT:
paint_shape(painter, shape)
+ paint_spotlights(painter, shapes, sel)
painter.end()
data = QByteArray()
buffer = QBuffer(data)
@@ -449,7 +692,7 @@ def _icon_pixmap(draw: Callable[[QPainter, QColor], None], colour: QColor, dpr:
return pixmap
-def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one table of glyphs
+def _glyphs() -> dict[str, Callable[[QPainter, QColor], None]]: # noqa: C901 - one glyph table
def move(p: QPainter, c: QColor) -> None:
path = QPainterPath(QPointF(4, 3))
for x, y in ((4, 15), (7.5, 11.5), (10, 16.5), (12, 15.5), (9.6, 10.6), (14.5, 10.6)):
@@ -458,15 +701,19 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
p.drawPath(path)
def rect(p: QPainter, c: QColor) -> None:
- p.drawRoundedRect(QRectF(3, 4.5, 12, 9), 1.5, 1.5)
+ p.drawRoundedRect(QRectF(3, 4.5, 12, 9), 2, 2)
+
+ def filled(p: QPainter, c: QColor) -> None:
+ p.setBrush(c)
+ p.drawRoundedRect(QRectF(3, 4.5, 12, 9), 2, 2)
def ellipse(p: QPainter, c: QColor) -> None:
p.drawEllipse(QRectF(2.5, 4, 13, 10))
def arrow(p: QPainter, c: QColor) -> None:
- p.drawLine(QPointF(4, 14), QPointF(14, 4))
- p.drawLine(QPointF(14, 4), QPointF(8, 4))
- p.drawLine(QPointF(14, 4), QPointF(14, 10))
+ p.setPen(Qt.PenStyle.NoPen)
+ p.setBrush(c)
+ p.drawPolygon(_polygon(tapered_arrow_outline((3.5, 14.5), (15, 3), 2.2)))
def line(p: QPainter, c: QColor) -> None:
p.drawLine(QPointF(4, 14), QPointF(14, 4))
@@ -487,8 +734,17 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
p.drawLine(QPointF(3, 15.5), QPointF(15, 15.5))
def text(p: QPainter, c: QColor) -> None:
- p.drawLine(QPointF(4, 4.5), QPointF(14, 4.5))
- p.drawLine(QPointF(9, 4.5), QPointF(9, 14.5))
+ # A text field with a bold T in it — reads as "write text here".
+ frame = QColor(c)
+ frame.setAlpha(150)
+ dashed = QPen(frame, 1.1, Qt.PenStyle.DashLine)
+ p.setPen(dashed)
+ p.drawRoundedRect(QRectF(1.5, 1.5, 15, 15), 3, 3)
+ bold = QPen(c, 2.3)
+ bold.setCapStyle(Qt.PenCapStyle.RoundCap)
+ p.setPen(bold)
+ p.drawLine(QPointF(5.5, 5.5), QPointF(12.5, 5.5))
+ p.drawLine(QPointF(9, 5.5), QPointF(9, 13))
def counter(p: QPainter, c: QColor) -> None:
p.drawEllipse(QRectF(2.5, 2.5, 13, 13))
@@ -498,6 +754,14 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
p.setFont(font)
p.drawText(QRectF(2.5, 2.5, 13, 13), Qt.AlignmentFlag.AlignCenter, "1")
+ def redact(p: QPainter, c: QColor) -> None:
+ p.setPen(Qt.PenStyle.NoPen)
+ for i in range(3):
+ for j in range(3):
+ shade = QColor(c)
+ shade.setAlpha(255 if (i + j) % 2 == 0 else 110)
+ p.fillRect(QRectF(3 + i * 4.2, 3 + j * 4.2, 3.6, 3.6), shade)
+
def blur(p: QPainter, c: QColor) -> None:
path = QPainterPath(QPointF(9, 2.5))
path.cubicTo(QPointF(9, 2.5), QPointF(3.5, 9), QPointF(3.5, 11.5))
@@ -506,13 +770,53 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
path.cubicTo(QPointF(14.5, 9), QPointF(9, 2.5), QPointF(9, 2.5))
p.drawPath(path)
- def pixelate(p: QPainter, c: QColor) -> None:
+ def spotlight(p: QPainter, c: QColor) -> None:
+ p.drawEllipse(QRectF(4.5, 4.5, 9, 9))
+ for x0, y0, x1, y1 in ((9, 1.5, 9, 3), (9, 15, 9, 16.5), (1.5, 9, 3, 9), (15, 9, 16.5, 9)):
+ p.drawLine(QPointF(x0, y0), QPointF(x1, y1))
+
+ def background(p: QPainter, c: QColor) -> None:
+ p.drawRoundedRect(QRectF(2, 3, 14, 12), 2.5, 2.5)
+ p.drawEllipse(QPointF(6.5, 7), 1.4, 1.4)
+ path = QPainterPath(QPointF(2.5, 13.5))
+ path.lineTo(QPointF(7, 9.5))
+ path.lineTo(QPointF(10, 12))
+ path.lineTo(QPointF(12.5, 10))
+ path.lineTo(QPointF(15.5, 13))
+ p.drawPath(path)
+
+ def classic(p: QPainter, c: QColor) -> None:
+ p.drawLine(QPointF(4, 14), QPointF(13, 5))
p.setPen(Qt.PenStyle.NoPen)
- for i in range(3):
- for j in range(3):
- shade = QColor(c)
- shade.setAlpha(255 if (i + j) % 2 == 0 else 110)
- p.fillRect(QRectF(3 + i * 4.2, 3 + j * 4.2, 3.6, 3.6), shade)
+ p.setBrush(c)
+ p.drawPolygon(_polygon([(15, 3), (9.5, 4.6), (13.4, 8.5)]))
+
+ def double(p: QPainter, c: QColor) -> None:
+ p.drawLine(QPointF(5, 13), QPointF(13, 5))
+ p.setPen(Qt.PenStyle.NoPen)
+ p.setBrush(c)
+ p.drawPolygon(_polygon([(15, 3), (9.5, 4.6), (13.4, 8.5)]))
+ p.drawPolygon(_polygon([(3, 15), (4.6, 9.5), (8.5, 13.4)]))
+
+ def text_plain(p: QPainter, c: QColor) -> None:
+ font = text_font(12)
+ p.setFont(font)
+ p.drawText(QRectF(0, 0, 18, 18), Qt.AlignmentFlag.AlignCenter, "Aa")
+
+ def text_label(p: QPainter, c: QColor) -> None:
+ p.setPen(Qt.PenStyle.NoPen)
+ p.setBrush(c)
+ p.drawRoundedRect(QRectF(1.5, 3.5, 15, 11), 3, 3)
+ p.setPen(QColor(30, 30, 34) if c.lightness() > 128 else QColor(250, 250, 250))
+ p.setFont(text_font(10))
+ p.drawText(QRectF(0, 0, 18, 18), Qt.AlignmentFlag.AlignCenter, "Aa")
+
+ def text_outline(p: QPainter, c: QColor) -> None:
+ path = QPainterPath()
+ path.addText(QPointF(1.5, 13.5), text_font(12), "Aa")
+ p.setBrush(Qt.BrushStyle.NoBrush)
+ p.setPen(QPen(c, 1.0))
+ p.drawPath(path)
def undo(p: QPainter, c: QColor) -> None:
path = QPainterPath(QPointF(5, 7))
@@ -561,9 +865,14 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
path.lineTo(QPointF(14, 5.5))
p.drawPath(path)
- table = {
+ def off(p: QPainter, c: QColor) -> None:
+ p.drawEllipse(QRectF(3, 3, 12, 12))
+ p.drawLine(QPointF(5, 13), QPointF(13, 5))
+
+ return {
mm.MOVE: move,
mm.RECT: rect,
+ mm.FILLED: filled,
mm.ELLIPSE: ellipse,
mm.ARROW: arrow,
mm.LINE: line,
@@ -571,8 +880,18 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
mm.HIGHLIGHT: highlight,
mm.TEXT: text,
mm.COUNTER: counter,
- mm.BLUR: blur,
- mm.PIXELATE: pixelate,
+ mm.REDACT: redact,
+ mm.SPOTLIGHT: spotlight,
+ mm.BACKGROUND: background,
+ "arrow_tapered": arrow,
+ "arrow_classic": classic,
+ "arrow_double": double,
+ "text_plain": text_plain,
+ "text_label": text_label,
+ "text_outline": text_outline,
+ "redact_pixelate": redact,
+ "redact_blur": blur,
+ "background_off": off,
"undo": undo,
"redo": redo,
"copy": copy,
@@ -581,16 +900,23 @@ def _glyph(name: str) -> Callable[[QPainter, QColor], None]: # noqa: C901 - one
"cancel": cancel,
"done": done,
}
- return table[name]
class Toolbar(QFrame):
- """The floating marking toolbar, docked to the selection by its window."""
+ """The floating marking toolbar, docked to the selection by its window.
+
+ The main row holds the tools, colours, size, history and the finishing
+ actions; an option row under it appears for tools that have looks to
+ choose from (arrow, text, pixelate/blur, background).
+ """
tool_chosen = Signal(str)
color_chosen = Signal(str)
width_cycled = Signal()
action = Signal(str)
+ #: ``(group, value)``: arrow / text / redact style, or background preset
+ #: (``"off"`` switches the frame off).
+ option_chosen = Signal(str, str)
_BTN = 30
_SWATCH = 18
@@ -602,6 +928,7 @@ class Toolbar(QFrame):
self.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self._labels = labels_for(language)
self._dpr = max(1.0, dpr)
+ self._glyphs = _glyphs()
dark = _dark_hud()
self._fg = QColor(236, 236, 240) if dark else QColor(28, 28, 32)
bg = "rgba(28, 28, 31, 242)" if dark else "rgba(250, 250, 252, 245)"
@@ -612,11 +939,13 @@ class Toolbar(QFrame):
f"""
QFrame#hud {{ background: {bg}; border: 1px solid {edge}; border-radius: 11px; }}
QFrame#sep {{ background: {sep}; border: none; }}
+ QFrame#rowsep {{ background: {sep}; border: none; }}
QToolButton {{ border: none; border-radius: 7px; background: transparent; }}
QToolButton:hover {{ background: {hover}; }}
QToolButton:checked {{ background: #0A84FF; }}
QToolButton#done {{ background: #0A84FF; }}
QToolButton#done:hover {{ background: #3B9BFF; }}
+ QToolButton#swatch:checked {{ background: transparent; }}
QToolButton:disabled {{ background: transparent; }}
"""
)
@@ -626,47 +955,79 @@ class Toolbar(QFrame):
shadow.setColor(QColor(0, 0, 0, 110))
self.setGraphicsEffect(shadow)
- layout = QHBoxLayout(self)
- layout.setContentsMargins(6, 5, 6, 5)
- layout.setSpacing(2)
+ rows = QVBoxLayout(self)
+ rows.setContentsMargins(6, 5, 6, 5)
+ rows.setSpacing(4)
+ main = QHBoxLayout()
+ main.setSpacing(2)
+ rows.addLayout(main)
+ self._rowsep = QFrame(self)
+ self._rowsep.setObjectName("rowsep")
+ self._rowsep.setFixedHeight(1)
+ rows.addWidget(self._rowsep)
+ self._options = QHBoxLayout()
+ self._options.setSpacing(2)
+ rows.addLayout(self._options)
+ self._option_buttons: list[QToolButton] = []
+
self._tools = QButtonGroup(self)
self._tools.setExclusive(True)
self._tool_buttons: dict[str, QToolButton] = {}
- for kind, key in mm.TOOL_KEYS.items():
- button = self._button(kind, f"{self._labels[kind]} ({key})", checkable=True)
+ for kind in TOOL_ORDER:
+ tip = f"{self._labels[kind]} ({mm.TOOL_KEYS[kind]})"
+ button = self._button(kind, tip, checkable=True)
self._tools.addButton(button)
self._tool_buttons[kind] = button
button.clicked.connect(lambda _c=False, k=kind: self.tool_chosen.emit(k))
- layout.addWidget(button)
- layout.addWidget(self._separator())
+ if kind == mm.BACKGROUND:
+ main.addWidget(self._separator())
+ main.addWidget(button)
+ main.addWidget(self._separator())
self._swatches: dict[str, QToolButton] = {}
- for index, colour in enumerate(mm.PALETTE):
+ for colour in mm.PALETTE:
swatch = QToolButton(self)
+ swatch.setObjectName("swatch")
swatch.setFocusPolicy(Qt.FocusPolicy.NoFocus)
swatch.setCheckable(True)
swatch.setFixedSize(QSize(self._SWATCH + 4, self._BTN))
- swatch.setToolTip(f"{self._labels['color']} ({index + 1})")
+ swatch.setIconSize(QSize(self._SWATCH, self._SWATCH))
+ swatch.setToolTip(self._labels["color"])
swatch.clicked.connect(lambda _c=False, col=colour: self.color_chosen.emit(col))
self._swatches[colour] = swatch
- layout.addWidget(swatch)
+ main.addWidget(swatch)
self._width = self._button("width", self._labels["width"], glyph=False)
self._width.clicked.connect(lambda _c=False: self.width_cycled.emit())
- layout.addWidget(self._width)
- layout.addWidget(self._separator())
- self._undo = self._action_button("undo", layout)
- self._redo = self._action_button("redo", layout)
- layout.addWidget(self._separator())
+ main.addWidget(self._width)
+ main.addWidget(self._separator())
+ self._undo = self._action_button("undo", main)
+ self._redo = self._action_button("redo", main)
+ main.addWidget(self._separator())
for name in ("copy", "save", "edit"):
- self._action_button(name, layout)
- layout.addWidget(self._separator())
- self._action_button("cancel", layout)
- done = self._action_button("done", layout, colour=QColor(255, 255, 255))
+ self._action_button(name, main)
+ main.addWidget(self._separator())
+ self._action_button("cancel", main)
+ done = self._action_button("done", main, colour=QColor(255, 255, 255))
done.setObjectName("done")
self._colour = mm.PALETTE[0]
self._width_value = mm.WIDTHS[mm.DEFAULT_WIDTH_INDEX]
+ self.set_options([], "")
self.adjustSize()
# -- building ------------------------------------------------------------
+ def _icon(self, name: str, colour: QColor | None = None) -> QIcon:
+ icon = QIcon()
+ draw = self._glyphs[name]
+ normal = colour or self._fg
+ disabled = QColor(normal)
+ disabled.setAlpha(80)
+ for tint, mode, state in (
+ (normal, QIcon.Mode.Normal, QIcon.State.Off),
+ (QColor(255, 255, 255), QIcon.Mode.Normal, QIcon.State.On),
+ (disabled, QIcon.Mode.Disabled, QIcon.State.Off),
+ ):
+ icon.addPixmap(_icon_pixmap(draw, tint, self._dpr), mode, state)
+ return icon
+
def _button(
self,
name: str,
@@ -683,18 +1044,7 @@ class Toolbar(QFrame):
button.setToolTip(tip)
button.setCheckable(checkable)
if glyph:
- icon = QIcon()
- draw = _glyph(name)
- normal = colour or self._fg
- disabled = QColor(normal)
- disabled.setAlpha(80)
- for tint, mode, state in (
- (normal, QIcon.Mode.Normal, QIcon.State.Off),
- (QColor(255, 255, 255), QIcon.Mode.Normal, QIcon.State.On),
- (disabled, QIcon.Mode.Disabled, QIcon.State.Off),
- ):
- icon.addPixmap(_icon_pixmap(draw, tint, self._dpr), mode, state)
- button.setIcon(icon)
+ button.setIcon(self._icon(name, colour))
return button
def _action_button(
@@ -712,24 +1062,7 @@ class Toolbar(QFrame):
line.setFixedSize(QSize(1, 18))
return line
- # -- state ---------------------------------------------------------------
- def set_tool(self, kind: str) -> None:
- button = self._tool_buttons.get(kind)
- if button is not None:
- button.setChecked(True)
-
- def set_color(self, colour: str) -> None:
- self._colour = colour
- for value, swatch in self._swatches.items():
- chosen = value == colour
- swatch.setChecked(chosen)
- # The ring in the icon marks the choice; no blue checked fill.
- swatch.setStyleSheet("QToolButton:checked { background: transparent; }")
- swatch.setIcon(self._swatch_icon(value, chosen))
- swatch.setIconSize(QSize(self._SWATCH, self._SWATCH))
- self._refresh_width()
-
- def _swatch_icon(self, colour: str, chosen: bool) -> QIcon:
+ def _swatch_icon(self, colour: str, chosen: bool, *, square: bool = False) -> QIcon:
size = self._SWATCH
pixmap = QPixmap(round(size * self._dpr), round(size * self._dpr))
pixmap.setDevicePixelRatio(self._dpr)
@@ -739,14 +1072,37 @@ class Toolbar(QFrame):
if chosen:
painter.setPen(QPen(self._fg, 1.6))
painter.setBrush(Qt.BrushStyle.NoBrush)
- painter.drawEllipse(QRectF(0.8, 0.8, size - 1.6, size - 1.6))
- painter.setPen(QPen(QColor(128, 128, 128, 150), 1.0))
- painter.setBrush(QColor(colour))
+ outer = QRectF(0.8, 0.8, size - 1.6, size - 1.6)
+ if square:
+ painter.drawRoundedRect(outer, 4, 4)
+ else:
+ painter.drawEllipse(outer)
inset = 3.6 if chosen else 2.4
- painter.drawEllipse(QRectF(inset, inset, size - 2 * inset, size - 2 * inset))
+ inner = QRectF(inset, inset, size - 2 * inset, size - 2 * inset)
+ painter.setPen(QPen(QColor(128, 128, 128, 150), 1.0))
+ if square:
+ painter.setBrush(background_brush(colour, inner))
+ painter.drawRoundedRect(inner, 3, 3)
+ else:
+ painter.setBrush(QColor(colour))
+ painter.drawEllipse(inner)
painter.end()
return QIcon(pixmap)
+ # -- state ---------------------------------------------------------------
+ def set_tool(self, kind: str) -> None:
+ button = self._tool_buttons.get(kind)
+ if button is not None:
+ button.setChecked(True)
+
+ def set_color(self, colour: str) -> None:
+ self._colour = colour
+ for value, swatch in self._swatches.items():
+ chosen = value.upper() == colour.upper()
+ swatch.setChecked(chosen)
+ swatch.setIcon(self._swatch_icon(value, chosen))
+ self._refresh_width()
+
def set_width(self, width: float) -> None:
self._width_value = width
self._refresh_width()
@@ -769,15 +1125,56 @@ class Toolbar(QFrame):
self._undo.setEnabled(can_undo)
self._redo.setEnabled(can_redo)
+ def set_options(self, options: list[tuple[str, str]], chosen: str, group: str = "") -> None:
+ """Fill the option row: ``(value, kind)`` pairs, ``kind`` = glyph or ``preset``."""
+ # Empty the row completely — buttons and the stretch after them.
+ while self._options.count():
+ item = self._options.takeAt(0)
+ widget = item.widget() if item is not None else None
+ if widget is not None:
+ widget.hide()
+ widget.deleteLater()
+ self._option_buttons = []
+ for value, kind in options:
+ button = QToolButton(self)
+ button.setFocusPolicy(Qt.FocusPolicy.NoFocus)
+ button.setCheckable(True)
+ button.setChecked(value == chosen)
+ button.setFixedSize(QSize(self._BTN, 26))
+ button.setIconSize(QSize(18, 18))
+ if kind == "preset":
+ button.setObjectName("swatch")
+ button.setIcon(self._swatch_icon(value, value == chosen, square=True))
+ button.setToolTip(value)
+ else:
+ button.setIcon(self._icon(kind))
+ button.setToolTip(self._labels.get(kind, value))
+ button.clicked.connect(lambda _c=False, g=group, v=value: self.option_chosen.emit(g, v))
+ self._options.addWidget(button)
+ self._option_buttons.append(button)
+ if self._option_buttons:
+ self._options.addStretch(1)
+ visible = bool(self._option_buttons)
+ self._rowsep.setVisible(visible)
+ self.adjustSize()
+
__all__ = [
"DIM_OUTSIDE",
+ "TOOL_ORDER",
"HidePatches",
"Toolbar",
+ "background_brush",
+ "contrast_on",
"labels_for",
+ "paint_background_frame",
"paint_picked",
"paint_selection_frame",
"paint_shape",
+ "paint_spotlights",
+ "paint_typing",
+ "qt_measure",
"render_overlay",
- "text_rect",
+ "tapered_arrow_outline",
+ "text_font",
]
diff --git a/jarvis/appshot/picker/markup_model.py b/jarvis/appshot/picker/markup_model.py
index 12acfd9a74..592f4d445c 100644
--- a/jarvis/appshot/picker/markup_model.py
+++ b/jarvis/appshot/picker/markup_model.py
@@ -1,9 +1,12 @@
"""The picker's markings as plain data — no Qt, so the rules are testable anywhere.
-After an area is chosen the picker stays open and the user
-marks it up in place: boxes, circles, arrows, lines, pen and highlighter
-strokes, text, numbered steps, and blur or pixelate patches. Every marking is
-a :class:`Shape` in the overlay window's logical pixels; the Qt side
+After an area is chosen the picker stays open and the user marks it up in
+place with the same tools as the full appshot editor
+(``frontend/src/lib/appshotEditorModel.ts``): select and move, rectangle,
+filled rectangle, ellipse, line, arrow (three looks), text (three looks),
+pixelate/blur, spotlight, numbered steps, pen, highlighter and a background
+frame — with the same one-letter keys. Every marking is a :class:`Shape` in
+the overlay window's logical pixels; the Qt side
(:mod:`jarvis.appshot.picker.annotate`) only paints them and turns mouse and
keys into calls here.
@@ -14,13 +17,17 @@ the whole list cheaply and these rules run on a headless CI box.
from __future__ import annotations
import math
+from collections.abc import Callable
from dataclasses import dataclass, field, replace
Point = tuple[float, float]
#: ``(left, top, width, height)``.
Box = tuple[float, float, float, float]
+#: Width of ``text`` drawn at ``size`` px — the Qt side passes a real one.
+Measure = Callable[[str, float], float]
RECT = "rect"
+FILLED = "filled"
ELLIPSE = "ellipse"
ARROW = "arrow"
LINE = "line"
@@ -28,37 +35,44 @@ PEN = "pen"
HIGHLIGHT = "highlight"
TEXT = "text"
COUNTER = "counter"
-BLUR = "blur"
-PIXELATE = "pixelate"
-#: Not a marking: picks, moves and deletes existing ones.
+SPOTLIGHT = "spotlight"
+REDACT = "redact"
+#: Not markings: the select tool, and the background frame switch.
MOVE = "move"
+BACKGROUND = "background"
#: Drawn by dragging a box.
-BOX_KINDS = frozenset({RECT, ELLIPSE, BLUR, PIXELATE})
+BOX_KINDS = frozenset({RECT, FILLED, ELLIPSE, SPOTLIGHT, REDACT})
#: Drawn by dragging from a start to an end point.
SEGMENT_KINDS = frozenset({ARROW, LINE})
#: Drawn by dragging a free path.
STROKE_KINDS = frozenset({PEN, HIGHLIGHT})
-#: Hide what is under them. They are applied to the real capture first, under
-#: every other marking, so the picker paints them first as well.
-HIDE_KINDS = frozenset({BLUR, PIXELATE})
+#: Cover what lies in them: picked by a click inside only when nothing drawn
+#: on top is hit, and only by the select tool.
+AREA_KINDS = frozenset({SPOTLIGHT, REDACT})
-#: Tool order in the toolbar, with the key that selects each one.
+#: Tool → key, in the full editor's toolbar order; the same keys as there.
TOOL_KEYS: dict[str, str] = {
MOVE: "V",
RECT: "R",
+ FILLED: "F",
ELLIPSE: "E",
- ARROW: "A",
LINE: "L",
- PEN: "P",
- HIGHLIGHT: "H",
+ ARROW: "A",
TEXT: "T",
- COUNTER: "N",
- BLUR: "B",
- PIXELATE: "X",
+ REDACT: "P",
+ SPOTLIGHT: "H",
+ COUNTER: "C",
+ PEN: "D",
+ HIGHLIGHT: "M",
+ BACKGROUND: "B",
}
-#: Marking colours; red is the default.
+ARROW_STYLES = ("tapered", "classic", "double")
+TEXT_STYLES = ("plain", "label", "outline")
+REDACT_MODES = ("pixelate", "blur")
+
+#: Marking colours — the full editor's presets.
PALETTE: tuple[str, ...] = (
"#FF3B30",
"#FF9500",
@@ -67,19 +81,35 @@ PALETTE: tuple[str, ...] = (
"#0A84FF",
"#AF52DE",
"#FFFFFF",
- "#111111",
+ "#1C1C1E",
)
-#: Stroke widths the mouse wheel and the width button step through (logical px).
+#: Stroke widths (logical px): keys 1-5, the mouse wheel, the width button.
WIDTHS: tuple[float, ...] = (2.0, 3.0, 4.0, 6.0, 9.0)
DEFAULT_WIDTH_INDEX = 1
+#: Background frames: the full editor's presets (id, colour stops), kept in
+#: step by a parity test.
+BACKGROUND_PRESETS: tuple[tuple[str, tuple[str, ...]], ...] = (
+ ("dawn", ("#ff9a8b", "#ff6a88", "#ff99ac")),
+ ("lagoon", ("#43cea2", "#185a9d")),
+ ("dusk", ("#7f7fd5", "#86a8e7", "#91eae4")),
+ ("ember", ("#f7971e", "#ffd200")),
+ ("orchid", ("#c471f5", "#fa71cd")),
+ ("slate", ("#434343", "#1c1c1e")),
+ ("paper", ("#f5f5f7",)),
+ ("ink", ("#111113",)),
+)
+DEFAULT_BACKGROUND = {"preset": "dusk", "padding": 0.08, "radius": 12, "shadow": True}
+
#: A drag shorter than this (logical px) draws nothing, except where a click
#: is the whole gesture (text, counter).
-MIN_DRAG_PX = 3.0
-#: Grab distance for the selection's resize handles.
+MIN_DRAG_PX = 4.0
+#: Grab distance for the selection's resize handles and a marking's grips.
HANDLE_GRAB_PX = 8.0
#: The selection never shrinks below this while it is resized.
MIN_SELECTION_PX = 8.0
+#: The smallest text and counter a grip can shrink them to (px).
+MIN_TEXT_PX = 8.0
@dataclass(slots=True)
@@ -87,7 +117,9 @@ class Shape:
"""One marking. What ``points`` holds depends on ``kind``:
box kinds and segments: ``[start, end]``; strokes: the whole path; text:
- ``[anchor]`` (top-left of the first line); counter: ``[centre]``.
+ ``[top-left]``; counter: ``[centre]``. ``size`` is the text height or the
+ counter's radius; ``style`` the arrow or text look; ``mode`` pixelate or
+ blur for a redaction.
"""
kind: str
@@ -96,11 +128,47 @@ class Shape:
points: list[Point] = field(default_factory=list)
text: str = ""
number: int = 0
+ size: float = 0.0
+ style: str = ""
+ mode: str = ""
def copy(self) -> Shape:
return replace(self, points=list(self.points))
+def rough_measure(text: str, size: float) -> float:
+ return len(text) * size * 0.55
+
+
+# --------------------------------------------------------------------------
+# Sizes
+# --------------------------------------------------------------------------
+
+
+def text_size(width: float) -> float:
+ """Text height for a stroke width (the width step scales new text too)."""
+ return max(14.0, round(width * 5.0))
+
+
+def counter_size(width: float) -> float:
+ """Counter badge radius for a stroke width."""
+ return max(12.0, round(width * 3.0))
+
+
+def grab_scope(tool: str) -> str:
+ """What a press with ``tool`` may take hold of instead of drawing.
+
+ ``any``: the select tool picks up every marking; ``selected``: a shape
+ tool reshapes or moves only the marking it has selected (the one it just
+ drew); ``none``: pen, highlighter and counter always draw.
+ """
+ if tool == MOVE:
+ return "any"
+ if tool in (PEN, HIGHLIGHT, COUNTER, BACKGROUND):
+ return "none"
+ return "selected"
+
+
# --------------------------------------------------------------------------
# Geometry
# --------------------------------------------------------------------------
@@ -129,33 +197,27 @@ def constrain(kind: str, start: Point, end: Point) -> Point:
return end
-def counter_radius(width: float) -> float:
- return 9.0 + width * 1.6
-
-
-def text_size(width: float) -> float:
- """Text height in logical px for a stroke width — the width step scales text too."""
- return 12.0 + width * 2.5
+def label_pad(shape: Shape) -> float:
+ return shape.size * 0.4 if shape.kind == TEXT and shape.style == "label" else 0.0
-def text_box(shape: Shape, char_w: float = 0.6) -> Box:
- """A text marking's rough bounds (the Qt side measures exactly when painting)."""
- size = text_size(shape.width)
- lines = shape.text.split("\n") or [""]
- width = max(len(line) for line in lines) * size * char_w
- height = len(lines) * size * 1.25
- x, y = shape.points[0]
- return (x, y, max(width, size * 0.5), height)
+def text_lines(shape: Shape) -> list[str]:
+ return shape.text.split("\n") or [""]
-def bounds(shape: Shape) -> Box:
+def bounds(shape: Shape, measure: Measure = rough_measure) -> Box:
"""The area a marking covers, before its stroke width."""
if not shape.points:
return (0.0, 0.0, 0.0, 0.0)
if shape.kind == TEXT:
- return text_box(shape)
+ lines = text_lines(shape)
+ width = max(measure(line, shape.size) for line in lines)
+ width = max(width, shape.size * 0.5)
+ pad = label_pad(shape)
+ x, y = shape.points[0]
+ return (x - pad, y - pad, width + pad * 2, len(lines) * shape.size * 1.25 + pad * 2)
if shape.kind == COUNTER:
- r = counter_radius(shape.width)
+ r = shape.size
x, y = shape.points[0]
return (x - r, y - r, 2 * r, 2 * r)
xs = [p[0] for p in shape.points]
@@ -174,38 +236,58 @@ def _distance_to_segment(p: Point, a: Point, b: Point) -> float:
return math.hypot(p[0] - (ax + t * dx), p[1] - (ay + t * dy))
-def hit(shape: Shape, p: Point, tolerance: float = 6.0) -> bool:
- """Whether a click at ``p`` lands on ``shape`` (outlines, not empty insides)."""
- tol = tolerance + shape.width / 2.0
- if shape.kind in (TEXT, COUNTER, BLUR, PIXELATE):
- x, y, w, h = bounds(shape)
- return x - tol <= p[0] <= x + w + tol and y - tol <= p[1] <= y + h + tol
+def _inside(p: Point, box: Box, slop: float) -> bool:
+ x, y, w, h = box
+ return x - slop <= p[0] <= x + w + slop and y - slop <= p[1] <= y + h + slop
+
+
+def hit(shape: Shape, p: Point, slop: float = 6.0, measure: Measure = rough_measure) -> bool:
+ """Whether a click at ``p`` lands on ``shape``.
+
+ Lines and strokes count near their path, outlines near their edge, filled
+ boxes, text, counters and areas anywhere on them.
+ """
+ if not shape.points:
+ return False
if shape.kind in SEGMENT_KINDS or shape.kind in STROKE_KINDS:
+ reach = slop + (shape.width * 2.0 if shape.kind == HIGHLIGHT else shape.width)
pts = shape.points
if len(pts) == 1:
- return math.hypot(p[0] - pts[0][0], p[1] - pts[0][1]) <= tol
- return any(_distance_to_segment(p, a, b) <= tol for a, b in zip(pts, pts[1:], strict=False))
- x, y, w, h = normalized(shape.points[0], shape.points[-1])
+ return math.hypot(p[0] - pts[0][0], p[1] - pts[0][1]) <= reach
+ pairs = zip(pts, pts[1:], strict=False)
+ return any(_distance_to_segment(p, a, b) <= reach for a, b in pairs)
if shape.kind == RECT:
- inside_outer = x - tol <= p[0] <= x + w + tol and y - tol <= p[1] <= y + h + tol
- inside_inner = x + tol < p[0] < x + w - tol and y + tol < p[1] < y + h - tol
- return inside_outer and not inside_inner
+ reach = slop + shape.width
+ x, y, w, h = normalized(shape.points[0], shape.points[-1])
+ inner = (x + reach, y + reach, w - reach * 2, h - reach * 2)
+ return _inside(p, (x, y, w, h), reach) and not (
+ inner[2] > 0 and inner[3] > 0 and _inside(p, inner, 0)
+ )
if shape.kind == ELLIPSE:
- rx, ry = w / 2.0, h / 2.0
- if rx <= 0 or ry <= 0:
- return False
- cx, cy = x + rx, y + ry
- # Normalised radial distance; 1.0 is the outline.
- d = math.hypot((p[0] - cx) / rx, (p[1] - cy) / ry)
- return abs(d - 1.0) * min(rx, ry) <= tol
- return False
-
-
-def shape_at(shapes: list[Shape], p: Point) -> int | None:
- """Index of the top-most marking under ``p``."""
- for index in range(len(shapes) - 1, -1, -1):
- if hit(shapes[index], p):
- return index
+ x, y, w, h = normalized(shape.points[0], shape.points[-1])
+ rx, ry = max(1.0, w / 2.0), max(1.0, h / 2.0)
+ d = math.hypot((p[0] - (x + rx)) / rx, (p[1] - (y + ry)) / ry)
+ return abs(d - 1.0) * min(rx, ry) <= slop + shape.width
+ return _inside(p, bounds(shape, measure), slop)
+
+
+def shape_at(
+ shapes: list[Shape],
+ p: Point,
+ measure: Measure = rough_measure,
+ *,
+ areas: bool = True,
+ slop: float = 6.0,
+) -> int | None:
+ """Index of the top-most marking under ``p``; areas only when nothing else hits."""
+ passes = (False, True) if areas else (False,)
+ for want_area in passes:
+ for index in range(len(shapes) - 1, -1, -1):
+ shape = shapes[index]
+ if (shape.kind in AREA_KINDS) != want_area:
+ continue
+ if hit(shape, p, slop, measure):
+ return index
return None
@@ -221,10 +303,10 @@ def is_meaningful(shape: Shape) -> bool:
return bool(shape.text.strip())
if shape.kind == COUNTER:
return bool(shape.points)
+ if shape.kind in STROKE_KINDS:
+ return bool(shape.points)
if len(shape.points) < 2:
return False
- if shape.kind in STROKE_KINDS:
- return True
x, y, w, h = normalized(shape.points[0], shape.points[-1])
if shape.kind in SEGMENT_KINDS:
return math.hypot(w, h) >= MIN_DRAG_PX
@@ -236,6 +318,106 @@ def next_counter(shapes: list[Shape]) -> int:
return max((s.number for s in shapes if s.kind == COUNTER), default=0) + 1
+# --------------------------------------------------------------------------
+# Grips: reshaping a marking in place (the full editor's rules)
+# --------------------------------------------------------------------------
+
+_CORNERS = ("nw", "ne", "sw", "se")
+_OPPOSITE = {"nw": "se", "ne": "sw", "sw": "ne", "se": "nw"}
+
+
+def _corner(box: Box, name: str) -> Point:
+ x, y, w, h = box
+ return (x if name in ("nw", "sw") else x + w, y if name in ("nw", "ne") else y + h)
+
+
+def grips(shape: Shape, measure: Measure = rough_measure) -> list[tuple[str, Point]]:
+ """The grips a selected marking shows.
+
+ Lines and arrows one at each end; boxes, strokes and text one at each
+ corner (text scales with them); a counter one on its rim.
+ """
+ if not shape.points:
+ return []
+ if shape.kind in SEGMENT_KINDS:
+ return [("from", shape.points[0]), ("to", shape.points[-1])]
+ if shape.kind == COUNTER:
+ x, y = shape.points[0]
+ return [("size", (x + shape.size, y))]
+ box = bounds(shape, measure)
+ return [(name, _corner(box, name)) for name in _CORNERS]
+
+
+def grip_at(
+ shape: Shape, p: Point, radius: float = HANDLE_GRAB_PX, measure: Measure = rough_measure
+) -> str | None:
+ """The grip under ``p`` (within ``radius``), nearest first."""
+ best, best_distance = None, radius
+ for name, (gx, gy) in grips(shape, measure):
+ distance = math.hypot(gx - p[0], gy - p[1])
+ if distance <= best_distance:
+ best, best_distance = name, distance
+ return best
+
+
+def reshape(original: Shape, grip: str, p: Point, measure: Measure = rough_measure) -> Shape:
+ """``original`` with ``grip`` dragged to ``p``.
+
+ Always computed from the marking as it was when the drag began, so a long
+ drag never drifts. An arrow's end follows the pointer; a box keeps the
+ opposite corner fixed; a stroke scales from it; text grows or shrinks
+ from the opposite corner, font size and all; a counter grows with its rim.
+ """
+ shape = original.copy()
+ if shape.kind in SEGMENT_KINDS:
+ if grip == "from":
+ shape.points[0] = p
+ elif grip == "to":
+ shape.points[-1] = p
+ return shape
+ if shape.kind == COUNTER:
+ x, y = shape.points[0]
+ shape.size = max(MIN_TEXT_PX, math.hypot(p[0] - x, p[1] - y))
+ return shape
+ if grip not in _OPPOSITE:
+ return shape
+ box = bounds(original, measure)
+ anchor = _corner(box, _OPPOSITE[grip])
+ if shape.kind == TEXT:
+ factor = max(0.1, abs(p[1] - anchor[1]) / max(1.0, box[3]))
+ shape.size = max(MIN_TEXT_PX, round(original.size * factor))
+ grown = bounds(replace(shape, points=[(0.0, 0.0)]), measure)
+ pad = label_pad(shape)
+ # Keep the opposite corner where it was.
+ left = anchor[0] if grip in ("ne", "se") else anchor[0] - grown[2]
+ top = anchor[1] if grip in ("sw", "se") else anchor[1] - grown[3]
+ shape.points = [(left + pad, top + pad)]
+ return shape
+ if shape.kind in STROKE_KINDS:
+ corner = _corner(box, grip)
+ span_x, span_y = corner[0] - anchor[0], corner[1] - anchor[1]
+ sx = 1.0 if abs(span_x) < 1e-6 else (p[0] - anchor[0]) / span_x
+ sy = 1.0 if abs(span_y) < 1e-6 else (p[1] - anchor[1]) / span_y
+ shape.points = [
+ (anchor[0] + (x - anchor[0]) * sx, anchor[1] + (y - anchor[1]) * sy)
+ for x, y in original.points
+ ]
+ return shape
+ shape.points = [anchor, p]
+ return shape
+
+
+def grip_cursor(grip: str) -> str:
+ """``fdiag`` / ``bdiag`` / ``hor`` / ``grab`` — mapped to Qt cursors by the caller."""
+ if grip in ("nw", "se"):
+ return "fdiag"
+ if grip in ("ne", "sw"):
+ return "bdiag"
+ if grip == "size":
+ return "hor"
+ return "grab"
+
+
# --------------------------------------------------------------------------
# The selection frame
# --------------------------------------------------------------------------
@@ -316,22 +498,28 @@ def clip_to(box: Box, sel: Box) -> Box | None:
def hide_fractions(shapes: list[Shape], sel: Box) -> list[dict[str, object]]:
- """Blur/pixelate patches as fractions of the selection, for the main process."""
+ """Pixelate/blur patches as fractions of the selection, for the main process."""
out: list[dict[str, object]] = []
sx, sy, sw, sh = sel
if sw <= 0 or sh <= 0:
return out
for shape in shapes:
- if shape.kind not in HIDE_KINDS or len(shape.points) < 2:
+ if shape.kind != REDACT or len(shape.points) < 2:
continue
clipped = clip_to(normalized(shape.points[0], shape.points[-1]), sel)
if clipped is None:
continue
x, y, w, h = clipped
- out.append({"kind": shape.kind, "rect": [(x - sx) / sw, (y - sy) / sh, w / sw, h / sh]})
+ kind = shape.mode if shape.mode in REDACT_MODES else "pixelate"
+ out.append({"kind": kind, "rect": [(x - sx) / sw, (y - sy) / sh, w / sw, h / sh]})
return out
+def frame_padding(sel: Box, padding: float) -> float:
+ """The background frame's margin around an area (the full editor's rule)."""
+ return round(max(sel[2], sel[3]) * max(0.0, min(0.25, padding)))
+
+
# --------------------------------------------------------------------------
# Undo
# --------------------------------------------------------------------------
@@ -375,23 +563,30 @@ class History:
__all__ = [
+ "AREA_KINDS",
"ARROW",
- "BLUR",
+ "ARROW_STYLES",
+ "BACKGROUND",
+ "BACKGROUND_PRESETS",
"BOX_KINDS",
"COUNTER",
+ "DEFAULT_BACKGROUND",
"ELLIPSE",
+ "FILLED",
"HANDLES",
- "HIDE_KINDS",
"HIGHLIGHT",
"LINE",
"MOVE",
"PALETTE",
"PEN",
- "PIXELATE",
"RECT",
+ "REDACT",
+ "REDACT_MODES",
"SEGMENT_KINDS",
+ "SPOTLIGHT",
"STROKE_KINDS",
"TEXT",
+ "TEXT_STYLES",
"TOOL_KEYS",
"WIDTHS",
"History",
@@ -399,7 +594,12 @@ __all__ = [
"bounds",
"clip_to",
"constrain",
- "counter_radius",
+ "counter_size",
+ "frame_padding",
+ "grab_scope",
+ "grip_at",
+ "grip_cursor",
+ "grips",
"handle_at",
"handle_points",
"hide_fractions",
@@ -407,7 +607,9 @@ __all__ = [
"is_meaningful",
"next_counter",
"normalized",
+ "reshape",
"resize",
+ "rough_measure",
"shape_at",
"text_size",
"toolbar_origin",
diff --git a/jarvis/appshot/picker/renderer.py b/jarvis/appshot/picker/renderer.py
index 7e9aab276d..1c4f05c4b2 100644
--- a/jarvis/appshot/picker/renderer.py
+++ b/jarvis/appshot/picker/renderer.py
@@ -91,6 +91,20 @@ _HANDLE_CURSORS = {
"e": Qt.CursorShape.SizeHorCursor,
"w": Qt.CursorShape.SizeHorCursor,
}
+#: Cursor per marking grip kind (:func:`markup_model.grip_cursor`).
+_GRIP_CURSORS = {
+ "fdiag": Qt.CursorShape.SizeFDiagCursor,
+ "bdiag": Qt.CursorShape.SizeBDiagCursor,
+ "hor": Qt.CursorShape.SizeHorCursor,
+ "grab": Qt.CursorShape.OpenHandCursor,
+}
+#: Arrow keys nudge the selected marking.
+_NUDGE = {
+ Qt.Key.Key_Left: (-1.0, 0.0),
+ Qt.Key.Key_Right: (1.0, 0.0),
+ Qt.Key.Key_Up: (0.0, -1.0),
+ Qt.Key.Key_Down: (0.0, 1.0),
+}
#: Keyboard shortcuts for the toolbar's actions (with Ctrl / Cmd).
_ACTION_KEYS = {
Qt.Key.Key_C: wire.ACTION_COPY,
@@ -171,14 +185,24 @@ class _MarkupState:
self.tool = mm.ARROW
self.color = mm.PALETTE[0]
self.width_index = mm.DEFAULT_WIDTH_INDEX
+ self.arrow_style = mm.ARROW_STYLES[0]
+ self.text_style = mm.TEXT_STYLES[0]
+ self.redact_mode = mm.REDACT_MODES[0]
+ #: The background frame (preset, padding, radius, shadow), or ``None``.
+ self.background: dict | None = None
#: The marking being dragged out right now.
self.drawing: mm.Shape | None = None
- #: The text marking being typed (not yet in ``shapes``).
+ #: The text being typed (not in ``shapes`` while open); where an edited
+ #: text came from, and how it was, so Esc can put it back.
self.typing: mm.Shape | None = None
- #: Index into ``shapes`` the move tool holds.
- self.picked: int | None = None
- #: ``("resize", handle)`` / ``("move", None)`` while a drag runs.
+ self.typing_index: int | None = None
+ self.typing_original: mm.Shape | None = None
+ #: Index into ``shapes`` of the selected marking (grips shown).
+ self.selected: int | None = None
+ #: ``("area", handle)`` / ``("grip", grip)`` / ``("move", None)``.
self.drag: tuple[str, str | None] | None = None
+ #: The selected marking as it was when a grip drag began.
+ self.original: mm.Shape | None = None
self.last: QPointF | None = None
self.moved = False
@@ -191,6 +215,11 @@ class _MarkupState:
assert sel is not None
return (sel.x(), sel.y(), sel.width(), sel.height())
+ def selected_shape(self) -> mm.Shape | None:
+ if self.selected is None or not 0 <= self.selected < len(self.shapes):
+ return None
+ return self.shapes[self.selected]
+
class _SelectWindow(QWidget):
"""One screen's selection canvas."""
@@ -482,10 +511,12 @@ class _SelectWindow(QWidget):
bar.color_chosen.connect(self._set_color)
bar.width_cycled.connect(lambda: self._step_width(1, wrap=True))
bar.action.connect(self._toolbar_action)
+ bar.option_chosen.connect(self._set_option)
self._toolbar = bar
self._toolbar.set_tool(m.tool)
self._toolbar.set_color(m.color)
self._toolbar.set_width(m.width)
+ self._sync_options()
self._sync_history()
self._place_toolbar()
self._toolbar.show()
@@ -518,23 +549,59 @@ class _SelectWindow(QWidget):
h = self._markup.history
self._toolbar.set_history(h.can_undo, h.can_redo)
+ def _sync_options(self) -> None:
+ """The option row for the current tool (or the selected marking's kind)."""
+ bar = self._toolbar
+ if bar is None:
+ return
+ m = self._markup
+ kind = m.tool
+ selected = m.selected_shape()
+ if kind == mm.MOVE and selected is not None:
+ kind = selected.kind
+ if kind == mm.ARROW:
+ style = selected.style if selected is not None and selected.kind == mm.ARROW else ""
+ options = [(s, f"arrow_{s}") for s in mm.ARROW_STYLES]
+ bar.set_options(options, style or m.arrow_style, "arrow")
+ elif kind == mm.TEXT:
+ style = selected.style if selected is not None and selected.kind == mm.TEXT else ""
+ options = [(s, f"text_{s}") for s in mm.TEXT_STYLES]
+ bar.set_options(options, style or m.text_style, "text")
+ elif kind == mm.REDACT:
+ mode = selected.mode if selected is not None and selected.kind == mm.REDACT else ""
+ options = [(s, f"redact_{s}") for s in mm.REDACT_MODES]
+ bar.set_options(options, mode or m.redact_mode, "redact")
+ elif kind == mm.BACKGROUND:
+ options = [("off", "background_off")]
+ options += [(preset, "preset") for preset, _stops in mm.BACKGROUND_PRESETS]
+ chosen = m.background["preset"] if m.background else "off"
+ bar.set_options(options, chosen, "background")
+ else:
+ bar.set_options([], "")
+ self._place_toolbar()
+
def _commit(self) -> None:
self._markup.history.push(self._markup.shapes)
self._sync_history()
def _set_tool(self, kind: str) -> None:
self._commit_typing()
- self._markup.tool = kind
- self._markup.picked = None
+ m = self._markup
+ m.tool = kind
+ if mm.grab_scope(kind) != "any":
+ m.selected = None
+ if kind == mm.BACKGROUND and m.background is None:
+ m.background = dict(mm.DEFAULT_BACKGROUND)
if self._toolbar is not None:
self._toolbar.set_tool(kind)
+ self._sync_options()
self._update_cursor(None)
self.update()
def _set_color(self, colour: str) -> None:
m = self._markup
m.color = colour
- target = m.typing or (m.shapes[m.picked] if m.picked is not None else None)
+ target = m.typing or m.selected_shape()
if target is not None:
target.color = colour
if target is not m.typing:
@@ -543,20 +610,55 @@ class _SelectWindow(QWidget):
self._toolbar.set_color(colour)
self.update()
- def _step_width(self, step: int, *, wrap: bool = False) -> None:
+ def _step_width(self, step: int, *, wrap: bool = False, to: int | None = None) -> None:
m = self._markup
count = len(mm.WIDTHS)
- index = m.width_index + step
- m.width_index = index % count if wrap else max(0, min(count - 1, index))
- target = m.typing or (m.shapes[m.picked] if m.picked is not None else None)
+ if to is not None:
+ m.width_index = max(0, min(count - 1, to))
+ else:
+ index = m.width_index + step
+ m.width_index = index % count if wrap else max(0, min(count - 1, index))
+ target = m.typing or m.selected_shape()
if target is not None:
target.width = m.width
+ if target.kind == mm.TEXT:
+ target.size = mm.text_size(m.width)
+ elif target.kind == mm.COUNTER:
+ target.size = mm.counter_size(m.width)
if target is not m.typing:
self._commit()
if self._toolbar is not None:
self._toolbar.set_width(m.width)
self.update()
+ def _set_option(self, group: str, value: str) -> None:
+ m = self._markup
+ selected = m.selected_shape()
+ changed = False
+ if group == "arrow":
+ m.arrow_style = value
+ if selected is not None and selected.kind == mm.ARROW:
+ selected.style, changed = value, True
+ elif group == "text":
+ m.text_style = value
+ target = m.typing or (selected if selected and selected.kind == mm.TEXT else None)
+ if target is not None:
+ target.style = value
+ changed = target is not m.typing
+ elif group == "redact":
+ m.redact_mode = value
+ if selected is not None and selected.kind == mm.REDACT:
+ selected.mode, changed = value, True
+ elif group == "background":
+ if value == "off":
+ m.background = None
+ else:
+ m.background = {**(m.background or mm.DEFAULT_BACKGROUND), "preset": value}
+ if changed:
+ self._commit()
+ self._sync_options()
+ self.update()
+
def _toolbar_action(self, name: str) -> None:
if name == "undo":
self._undo(redo=False)
@@ -570,11 +672,13 @@ class _SelectWindow(QWidget):
def _undo(self, *, redo: bool) -> None:
m = self._markup
m.typing = None
+ m.typing_index = None
state = m.history.redo() if redo else m.history.undo()
if state is not None:
m.shapes = state
- m.picked = None
+ m.selected = None
self._sync_history()
+ self._sync_options()
self.update()
def _deliver(self, action: str) -> None:
@@ -590,25 +694,81 @@ class _SelectWindow(QWidget):
sel.width() / self.width(),
sel.height() / self.height(),
)
- markup = None
- if m.shapes:
+ markup: dict | None = None
+ if m.shapes or m.background:
markup = {
- "overlay": annotate.render_overlay(m.shapes, sel, self._scale()),
+ "overlay": annotate.render_overlay(m.shapes, sel, self._scale())
+ if m.shapes
+ else "",
"hides": mm.hide_fractions(m.shapes, m.box()),
}
+ if m.background:
+ markup["background"] = dict(m.background)
self._owner.finish(self, frac, action=action, markup=markup)
- def _commit_typing(self) -> None:
+ def _start_typing(self, at: mm.Point, *, index: int | None = None) -> None:
+ """Open the text box: a new one at ``at``, or the text at ``index``."""
m = self._markup
- shape = m.typing
+ if index is not None:
+ m.typing = m.shapes.pop(index)
+ m.typing_index = index
+ m.typing_original = m.typing.copy()
+ else:
+ size = mm.text_size(m.width)
+ m.typing = mm.Shape(
+ mm.TEXT,
+ color=m.color,
+ width=m.width,
+ size=size,
+ style=m.text_style,
+ points=[(at[0], at[1] - size * 0.62)],
+ )
+ m.typing_index = None
+ m.selected = None
+ self._sync_options()
+ self.update()
+
+ def _commit_typing(self, *, cancel: bool = False) -> None:
+ m = self._markup
+ shape, index = m.typing, m.typing_index
if shape is None:
return
- m.typing = None
+ m.typing, m.typing_index = None, None
+ original, m.typing_original = m.typing_original, None
+ if cancel:
+ if index is not None and original is not None:
+ # Esc on an edited text keeps it as it was.
+ m.shapes.insert(min(index, len(m.shapes)), original)
+ self.update()
+ return
+ shape.text = shape.text.rstrip()
if mm.is_meaningful(shape):
- m.shapes.append(shape)
+ if index is not None:
+ m.shapes.insert(min(index, len(m.shapes)), shape)
+ else:
+ m.shapes.append(shape)
self._commit()
+ elif index is not None:
+ self._commit() # emptied: the text is gone
self.update()
+ def _text_at(self, point: mm.Point) -> int | None:
+ index = mm.shape_at(self._markup.shapes, point, annotate.qt_measure, areas=False)
+ if index is not None and self._markup.shapes[index].kind == mm.TEXT:
+ return index
+ return None
+
+ def _grabbable(self, point: mm.Point) -> int | None:
+ """The marking a press at ``point`` would move with the current tool."""
+ m = self._markup
+ scope = mm.grab_scope(m.tool)
+ if scope == "none":
+ return None
+ index = mm.shape_at(m.shapes, point, annotate.qt_measure, areas=m.tool == mm.MOVE)
+ if index is None or (m.tool == mm.TEXT and m.shapes[index].kind == mm.TEXT):
+ return None
+ return index if scope == "any" or index == m.selected else None
+
def _markup_key(self, event) -> None: # noqa: C901 - one key table
m = self._markup
key = event.key()
@@ -618,8 +778,9 @@ class _SelectWindow(QWidget):
self._typing_key(event, key, mods, ctrl)
return
if key == Qt.Key.Key_Escape:
- if m.picked is not None:
- m.picked = None
+ if m.selected is not None:
+ m.selected = None
+ self._sync_options()
self.update()
else:
self._owner.finish(None, None)
@@ -631,15 +792,20 @@ class _SelectWindow(QWidget):
self._undo(redo=True)
elif ctrl and key in _ACTION_KEYS:
self._deliver(_ACTION_KEYS[key])
- elif key in (Qt.Key.Key_Delete, Qt.Key.Key_Backspace) and m.picked is not None:
- del m.shapes[m.picked]
- m.picked = None
+ elif key in (Qt.Key.Key_Delete, Qt.Key.Key_Backspace) and m.selected is not None:
+ del m.shapes[m.selected]
+ m.selected = None
self._commit()
+ self._sync_options()
self.update()
- elif not ctrl and Qt.Key.Key_1 <= key <= Qt.Key.Key_9:
- index = int(key) - int(Qt.Key.Key_1)
- if index < len(mm.PALETTE):
- self._set_color(mm.PALETTE[index])
+ elif key in _NUDGE and m.selected is not None:
+ dx, dy = _NUDGE[key]
+ step = 10.0 if mods & Qt.KeyboardModifier.ShiftModifier else 1.0
+ m.shapes[m.selected] = mm.translated(m.shapes[m.selected], dx * step, dy * step)
+ self._commit()
+ self.update()
+ elif not ctrl and Qt.Key.Key_1 <= key <= Qt.Key.Key_5:
+ self._step_width(0, to=int(key) - int(Qt.Key.Key_1))
elif not ctrl and event.text():
letter = event.text().upper()
for kind, shortcut in mm.TOOL_KEYS.items():
@@ -651,12 +817,14 @@ class _SelectWindow(QWidget):
shape = self._markup.typing
assert shape is not None
if key == Qt.Key.Key_Escape:
- self._markup.typing = None
- elif key in (Qt.Key.Key_Return, Qt.Key.Key_Enter):
+ self._commit_typing(cancel=True)
+ return
+ if key in (Qt.Key.Key_Return, Qt.Key.Key_Enter):
if mods & Qt.KeyboardModifier.ShiftModifier:
shape.text += "\n"
else:
self._commit_typing()
+ return
elif key == Qt.Key.Key_Backspace:
shape.text = shape.text[:-1]
elif ctrl and key == Qt.Key.Key_V:
@@ -673,7 +841,7 @@ class _SelectWindow(QWidget):
if m.drawing is not None:
m.drawing = None
elif m.typing is not None:
- m.typing = None
+ self._commit_typing(cancel=True)
elif not m.shapes:
self._end_markup()
return
@@ -682,15 +850,32 @@ class _SelectWindow(QWidget):
if event.button() != Qt.MouseButton.LeftButton:
return
self._commit_typing()
+ selected = m.selected_shape()
+ if selected is not None and mm.grab_scope(m.tool) != "none":
+ grip = mm.grip_at(selected, point, mm.HANDLE_GRAB_PX + 1, annotate.qt_measure)
+ if grip is not None:
+ m.drag, m.original, m.moved = ("grip", grip), selected.copy(), False
+ return
handle = mm.handle_at(m.box(), point)
if handle is not None:
- m.drag = ("resize", handle)
+ m.drag = ("area", handle)
+ return
+ grabbed = self._grabbable(point)
+ if grabbed is not None:
+ m.selected = grabbed
+ m.drag, m.last, m.moved = ("move", None), p, False
+ self._sync_options()
+ self.update()
return
+ if m.selected is not None:
+ m.selected = None
+ self._sync_options()
inside = m.sel is not None and m.sel.contains(p)
- if m.tool == mm.MOVE:
- m.picked = mm.shape_at(m.shapes, point)
- if m.picked is not None:
- m.drag, m.last, m.moved = ("move", None), p, False
+ if m.tool in (mm.MOVE, mm.BACKGROUND):
+ if not inside and not m.shapes:
+ self._end_markup()
+ self.mousePressEvent(event)
+ return
self.update()
return
if not inside:
@@ -699,20 +884,17 @@ class _SelectWindow(QWidget):
self._end_markup()
self.mousePressEvent(event)
return
- m.picked = None
if m.tool == mm.TEXT:
- m.typing = mm.Shape(
- mm.TEXT,
- color=m.color,
- width=m.width,
- points=[(point[0], point[1] - mm.text_size(m.width) / 2.0)],
- )
- elif m.tool == mm.COUNTER:
+ index = self._text_at(point)
+ self._start_typing(point, index=index)
+ return
+ if m.tool == mm.COUNTER:
m.shapes.append(
mm.Shape(
mm.COUNTER,
color=m.color,
width=m.width,
+ size=mm.counter_size(m.width),
points=[point],
number=mm.next_counter(m.shapes),
)
@@ -721,22 +903,37 @@ class _SelectWindow(QWidget):
elif m.tool in mm.STROKE_KINDS:
m.drawing = mm.Shape(m.tool, color=m.color, width=m.width, points=[point])
else:
- m.drawing = mm.Shape(m.tool, color=m.color, width=m.width, points=[point, point])
+ m.drawing = mm.Shape(
+ m.tool,
+ color=m.color,
+ width=m.width,
+ points=[point, point],
+ style=m.arrow_style if m.tool == mm.ARROW else "",
+ mode=m.redact_mode if m.tool == mm.REDACT else "",
+ )
self._update_markup_area()
def _markup_move(self, event) -> None:
m = self._markup
p = event.position()
point = (p.x(), p.y())
- if m.drag is not None and m.drag[0] == "resize":
- sel = mm.resize(m.box(), m.drag[1] or "se", point, (self.width(), self.height()))
+ drag = m.drag
+ if drag is not None and drag[0] == "area":
+ sel = mm.resize(m.box(), drag[1] or "se", point, (self.width(), self.height()))
m.sel = QRectF(*sel)
self._place_toolbar()
self.update()
return
- if m.drag is not None and m.drag[0] == "move" and m.picked is not None and m.last:
+ if drag is not None and drag[0] == "grip" and m.selected is not None and m.original:
+ m.shapes[m.selected] = mm.reshape(
+ m.original, drag[1] or "se", point, annotate.qt_measure
+ )
+ m.moved = True
+ self._update_markup_area()
+ return
+ if drag is not None and drag[0] == "move" and m.selected is not None and m.last:
delta = p - m.last
- m.shapes[m.picked] = mm.translated(m.shapes[m.picked], delta.x(), delta.y())
+ m.shapes[m.selected] = mm.translated(m.shapes[m.selected], delta.x(), delta.y())
m.last, m.moved = p, True
self._update_markup_area()
return
@@ -760,14 +957,31 @@ class _SelectWindow(QWidget):
return
m = self._markup
drag, m.drag = m.drag, None
- if drag is not None and drag[0] == "move" and m.moved:
+ m.original = None
+ if drag is not None and drag[0] in ("move", "grip") and m.moved:
self._commit()
shape, m.drawing = m.drawing, None
if shape is not None and mm.is_meaningful(shape):
m.shapes.append(shape)
self._commit()
+ # A fresh shape is selected at once, so its grips are right there;
+ # ink is not, or its box would catch the next stroke beside it.
+ if mm.grab_scope(m.tool) != "none":
+ m.selected = len(m.shapes) - 1
+ self._sync_options()
self._update_markup_area()
+ def mouseDoubleClickEvent(self, event) -> None: # noqa: N802
+ """Double-click a text with the select or text tool to change it."""
+ m = self._markup
+ if m.sel is None or m.typing is not None or m.tool not in (mm.MOVE, mm.TEXT):
+ return
+ p = event.position()
+ index = self._text_at((p.x(), p.y()))
+ if index is not None:
+ m.drag = None
+ self._start_typing((p.x(), p.y()), index=index)
+
def wheelEvent(self, event) -> None: # noqa: N802
if self._markup.sel is None:
return
@@ -778,17 +992,22 @@ class _SelectWindow(QWidget):
def _update_cursor(self, point: mm.Point | None) -> None:
m = self._markup
if point is not None:
+ selected = m.selected_shape()
+ if selected is not None and mm.grab_scope(m.tool) != "none":
+ grip = mm.grip_at(selected, point, mm.HANDLE_GRAB_PX + 1, annotate.qt_measure)
+ if grip is not None:
+ self.setCursor(_GRIP_CURSORS[mm.grip_cursor(grip)])
+ return
handle = mm.handle_at(m.box(), point)
if handle is not None:
self.setCursor(_HANDLE_CURSORS[handle])
return
- if m.tool == mm.MOVE:
- over = mm.shape_at(m.shapes, point) is not None
- self.setCursor(Qt.CursorShape.SizeAllCursor if over else Qt.CursorShape.ArrowCursor)
+ if self._grabbable(point) is not None:
+ self.setCursor(Qt.CursorShape.SizeAllCursor)
return
if m.tool == mm.TEXT:
self.setCursor(Qt.CursorShape.IBeamCursor)
- elif m.tool == mm.MOVE:
+ elif m.tool in (mm.MOVE, mm.BACKGROUND):
self.setCursor(Qt.CursorShape.ArrowCursor)
else:
self.setCursor(self._cross)
@@ -813,16 +1032,23 @@ class _SelectWindow(QWidget):
# Live overlay: a fully clear pixel lets clicks fall through to the
# window below, so the area keeps an invisible film to draw on.
painter.fillRect(sel, QColor(0, 0, 0, 1))
- live =[*m.shapes, *(s for s in (m.drawing, m.typing) if s is not None)]
+ if m.background:
+ annotate.paint_background_frame(painter, sel, m.background)
+ live = [*m.shapes, *(s for s in (m.drawing,) if s is not None)]
painter.save()
painter.setClipRect(sel, Qt.ClipOperation.IntersectClip)
- for shape in (s for s in live if s.kind in mm.HIDE_KINDS):
+ for shape in (s for s in live if s.kind == mm.REDACT):
annotate.paint_shape(painter, shape, hide_source=self._patches)
- for shape in (s for s in live if s.kind not in mm.HIDE_KINDS):
- annotate.paint_shape(painter, shape, caret=shape is m.typing)
+ for shape in (s for s in live if s.kind != mm.REDACT):
+ annotate.paint_shape(painter, shape)
+ annotate.paint_spotlights(painter, live, sel)
painter.restore()
- if m.picked is not None and m.picked < len(m.shapes):
- annotate.paint_picked(painter, m.shapes[m.picked])
+ if m.typing is not None:
+ labels = annotate.labels_for(self._owner.language)
+ annotate.paint_typing(painter, m.typing, labels["placeholder"], caret=True)
+ selected = m.selected_shape()
+ if selected is not None:
+ annotate.paint_picked(painter, selected)
annotate.paint_selection_frame(painter, sel)
self._paint_size(painter, sel)
diff --git a/jarvis/appshot/service.py b/jarvis/appshot/service.py
index b961122c50..968ac44f51 100644
--- a/jarvis/appshot/service.py
+++ b/jarvis/appshot/service.py
@@ -187,10 +187,23 @@ async def _with_markup(shot: Appshot, markup: Any) -> Appshot:
try:
image = await asyncio.to_thread(apply_to_bytes, shot.image, shot.mime, markup)
+ width, height = await asyncio.to_thread(_image_size, image)
except Exception: # noqa: BLE001 - the plain appshot is still worth sending
log.warning("appshot: markings could not be applied; sent unmarked", exc_info=True)
return shot
- return replace(shot, image=image, note=f"{shot.note}\n\n{EDIT_NOTE}")
+ # A background frame makes the picture larger than the area.
+ return replace(
+ shot, image=image, width=width, height=height, note=f"{shot.note}\n\n{EDIT_NOTE}"
+ )
+
+
+def _image_size(image: bytes) -> tuple[int, int]:
+ import io # noqa: PLC0415
+
+ from PIL import Image # noqa: PLC0415
+
+ with Image.open(io.BytesIO(image)) as picture:
+ return picture.size
async def _finish_action(action: str, shot: Appshot, bus: Any | None) -> None:
diff --git a/tests/unit/appshot/test_appshot_markup.py b/tests/unit/appshot/test_appshot_markup.py
index 68e6001720..07750adaf4 100644
--- a/tests/unit/appshot/test_appshot_markup.py
+++ b/tests/unit/appshot/test_appshot_markup.py
@@ -71,8 +71,8 @@ def test_a_rectangle_is_picked_on_its_outline_not_its_empty_inside() -> None:
def test_the_top_most_marking_wins_and_moving_keeps_the_original() -> None:
- lower = mm.Shape(mm.BLUR, points=[(0, 0), (100, 100)])
- upper = mm.Shape(mm.COUNTER, points=[(50, 50)], number=1)
+ lower = mm.Shape(mm.REDACT, mode="blur", points=[(0, 0), (100, 100)])
+ upper = mm.Shape(mm.COUNTER, points=[(50, 50)], number=1, size=12)
assert mm.shape_at([lower, upper], (50, 50)) == 1
moved = mm.translated(upper, 10, -5)
assert moved.points == [(60, 45)] and upper.points == [(50, 50)]
@@ -128,10 +128,10 @@ def test_the_toolbar_docks_below_then_above_then_inside_and_stays_on_screen() ->
def test_hide_patches_leave_as_fractions_of_the_area_clipped_to_it() -> None:
sel = (100.0, 100.0, 200.0, 100.0)
shapes = [
- mm.Shape(mm.BLUR, points=[(150, 120), (250, 170)]),
- mm.Shape(mm.PIXELATE, points=[(50, 50), (150, 150)]),
+ mm.Shape(mm.REDACT, mode="blur", points=[(150, 120), (250, 170)]),
+ mm.Shape(mm.REDACT, mode="pixelate", points=[(50, 50), (150, 150)]),
mm.Shape(mm.RECT, points=[(150, 120), (250, 170)]),
- mm.Shape(mm.BLUR, points=[(0, 0), (20, 20)]),
+ mm.Shape(mm.REDACT, mode="blur", points=[(0, 0), (20, 20)]),
]
hides = mm.hide_fractions(shapes, sel)
assert hides == [
@@ -140,6 +140,68 @@ def test_hide_patches_leave_as_fractions_of_the_area_clipped_to_it() -> None:
]
+def _measure(text: str, size: float) -> float:
+ return len(text) * size * 0.5
+
+
+def test_text_has_a_grip_on_every_corner_and_scales_from_the_opposite_one() -> None:
+ text = mm.Shape(mm.TEXT, text="Hallo", size=20, points=[(100, 100)])
+ box = mm.bounds(text, _measure)
+ assert box == (100, 100, 50, 25)
+ assert [name for name, _ in mm.grips(text, _measure)] == ["nw", "ne", "sw", "se"]
+ # Drag the bottom-right corner down to twice the height: twice the size,
+ # top-left stays put.
+ bigger = mm.reshape(text, "se", (160, 150), _measure)
+ assert bigger.size == 40 and bigger.points == [(100, 100)]
+ # Drag the top-left corner: the bottom-right corner stays put.
+ smaller = mm.reshape(text, "nw", (0, 112.5), _measure)
+ assert smaller.size == 10
+ x, y, w, h = mm.bounds(smaller, _measure)
+ assert (x + w, y + h) == (150, 125)
+
+
+def test_grips_follow_the_full_editor_rules() -> None:
+ arrow = mm.Shape(mm.ARROW, points=[(0, 0), (50, 50)])
+ assert mm.reshape(arrow, "to", (80, 10)).points == [(0, 0), (80, 10)]
+ box = mm.Shape(mm.RECT, points=[(10, 10), (50, 50)])
+ assert mm.reshape(box, "nw", (0, 0)).points == [(50, 50), (0, 0)]
+ counter = mm.Shape(mm.COUNTER, points=[(0, 0)], size=12)
+ assert [name for name, _ in mm.grips(counter)] == ["size"]
+ assert mm.reshape(counter, "size", (30, 40)).size == 50
+ assert mm.grab_scope(mm.MOVE) == "any"
+ assert mm.grab_scope(mm.RECT) == "selected"
+ assert mm.grab_scope(mm.PEN) == "none"
+
+
+def test_areas_are_only_picked_when_nothing_on_them_is_hit() -> None:
+ spot = mm.Shape(mm.SPOTLIGHT, points=[(0, 0), (200, 200)])
+ line = mm.Shape(mm.LINE, points=[(0, 100), (200, 100)])
+ assert mm.shape_at([line, spot], (100, 100)) == 0
+ assert mm.shape_at([line, spot], (100, 30)) == 1
+ assert mm.shape_at([line, spot], (100, 30), areas=False) is None
+
+
+def test_the_picker_offers_the_full_editors_tools_keys_and_presets() -> None:
+ """Both editors must stay one tool set: same keys, colours and frames."""
+ import re
+ from pathlib import Path
+
+ model = (
+ Path(__file__).resolve().parents[3] / "jarvis/ui/web/frontend/src/lib/appshotEditorModel.ts"
+ ).read_text(encoding="utf-8")
+ keys = dict(re.findall(r'\{ tool: "(\w+)", key: "(\w)" \}', model))
+ keys.pop("crop") # the picker's area handles are its crop
+ assert {tool: key.upper() for tool, key in keys.items()} == mm.TOOL_KEYS
+ presets = re.findall(r'\{ id: "(\w+)", stops: \[([^\]]+)\] \}', model)
+ parsed = tuple((pid, tuple(re.findall(r'"(#[0-9a-f]{6})"', stops))) for pid, stops in presets)
+ assert parsed == mm.BACKGROUND_PRESETS
+ colours = re.search(r"export const COLORS = \[([^\]]+)\]", model)
+ assert colours is not None
+ assert tuple(c.upper() for c in re.findall(r'"(#[0-9a-fA-F]{6})"', colours.group(1))) == (
+ mm.PALETTE
+ )
+
+
# ---------------------------------------------------------------- the wire
@@ -248,6 +310,20 @@ def test_blur_and_pixelate_hide_what_was_under_them() -> None:
assert max(row) - min(row) < 60
+def test_a_background_frame_surrounds_the_picture() -> None:
+ markup = parse_markup(
+ {"background": {"preset": "ink", "padding": 0.1, "radius": 0, "shadow": False}}
+ )
+ assert markup is not None and markup.frame is not None
+ marked = apply_to_bytes(_jpeg((200, 100), (255, 255, 255)), "image/jpeg", markup)
+ with Image.open(io.BytesIO(marked)) as image:
+ assert image.size == (240, 140)
+ corner = image.getpixel((3, 3))
+ middle = image.getpixel((120, 70))
+ assert max(corner) < 40 and min(middle) > 220
+ assert parse_markup({"background": {"preset": "not-a-preset"}}) is None
+
+
def test_the_thumbnail_gets_the_markings_too() -> None:
overlay = _png((10, 10), (0, 255, 0, 255))
size, rgb = apply_to_rgb((10, 10), bytes(300), Markup(overlay_png=overlay))