Comments (6)
Possible duplicate of #13? (can you check if that applies to you?)
from tray-icon.
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.
but you do have libappindicator (can't remember the exact package name) or libayatana-appindicator3-dev installed, right?
from tray-icon.
but you do have libappindicator (can't remember the exact package name) or libayatana-appindicator3-dev installed, right?
Yes, both.
from tray-icon.
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.
Yes, this works for me. Thanks heaps!
from tray-icon.
Related Issues (20)
- how to use in tauri v2? HOT 1
- Tray click events on linux not firing HOT 6
- using gtk4? HOT 4
- Handle ctrl+left click as right click on macos HOT 1
- Program crash after closing tray-icon menu without clicking any options HOT 1
- It is not clear how to use it HOT 7
- `mouse-leave` and `mouse-enter` events HOT 1
- How to flash the tray icon? HOT 4
- macOS: Incorrect tray icon size HOT 2
- `Icon::from_resource` does not support icon without a ordinal id
- Unable to run example directly from terminal, works from IDE (RustRover) HOT 3
- Support template icon on macOS HOT 1
- tray icon menu renders under taskbar HOT 5
- Listening events for tray icons HOT 1
- tray-icon = "0.14.0" Failed to reference on win11 HOT 9
- The enter event is only triggered for the first time
- feat: query the tray icon rect without waiting for an event HOT 1
- Tao example don't show menu on click HOT 2
- MacOS Sonoma displaying more than one icon HOT 10
- How to use MenuItem on Linux? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tray-icon.