olback / tray-item-rs

Multi-platform Tray Indicator
https://crates.io/crates/tray-item
MIT License
287 stars 39 forks source link

Error when trying to start tray-item from a spawned thread in linux #15

Closed markjfisher closed 2 years ago

markjfisher commented 2 years ago

I'm trying to combine tray-item and druid so that I can show a window from a tray-item event (e.g. clicking a menu entry).

I'm using druid to create a window (which will be initially hidden), and give trayitem the handler to send commands to so the window can react to menu events from system tray.

My code is as follows:

pub fn create_window() -> Result<(), PlatformError> {
    let main_window = WindowDesc::new(build_root_widget)
        .title("Some window")
        .window_size((400.0, 400.0));

    let initial_state: HelloState = HelloState { name: "World".into() };

    let launcher = AppLauncher::with_window(main_window);
    let event_sink = launcher.get_external_handle();
    thread::spawn(move || create_tray_icon(event_sink));
    // ...
}

fn create_tray_icon(event_sink: ExtEventSink) {
    gtk::init().unwrap();
    let mut tray = TrayItem::new("Some Example", "accessories-calculator").unwrap();
    // ... other code as per linux.rs example.
}

// ... other functions to build the UI, etc.

So I'm using thread::spawn to create the tray item.

However, I'm getting the following error when the gtk::init() is run:

Gdk-ERROR **: 12:57:41.458: The program 'jack-testing' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadImplementation (server does not implement operation)'.
  (Details: serial 162 error_code 17 request_code 20 (core protocol) minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the GDK_SYNCHRONIZE environment
   variable to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)

Is it possible to create the tray in a separate thread in linux?

Cheers, Mark

olback commented 2 years ago

Unfortunately not. GTK requires that the gtk_main thread is the main thread in your program

markjfisher commented 2 years ago

Thanks for the clarification!