The map's find filter also matches a node's place
MapMarker carries a place, open_map reads it for each marker, and both the screen and the renderer match label or place through MapMarker.matches. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7dd7cb5a62
commit
538b0a5a69
4 files changed
+94
-3
No files matched your search
@@ -411,6 +411,9 @@ class MapMarker:
|
||||
key: The key of the node in hex (the longest form that the caller has). The hue
|
||||
of the label comes from this key. ``None`` leaves the label in the muted grey
|
||||
for a node with no key.
|
||||
place: The full label of the place of the node (``Villeray, Montréal``), or ``""``
|
||||
when the caller has none. The map does not draw it. The find filter of the map
|
||||
matches it, in the same way as it matches the label.
|
||||
"""
|
||||
|
||||
label: str
|
||||
@@ -420,6 +423,16 @@ class MapMarker:
|
||||
is_self: bool = False
|
||||
detail: str = ""
|
||||
key: str | None = None
|
||||
place: str = ""
|
||||
|
||||
def matches(self, needle: str) -> bool:
|
||||
"""Whether the find filter ``needle`` matches the label or the place of this node.
|
||||
|
||||
Args:
|
||||
needle: The text that the user typed, already case-folded. An empty ``needle``
|
||||
matches each node.
|
||||
"""
|
||||
return not needle or needle in self.label.casefold() or needle in self.place.casefold()
|
||||
|
||||
def _rank(self) -> int:
|
||||
"""Draw order: our node above the repeaters, and the repeaters above leaf nodes."""
|
||||
|
||||
@@ -347,7 +347,7 @@ def render_map(
|
||||
markers: The mesh nodes to put on top.
|
||||
max_labels: The maximum number of basemap labels to place, to keep the map
|
||||
readable.
|
||||
find: A live node-name filter. Markers whose label contains it (case-insensitive)
|
||||
find: A live node filter. Markers whose label or place contains it (case-insensitive)
|
||||
draw with bright white labels, and the other markers dim to context with no
|
||||
label. When it is empty, each node draws normally.
|
||||
ghost: Ground from an earlier raster to put down first, reprojected onto this
|
||||
@@ -796,7 +796,7 @@ def _draw_nodes(
|
||||
ix, iy = int(round(x)), int(round(y))
|
||||
if not (0 <= ix < viewport.dot_w and 0 <= iy < viewport.dot_h):
|
||||
continue
|
||||
matched = not needle or needle in marker.label.casefold()
|
||||
matched = marker.matches(needle)
|
||||
placed.append((marker, ix, iy, matched))
|
||||
|
||||
# Matches draw after (over) the dimmed nodes that do not match, at any rank. Thus the
|
||||
|
||||
@@ -33,6 +33,7 @@ a blank grid. The map still works, but it has no streets.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import math
|
||||
from collections import OrderedDict
|
||||
from collections.abc import Callable
|
||||
@@ -51,6 +52,7 @@ from .tui.screen import Screen
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..context import AppContext
|
||||
from ..core.places import Place
|
||||
|
||||
#: The fraction of the viewport that one (coarse) pan key press moves.
|
||||
_PAN_STEP = 0.30
|
||||
@@ -704,7 +706,7 @@ class MapScreen(Screen):
|
||||
needle = self._filter.strip().casefold()
|
||||
if not needle:
|
||||
return self._markers
|
||||
return [m for m in self._markers if needle in m.label.casefold()]
|
||||
return [m for m in self._markers if m.matches(needle)]
|
||||
|
||||
def _title(self, vp: Viewport) -> str:
|
||||
"""A compact status title: the zoom, the node count (or the find matches), and the scale.
|
||||
@@ -1462,6 +1464,26 @@ def basemap_source(ctx: AppContext) -> BasemapSource:
|
||||
return ctx.basemap_source
|
||||
|
||||
|
||||
def with_places(
|
||||
markers: list[MapMarker], place_of: Callable[[str | None], Place | None]
|
||||
) -> list[MapMarker]:
|
||||
"""Return copies of ``markers`` that carry the place of each node (the find filter reads it).
|
||||
|
||||
Args:
|
||||
markers: The located nodes. The function does not change them.
|
||||
place_of: Gives the place of a key or a key prefix, or ``None`` (for example,
|
||||
:meth:`~meshterm.services.places.PlaceService.get`).
|
||||
|
||||
Returns:
|
||||
The new markers, in the same order. A node with no place has an empty ``place``.
|
||||
"""
|
||||
out = []
|
||||
for marker in markers:
|
||||
place = place_of(marker.key) if marker.key else None
|
||||
out.append(dataclasses.replace(marker, place=(place.label if place else "")))
|
||||
return out
|
||||
|
||||
|
||||
async def open_map(
|
||||
ctx: AppContext,
|
||||
markers: list[MapMarker],
|
||||
@@ -1499,6 +1521,9 @@ async def open_map(
|
||||
if not isinstance(ctx.ui, TuiUi): # pragma: no cover - guarded by the menu-only caller
|
||||
raise RuntimeError("the interactive map is only available in the menu")
|
||||
session = ctx.ui.session
|
||||
# The find filter also matches the place of a node. The places are read now, so that a
|
||||
# place that the finder stored since the caller made the markers is in the filter.
|
||||
markers = with_places(markers, ctx.places.get)
|
||||
source = basemap_source(ctx)
|
||||
# Resolve the tile template and zoom in a worker thread, so the UI thread never blocks.
|
||||
max_zoom = await asyncio.to_thread(lambda: source.max_zoom)
|
||||
|
||||
Reference in new issue
Block a user