Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): draw the places that answer a click in the colour of one

Elian Doran5c090c60ef0b
diff --git a/apps/client/src/widgets/collections/geomap/Pois.spec.tsx b/apps/client/src/widgets/collections/geomap/Pois.spec.tsx
index f6db030c4e..eff13f477d 100644
--- a/apps/client/src/widgets/collections/geomap/Pois.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/Pois.spec.tsx
@@ -12,7 +12,8 @@ import { CLUSTER_LAYER } from "./clusters";
import type { GeoSearchResult } from "./geocoding";
import { MapStyleLoaded, ParentMap } from "./map";
import { MARKER_LAYER } from "./Markers";
-import Pois, { poiFromFeature, poiLayers } from "./Pois";
+import Pois, { clickableTint, poiFromFeature, poiLayers } from "./Pois";
+import { PLACE_MARKER_COLOR } from "./PlaceMarker";
/**
* The popup MapLibre would draw, recording what it was shown and whether it is up. Hoisted because
@@ -64,9 +65,15 @@ function poiFeature(properties: Record<string, unknown>, coordinates: [number, n
* A map whose style draws places, standing in for MapLibre. What it has to answer is the hit test:
* what the click landed on, told apart by which layers the query names.
*/
-function fakeMap({ poiLayerIds = [ "poi-amenity", "poi-shop" ], ownLayerIds = [ MARKER_LAYER ], zoom = 18 } = {}) {
+function fakeMap({
+ poiLayerIds = [ "poi-amenity", "poi-shop" ],
+ ownLayerIds = [ MARKER_LAYER ],
+ zoom = 18,
+ styleColor = "rgb(85,85,85)" as unknown
+} = {}) {
const listeners = new Map<string, Set<Listener>>();
const canvas = { style: { cursor: "" } };
+ const paint = new Map<string, unknown>(poiLayerIds.map((id) => [ id, styleColor ]));
let onPoi: unknown[] = [];
let onOwn: unknown[] = [];
@@ -116,6 +123,12 @@ function fakeMap({ poiLayerIds = [ "poi-amenity", "poi-shop" ], ownLayerIds = [
queryRenderedFeatures(_point: unknown, { layers: queried }: { layers: string[] }) {
return queried.some((id) => poiLayerIds.includes(id)) ? onPoi : onOwn;
},
+ /** What a layer's places are currently painted in. */
+ iconColor: (layer: string) => paint.get(layer),
+ getPaintProperty: (layer: string, name: string) => name === "icon-color" ? paint.get(layer) : undefined,
+ setPaintProperty(layer: string, name: string, value: unknown) {
+ if (name === "icon-color") paint.set(layer, value);
+ },
getLayersOrder: () => [ ...layers.keys() ],
getLayer: (id: string) => layers.get(id),
getCanvas: () => canvas,
@@ -447,3 +460,84 @@ describe("geo map place tooltips", () => {
expect(tooltipText()).toBeNull();
});
});
+
+
+describe("geo map place colouring", () => {
+ it("draws the places that answer a click in the colour a place is pinned in", async () => {
+ const map = fakeMap();
+ await renderPois(map);
+
+ // Composed over what the style painted, which is what the rest of the places keep.
+ expect(map.iconColor("poi-amenity")).toEqual(clickableTint("rgb(85,85,85)"));
+ expect(map.iconColor("poi-shop")).toEqual(clickableTint("rgb(85,85,85)"));
+ });
+
+ it("asks of a place exactly what the click asks of it", () => {
+ const [ step, input, tooFarOut, threshold, closeIn ] = clickableTint("rgb(85,85,85)") as unknown[];
+
+ expect(step).toBe("step");
+ expect(input).toEqual([ "zoom" ]);
+ // Too far out to pick a place, so the style's own grey stands unchanged.
+ expect(tooFarOut).toBe("rgb(85,85,85)");
+ expect(threshold).toBe(17);
+ // Close enough in, a place carrying a name is drawn in the colour one is pinned in, and one
+ // without a name keeps the grey along with the click it does not answer.
+ expect(closeIn).toEqual([
+ "case",
+ [ "to-boolean", [ "coalesce", [ "get", "name_en" ], [ "get", "name" ] ] ],
+ PLACE_MARKER_COLOR,
+ "rgb(85,85,85)"
+ ]);
+ });
+
+ it("asks the zoom only where MapLibre takes it, at the top of the expression", () => {
+ const tint = clickableTint("rgb(85,85,85)") as unknown[];
+
+ // A paint value whose `["zoom"]` stands anywhere but the input of an outermost `step` or
+ // `interpolate` is refused, and a refused value leaves the places painted as they were —
+ // which is the whole of what a reader would see of the mistake.
+ expect(tint[0]).toBe("step");
+ expect(tint[1]).toEqual([ "zoom" ]);
+ expect(mentionsZoom(tint.slice(2))).toBe(false);
+ });
+
+ it("leaves a style that paints its places some other way alone", async () => {
+ // The old function syntax is an object rather than an expression, and cannot be composed with.
+ const stops = { stops: [ [ 16, "#111" ], [ 20, "#999" ] ] };
+ const map = fakeMap({ styleColor: stops });
+ await renderPois(map);
+
+ expect(map.iconColor("poi-amenity")).toBe(stops);
+ });
+
+ it("puts the style's own colour back when it goes", async () => {
+ const map = fakeMap();
+ const { unmount } = await renderPois(map);
+
+ await unmount();
+
+ expect(map.iconColor("poi-amenity")).toBe("rgb(85,85,85)");
+ expect(map.iconColor("poi-shop")).toBe("rgb(85,85,85)");
+ });
+
+ it("colours the places a style switch brought in, over what that style painted them", async () => {
+ const map = fakeMap();
+ await renderPois(map);
+
+ // A style paints its places afresh, so what was composed before is gone and the colour read
+ // back is the new style's own rather than the last composition.
+ map.setPaintProperty("poi-amenity", "icon-color", "rgb(20,20,20)");
+ await act(async () => { map.fireStyleLoad(); });
+
+ expect(map.iconColor("poi-amenity")).toEqual(clickableTint("rgb(20,20,20)"));
+ });
+});
+
+/** Whether `["zoom"]` is asked anywhere within a value, however deeply it is nested. */
+function mentionsZoom(value: unknown): boolean {
+ if (!Array.isArray(value)) {
+ return false;
+ }
+
+ return (value[0] === "zoom" && value.length === 1) || value.some(mentionsZoom);
+}
diff --git a/apps/client/src/widgets/collections/geomap/Pois.tsx b/apps/client/src/widgets/collections/geomap/Pois.tsx
index 2b8362ea76..c40d55db3b 100644
--- a/apps/client/src/widgets/collections/geomap/Pois.tsx
+++ b/apps/client/src/widgets/collections/geomap/Pois.tsx
@@ -9,7 +9,7 @@ import { trackHitLayers } from "./GpxTrack";
import { MapStyleLoaded, ParentMap } from "./map";
import { MARKER_LAYER } from "./Markers";
import { placeIcon } from "./osm_icons";
-import { PLACE_LAYER } from "./PlaceMarker";
+import { PLACE_LAYER, PLACE_MARKER_COLOR } from "./PlaceMarker";
/** The source layer the vector styles draw shops, cafes and the rest of the base map's places from. */
const POI_SOURCE_LAYER = "pois";
@@ -212,6 +212,65 @@ export default function Pois({ placing, onPick }: PoisProps) {
};
}, [ parentMap, placing, styleLoaded ]);
+ /**
+ * Draws the places that answer a click in the colour a place is pinned in, so which of them do
+ * is read off the map rather than found by hovering each in turn.
+ *
+ * Composed over what the style painted rather than replacing it, so the places that answer no
+ * click -- an unnamed bench, anything at a zoom too far out to pick from -- keep the grey the
+ * style drew them in and stand as the background they are.
+ */
+ useEffect(() => {
+ if (!parentMap) return;
+ // Aliased so the narrowing above carries into the functions below.
+ const map = parentMap;
+ // What the style painted its places with, to be put back when this is taken off.
+ let painted: { layer: string; color: string }[] = [];
+
+ /** Puts back what the style painted, for a map that keeps its layers after this goes. */
+ function restore() {
+ for (const { layer, color } of painted) {
+ try {
+ if (map.getLayer(layer)) {
+ map.setPaintProperty(layer, "icon-color", color);
+ }
+ } catch {
+ // The style has moved on, and took the layer with it.
+ }
+ }
+ painted = [];
+ }
+
+ function tint() {
+ // Forgotten rather than put back: a style paints its places itself, and writing the
+ // colours of the style before it over them is how the wrong grey lands on a dark map.
+ painted = [];
+
+ for (const layer of poiLayers(map)) {
+ const color = map.getPaintProperty(layer, "icon-color");
+ // Only a plain colour can be composed with: the old function syntax a style may
+ // paint with is an object rather than an expression, and cannot stand inside one.
+ if (typeof color !== "string") continue;
+
+ try {
+ map.setPaintProperty(layer, "icon-color", clickableTint(color));
+ painted.push({ layer, color });
+ } catch (e) {
+ console.warn("Geo map: could not colour the places that answer a click --", e);
+ }
+ }
+ }
+
+ tint();
+ // Switching the style paints its places afresh.
+ map.on("style.load", tint);
+
+ return () => {
+ map.off("style.load", tint);
+ restore();
+ };
+ }, [ parentMap, styleLoaded ]);
+
return null;
}
@@ -269,6 +328,28 @@ function isOwnUnderPointer(map: MapLibreGLMap, point: MapMouseEvent["point"]) {
return own.length > 0 && map.queryRenderedFeatures(point, { layers: own }).length > 0;
}
+/**
+ * What a place is drawn in: the colour a picked place is pinned in where it answers a click, and
+ * `styleColor` -- what the style painted it -- where it does not.
+ *
+ * The two conditions are the ones the click itself asks (see the click handler above): the map has
+ * to be zoomed in far enough for a place to be picked, and the place has to carry a name.
+ *
+ * The zoom is asked at the top of the expression rather than beside the name, MapLibre taking
+ * `["zoom"]` only as the input of an outermost `step` or `interpolate`. What each zoom is drawn in
+ * is where the name is then asked about.
+ */
+export function clickableTint(styleColor: string) {
+ const named = [ "to-boolean", [ "coalesce", [ "get", "name_en" ], [ "get", "name" ] ] ];
+
+ return [
+ "step",
+ [ "zoom" ],
+ styleColor,
+ MIN_POI_ZOOM, [ "case", named, PLACE_MARKER_COLOR, styleColor ]
+ ];
+}
+
/** The name of a place and the icon it would wear as a marker, as the tooltip shows them. */
function placeLabel(place: GeoSearchResult) {
const label = document.createElement("span");
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html
index e8ae794e1a..ae86682d13 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html
@@ -180,10 +180,13 @@
museums and other places around the area being viewed. Once the map is
zoomed in far enough for these to be drawn, they can be clicked:</p>
<ol>
- <li>Rest the mouse on one of the place icons drawn by the map. Its name
- appears above it, and the cursor turns into a pointer. The map draws these
- places as bare icons, so hovering is how their names are read — showing
- them all at once would crowd out the titles of your own markers.</li>
+ <li>Look for the places drawn in orange — the same colour a searched place
+ is pinned in. Those are the ones that can be clicked; the rest of the map's
+ places stay grey.</li>
+ <li>Rest the mouse on one of them. Its name appears above it, and the cursor
+ turns into a pointer. The map draws these places as bare icons, so hovering
+ is how their names are read — showing them all at once would crowd out the
+ titles of your own markers.</li>
<li>Click it. The same panel used for a searched place appears, showing the
place's name and its coordinates, and a pin is placed on it.</li>
<li>Press <em>Add as marker</em> to keep it. The note is created with the
diff --git a/docs/User Guide/User Guide/Collections/Geo Map.md b/docs/User Guide/User Guide/Collections/Geo Map.md
index dc737772c2..5409d011d3 100644
--- a/docs/User Guide/User Guide/Collections/Geo Map.md
+++ b/docs/User Guide/User Guide/Collections/Geo Map.md
@@ -128,9 +128,10 @@ This works for:
When a vector map style is used, the map itself draws the shops, cafés, museums and other places around the area being viewed. Once the map is zoomed in far enough for these to be drawn, they can be clicked:
-1. Rest the mouse on one of the place icons drawn by the map. Its name appears above it, and the cursor turns into a pointer. The map draws these places as bare icons, so hovering is how their names are read — showing them all at once would crowd out the titles of your own markers.
-2. Click it. The same panel used for a searched place appears, showing the place's name and its coordinates, and a pin is placed on it.
-3. Press _Add as marker_ to keep it. The note is created with the place's name as its title, and the icon matching the kind of place it is.
+1. Look for the places drawn in orange — the same colour a searched place is pinned in. Those are the ones that can be clicked; the rest of the map's places stay grey.
+2. Rest the mouse on one of them. Its name appears above it, and the cursor turns into a pointer. The map draws these places as bare icons, so hovering is how their names are read — showing them all at once would crowd out the titles of your own markers.
+3. Click it. The same panel used for a searched place appears, showing the place's name and its coordinates, and a pin is placed on it.
+4. Press _Add as marker_ to keep it. The note is created with the place's name as its title, and the icon matching the kind of place it is.
Places are read from the map data that has already been downloaded, so clicking one does not send anything to the internet.