Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions PSExchangeClock/PSExchangeClock.psm1
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions PSExchangeClock/Private/Get-MapPreferenceState.ps1
Original file line number Diff line number Diff line change
@@ -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
}
80 changes: 59 additions & 21 deletions PSExchangeClock/Public/New-StockExchangeCountdownDashboard.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = @{
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |

Expand Down
75 changes: 75 additions & 0 deletions Tests/MapPreferences.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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')
}
}