[bug][linux][v2] App window is not properly focused on creation since v2
Describe the bug
Hi, I upgraded from v1 to the latest v2 RC, and the window is not focused anymore on creation, even though I have set focus: true in the tauri.conf.json
I also added those permissions to the capabilities/main.json:
"core:window:default",
"core:window:allow-set-focus",
Note that tauri v2 docs still mention focus in the window config: https://v2.tauri.app/reference/config/#windowconfig
Reproduction
Relevant project where I use tauri: https://github.com/vemonet/EmojiMart
Minimal repro:
- Initialize new tauri project for solidjs:
npm create tauri-app@latest -- --rc - Enable focus in
tauri.conf.json
{
"app": {
"windows": [
{
"title": "new-tauri-app",
"width": 800,
"height": 600,
"focus": true
}
],
}
- Enable permissions to close window in
src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"shell:allow-open",
"core:window:default",
"core:window:allow-close",
"core:window:allow-set-focus"
]
}
- Add code to close window when hitting esc key (to find out if the window is focused or not) in
src/App.tsx
import { getCurrentWindow } from "@tauri-apps/api/window";
function App() {
// ...
onMount(() => {
document.addEventListener('keypress', (event: any) => {
if (event.code === 'Escape') getCurrentWindow().close()
})
})
// ...
}
- Start the app in dev with
npm run tauri dev - Try to hit esc after the window pop (without clicking on the window or hitting tab): the window does not close, because it is not focused properly. If you click on the window or hit tab then the window is focused and esc will properly close hit.
Expected behavior
When the tauri app window pops it should be fully focus without the need to click on it or hit tab (which was the behavior in tauri v1)
Full tauri info output
[✔] Environment
- OS: Ubuntu 22.04 X64
✔ webkit2gtk-4.1: 2.44.2
✔ rsvg2: 2.52.5
✔ rustc: 1.80.1 (3f5fd8dd4 2024-08-06)
✔ cargo: 1.80.1 (376290515 2024-07-16)
✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (default)
- node: 20.17.0
- yarn: 1.22.22
- npm: 10.8.2
[-] Packages
- tauri [RUST]: 2.0.0-rc.6
- tauri-build [RUST]: 2.0.0-rc.6
- wry [RUST]: 0.42.0
- tao [RUST]: 0.29.1
- @tauri-apps/api [NPM]: 2.0.0-rc.3
- @tauri-apps/cli [NPM]: 2.0.0-rc.7
[-] App
- build-type: bundle
- CSP: unset
- frontendDist: ../build
- devUrl: http://localhost:5173/
- framework: Svelte
- bundler: Vite
Stack trace
No response
Additional context
No response
I can reproduce this bug. Actually, the App is focused. So, for vanilla JS template, I performed the following steps:
- Add
greet();in JS file, inside thewindow.addEventListener("DOMContentLoaded")like this, so thatgreet()is called as soon as DOM is loaded. Also, addEscapekeypress event.
window.addEventListener("DOMContentLoaded", () => {
greetInputEl = document.querySelector("#greet-input");
greetMsgEl = document.querySelector("#greet-msg");
greet();
});
document.addEventListener('keypress', (event) => {
if (event.code === 'Escape'){
greet()
}
})
- Edit
greet()inmain.rsto printis_focused()for eachwebview_window().
#[tauri::command]
fn greet(app: tauri::AppHandle ,name: &str) -> String {
for i in app.webview_windows() {
println!("{:?}", i.1.is_focused());
}
format!("Hello, {}! You've been greeted from Rust!", name)
}
Result: It printed Ok(true), and App is indeed in focus. However, nothing happens on pressing Escape, till I click inside the window.
Interesting, so using app.webview_windows() show that Tauri thinks the app is focus when it is actually not... So the whole API about focused window is broken (at least on x11)
I don't trust what the APIs says, it is probably wrong if there is a problem
The fact is: if the window view was really focused on popup then hitting esc would close the app wihout the need to close the window.
The problem is still there after upgrading to tauri 2.0.1, this is quite annoying because we are losing basic features by upgrading. Any chances this behavior get fixed?
I am having the same problem, which is a bit annoying because I use my tauri v2 app as a search tool that has the search-field focused on startup. now I have to hit tab or click into the app to get the field focused (it works without problems on macos. It also works if I only run the frontend inside the browser without tauri) hotkeys also only work after clicking into the app-window.
[using arch-linux + i3]
edit: I just updated and it works now. Thank you <3