floem icon indicating copy to clipboard operation
floem copied to clipboard

Global shortcuts on the main view does not work for Linux platform

Open dajoha opened this issue 1 year ago • 0 comments

On a Linux OS, when adding .on_key_up() or .on_key_down() to the main view in order to register some global shortcuts, nothing happens. Hitting any key does not trigger the keyboard event.

How to reproduce

On a Linux OS, run this code (floem = 0.1.1):

use floem::{
    keyboard::{Key, NamedKey, ModifiersState},
    view::View,
    views::{Decorators, empty},
};

fn app_view() -> impl View {
    empty().on_key_up(Key::Named(NamedKey::F11), ModifiersState::empty(), move |_| {
        println!("Hit F11");
    })
}

fn main() {
    floem::launch(app_view);
}

Then press the "F11" key => nothing is printed to the console.

The same problem happens:

  • when running the floem example "counter" (hitting "F11" should open the inspector);
  • when using another keystroke (nothing to deal with "Fxx" keys).

Tiny investigation

Maybe this is because the user's main view is wrapped into a stack(container()) for linux platforms: https://github.com/lapce/floem/blob/ab125b7f8171f7fd1dcdcb0c250de7b05f77e0de/src/window_handle.rs#L108

        #[cfg(target_os = "linux")]
        let view = with_scope(scope, move || {
            stack((
                container(view_fn(window_id)).style(|s| s.size(100.pct(), 100.pct())),
                context_menu_view(scope, window_id, context_menu, size),
            ))
            .style(|s| s.size(100.pct(), 100.pct()))
            .any()
        });

As an experiment I tried to replace this block by:

        #[cfg(target_os = "linux")]
        let view = with_scope(scope, move || view_fn(window_id));

Then the global shortcut worked (it crashed the app totally, but it prooves that the keyboard event was triggered, indeed other keys did nothing at all).

dajoha avatar Apr 04 '24 20:04 dajoha