Skip to main content

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_loop;
12mod server_status;
13mod tray_wrapper;
14use event_loop::setup_event_loop;
15use tray_wrapper::{TrayWrapper, TrayWrapperError};
16
17//Public interface
18pub use server_generator::{ContinueRunning, ServerGenerator, ServerGeneratorResult};
19use thiserror::Error;
20
21pub fn create_tray_wrapper(
22    icon_data: &[u8],
23    version: Option<String>,
24    server_gen: ServerGenerator,
25) -> Result<(), CreateTrayWrapperError> {
26    let event_loop = setup_event_loop();
27    let mut tw = TrayWrapper::new(icon_data, version, event_loop.create_proxy(), server_gen)?;
28
29    //Fix to ensure GTK has been started on linux (see tray-icon examples)
30    #[cfg(target_os = "linux")]
31    {
32        gtk::init().unwrap();
33    }
34
35    event_loop.run_app(&mut tw)?;
36
37    Ok(())
38}
39
40#[derive(Error, Debug)]
41pub enum CreateTrayWrapperError {
42    #[error(transparent)]
43    TrayWrapper(#[from] TrayWrapperError),
44    #[error(transparent)]
45    Winit(#[from] winit::error::EventLoopError),
46    #[cfg(target_os = "linux")]
47    #[error("Gtk Failed to Init {0}")]
48    Gtk(#[from] glib::error::BoolError),
49}