GithubHelp home page GithubHelp logo

Does this work on KDE? about tray-icon HOT 6 CLOSED

virtualritz avatar virtualritz commented on June 14, 2024
Does this work on KDE?

from tray-icon.

Comments (6)

FabianLars avatar FabianLars commented on June 14, 2024

Possible duplicate of #13? (can you check if that applies to you?)

from tray-icon.

virtualritz avatar virtualritz commented on June 14, 2024

This is an egui (eframe) app.
If I add:

gtk::init().unwrap();

the errors disappears and I instead get:

Gtk-Message: 00:49:14.151: Failed to load module ""appmenu-gtk-module""

but still no tray icon.

I tried:

sudo apt reinstall appmenu-gtk3-module appmenu-gtk2-module

but no cigar.

from tray-icon.

FabianLars avatar FabianLars commented on June 14, 2024

but you do have libappindicator (can't remember the exact package name) or libayatana-appindicator3-dev installed, right?

from tray-icon.

virtualritz avatar virtualritz commented on June 14, 2024

but you do have libappindicator (can't remember the exact package name) or libayatana-appindicator3-dev installed, right?

Yes, both.

from tray-icon.

amrbashir avatar amrbashir commented on June 14, 2024

Unfortunately on Linux, sometimes you need to set a menu (an empty one will do) I will update the docs accordingly.

here is an example with egui:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use eframe::egui;

fn main() -> Result<(), eframe::Error> {
    // Log to stdout (if you run with `RUST_LOG=debug`).
    tracing_subscriber::fmt::init();

    let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
    let icon = load_icon(std::path::Path::new(path));

    #[cfg(target_os = "linux")]
    std::thread::spawn(|| {
        use tray_icon::{menu::Menu, TrayIconBuilder};

        gtk::init().unwrap();
        let tray_icon = TrayIconBuilder::new()
            .with_menu(Box::new(Menu::new()))
            .with_icon(icon)
            .build()
            .unwrap();

        gtk::main();
    });

    #[cfg(not(target_os = "linux"))]
    let tray_icon = TrayIconBuilder::new().with_icon(icon).build().unwrap();

    let options = eframe::NativeOptions {
        initial_window_size: Some(egui::vec2(320.0, 240.0)),
        ..Default::default()
    };

    eframe::run_native(
        "My egui App",
        options,
        Box::new(|_cc| Box::new(MyApp::default())),
    )
}

struct MyApp {
    name: String,
    age: u32,
}

impl Default for MyApp {
    fn default() -> Self {
        Self {
            name: "Arthur".to_owned(),
            age: 42,
        }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        use tray_icon::{menu::MenuEvent, TrayEvent};

        if let Ok(event) = TrayEvent::receiver().try_recv() {
            println!("tray event: {:?}", event);
        }

        if let Ok(event) = MenuEvent::receiver().try_recv() {
            println!("menu event: {:?}", event);
        }

        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("My egui Application");
            ui.horizontal(|ui| {
                let name_label = ui.label("Your name: ");
                ui.text_edit_singleline(&mut self.name)
                    .labelled_by(name_label.id);
            });
            ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
            if ui.button("Click each year").clicked() {
                self.age += 1;
            }
            ui.label(format!("Hello '{}', age {}", self.name, self.age));
        });
    }
}

fn load_icon(path: &std::path::Path) -> tray_icon::icon::Icon {
    let (icon_rgba, icon_width, icon_height) = {
        let image = image::open(path)
            .expect("Failed to open icon path")
            .into_rgba8();
        let (width, height) = image.dimensions();
        let rgba = image.into_raw();
        (rgba, width, height)
    };
    tray_icon::icon::Icon::from_rgba(icon_rgba, icon_width, icon_height)
        .expect("Failed to open icon")
}

but keep in mind that on Linux you need to modify the icon from within the thread that created it. It is a bit annoying but unfortunately.

from tray-icon.

virtualritz avatar virtualritz commented on June 14, 2024

Yes, this works for me. Thanks heaps!

from tray-icon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.