Skip to content

Commit a395c68

Browse files
authored
Handle detecting Dart app when tab changes (#1796)
1 parent 4fb4328 commit a395c68

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

dwds/debug_extension_mv3/web/background.dart

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ void _registerListeners() {
2929
chrome.runtime.onMessage.addListener(allowInterop(_handleRuntimeMessages));
3030
chrome.tabs.onRemoved
3131
.addListener(allowInterop((tabId, _) => maybeRemoveLifelinePort(tabId)));
32+
// Update the extension icon on tab navigation:
33+
chrome.tabs.onActivated.addListener(allowInterop((ActiveInfo info) {
34+
_updateIcon(info.tabId);
35+
}));
36+
chrome.windows.onFocusChanged.addListener(allowInterop((_) async {
37+
final currentTab = await _getTab();
38+
if (currentTab?.id != null) {
39+
_updateIcon(currentTab!.id);
40+
}
41+
}));
3242

3343
// Detect clicks on the Dart Debug Extension icon.
3444
chrome.action.onClicked.addListener(allowInterop(_startDebugSession));
@@ -86,27 +96,39 @@ void _handleRuntimeMessages(
8696
expectedSender: Script.detector,
8797
expectedRecipient: Script.background,
8898
messageHandler: (DebugInfo debugInfo) async {
89-
final currentTab = await _getTab();
90-
final currentUrl = currentTab?.url ?? '';
91-
final appUrl = debugInfo.appUrl ?? '';
92-
if (currentTab == null ||
93-
currentUrl.isEmpty ||
94-
appUrl.isEmpty ||
95-
currentUrl != appUrl) {
96-
console.warn(
97-
'Dart app detected at $appUrl but current tab is $currentUrl.');
99+
final dartTab = sender.tab;
100+
if (dartTab == null) {
101+
console.warn('Received debug info but tab is missing.');
98102
return;
99103
}
100104
// Save the debug info for the Dart app in storage:
101105
await setStorageObject<DebugInfo>(
102-
type: StorageObject.debugInfo,
103-
value: debugInfo,
104-
tabId: currentTab.id);
106+
type: StorageObject.debugInfo, value: debugInfo, tabId: dartTab.id);
105107
// Update the icon to show that a Dart app has been detected:
106-
chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null);
108+
final currentTab = await _getTab();
109+
if (currentTab?.id == dartTab.id) {
110+
_setDebuggableIcon();
111+
}
107112
});
108113
}
109114

115+
void _updateIcon(int activeTabId) async {
116+
final debugInfo = await _fetchDebugInfo(activeTabId);
117+
if (debugInfo != null) {
118+
_setDebuggableIcon();
119+
} else {
120+
_setDefaultIcon();
121+
}
122+
}
123+
124+
void _setDebuggableIcon() {
125+
chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null);
126+
}
127+
128+
void _setDefaultIcon() {
129+
chrome.action.setIcon(IconInfo(path: 'dart_grey.png'), /*callback*/ null);
130+
}
131+
110132
Future<DebugInfo?> _fetchDebugInfo(int tabId) {
111133
return fetchStorageObject<DebugInfo>(
112134
type: StorageObject.debugInfo,

dwds/debug_extension_mv3/web/chrome_api.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,29 @@ class Tabs {
177177

178178
external Object create(TabInfo tabInfo);
179179

180+
external OnActivatedHandler get onActivated;
181+
180182
external OnRemovedHandler get onRemoved;
181183
}
182184

185+
@JS()
186+
@anonymous
187+
class OnActivatedHandler {
188+
external void addListener(void Function(ActiveInfo activeInfo) callback);
189+
}
190+
183191
@JS()
184192
@anonymous
185193
class OnRemovedHandler {
186194
external void addListener(void Function(int tabId, dynamic info) callback);
187195
}
188196

197+
@JS()
198+
@anonymous
199+
class ActiveInfo {
200+
external int get tabId;
201+
}
202+
189203
@JS()
190204
@anonymous
191205
class TabInfo {
@@ -218,6 +232,14 @@ class Tab {
218232
@anonymous
219233
class Windows {
220234
external Object create(WindowInfo? createData);
235+
236+
external OnFocusChangedHandler get onFocusChanged;
237+
}
238+
239+
@JS()
240+
@anonymous
241+
class OnFocusChangedHandler {
242+
external void addListener(void Function(int windowId) callback);
221243
}
222244

223245
@JS()
2.75 KB
Loading

0 commit comments

Comments
 (0)