From dfb7de4954c942e4cd1cd541f4c305a6c8367910 Mon Sep 17 00:00:00 2001 From: Marcus Lundblad Date: Wed, 3 May 2023 21:28:34 +0200 Subject: mapView: Save and restore map rotation Save map rotation and used stored value when starting up. --- src/mapView.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/mapView.js b/src/mapView.js index 537373a7..d4915c16 100644 --- a/src/mapView.js +++ b/src/mapView.js @@ -135,6 +135,7 @@ export class MapView extends Gtk.Overlay { this._mainWindow = mainWindow; this._storeId = 0; + this._storeRotationId = 0; this.map = this._initMap(); this.child = this.map; @@ -145,8 +146,10 @@ export class MapView extends Gtk.Overlay { this._initScale(); this._initLayers(); - if (Application.normalStartup) + if (Application.normalStartup) { this._goToStoredLocation(); + this._setStoredRotation(); + } this.shapeLayerStore = new Gio.ListStore(GObject.TYPE_OBJECT); @@ -268,6 +271,7 @@ export class MapView extends Gtk.Overlay { map.viewport.min_zoom_level = MapMinZoom; map.viewport.connect('notify::latitude', this._onViewMoved.bind(this)); + map.viewport.connect('notify::rotation', this._onViewRotated.bind(this)); // switching map type will set view min-zoom-level from map source map.viewport.connect('notify::min-zoom-level', () => { if (map.viewport.min_zoom_level < MapMinZoom) { @@ -694,6 +698,14 @@ export class MapView extends Gtk.Overlay { } } + _storeRotation() { + let viewport = this.map.viewport; + let rotation = viewport.rotation; + + if (!isNaN(rotation)) + Application.settings.set('rotation', rotation); + } + _goToStoredLocation() { let location = Application.settings.get('last-viewed-location'); @@ -727,6 +739,18 @@ export class MapView extends Gtk.Overlay { } } + _setStoredRotation() { + let rotation = Application.settings.get('rotation'); + + if (rotation < 0.0 || rotation >= 2 * Math.PI) { + // safeguard agains out-of-bounds rotation values + Utils.debug('Invalid stored rotation, set no rotation'); + rotation = 0; + } + + this.map.viewport.rotation = rotation; + } + gotoBBox(bbox, linear) { if (!bbox.isValid()) { Utils.debug('Bounding box is invalid'); @@ -958,6 +982,16 @@ export class MapView extends Gtk.Overlay { }); } + _onViewRotated() { + if (this._storeRotationId !== 0) + return; + + this._storeRotationId = GLib.timeout_add(null, _LOCATION_STORE_TIMEOUT, () => { + this._storeRotationId = 0; + this._storeRotation(); + }); + } + onSetMarkerSelected(selectedMarker) { this.emit('marker-selected', selectedMarker); } -- cgit v1.2.1