Files
raucao 5c7d5d6de0 WIP: Per-sheet drawer heights and minimized background sheets
Give each bottom sheet its own default height and remember the size the
user drags it to, per sheet, so one sheet's height no longer leaks into the
next.

- App menu now opens at the medium detent instead of near-full-screen.
- Place details opens at the short (collapsed) detent; the results and list
  sheets stay at medium.
- Detent memory is keyed by route (map-ui.sheetDetents) instead of one
  shared slot, and the `detent` arg is authoritative on mount.
- Background (parent) sheets are pinned to the minimum reachable detent on
  mount and on viewport changes, so they can never peek above the top sheet
  no matter how short it is. They still never write the detent memory.
2026-11-30 15:43:15 +04:00

215 lines
5.8 KiB
JavaScript

import Service, { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class MapUiService extends Service {
@service nostrData;
@tracked selectedPlace = null;
@tracked isSearching = false;
@tracked isCreating = false;
@tracked creationCoordinates = null;
@tracked returnToSearch = false;
@tracked returnToRoute = null;
@tracked currentCenter = null;
@tracked currentBounds = null;
@tracked currentZoom = null;
@tracked searchBoxHasFocus = false;
@tracked selectionOptions = {};
@tracked preventNextZoom = false;
@tracked searchResults = [];
@tracked currentSearch = null;
@tracked loadingState = null;
@tracked isSidebarVisible = false;
@tracked isSidebarOpening = false;
// Mobile bottom-sheet state. `sheetInsetPx` is how many pixels the sheet
// currently covers from the bottom of the viewport, and `sheetSnapSeq` bumps
// whenever a drag settles so the map can react once (not per frame).
//
// `sheetDetents` remembers the detent per sheet, keyed by sheet identity
// (route name), so a sheet reopens at the size the user last dragged it to.
// `sheetDetent` mirrors the interactive (top) sheet's current detent. Both
// are intentionally NOT tracked: the modifier reads them while setting up to
// restore the previous position, and reading a tracked value there would
// re-run setup on every drag frame.
@tracked sheetInsetPx = 0;
@tracked sheetSnapSeq = 0;
sheetDetents = {};
sheetDetent = null;
// The modifier that currently owns the covered-height signal. A plain field
// so it is safe to write during render. Writes to sheetInsetPx/sheetSnapSeq
// are gated on this owner so a stale modifier can never corrupt the value
// (see the ownership-token note on the setters below).
sheetInsetOwner = null;
scrollPositions = {};
@action
saveScrollPosition(key, value) {
this.scrollPositions[key] = value;
}
@action
getScrollPosition(key) {
return this.scrollPositions[key] || 0;
}
showSidebar() {
if (!this.isSidebarVisible) {
this.isSidebarVisible = true;
this.isSidebarOpening = true;
setTimeout(() => {
this.isSidebarOpening = false;
}, 250);
}
}
hideSidebar() {
this.isSidebarVisible = false;
this.isSidebarOpening = false;
this.resetSheet();
}
// --- Bottom-sheet covered-height signal ----------------------------------
//
// The Map reads sheetInsetPx during render, so the modifier must never write
// it (or sheetSnapSeq) while a render is in progress. Instead the modifier
// owns the slot and writes through an afterRender flush. Ownership makes the
// result independent of the order in which a stale modifier's release and a
// live modifier's flush land in the afterRender queue.
claimSheetInset(owner) {
this.sheetInsetOwner = owner;
}
setSheetInsetFor(owner, px) {
if (owner !== this.sheetInsetOwner) return;
const next = Math.max(0, Math.round(px));
// Guard against no-op writes: this build's tracked setter invalidates even
// when the value is unchanged, and the drag calls this per frame.
if (next !== this.sheetInsetPx) {
this.sheetInsetPx = next;
}
}
snapSheetFor(owner) {
if (owner !== this.sheetInsetOwner) return;
this.sheetSnapSeq++;
}
releaseSheetInset(owner) {
if (owner !== this.sheetInsetOwner) return;
this.sheetInsetOwner = null;
if (this.sheetInsetPx !== 0) {
this.sheetInsetPx = 0;
}
}
getSheetDetent(key) {
return this.sheetDetents[key] ?? null;
}
// A null name forgets the sheet (used when a drag dismisses it), so the next
// open falls back to the sheet's declared default.
setSheetDetent(key, name) {
if (key == null) return;
if (name == null) {
delete this.sheetDetents[key];
} else {
this.sheetDetents[key] = name;
}
this.sheetDetent = name ?? null;
}
resetSheet() {
this.sheetInsetOwner = null;
this.sheetInsetPx = 0;
this.sheetDetents = {};
this.sheetDetent = null;
}
selectPlace(place, options = {}) {
this.selectedPlace = place;
this.selectionOptions = options;
this.nostrData.loadPhotosForPlace(place);
}
clearSelection() {
this.selectedPlace = null;
this.selectionOptions = {};
this.preventNextZoom = false;
this.nostrData.loadPhotosForPlace(null);
}
setSearchResults(results) {
this.searchResults = results || [];
}
clearSearchResults() {
this.searchResults = [];
this.currentSearch = null;
}
startSearch() {
this.isSearching = true;
this.isCreating = false;
this.preventNextZoom = false;
}
stopSearch() {
this.isSearching = false;
}
startCreating() {
this.isCreating = true;
this.isSearching = false;
}
stopCreating() {
this.isCreating = false;
this.creationCoordinates = null;
}
updateCreationCoordinates(lat, lon) {
this.creationCoordinates = { lat, lon };
}
setSearchBoxFocus(isFocused) {
this.searchBoxHasFocus = isFocused;
}
updateCenter(lat, lon) {
this.currentCenter = { lat, lon };
}
updateZoom(zoom) {
this.currentZoom = zoom;
}
updateBounds(bounds) {
this.currentBounds = bounds;
}
startLoading(type, value) {
this.loadingState = { type, value };
}
stopLoading(type = null, value = null) {
// If no arguments provided, force stop (legacy/cleanup)
if (!type && !value) {
this.loadingState = null;
return;
}
// Only clear if the current state matches the request
// This prevents a previous search from clearing the state of a new search
if (
this.loadingState &&
this.loadingState.type === type &&
this.loadingState.value === value
) {
this.loadingState = null;
}
}
}