diff --git a/dwds/debug_extension_mv3/web/background.dart b/dwds/debug_extension_mv3/web/background.dart index 552a5f571..9de0c0f60 100644 --- a/dwds/debug_extension_mv3/web/background.dart +++ b/dwds/debug_extension_mv3/web/background.dart @@ -29,6 +29,16 @@ void _registerListeners() { chrome.runtime.onMessage.addListener(allowInterop(_handleRuntimeMessages)); chrome.tabs.onRemoved .addListener(allowInterop((tabId, _) => maybeRemoveLifelinePort(tabId))); + // Update the extension icon on tab navigation: + chrome.tabs.onActivated.addListener(allowInterop((ActiveInfo info) { + _updateIcon(info.tabId); + })); + chrome.windows.onFocusChanged.addListener(allowInterop((_) async { + final currentTab = await _getTab(); + if (currentTab?.id != null) { + _updateIcon(currentTab!.id); + } + })); // Detect clicks on the Dart Debug Extension icon. chrome.action.onClicked.addListener(allowInterop(_startDebugSession)); @@ -86,27 +96,39 @@ void _handleRuntimeMessages( expectedSender: Script.detector, expectedRecipient: Script.background, messageHandler: (DebugInfo debugInfo) async { - final currentTab = await _getTab(); - final currentUrl = currentTab?.url ?? ''; - final appUrl = debugInfo.appUrl ?? ''; - if (currentTab == null || - currentUrl.isEmpty || - appUrl.isEmpty || - currentUrl != appUrl) { - console.warn( - 'Dart app detected at $appUrl but current tab is $currentUrl.'); + final dartTab = sender.tab; + if (dartTab == null) { + console.warn('Received debug info but tab is missing.'); return; } // Save the debug info for the Dart app in storage: await setStorageObject( - type: StorageObject.debugInfo, - value: debugInfo, - tabId: currentTab.id); + type: StorageObject.debugInfo, value: debugInfo, tabId: dartTab.id); // Update the icon to show that a Dart app has been detected: - chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null); + final currentTab = await _getTab(); + if (currentTab?.id == dartTab.id) { + _setDebuggableIcon(); + } }); } +void _updateIcon(int activeTabId) async { + final debugInfo = await _fetchDebugInfo(activeTabId); + if (debugInfo != null) { + _setDebuggableIcon(); + } else { + _setDefaultIcon(); + } +} + +void _setDebuggableIcon() { + chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null); +} + +void _setDefaultIcon() { + chrome.action.setIcon(IconInfo(path: 'dart_grey.png'), /*callback*/ null); +} + Future _fetchDebugInfo(int tabId) { return fetchStorageObject( type: StorageObject.debugInfo, diff --git a/dwds/debug_extension_mv3/web/chrome_api.dart b/dwds/debug_extension_mv3/web/chrome_api.dart index 05ccbfedc..14d4c368f 100644 --- a/dwds/debug_extension_mv3/web/chrome_api.dart +++ b/dwds/debug_extension_mv3/web/chrome_api.dart @@ -177,15 +177,29 @@ class Tabs { external Object create(TabInfo tabInfo); + external OnActivatedHandler get onActivated; + external OnRemovedHandler get onRemoved; } +@JS() +@anonymous +class OnActivatedHandler { + external void addListener(void Function(ActiveInfo activeInfo) callback); +} + @JS() @anonymous class OnRemovedHandler { external void addListener(void Function(int tabId, dynamic info) callback); } +@JS() +@anonymous +class ActiveInfo { + external int get tabId; +} + @JS() @anonymous class TabInfo { @@ -218,6 +232,14 @@ class Tab { @anonymous class Windows { external Object create(WindowInfo? createData); + + external OnFocusChangedHandler get onFocusChanged; +} + +@JS() +@anonymous +class OnFocusChangedHandler { + external void addListener(void Function(int windowId) callback); } @JS() diff --git a/dwds/debug_extension_mv3/web/dart_grey.png b/dwds/debug_extension_mv3/web/dart_grey.png new file mode 100644 index 000000000..3afba3fbe Binary files /dev/null and b/dwds/debug_extension_mv3/web/dart_grey.png differ