BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / linux_install.rs

linux_install.rs

linux_install.rs 7.6 KiB Download Raw
1//! Linux desktop integration on start: offer to install the binary into
2//! `~/.local/share/rusty-teams` with a menu entry.
3
4use std::collections::HashMap;
5use std::path::{Path, PathBuf};
6
7use app_base::linux_desktop::{self, Layout};
8use gpui_kit::component::button::{Button, ButtonVariants as _};
9use gpui_kit::component::dialog::{Dialog, DialogFooter};
10use gpui_kit::component::{WindowExt as _, v_flex};
11use gpui_kit::*;
12
13use crate::app_state::AppHandle;
14use crate::i18n::{tr, tr_args};
15use crate::theme;
16
17const DIALOG_WIDTH: f32 = 460.;
18
19pub fn start(cx: &mut App) {
20 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
21 return;
22 };
23 if linux_desktop::is_installed(&executable, &layout) {
24 // Keeps the menu entry pointing at this file, e.g. after it was deleted by hand.
25 if !layout.desktop_file().is_file()
26 && linux_desktop::write_desktop_entry(&layout, &executable).is_ok()
27 {
28 linux_desktop::refresh_desktop_caches(&layout);
29 }
30 return;
31 }
32 let dont_ask = cx
33 .try_global::<AppHandle>()
34 .is_some_and(|handle| linux_desktop::dont_ask_again(&handle.0.read(cx).store));
35 if linux_desktop::should_offer_install(&executable, &layout, dont_ask) {
36 // After the shell's first frame, so the dialog sits on top of the splash.
37 cx.defer(move |cx| offer_install(executable, layout, cx));
38 }
39}
40
41fn offer_install(executable: PathBuf, layout: Layout, cx: &mut App) {
42 let Some(window) = crate::main_window::current(cx) else {
43 return;
44 };
45 let folder = layout.install_dir();
46 let mut args = HashMap::new();
47 args.insert("folder".to_owned(), display_home(&folder));
48 let body = tr_args("install.body", &args);
49 window
50 .update(cx, move |_, window, cx| {
51 window.open_dialog(cx, move |dialog: Dialog, _, _| {
52 let (install_from, install_layout) = (executable.clone(), layout.clone());
53 let footer = DialogFooter::new()
54 .child(
55 Button::new("install-dont-ask")
56 .label(tr("install.dont_ask"))
57 .on_click(|_, window, cx| {
58 if let Some(handle) = cx.try_global::<AppHandle>() {
59 linux_desktop::set_dont_ask_again(&handle.0.read(cx).store);
60 }
61 window.close_dialog(cx);
62 }),
63 )
64 .child(
65 Button::new("install-not-now")
66 .label(tr("install.not_now"))
67 .on_click(|_, window, cx| window.close_dialog(cx)),
68 )
69 .child(
70 Button::new("install-move")
71 .primary()
72 .label(tr("install.move"))
73 .on_click(move |_, window, cx| {
74 window.close_dialog(cx);
75 install(&install_from, &install_layout, cx);
76 }),
77 );
78 dialog
79 .title(tr("install.title"))
80 .w(px(DIALOG_WIDTH))
81 .child(
82 v_flex()
83 .gap(px(8.))
84 .text_color(theme::text())
85 .child(body.clone()),
86 )
87 .footer(footer)
88 });
89 })
90 .ok();
91}
92
93fn install(executable: &Path, layout: &Layout, cx: &mut App) {
94 match linux_desktop::install_binary(executable, layout) {
95 Ok(installed) => relaunch(&installed, cx),
96 Err(error) => {
97 log::warn!("install failed: {error}");
98 notice(failure("install.failed", &error.to_string()), cx);
99 }
100 }
101}
102
103fn failure(key: &str, detail: &str) -> String {
104 let mut args = HashMap::new();
105 args.insert("detail".to_owned(), detail.to_owned());
106 tr_args(key, &args)
107}
108
109fn notice(text: String, cx: &mut App) {
110 if let Some(handle) = cx.try_global::<AppHandle>() {
111 handle
112 .0
113 .clone()
114 .update(cx, |state, cx| state.raise_notice(text, None, cx));
115 }
116}
117
118/// Where this process stands relative to the install folder.
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum Installation {
121 /// This process is the installed copy.
122 Running,
123 /// Another binary is installed; this one runs from elsewhere.
124 OtherCopy,
125 NotInstalled,
126}
127
128pub fn installation() -> Installation {
129 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
130 return Installation::NotInstalled;
131 };
132 if linux_desktop::is_installed(&executable, &layout) {
133 Installation::Running
134 } else if layout.installed_binary().is_file() {
135 Installation::OtherCopy
136 } else {
137 Installation::NotInstalled
138 }
139}
140
141pub fn install_folder() -> String {
142 Layout::current()
143 .map(|layout| display_home(&layout.install_dir()))
144 .unwrap_or_default()
145}
146
147/// Settings > General: installs (or replaces the installed copy with) this binary.
148pub fn install_now(cx: &mut App) {
149 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
150 return;
151 };
152 if let Some(handle) = cx.try_global::<AppHandle>() {
153 linux_desktop::ask_again(&handle.0.read(cx).store);
154 }
155 install(&executable, &layout, cx);
156}
157
158/// Settings > General: removes the menu entry and the installed binary.
159pub fn remove(cx: &mut App) {
160 let Some(layout) = Layout::current() else {
161 return;
162 };
163 match linux_desktop::uninstall(&layout) {
164 Ok(()) => notice(tr("install.removed"), cx),
165 Err(error) => notice(failure("install.remove_failed", &error.to_string()), cx),
166 }
167 cx.refresh_windows();
168}
169
170pub fn asks_on_start(cx: &App) -> bool {
171 cx.try_global::<AppHandle>()
172 .is_some_and(|handle| !linux_desktop::dont_ask_again(&handle.0.read(cx).store))
173}
174
175pub fn set_asks_on_start(ask: bool, cx: &mut App) {
176 if let Some(handle) = cx.try_global::<AppHandle>() {
177 let store = &handle.0.read(cx).store;
178 if ask {
179 linux_desktop::ask_again(store);
180 } else {
181 linux_desktop::set_dont_ask_again(store);
182 }
183 }
184 cx.refresh_windows();
185}
186
187/// The release this build counts as: its tag in CI release builds, the workspace
188/// version (kept equal to the latest release) in a local build.
189pub fn running_release() -> &'static str {
190 app_base::version::release_tag().unwrap_or(concat!("v", env!("CARGO_PKG_VERSION")))
191}
192
193/// The release to update from, when this process is the installed copy.
194pub fn updatable_release() -> Option<&'static str> {
195 crate::updater::self_update_allowed().then(running_release)
196}
197
198/// Starts the installed copy and quits; the new process waits for this one to let go
199/// of the single-instance socket.
200fn relaunch(installed: &Path, cx: &mut App) {
201 let started = std::process::Command::new(installed)
202 .env(crate::updater::RELAUNCH_ENV, std::process::id().to_string())
203 .spawn();
204 match started {
205 Ok(_) => cx.quit(),
206 Err(error) => log::warn!("could not start {}: {error}", installed.display()),
207 }
208}
209
210fn display_home(path: &Path) -> String {
211 match directories::BaseDirs::new().and_then(|base| {
212 path.strip_prefix(base.home_dir())
213 .ok()
214 .map(Path::to_path_buf)
215 }) {
216 Some(relative) => format!("~/{}", relative.display()),
217 None => path.display().to_string(),
218 }
219}