tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
81.88k stars 2.46k forks source link

[bug][linux][v2] App window is not properly focused on creation since v2 #10746

Open vemonet opened 3 weeks ago

vemonet commented 3 weeks ago

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:

  1. Initialize new tauri project for solidjs: npm create tauri-app@latest -- --rc
  2. Enable focus in tauri.conf.json
{
  "app": {
    "windows": [
      {
        "title": "new-tauri-app",
        "width": 800,
        "height": 600,
        "focus": true
      }
    ],
}
  1. 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"
  ]
}
  1. 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() }) }) // ... }


5. Start the app in dev with `npm run tauri dev`
6. 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

```text
[✔] 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

naman-crabnebula commented 1 week ago

I can reproduce this bug. Actually, the App is focused. So, for vanilla JS template, I performed the following steps:

  1. Add greet(); in JS file, inside the window.addEventListener("DOMContentLoaded") like this, so that greet() is called as soon as DOM is loaded. Also, add Escape keypress event.
window.addEventListener("DOMContentLoaded", () => {
  greetInputEl = document.querySelector("#greet-input");
  greetMsgEl = document.querySelector("#greet-msg");
  greet();
});

document.addEventListener('keypress', (event) =>  {
  if (event.code === 'Escape'){
    greet()
  }
})
  1. Edit greet() in main.rs to print is_focused() for each webview_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.