From 7b34097ba6bd43fd009a20f23222aa637fd11bfa Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Wed, 22 Jan 2025 09:33:03 +0100 Subject: [PATCH] Make sources popup respond to changes in sources --- .../views/fontoverview/panel-navigation.js | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/fontra/views/fontoverview/panel-navigation.js b/src/fontra/views/fontoverview/panel-navigation.js index cc1176d81..28690bab6 100644 --- a/src/fontra/views/fontoverview/panel-navigation.js +++ b/src/fontra/views/fontoverview/panel-navigation.js @@ -210,18 +210,27 @@ export class FontOverviewNavigation extends HTMLElement { async _makeFontSourcePopup() { const fontSources = await this.fontController.getSources(); + const popupItems = []; const selectedSourceIdentifier = () => this.fontController.fontSourcesInstancer.getLocationIdentifierForLocation( this.fontOverviewSettings.fontLocationSource ); - const popupItems = this.fontController - .getSortedSourceIdentifiers() - .map((fontSourceIdentifier) => ({ - value: fontSourceIdentifier, - label: fontSources[fontSourceIdentifier].name, - })); + const updatePopupItems = () => { + popupItems.splice( + 0, + popupItems.length, + ...this.fontController + .getSortedSourceIdentifiers() + .map((fontSourceIdentifier) => ({ + value: fontSourceIdentifier, + label: fontSources[fontSourceIdentifier].name, + })) + ); + }; + + updatePopupItems(); const controller = new ObservableController({ value: selectedSourceIdentifier(), @@ -240,18 +249,27 @@ export class FontOverviewNavigation extends HTMLElement { controller.addKeyListener("value", (event) => { const fontSourceIdentifier = event.newValue; - const sourceLocation = { - ...fontSources[fontSourceIdentifier]?.location, - }; // A font may not have any font sources, therefore the ?-check - if (!event.senderInfo?.sentFromSourceLocationListener) { + const sourceLocation = fontSources[fontSourceIdentifier]?.location; + if (sourceLocation && !event.senderInfo?.sentFromSourceLocationListener) { this.fontOverviewSettingsController.setItem( "fontLocationSource", - sourceLocation, + { ...sourceLocation }, { sentFromInput: true } ); } }); + this.fontController.addChangeListener( + { sources: null }, + (change, isExternalChange) => { + updatePopupItems(); + // Trigger *label* refresh. The *value* may not have changed, so we'll + // briefly set it to null to ensure the listeners get triggered + controller.model.value = null; + controller.model.value = selectedSourceIdentifier(); + } + ); + return popupSelect(controller, "value", popupItems); }