Skip to content

Commit 38e1b33

Browse files
committed
impl windows tray icon
1 parent bb4d3b9 commit 38e1b33

File tree

5 files changed

+429
-10
lines changed

5 files changed

+429
-10
lines changed

crates/gpui/examples/tray.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use gpui::{
2-
App, Application, Context, Global, Image, MenuItem, SharedString, Tray, Window, WindowOptions,
2+
App, Application, Context, Global, MenuItem, SharedString, Tray, Window, WindowOptions,
33
actions, div, prelude::*,
44
};
55

@@ -90,13 +90,11 @@ struct AppState {
9090

9191
impl AppState {
9292
fn new() -> Self {
93+
let icon = image::load_from_memory(include_bytes!("image/app-icon.png")).unwrap();
9394
Self {
9495
view_mode: ViewMode::List,
9596
tray: Tray::new()
96-
.icon(Image::from_bytes(
97-
gpui::ImageFormat::Png,
98-
include_bytes!("image/app-icon.png").to_vec(),
99-
))
97+
.icon(icon.to_rgba8())
10098
.title("Tray App")
10199
.tooltip("This is a tray icon")
102100
.menu(Self::build_menus),

crates/gpui/src/platform/windows.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod events;
1010
mod keyboard;
1111
mod platform;
1212
mod system_settings;
13+
mod tray;
1314
mod util;
1415
mod vsync;
1516
mod window;
@@ -27,6 +28,7 @@ pub(crate) use events::*;
2728
pub(crate) use keyboard::*;
2829
pub(crate) use platform::*;
2930
pub(crate) use system_settings::*;
31+
pub(crate) use tray::*;
3032
pub(crate) use util::*;
3133
pub(crate) use vsync::*;
3234
pub(crate) use window::*;

crates/gpui/src/platform/windows/platform.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use windows::{
2727
core::*,
2828
};
2929

30-
use crate::*;
30+
use crate::{platform::windows::WindowsTray, *};
3131

3232
pub(crate) struct WindowsPlatform {
3333
inner: Rc<WindowsPlatformInner>,
@@ -59,6 +59,7 @@ pub(crate) struct WindowsPlatformState {
5959
callbacks: PlatformCallbacks,
6060
menus: RefCell<Vec<OwnedMenu>>,
6161
jump_list: RefCell<JumpList>,
62+
tray: RefCell<Option<WindowsTray>>,
6263
// NOTE: standard cursor handles don't need to close.
6364
pub(crate) current_cursor: Cell<Option<HCURSOR>>,
6465
directx_devices: RefCell<Option<DirectXDevices>>,
@@ -88,6 +89,7 @@ impl WindowsPlatformState {
8889
current_cursor: Cell::new(current_cursor),
8990
directx_devices: RefCell::new(directx_devices),
9091
menus: RefCell::new(Vec::new()),
92+
tray: RefCell::new(None),
9193
}
9294
}
9395
}
@@ -559,6 +561,23 @@ impl Platform for WindowsPlatform {
559561
self.set_dock_menus(menus);
560562
}
561563

564+
fn set_tray(&self, mut tray: Tray, _menus: Option<Vec<MenuItem>>, _keymap: &Keymap) {
565+
// let mut actions = Vec::new();
566+
// if let Some(menus) = menus {
567+
// menus.into_iter().for_each(|menu| {
568+
// if let Some(dock_menu) = DockMenuItem::new(menu).log_err() {
569+
// actions.push(dock_menu);
570+
// }
571+
// });
572+
// }
573+
let mut windows_tray = self.inner.state.tray.borrow_mut();
574+
if let Some(windows_tray) = windows_tray.as_mut() {
575+
windows_tray.update(&tray);
576+
} else {
577+
windows_tray.replace(WindowsTray::create(&tray));
578+
}
579+
}
580+
562581
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
563582
self.inner
564583
.state
@@ -1202,7 +1221,7 @@ fn handle_gpu_device_lost(
12021221
Ok(())
12031222
}
12041223

1205-
const PLATFORM_WINDOW_CLASS_NAME: PCWSTR = w!("Zed::PlatformWindow");
1224+
const PLATFORM_WINDOW_CLASS_NAME: PCWSTR = w!("GPUI::PlatformWindow");
12061225

12071226
fn register_platform_window_class() {
12081227
let wc = WNDCLASSW {

0 commit comments

Comments
 (0)