From 8d08082f22e044b232402d3463d7999567e46ed7 Mon Sep 17 00:00:00 2001 From: knightmare2600 Date: Fri, 9 Oct 2026 22:59:50 +0100 Subject: [PATCH] fix: save and restore map style, projection and overlays user-preferences.example.json has shipped MapStyle, MapProjection and MapOverlays since the map overhaul, but nothing read them: the map always started on Earth at Night / Flat / Day-Night, and Save-UserPreferences rewrote the file with a fixed key set, deleting the three keys on the first save. There is no save on window close (it minimises to the tray), so map changes were never persisted. - Save-UserPreferences now writes MapStyle, MapProjection and MapOverlays from the current toolbar state. - The style and projection pickers and the ten overlay toggles save when changed, as the exchange toggles already do. - Saved settings are applied straight after the pickers are populated, before change handlers are attached, so start-up does not redraw the map once per toggle. - New Private/Get-MapPreferenceState resolves the saved values. Unknown styles/projections fall back to the defaults; overlay names with no toggle (e.g. 'TimeLine' from the original example file) are ignored; an empty MapOverlays list means all overlays off; a missing key keeps the XAML defaults. - PSExchangeClock.psm1 now dot-sources Private/*.ps1 (not exported). - Pester tests in Tests/MapPreferences.Tests.ps1. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 14 ++++ PSExchangeClock/PSExchangeClock.psm1 | 11 +++ .../Private/Get-MapPreferenceState.ps1 | 37 +++++++++ .../New-StockExchangeCountdownDashboard.ps1 | 80 ++++++++++++++----- README.md | 3 + Tests/MapPreferences.Tests.ps1 | 75 +++++++++++++++++ 6 files changed, 199 insertions(+), 21 deletions(-) create mode 100644 PSExchangeClock/Private/Get-MapPreferenceState.ps1 create mode 100644 Tests/MapPreferences.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d10eca..688a613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ All notable changes to PSExchangeClock will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Pester tests under `Tests/` (run with `Invoke-Pester ./Tests`), starting with map preference restore + +### Fixed + +- Map style, projection and overlay choices were never restored at start-up and were dropped whenever preferences were saved. They are now saved when changed and restored on launch (`MapStyle`, `MapProjection`, `MapOverlays`) + +### Changed + +- `PSExchangeClock.psm1` also loads non-exported helpers from `Private/` + ## [1.1.0] - 2026-09-28 ### Fixed diff --git a/PSExchangeClock/PSExchangeClock.psm1 b/PSExchangeClock/PSExchangeClock.psm1 index 6bb50e1..159a9c2 100644 --- a/PSExchangeClock/PSExchangeClock.psm1 +++ b/PSExchangeClock/PSExchangeClock.psm1 @@ -1,6 +1,17 @@ # PSExchangeClock module loader $PSModuleRoot = $PSScriptRoot +# Dot-source private helpers (not exported) +$privateFunctions = Get-ChildItem -Path "$PSModuleRoot\Private\*.ps1" -ErrorAction SilentlyContinue +foreach ($function in $privateFunctions) { + try { + . $function.FullName + } + catch { + Write-Error "Failed to import private helpers $($function.BaseName): $_" + } +} + # Dot-source all public functions $publicFunctions = Get-ChildItem -Path "$PSModuleRoot\Public\*.ps1" -ErrorAction SilentlyContinue foreach ($function in $publicFunctions) { diff --git a/PSExchangeClock/Private/Get-MapPreferenceState.ps1 b/PSExchangeClock/Private/Get-MapPreferenceState.ps1 new file mode 100644 index 0000000..b6e10d5 --- /dev/null +++ b/PSExchangeClock/Private/Get-MapPreferenceState.ps1 @@ -0,0 +1,37 @@ +function Get-MapPreferenceState { + <# + .SYNOPSIS + Resolves the saved map style, projection and overlays from user preferences. + .DESCRIPTION + Unknown or missing values resolve to $null so the caller keeps its own + defaults. Overlays resolve to $null only when MapOverlays is absent; an + empty saved list means "all overlays off". Unknown overlay names (for + example 'TimeLine' from older example files, which has no toggle) are + ignored. + .OUTPUTS + Hashtable with Style, Projection and Overlays. + #> + [CmdletBinding()] + [OutputType([hashtable])] + param( + $UserPreferences, + [string[]]$ValidStyles = @(), + [string[]]$ValidProjections = @(), + [string[]]$OverlayNames = @() + ) + + $state = @{ Style = $null; Projection = $null; Overlays = $null } + if (-not $UserPreferences) { return $state } + + if ($UserPreferences.MapStyle -and $ValidStyles -contains $UserPreferences.MapStyle) { + $state.Style = [string]$UserPreferences.MapStyle + } + if ($UserPreferences.MapProjection -and $ValidProjections -contains $UserPreferences.MapProjection) { + $state.Projection = [string]$UserPreferences.MapProjection + } + if ($UserPreferences.PSObject.Properties['MapOverlays']) { + $saved = @($UserPreferences.MapOverlays | Where-Object { $_ }) + $state.Overlays = @($OverlayNames | Where-Object { $saved -contains $_ }) + } + return $state +} diff --git a/PSExchangeClock/Public/New-StockExchangeCountdownDashboard.ps1 b/PSExchangeClock/Public/New-StockExchangeCountdownDashboard.ps1 index 8c48fcb..a41abf5 100644 --- a/PSExchangeClock/Public/New-StockExchangeCountdownDashboard.ps1 +++ b/PSExchangeClock/Public/New-StockExchangeCountdownDashboard.ps1 @@ -143,6 +143,11 @@ function New-StockExchangeCountdownDashboard { if ($chkNotify15 -and $chkNotify15.IsChecked) { $prefs.NotifyThresholds += 15 } if ($chkNotify5 -and $chkNotify5.IsChecked) { $prefs.NotifyThresholds += 5 } if ($chkAlwaysOnTop) { $prefs.AlwaysOnTop = [bool]$chkAlwaysOnTop.IsChecked } + if ($cmbMapStyle -and $cmbMapStyle.SelectedItem) { $prefs.MapStyle = [string]$cmbMapStyle.SelectedItem.Tag } + if ($script:mapProjection) { $prefs.MapProjection = [string]$script:mapProjection } + if ($script:mapOverlayToggles) { + $prefs.MapOverlays = @($script:mapOverlayToggles.Keys | Where-Object { $script:mapOverlayToggles[$_].IsChecked }) + } } catch { } # API keys are stored via the secrets backend, not in JSON @@ -995,6 +1000,38 @@ public static class CredManager { } $cmbMapProjection.SelectedIndex = 0 + # Overlay toggles by preference name (MapOverlays in user-preferences.json) + $script:mapOverlayToggles = [ordered]@{ + DayNight = $chkTerminator + TimeZoneBands = $chkTimeZoneBands + PoliticalBorders = $chkPoliticalBounds + TimeZoneBoundaries = $chkTZBoundaries + Earthquakes = $chkEarthquakes + Volcanoes = $chkVolcanoes + NaturalEvents = $chkNaturalEvents + ConflictZones = $chkConflictZones + SubmarineCables = $chkSubmarineCables + PowerPlants = $chkPowerPlants + } + + # Restore saved map settings (runs before the change handlers are attached, so no redraws) + $mapState = Get-MapPreferenceState -UserPreferences $script:userPrefs ` + -ValidStyles @($mapStyleOptions | ForEach-Object { $_.Value }) ` + -ValidProjections @($projOptions | ForEach-Object { $_.Value }) ` + -OverlayNames @($script:mapOverlayToggles.Keys) + if ($mapState.Style) { + $cmbMapStyle.SelectedIndex = [array]::IndexOf(@($mapStyleOptions | ForEach-Object { $_.Value }), $mapState.Style) + } + if ($mapState.Projection) { + $cmbMapProjection.SelectedIndex = [array]::IndexOf(@($projOptions | ForEach-Object { $_.Value }), $mapState.Projection) + $script:mapProjection = $mapState.Projection + } + if ($null -ne $mapState.Overlays) { + foreach ($name in $script:mapOverlayToggles.Keys) { + $script:mapOverlayToggles[$name].IsChecked = $mapState.Overlays -contains $name + } + } + # ── Market Data Cache ───────────────────────────────────────── $script:newsCache = @{ @@ -4737,7 +4774,7 @@ public static class CredManager { # ── Map Toolbar Event Handlers ──────────────────────────────── # Map style ComboBox - $cmbMapStyle.Add_SelectionChanged({ Initialize-WorldMap }) + $cmbMapStyle.Add_SelectionChanged({ Initialize-WorldMap; Save-UserPreferences }) # Map projection ComboBox $cmbMapProjection.Add_SelectionChanged({ @@ -4746,30 +4783,31 @@ public static class CredManager { $script:mapProjection = $sel.Tag $script:expandedCluster = $null Initialize-WorldMap + Save-UserPreferences } }) # Overlay toggles - $chkTerminator.Add_Checked({ Initialize-WorldMap }) - $chkTerminator.Add_Unchecked({ Initialize-WorldMap }) - $chkTimeZoneBands.Add_Checked({ Initialize-WorldMap }) - $chkTimeZoneBands.Add_Unchecked({ Initialize-WorldMap }) - $chkPoliticalBounds.Add_Checked({ Initialize-WorldMap }) - $chkPoliticalBounds.Add_Unchecked({ Initialize-WorldMap }) - $chkEarthquakes.Add_Checked({ Initialize-WorldMap }) - $chkEarthquakes.Add_Unchecked({ Initialize-WorldMap }) - $chkVolcanoes.Add_Checked({ Initialize-WorldMap }) - $chkVolcanoes.Add_Unchecked({ Initialize-WorldMap }) - $chkNaturalEvents.Add_Checked({ Initialize-WorldMap }) - $chkNaturalEvents.Add_Unchecked({ Initialize-WorldMap }) - $chkSubmarineCables.Add_Checked({ Initialize-WorldMap }) - $chkSubmarineCables.Add_Unchecked({ Initialize-WorldMap }) - $chkPowerPlants.Add_Checked({ Initialize-WorldMap }) - $chkPowerPlants.Add_Unchecked({ Initialize-WorldMap }) - $chkTZBoundaries.Add_Checked({ Initialize-WorldMap }) - $chkTZBoundaries.Add_Unchecked({ Initialize-WorldMap }) - $chkConflictZones.Add_Checked({ Initialize-WorldMap }) - $chkConflictZones.Add_Unchecked({ Initialize-WorldMap }) + $chkTerminator.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkTerminator.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkTimeZoneBands.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkTimeZoneBands.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkPoliticalBounds.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkPoliticalBounds.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkEarthquakes.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkEarthquakes.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkVolcanoes.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkVolcanoes.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkNaturalEvents.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkNaturalEvents.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkSubmarineCables.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkSubmarineCables.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkPowerPlants.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkPowerPlants.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkTZBoundaries.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkTZBoundaries.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) + $chkConflictZones.Add_Checked({ Initialize-WorldMap; Save-UserPreferences }) + $chkConflictZones.Add_Unchecked({ Initialize-WorldMap; Save-UserPreferences }) # Overlays flyout popup toggle $btnOverlays.Add_Checked({ diff --git a/README.md b/README.md index 6915f89..7ae594c 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,9 @@ User preferences are stored in `%APPDATA%\PSExchangeClock\user-preferences.json` | `ActiveExchanges` | Array of exchange codes to display | All 20 exchanges | | `NotifyThresholds` | Minutes before close to show notifications | `[30, 15, 5]` | | `AlwaysOnTop` | Keep dashboard window above other windows | `false` | +| `MapStyle` | World map style: `NightSatellite`, `BlueMarble`, `VectorDark` or `Minimal` | `NightSatellite` | +| `MapProjection` | `Flat` or `Globe` | `Flat` | +| `MapOverlays` | Overlays switched on: `DayNight`, `TimeZoneBands`, `PoliticalBorders`, `TimeZoneBoundaries`, `Earthquakes`, `Volcanoes`, `NaturalEvents`, `ConflictZones`, `SubmarineCables`, `PowerPlants`. An empty list means all off; unknown names are ignored | `["DayNight"]` | | `CommodityBaseCurrency` | Currency for commodity price display | `EUR` | | `SecretsBackend` | Where API keys are stored | `CredentialManager` | diff --git a/Tests/MapPreferences.Tests.ps1 b/Tests/MapPreferences.Tests.ps1 new file mode 100644 index 0000000..870d41e --- /dev/null +++ b/Tests/MapPreferences.Tests.ps1 @@ -0,0 +1,75 @@ +#requires -Version 7.0 +#requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' } + +BeforeAll { + $script:moduleRoot = Join-Path (Split-Path $PSScriptRoot -Parent) 'PSExchangeClock' + . (Join-Path $script:moduleRoot 'Private/Get-MapPreferenceState.ps1') + + # Mirrors the option values and toggle names in New-StockExchangeCountdownDashboard + $script:styles = @('NightSatellite', 'BlueMarble', 'VectorDark', 'Minimal') + $script:projections = @('Flat', 'Globe') + $script:overlays = @('DayNight', 'TimeZoneBands', 'PoliticalBorders', 'TimeZoneBoundaries', 'Earthquakes', + 'Volcanoes', 'NaturalEvents', 'ConflictZones', 'SubmarineCables', 'PowerPlants') + + function Resolve-Map($Prefs) { + Get-MapPreferenceState -UserPreferences $Prefs -ValidStyles $script:styles -ValidProjections $script:projections -OverlayNames $script:overlays + } + function ConvertTo-SavedPrefs([hashtable]$Prefs) { + # Same serialisation as Save-UserPreferences + $Prefs | ConvertTo-Json -Depth 3 | ConvertFrom-Json + } +} + +Describe 'Map preference restore' { + It 'restores style, projection and overlays after a save/load round trip' { + $saved = ConvertTo-SavedPrefs @{ MapStyle = 'BlueMarble'; MapProjection = 'Globe'; MapOverlays = @('Earthquakes', 'PowerPlants') } + $s = Resolve-Map $saved + $s.Style | Should -Be 'BlueMarble' + $s.Projection | Should -Be 'Globe' + $s.Overlays | Should -Be @('Earthquakes', 'PowerPlants') + } + + It 'restores a single saved overlay' { + $s = Resolve-Map (ConvertTo-SavedPrefs @{ MapOverlays = @('Volcanoes') }) + $s.Overlays | Should -Be @('Volcanoes') + } + + It 'treats an empty saved overlay list as all overlays off' { + $s = Resolve-Map (ConvertTo-SavedPrefs @{ MapOverlays = @() }) + # An empty list is "all off", not "unset" + $null -eq $s.Overlays | Should -BeFalse + @($s.Overlays).Count | Should -Be 0 + } + + It 'keeps the built-in defaults when nothing is saved' { + $s = Resolve-Map (ConvertTo-SavedPrefs @{ ActiveExchanges = @('XNYS') }) + $s.Style | Should -BeNullOrEmpty + $s.Projection | Should -BeNullOrEmpty + $null -eq $s.Overlays | Should -BeTrue + } + + It 'keeps the built-in defaults when there is no preferences file' { + $s = Resolve-Map $null + $s.Style | Should -BeNullOrEmpty + $null -eq $s.Overlays | Should -BeTrue + } + + It 'ignores unknown style and projection values' { + $s = Resolve-Map (ConvertTo-SavedPrefs @{ MapStyle = 'Sepia'; MapProjection = 'Mercator' }) + $s.Style | Should -BeNullOrEmpty + $s.Projection | Should -BeNullOrEmpty + } + + It 'ignores overlay names with no toggle, such as TimeLine from older example files' { + $s = Resolve-Map (ConvertTo-SavedPrefs @{ MapOverlays = @('DayNight', 'TimeLine') }) + $s.Overlays | Should -Be @('DayNight') + } + + It 'loads the shipped example preferences' { + $example = Get-Content (Join-Path $script:moduleRoot 'Data/user-preferences.example.json') -Raw | ConvertFrom-Json + $s = Resolve-Map $example + $s.Style | Should -Be 'NightSatellite' + $s.Projection | Should -Be 'Flat' + $s.Overlays | Should -Be @('DayNight') + } +}