From 0e746f2722cb72ad33435902399cd1038d14b8ef Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Fri, 24 Jul 2026 23:15:14 -0400 Subject: [PATCH] Node detail: open the full map centred on the node MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Map tab's inline preview centres on the node's own fix ("… centred here"), but its "Open full map" action called open_map with only the markers — so the full map fell back to the persisted global view (wherever the map was last left) or a fit-to-all-nodes frame, landing somewhere other than the node the preview had just shown. open_map gains an optional focus=(lat, lon): given it, the map opens on that point at the preview's zoom (min(13, max_zoom)) and — mirroring how LocationPickScreen uses its transient `initial` — deliberately wires no on_view_change, so a focused peek never overwrites the user's saved global map_view, even as it is panned. The node detail passes the node's own coordinates, the same spot the preview is centred on. Tests drive the real open_map both ways: focused (centres on the node, leaves the saved view untouched after a pan/zoom) and plain (still restores and persists the global view). Co-Authored-By: Claude Opus 4.8 (1M context) --- meshterm/ui/map_screen.py | 42 ++++++++++++++----- meshterm/ui/node_detail_screen.py | 5 ++- tests/test_map.py | 69 +++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/meshterm/ui/map_screen.py b/meshterm/ui/map_screen.py index 09b638f..f80820f 100644 --- a/meshterm/ui/map_screen.py +++ b/meshterm/ui/map_screen.py @@ -512,6 +512,7 @@ async def open_map( ctx: "AppContext", markers: list[MapMarker], *, + focus: Optional[tuple[float, float]] = None, fraction: float = DEFAULT_VIEW_FRACTION, ) -> None: """Open the interactive full-screen map over ``markers`` and run until dismissed. @@ -522,6 +523,11 @@ async def open_map( Args: ctx: Shared application context (must be in the interactive menu). markers: The located mesh nodes to plot (non-empty). + focus: A ``(lat, lon)`` to open centred on — the node you opened the map from, + say — instead of the persisted "where you left the map" view. A focused open + is a transient peek: it deliberately wires no ``on_view_change``, so panning + around it never overwrites that saved view and the Map tool still reopens + where the user last left it. fraction: Fraction of the nodes the default view frames (see :class:`MapScreen`). Raises: @@ -535,17 +541,31 @@ async def open_map( source = basemap_source(ctx) # Resolve the tile template/zoom in a worker thread so the UI thread never blocks. max_zoom = await asyncio.to_thread(lambda: source.max_zoom) - screen = MapScreen( - session, - markers, - source, - max_zoom, - saved_view=ctx.repo.get_map_view(), - on_view_change=lambda vp: ctx.repo.set_map_view( - vp.center_lat, vp.center_lon, vp.zoom - ), - view_fraction=fraction, - ) + if focus is not None: + # Open on the focused node — matching the detail preview's centre and zoom + # (``min(13, max_zoom)``) so the full map is visibly the same place, larger. No + # ``on_view_change``: a focused peek must leave the persisted global view alone. + lat, lon = focus + screen = MapScreen( + session, + markers, + source, + max_zoom, + saved_view=(clamp_lat(lat), lon, min(13, max_zoom)), + view_fraction=fraction, + ) + else: + screen = MapScreen( + session, + markers, + source, + max_zoom, + saved_view=ctx.repo.get_map_view(), + on_view_change=lambda vp: ctx.repo.set_map_view( + vp.center_lat, vp.center_lon, vp.zoom + ), + view_fraction=fraction, + ) try: await session.run_screen(screen) finally: diff --git a/meshterm/ui/node_detail_screen.py b/meshterm/ui/node_detail_screen.py index b752e70..6ba5730 100644 --- a/meshterm/ui/node_detail_screen.py +++ b/meshterm/ui/node_detail_screen.py @@ -835,7 +835,10 @@ async def open_node_detail(ctx: "AppContext", contact: Optional["Contact"]) -> N await open_trace(ctx, name or key, initial_spec=screen.selected_spec()) elif action == "map": if markers: - await open_map(ctx, markers) + # Open centred on this node — the very spot the inline preview showed — + # not wherever the global map was last left. (The map action only exists + # when the node has a fix, so lat/lon are set here.) + await open_map(ctx, markers, focus=(lat, lon)) elif action == "timemachine": if you: await open_timemachine_self(ctx) diff --git a/tests/test_map.py b/tests/test_map.py index e68bd2c..40f0444 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -481,6 +481,28 @@ class _StubSource: return None +class _CapturingSession(_StubSession): + """Drives :func:`open_map` headless: captures the pushed screen, renders it once, then + replays a canned key sequence — so a test can assert on where the map opened and what + it did (or didn't) persist as the view moves.""" + + def __init__(self, cols: int, rows: int, keys: tuple[str, ...] = ()) -> None: + super().__init__(cols, rows) + self.keys = keys + self.screen = None + self.repainted = False + + async def run_screen(self, screen): # noqa: ANN001, ANN201 + self.screen = screen + screen.render_body(self._cols) + for key in self.keys: + screen.handle(key) + return None + + def request_full_repaint(self) -> None: + self.repainted = True + + def test_map_screen_renders_pans_zooms_and_resets() -> None: """The interactive screen fills its body, and wasd/zoom/reset move the viewport.""" from meshterm.core.geo import Viewport @@ -648,6 +670,53 @@ def test_map_screen_restores_and_persists_view() -> None: assert saved[-1][2] == 13 +async def test_open_map_focus_centres_on_the_node_without_clobbering_the_saved_view( + ctx, monkeypatch +) -> None: + """Opening the full map focused on a node (from its detail page) centres there, at the + inline preview's zoom — and, being a transient peek, never overwrites the persisted + 'where you left the map' global view, even as the peek is panned and zoomed.""" + from meshterm.core.geo import clamp_lat + from meshterm.ui.map_render import MapMarker + from meshterm.ui.map_screen import open_map + from meshterm.ui.surface import TuiUi + + monkeypatch.setattr("meshterm.ui.map_screen.basemap_source", lambda _c: _StubSource()) + ctx.repo.set_map_view(10.0, 20.0, 8) # where the user last left the global map + + session = _CapturingSession(80, 24, keys=("right", "pageup")) # pan + zoom the peek + ctx.ui = TuiUi(session) + await open_map(ctx, [MapMarker("Hub", 45.5, -73.6, is_repeater=True)], focus=(45.5, -73.6)) + + # Opened on the node, at the detail preview's zoom (min(13, max_zoom)). + assert session.screen._saved_view == (clamp_lat(45.5), -73.6, 13) + assert session.screen._on_view_change is None # a focused peek wires no persistence + # Panning/zooming the peek left the global 'where you left the map' view untouched. + assert ctx.repo.get_map_view() == pytest.approx((10.0, 20.0, 8)) + assert session.repainted # cleaned the terminal on the way out, like the plain open + + +async def test_open_map_without_focus_restores_and_persists_the_global_view( + ctx, monkeypatch +) -> None: + """With no focus the full map is unchanged: it reopens where the user left it and pans + persist straight back to that global view.""" + from meshterm.ui.map_render import MapMarker + from meshterm.ui.map_screen import open_map + from meshterm.ui.surface import TuiUi + + monkeypatch.setattr("meshterm.ui.map_screen.basemap_source", lambda _c: _StubSource()) + ctx.repo.set_map_view(46.80, -71.20, 12) # the saved view to reopen on + + session = _CapturingSession(80, 24, keys=("right",)) # pan east + ctx.ui = TuiUi(session) + await open_map(ctx, [MapMarker("Hub", 46.8, -71.2)]) + + assert session.screen._viewport.zoom == 12 # reopened on the saved view, not a node fit + lat, lon, zoom = ctx.repo.get_map_view() + assert zoom == 12 and lon > -71.20 # the eastward pan persisted back to the global view + + def test_map_screen_scrubs_right_edge_after_move() -> None: """A move asks the session to scrub the right edge once, to clear any braille smear.""" from meshterm.ui.map_render import MapMarker