-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"tray-icon": "patch" | ||
--- | ||
|
||
Update docs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release | ||
|
||
use eframe::egui; | ||
use tray_icon::TrayIconBuilder; | ||
|
||
fn main() { | ||
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; | ||
|
||
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::<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::TrayEvent; | ||
|
||
if let Ok(event) = TrayEvent::receiver().try_recv() { | ||
println!("tray 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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters