tray_wrapper/
lib.rs

1//! The tray wrapper library is intended to make it simple to provide a GUI tray icon for a given running server
2//! process. This uses the excellent tray-icon library for the UI and re-exports some of its types.
3//!
4//! This is to make it convient to start and monitor a process on Mac (other operating systems are supported
5//! on a best effort basis). MacOS makes it challenging to manage a server process WITHOUT a tray icon.
6//!
7//! The tray icon provides a submenu to view the supplied server status and the ability to exit.
8mod event_loop;
9mod menu_state;
10mod server_generator;
11mod server_status;
12mod tray_wrapper;
13use event_loop::setup_event_loop;
14use tray_wrapper::{TrayWrapper, TrayWrapperError};
15
16//Public interface
17pub use server_generator::{ContinueRunning, ServerGenerator, ServerGeneratorResult};
18use thiserror::Error;
19
20pub fn create_tray_wrapper(
21    icon_data: &[u8],
22    version: Option<String>,
23    server_gen: ServerGenerator,
24) -> Result<(), CreateTrayWrapperError> {
25    let event_loop = setup_event_loop();
26    let mut tw = TrayWrapper::new(icon_data, version, event_loop.create_proxy(), server_gen)?;
27
28    //Fix to ensure GTK has been started on linux (see tray-icon examples)
29    #[cfg(target_os = "linux")]
30    {
31        gtk::init().unwrap();
32    }
33
34    event_loop.run_app(&mut tw)?;
35
36    Ok(())
37}
38
39#[derive(Error, Debug)]
40pub enum CreateTrayWrapperError {
41    #[error(transparent)]
42    TrayWrapper(#[from] TrayWrapperError),
43    #[error(transparent)]
44    Winit(#[from] winit::error::EventLoopError),
45    #[cfg(target_os = "linux")]
46    #[error("Gtk Failed to Init {0}")]
47    Gtk(#[from] glib::error::BoolError),
48}