Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 127 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/gpui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ wayland-protocols = { version = "0.31.2", features = [
] }
wayland-protocols-plasma = { version = "0.2.0", features = ["client"] }
oo7 = "0.3.0"
zbus = { version = "4.3.0", default-features = false, features = ["async-io"] }
filedescriptor = "0.8.2"
x11rb = { version = "0.13.0", features = [
"allow-unsafe-code",
Expand Down
69 changes: 69 additions & 0 deletions crates/gpui/examples/set_tray_icons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use gpui::*;

struct SetTrayIcons;

impl Render for SetTrayIcons {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.flex()
.bg(rgb(0x2e7d32))
.size_full()
.justify_center()
.items_center()
.text_xl()
.text_color(rgb(0xffffff))
.child("Set Tray Menu Example")
}
}

fn main() {
App::new().run(|cx: &mut AppContext| {
// Register the `quit` function so it can be referenced by the `AppContext::dispatch_action`
cx.on_action(quit);
let item = TrayItem::new()
.icon(TrayIcon::Name("folder"))
.title("Testing")
.tooltip("My Tooltip")
.description("Little description")
.submenu(TrayMenuItem::menu("Quit", "Quit", Vec::default()))
.submenu(TrayMenuItem::menu("Window", "Test", Vec::default()))
.on_event({
let mut show_app = false;
move |event, app| match event {
TrayEvent::TrayClick { button, .. } => match button {
MouseButton::Left => {
app.dispatch_action(&Quit);
}
_ => {}
},
TrayEvent::MenuClick { id } => match id.as_str() {
"Quit" => app.dispatch_action(&Quit),
"Window" => {
if show_app {
app.activate(true);
} else {
app.hide();
}
show_app ^= true;
}
_ => {}
},
_ => {}
}
});
cx.set_tray_item(item);
cx.open_window(WindowOptions::default(), |cx| {
cx.new_view(|_cx| SetTrayIcons {})
})
.unwrap();
});
}

// Associate actions using the `actions!` macro (or `impl_actions!` macro)
actions!(set_tray_menus, [Quit]);

// Define the quit function that is registered with the AppContext
fn quit(_: &Quit, cx: &mut AppContext) {
println!("Gracefully quitting the application . . .");
cx.quit();
}
17 changes: 15 additions & 2 deletions crates/gpui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::{
Entity, EventEmitter, ForegroundExecutor, Global, KeyBinding, Keymap, Keystroke, LayoutId,
Menu, MenuItem, OwnedMenu, PathPromptOptions, Pixels, Platform, PlatformDisplay, Point,
PromptBuilder, PromptHandle, PromptLevel, Render, RenderablePromptHandle, Reservation,
SharedString, SubscriberSet, Subscription, SvgRenderer, Task, TextSystem, View, ViewContext,
Window, WindowAppearance, WindowContext, WindowHandle, WindowId,
SharedString, SubscriberSet, Subscription, SvgRenderer, Task, TextSystem, TrayItem, View,
ViewContext, Window, WindowAppearance, WindowContext, WindowHandle, WindowId,
};

mod async_context;
Expand Down Expand Up @@ -1151,6 +1151,19 @@ impl AppContext {
.contains_key(&action.as_any().type_id())
}

/// Sets a single tray item in the System tray. This will replace any existing tray item.
pub fn set_tray_item(&mut self, mut item: TrayItem) {
if let Some(mut fun) = item.event.take() {
self.platform.on_tray_event(Box::new({
let cx = self.to_async();
move |event| {
cx.update(|cx| fun(event, cx)).log_err();
}
}));
}
self.platform.set_tray_item(item);
}

/// Sets the menu bar for this application. This will replace any existing menu bar.
pub fn set_menus(&mut self, menus: Vec<Menu>) {
self.platform.set_menus(menus, &self.keymap.borrow());
Expand Down
5 changes: 5 additions & 0 deletions crates/gpui/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod app_menu;
mod keystroke;
mod system_tray;

#[cfg(not(target_os = "macos"))]
mod cosmic_text;
Expand Down Expand Up @@ -50,6 +51,7 @@ use uuid::Uuid;

pub use app_menu::*;
pub use keystroke::*;
pub use system_tray::*;

#[cfg(not(target_os = "macos"))]
pub(crate) use cosmic_text::*;
Expand Down Expand Up @@ -144,6 +146,8 @@ pub(crate) trait Platform: 'static {
fn on_quit(&self, callback: Box<dyn FnMut()>);
fn on_reopen(&self, callback: Box<dyn FnMut()>);

fn set_tray_item(&self, item: TrayItem);

fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
fn get_menus(&self) -> Option<Vec<OwnedMenu>> {
None
Expand All @@ -154,6 +158,7 @@ pub(crate) trait Platform: 'static {
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
fn on_tray_event(&self, callback: Box<dyn FnMut(TrayEvent)>);

fn compositor_name(&self) -> &'static str {
""
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/platform/linux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod dbus;
mod dispatcher;
mod headless;
mod platform;
Expand Down
Loading