yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | #![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")] |
| 2 | |
| 3 | use client::{ |
| 4 | NAME, Server, |
| 5 | gui::{ |
| 6 | self, Yumush, |
| 7 | gallery::{Gallery, State}, |
| 8 | }, |
| 9 | net, settings, |
| 10 | tray::{self, TrayAction, TrayState}, |
| 11 | }; |
| 12 | use gpui_kit::{ |
| 13 | App, AppContext, Bounds, Entity, QuitMode, TitlebarOptions, Window, WindowBounds, |
| 14 | WindowOptions, application, component::Root, px, size, |
| 15 | }; |
| 16 | |
| 17 | use tracing::Level; |
| 18 | use tracing_subscriber::{filter::Targets, layer::SubscriberExt, util::SubscriberInitExt}; |
| 19 | |
| 20 | fn main() { |
| 21 | if std::env::args().any(|arg| arg == "--version" || arg == "-V") { |
| 22 | println!( |
| 23 | "yumush {} {} {}", |
| 24 | client::VERSION, |
| 25 | client::COMMIT, |
| 26 | client::TARGET |
| 27 | ); |
| 28 | return; |
| 29 | } |
| 30 | tracing_subscriber::registry() |
| 31 | .with(tracing_subscriber::fmt::layer()) |
| 32 | .with( |
| 33 | Targets::new() |
| 34 | .with_default(Level::INFO) |
| 35 | .with_target("zbus", Level::ERROR) |
| 36 | .with_target( |
| 37 | "metrics", |
| 38 | if std::env::var_os("YUMUSH_METRICS").is_some() { |
| 39 | Level::DEBUG |
| 40 | } else { |
| 41 | Level::INFO |
| 42 | }, |
| 43 | ), |
| 44 | ) |
| 45 | .init(); |
| 46 | client::emoji::warm(); |
| 47 | |
| 48 | if let Ok(spec) = std::env::var("YUMUSH_GALLERY") { |
| 49 | gallery(spec); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | let client::instance::Instance::First(mut wake) = client::instance::claim() else { |
| 54 | return; |
| 55 | }; |
| 56 | |
| 57 | #[cfg(target_os = "linux")] |
| 58 | if let Err(error) = client::logo::install() { |
| 59 | tracing::warn!("desktop entry: {error}"); |
| 60 | } |
| 61 | let settings = settings::load(); |
| 62 | let hidden = client::autostart::hidden(); |
| 63 | client::autostart::sync(settings.launch); |
| 64 | client::lang::set(settings.language); |
| 65 | let (network_handle, network_event_receiver) = net::start(Server::load()); |
| 66 | |
| 67 | application() |
| 68 | .with_assets(client::logo::Assets) |
| 69 | .with_quit_mode(QuitMode::Explicit) |
| 70 | .run(move |cx: &mut App| { |
| 71 | gui::init(cx); |
| 72 | settings::apply(settings.theme, settings.font_size, cx); |
| 73 | gui::bind_keys(cx); |
| 74 | |
| 75 | let Some(view) = open_window(cx, |window, cx| { |
| 76 | cx.new(|cx| { |
| 77 | Yumush::new(network_handle, network_event_receiver, settings, window, cx) |
| 78 | }) |
| 79 | }) else { |
| 80 | cx.quit(); |
| 81 | return; |
| 82 | }; |
| 83 | |
| 84 | if hidden { |
| 85 | hide(&view, cx); |
| 86 | } |
| 87 | |
| 88 | let (mut tray_receiver, tray_handle) = tray::start(); |
| 89 | let mut tray_state = TrayState::default(); |
| 90 | cx.observe(&view, move |view, cx| { |
| 91 | let state = view.read(cx).tray_state(); |
| 92 | if state != tray_state { |
| 93 | tray_state = state; |
| 94 | tray_handle.set(state); |
| 95 | } |
| 96 | }) |
| 97 | .detach(); |
| 98 | |
| 99 | let woken = view.clone(); |
| 100 | cx.spawn(async move |cx| { |
| 101 | while wake.recv().await.is_some() { |
| 102 | let view = woken.clone(); |
| 103 | cx.update(|cx| show(view, cx)); |
| 104 | } |
| 105 | }) |
| 106 | .detach(); |
| 107 | |
| 108 | cx.spawn(async move |cx| { |
| 109 | while let Some(action) = tray_receiver.recv().await { |
| 110 | let view = view.clone(); |
| 111 | cx.update(|cx| match action { |
| 112 | TrayAction::Show => show(view, cx), |
| 113 | TrayAction::Quit => cx.quit(), |
| 114 | }); |
| 115 | } |
| 116 | }) |
| 117 | .detach(); |
| 118 | |
| 119 | if !hidden { |
| 120 | cx.activate(true); |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | fn hide(view: &Entity<Yumush>, cx: &mut App) { |
| 126 | let Some(handle) = view.read(cx).window() else { |
| 127 | return; |
| 128 | }; |
| 129 | view.update(cx, |yumush, _| yumush.detach_window()); |
| 130 | let _ = handle.update(cx, |_, window, _| window.remove_window()); |
| 131 | } |
| 132 | |
| 133 | fn gallery(spec: String) { |
| 134 | let (page, state) = spec.split_once(':').unwrap_or((&spec, "base")); |
| 135 | let page = page.to_string(); |
| 136 | let Some(state) = State::parse(state) else { |
| 137 | eprintln!("unknown gallery state {state}"); |
| 138 | return; |
| 139 | }; |
| 140 | application() |
| 141 | .with_assets(client::logo::Assets) |
| 142 | .run(move |cx: &mut App| { |
| 143 | gui::init(cx); |
| 144 | settings::apply(state.theme(), state.font_size(), cx); |
| 145 | let (width, height) = state.window(); |
| 146 | let bounds = Bounds::centered(None, size(px(width), px(height)), cx); |
| 147 | let opened = cx.open_window( |
| 148 | WindowOptions { |
| 149 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 150 | titlebar: Some(TitlebarOptions { |
| 151 | title: Some(format!("{NAME} gallery").into()), |
| 152 | ..Default::default() |
| 153 | }), |
| 154 | ..Default::default() |
| 155 | }, |
| 156 | |window, cx| { |
| 157 | let view = cx.new(|_| Gallery::new(page, state)); |
| 158 | cx.new(|cx| Root::new(view, window, cx)) |
| 159 | }, |
| 160 | ); |
| 161 | if opened.is_err() { |
| 162 | cx.quit(); |
| 163 | } |
| 164 | cx.activate(true); |
| 165 | }); |
| 166 | } |
| 167 | |
| 168 | fn show(view: Entity<Yumush>, cx: &mut App) { |
| 169 | match view.read(cx).window() { |
| 170 | Some(handle) => { |
| 171 | let _ = handle.update(cx, |_, window, _| window.activate_window()); |
| 172 | } |
| 173 | None => { |
| 174 | open_window(cx, |_, _| view); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | fn open_window( |
| 180 | cx: &mut App, |
| 181 | build: impl FnOnce(&mut Window, &mut App) -> Entity<Yumush>, |
| 182 | ) -> Option<Entity<Yumush>> { |
| 183 | let window_bounds = Bounds::centered(None, size(px(1100.0), px(720.0)), cx); |
| 184 | let titlebar = TitlebarOptions { |
| 185 | title: Some(NAME.into()), |
| 186 | ..Default::default() |
| 187 | }; |
| 188 | let mut opened = None; |
| 189 | |
| 190 | cx.open_window( |
| 191 | WindowOptions { |
| 192 | window_bounds: Some(WindowBounds::Windowed(window_bounds)), |
| 193 | titlebar: Some(titlebar), |
| 194 | app_id: Some(client::logo::NAME.into()), |
| 195 | icon: Some(client::logo::window_icon()), |
| 196 | inactive_frame_interval: None, |
| 197 | ..Default::default() |
| 198 | }, |
| 199 | |window, cx| { |
| 200 | let view = build(window, cx); |
| 201 | view.update(cx, |yumush, cx| yumush.attach_window(window, cx)); |
| 202 | let closing = view.clone(); |
| 203 | window.on_window_should_close(cx, move |_, cx| { |
| 204 | let detached = |
| 205 | tray::AVAILABLE && closing.update(cx, |yumush, _| yumush.detach_window()); |
| 206 | if !detached { |
| 207 | cx.quit(); |
| 208 | } |
| 209 | true |
| 210 | }); |
| 211 | opened = Some(view.clone()); |
| 212 | cx.new(|cx| Root::new(view, window, cx)) |
| 213 | }, |
| 214 | ) |
| 215 | .ok()?; |
| 216 | |
| 217 | opened |
| 218 | } |