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    server_gen: ServerGenerator,
23) -> Result<(), CreateTrayWrapperError> {
24    let event_loop = setup_event_loop();
25    let mut tw = TrayWrapper::new(icon_data, event_loop.create_proxy(), server_gen)?;
26
27    //Fix to ensure GTK has been started on linux (see tray-icon examples)
28    #[cfg(target_os = "linux")]
29    {
30        gtk::init().unwrap();
31    }
32
33    event_loop.run_app(&mut tw)?;
34
35    Ok(())
36}
37
38#[derive(Error, Debug)]
39pub enum CreateTrayWrapperError {
40    #[error(transparent)]
41    TrayWrapper(#[from] TrayWrapperError),
42    #[error(transparent)]
43    Winit(#[from] winit::error::EventLoopError),
44    #[cfg(target_os = "linux")]
45    #[error("Gtk Failed to Init {0}")]
46    Gtk(#[from] glib::error::BoolError),
47}