Skip to content

Commit 8e74198

Browse files
committed
fix: address code review comments
1 parent f30f1e0 commit 8e74198

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

packages/tauri-plugin/src/commands.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ pub(crate) async fn execute<R: Runtime>(
171171
}
172172

173173
/// Get the label of the currently focused/active window
174+
/// Note: Tauri 2.x doesn't expose window focus state, so this returns
175+
/// the "main" window if it exists, or the first window in lexicographic order
174176
#[command]
175177
pub(crate) async fn get_active_window_label<R: Runtime>(
176178
app: tauri::AppHandle<R>,
@@ -179,14 +181,16 @@ pub(crate) async fn get_active_window_label<R: Runtime>(
179181
if windows.is_empty() {
180182
return Err(Error::WindowError("No windows available".to_string()));
181183
}
182-
// In Tauri 2.x, we can't easily detect which window is focused
183-
// Return the main window if it exists, otherwise the first window
184+
// Return the "main" window if it exists for predictable behavior
184185
if let Some(main) = windows.get("main") {
185186
return Ok(main.label().to_string());
186187
}
187-
let first = windows.values().next()
188+
// Otherwise, return the first window in lexicographic order for consistency
189+
let mut labels: Vec<_> = windows.keys().collect();
190+
labels.sort();
191+
let first_label = labels.first()
188192
.ok_or_else(|| Error::WindowError("No windows available".to_string()))?;
189-
Ok(first.label().to_string())
193+
Ok(first_label.to_string())
190194
}
191195

192196
/// List all window labels in the application

packages/tauri-service/src/window.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,18 @@ export async function getWindowPort(browser: WebdriverIO.Browser, label: string)
5050
return windowPortMap.get(label);
5151
}
5252

53-
const windows = await listWindowLabels(browser);
5453
const endpoints = await pollDevtoolsEndpoints();
5554

5655
if (endpoints.length === 0) {
5756
return undefined;
5857
}
5958

60-
for (let i = 0; i < Math.min(windows.length, endpoints.length); i++) {
61-
const windowLabel = windows[i];
62-
const endpoint = endpoints[i];
59+
// Map endpoints by their ID (which corresponds to window label in Tauri)
60+
for (const endpoint of endpoints) {
6361
const port = extractPortFromWebSocketUrl(endpoint.webSocketDebuggerUrl);
64-
windowPortMap.set(windowLabel, port);
65-
log.debug(`Mapped window "${windowLabel}" to port ${port}`);
62+
// The endpoint id typically matches the window label (e.g., "main", "secondary")
63+
windowPortMap.set(endpoint.id, port);
64+
log.debug(`Mapped window "${endpoint.id}" to port ${port}`);
6665
}
6766

6867
return windowPortMap.get(label);

0 commit comments

Comments
 (0)