#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")] use client::{ NAME, Server, gui::{ self, Yumush, gallery::{Gallery, Page, State}, }, net, settings, tray::{self, TrayAction, TrayState}, }; use gpui_kit::{ App, AppContext, Bounds, Entity, QuitMode, TitlebarOptions, Window, WindowBounds, WindowOptions, application, component::Root, px, size, }; use tracing::Level; use tracing_subscriber::{filter::Targets, layer::SubscriberExt, util::SubscriberInitExt}; fn main() { if std::env::args().any(|arg| arg == "--version" || arg == "-V") { println!( "yumush {} {} {}", client::VERSION, client::COMMIT, client::TARGET ); return; } #[cfg(target_os = "linux")] if client::cli::wanted() { client::cli::run(); return; } tracing_subscriber::registry() .with(tracing_subscriber::fmt::layer()) .with( Targets::new() .with_default(Level::INFO) .with_target("zbus", Level::ERROR) .with_target( "metrics", if std::env::var_os("YUMUSH_METRICS").is_some() { Level::DEBUG } else { Level::INFO }, ), ) .init(); client::emoji::warm(); if let Ok(spec) = std::env::var("YUMUSH_GALLERY") { gallery(spec); return; } let client::instance::Instance::First(mut wake) = client::instance::claim() else { return; }; #[cfg(target_os = "linux")] if let Err(error) = client::logo::install() { tracing::warn!("desktop entry: {error}"); } let settings = settings::load(); let hidden = client::autostart::hidden(); client::autostart::sync(settings.launch); client::lang::set(settings.language); let (network_handle, network_event_receiver) = net::start(Server::load()); application() .with_assets(client::logo::Assets) .with_quit_mode(QuitMode::Explicit) .run(move |cx: &mut App| { gui::init(cx); settings::apply(settings.theme, settings.font_size, cx); gui::bind_keys(cx); let Some(view) = open_window(cx, |window, cx| { cx.new(|cx| { Yumush::new(network_handle, network_event_receiver, settings, window, cx) }) }) else { cx.quit(); return; }; if hidden { hide(&view, cx); } let (mut tray_receiver, tray_handle) = tray::start(); let mut tray_state = TrayState::default(); cx.observe(&view, move |view, cx| { let state = view.read(cx).tray_state(); if state != tray_state { tray_state = state; tray_handle.set(state); } }) .detach(); let woken = view.clone(); cx.spawn(async move |cx| { while wake.recv().await.is_some() { let view = woken.clone(); cx.update(|cx| show(view, cx)); } }) .detach(); cx.spawn(async move |cx| { while let Some(action) = tray_receiver.recv().await { let view = view.clone(); cx.update(|cx| match action { TrayAction::Show => show(view, cx), TrayAction::Quit => cx.quit(), }); } }) .detach(); if !hidden { cx.activate(true); } }); } fn hide(view: &Entity, cx: &mut App) { let Some(handle) = view.read(cx).window() else { return; }; view.update(cx, |yumush, _| yumush.detach_window()); let _ = handle.update(cx, |_, window, _| window.remove_window()); } fn gallery(spec: String) { let (page, state) = spec.split_once(':').unwrap_or((&spec, "base")); let Some(page) = Page::parse(page) else { eprintln!("unknown gallery page {page}"); return; }; let Some(state) = State::parse(state) else { eprintln!("unknown gallery state {state}"); return; }; application() .with_assets(client::logo::Assets) .run(move |cx: &mut App| { gui::init(cx); settings::apply(state.theme(), state.font_size(), cx); let (width, height) = state.window(); let bounds = Bounds::centered(None, size(px(width), px(height)), cx); let opened = cx.open_window( WindowOptions { window_bounds: Some(WindowBounds::Windowed(bounds)), titlebar: Some(TitlebarOptions { title: Some(format!("{NAME} gallery").into()), ..Default::default() }), ..Default::default() }, |window, cx| { let view = cx.new(|_| Gallery::new(page, state)); cx.new(|cx| Root::new(view, window, cx)) }, ); if opened.is_err() { cx.quit(); } cx.activate(true); }); } fn show(view: Entity, cx: &mut App) { match view.read(cx).window() { Some(handle) => { let _ = handle.update(cx, |_, window, _| window.activate_window()); } None => { open_window(cx, |_, _| view); } } } fn open_window( cx: &mut App, build: impl FnOnce(&mut Window, &mut App) -> Entity, ) -> Option> { let window_bounds = Bounds::centered(None, size(px(1100.0), px(720.0)), cx); let titlebar = TitlebarOptions { title: Some(NAME.into()), ..Default::default() }; let mut opened = None; cx.open_window( WindowOptions { window_bounds: Some(WindowBounds::Windowed(window_bounds)), titlebar: Some(titlebar), app_id: Some(client::logo::NAME.into()), icon: Some(client::logo::window_icon()), inactive_frame_interval: None, ..Default::default() }, |window, cx| { let view = build(window, cx); view.update(cx, |yumush, cx| yumush.attach_window(window, cx)); let closing = view.clone(); window.on_window_should_close(cx, move |_, cx| { let detached = tray::AVAILABLE && closing.update(cx, |yumush, _| yumush.detach_window()); if !detached { cx.quit(); } true }); opened = Some(view.clone()); cx.new(|cx| Root::new(view, window, cx)) }, ) .ok()?; opened }