BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / help.rs

help.rs

help.rs 21.6 KiB Download Raw
1use std::path::{Path, PathBuf};
2
3use app_base::downloads::reveal_in_file_manager;
4use app_base::support::{
5 self, BundleFileEntry, SupportBundleEntry, ensure_dir, format_bytes, list_bundle_files,
6 list_support_bundles,
7};
8use app_ui_kit::render::{layout_blocks, render_blocks, strip_image_placeholders};
9use gpui_kit::assets::IconName;
10use gpui_kit::component::scroll::ScrollableElement as _;
11use gpui_kit::component::tooltip::Tooltip;
12use gpui_kit::component::{ActiveTheme as _, h_flex, v_flex};
13use gpui_kit::prelude::FluentBuilder as _;
14use gpui_kit::*;
15
16use super::rows::{ROW_INSET, caption, control_row, text_button, toggle_row};
17use crate::app_state::AppState;
18use crate::i18n::tr;
19use crate::theme::{self, ElevatedSurface as _};
20use crate::views::widgets::icon;
21
22const COPY_ICON_SIZE: f32 = 14.;
23const PREVIEW_LIMIT: usize = 256 * 1024;
24const PREVIEW_RADIUS: f32 = 10.;
25const PREVIEW_PADDING: f32 = 12.;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28enum PreviewKind {
29 Markdown,
30 Text,
31}
32
33struct FilePreview {
34 name: String,
35 text: String,
36 kind: PreviewKind,
37 truncated: bool,
38}
39
40pub struct HelpView {
41 app: Entity<AppState>,
42 bundles: Vec<SupportBundleEntry>,
43 expanded: Option<PathBuf>,
44 expanded_files: Vec<BundleFileEntry>,
45 preview: Option<FilePreview>,
46}
47
48impl HelpView {
49 pub fn new(app: Entity<AppState>, cx: &mut Context<Self>) -> Self {
50 let mut view = HelpView {
51 app,
52 bundles: Vec::new(),
53 expanded: None,
54 expanded_files: Vec::new(),
55 preview: None,
56 };
57 view.reload_bundles();
58 let _ = cx;
59 view
60 }
61
62 pub(crate) fn is_previewing(&self) -> bool {
63 self.preview.is_some()
64 }
65
66 pub(crate) fn reload_bundles(&mut self) {
67 let Some(root) = support::support_bundles_dir() else {
68 self.bundles.clear();
69 self.expanded = None;
70 self.expanded_files.clear();
71 return;
72 };
73 self.bundles = list_support_bundles(&root).unwrap_or_default();
74 if let Some(expanded) = self.expanded.clone() {
75 if self.bundles.iter().any(|entry| entry.directory == expanded) {
76 self.expanded_files = list_bundle_files(&expanded).unwrap_or_default();
77 } else {
78 self.expanded = None;
79 self.expanded_files.clear();
80 }
81 }
82 }
83
84 fn toggle_expand(&mut self, directory: PathBuf, cx: &mut Context<Self>) {
85 if self.expanded.as_ref() == Some(&directory) {
86 self.expanded = None;
87 self.expanded_files.clear();
88 } else {
89 self.expanded_files = list_bundle_files(&directory).unwrap_or_default();
90 self.expanded = Some(directory);
91 }
92 cx.notify();
93 }
94
95 fn delete_bundle(&mut self, directory: PathBuf, cx: &mut Context<Self>) {
96 let parent = support::support_bundles_dir();
97 if let Err(error) = support::delete_support_bundle(&directory, parent.as_deref()) {
98 app_base::diagnostics::record(format!("support delete: {error}"));
99 return;
100 }
101 if self.expanded.as_ref() == Some(&directory) {
102 self.expanded = None;
103 self.expanded_files.clear();
104 }
105 self.preview = None;
106 self.reload_bundles();
107 cx.notify();
108 }
109
110 fn reveal_path(path: &Path) {
111 let _ = ensure_dir(path);
112 reveal_in_file_manager(path);
113 }
114
115 fn open_preview(&mut self, path: PathBuf, cx: &mut Context<Self>) {
116 let name = path
117 .file_name()
118 .map(|value| value.to_string_lossy().into_owned())
119 .unwrap_or_else(|| path.display().to_string());
120 let kind = if name.ends_with(".md") {
121 PreviewKind::Markdown
122 } else {
123 PreviewKind::Text
124 };
125 match read_preview_text(&path) {
126 Ok(ReadPreview {
127 text,
128 truncated,
129 binary,
130 }) => {
131 self.preview = Some(FilePreview {
132 name,
133 text: if binary {
134 tr("settings.help.preview_binary")
135 } else {
136 text
137 },
138 kind: if binary { PreviewKind::Text } else { kind },
139 truncated: truncated && !binary,
140 });
141 }
142 Err(error) => {
143 app_base::diagnostics::record(format!("support preview: {error}"));
144 self.preview = Some(FilePreview {
145 name,
146 text: tr("settings.help.preview_error"),
147 kind: PreviewKind::Text,
148 truncated: false,
149 });
150 }
151 }
152 cx.notify();
153 }
154
155 fn close_preview(&mut self, cx: &mut Context<Self>) {
156 self.preview = None;
157 cx.notify();
158 }
159
160 fn copy_path_button(id: &'static str, path: String) -> Stateful<Div> {
161 div()
162 .id(id)
163 .cursor_pointer()
164 .p(px(4.))
165 .rounded(px(6.))
166 .hover(|button| button.bg(theme::row_hover()))
167 .tooltip(|window, cx| {
168 Tooltip::new(tr("settings.help.copy_path_tooltip")).build(window, cx)
169 })
170 .on_click(move |_, _, cx| {
171 cx.stop_propagation();
172 cx.write_to_clipboard(ClipboardItem::new_string(path.clone()));
173 })
174 .child(icon(IconName::Copy, COPY_ICON_SIZE, theme::text_muted()))
175 }
176
177 fn path_row(
178 &self,
179 title: impl Into<SharedString>,
180 path: String,
181 copy_id: &'static str,
182 open_id: &'static str,
183 cx: &App,
184 ) -> Div {
185 let title = title.into();
186 let open_path = PathBuf::from(path.clone());
187 v_flex()
188 .w_full()
189 .px(px(ROW_INSET))
190 .py(px(12.))
191 .gap(px(8.))
192 .rounded(px(8.))
193 .child(
194 h_flex()
195 .w_full()
196 .gap(px(8.))
197 .items_center()
198 .child(
199 div()
200 .flex_1()
201 .min_w(px(0.))
202 .text_size(px(14.))
203 .line_height(px(20.))
204 .font_weight(FontWeight::SEMIBOLD)
205 .text_color(theme::text())
206 .child(title),
207 )
208 .child(Self::copy_path_button(copy_id, path.clone()))
209 .child(
210 text_button(open_id, tr("common.open")).on_click(move |_, _, _| {
211 Self::reveal_path(&open_path);
212 }),
213 ),
214 )
215 .child(
216 div()
217 .w_full()
218 .min_w(px(0.))
219 .font_family(cx.theme().mono_font_family.clone())
220 .text_size(px(12.))
221 .line_height(px(18.))
222 .text_color(theme::text_muted())
223 .whitespace_normal()
224 .child(path),
225 )
226 }
227
228 fn bundle_row(&self, entry: &SupportBundleEntry, cx: &mut Context<Self>) -> Div {
229 let directory = entry.directory.clone();
230 let toggle_dir = directory.clone();
231 let delete_dir = directory.clone();
232 let expanded = self.expanded.as_ref() == Some(&directory);
233 v_flex()
234 .w_full()
235 .child(
236 h_flex()
237 .w_full()
238 .px(px(ROW_INSET))
239 .py(px(10.))
240 .gap(px(12.))
241 .items_center()
242 .rounded(px(8.))
243 .when(expanded, |row| row.bg(theme::popup_hover()))
244 .child(
245 h_flex()
246 .id(SharedString::from(format!(
247 "settings-help-bundle-{}",
248 entry.name
249 )))
250 .flex_1()
251 .min_w(px(0.))
252 .gap(px(8.))
253 .items_center()
254 .cursor_pointer()
255 .hover(|row| row.bg(theme::popup_hover()))
256 .on_click(cx.listener(move |this, _, _, cx| {
257 this.toggle_expand(toggle_dir.clone(), cx);
258 }))
259 .child(
260 div()
261 .flex_1()
262 .min_w(px(0.))
263 .text_size(px(14.))
264 .font_weight(FontWeight::SEMIBOLD)
265 .text_color(theme::text())
266 .child(entry.name.clone()),
267 ),
268 )
269 .child(
270 text_button(
271 SharedString::from(format!("settings-help-delete-{}", entry.name)),
272 tr("common.delete"),
273 )
274 .on_click(cx.listener(move |this, _, _, cx| {
275 this.delete_bundle(delete_dir.clone(), cx);
276 })),
277 ),
278 )
279 .when(expanded, |column| column.child(self.expanded_panel(cx)))
280 }
281
282 fn expanded_panel(&self, cx: &mut Context<Self>) -> Div {
283 v_flex()
284 .w_full()
285 .px(px(ROW_INSET + 8.))
286 .pb(px(8.))
287 .gap(px(4.))
288 .children(self.expanded_files.iter().map(|file| {
289 let path = file.path.clone();
290 let label = format!("{} · {}", file.name, format_bytes(file.size_bytes));
291 div()
292 .id(SharedString::from(format!(
293 "settings-help-file-{}",
294 file.name
295 )))
296 .w_full()
297 .px(px(8.))
298 .py(px(6.))
299 .rounded(px(6.))
300 .cursor_pointer()
301 .hover(|row| row.bg(theme::popup_hover()))
302 .on_click(cx.listener(move |this, _, _, cx| {
303 this.open_preview(path.clone(), cx);
304 }))
305 .child(
306 div()
307 .text_size(px(12.))
308 .text_color(theme::text_muted())
309 .child(label),
310 )
311 }))
312 }
313
314 fn packages_section(&self, cx: &mut Context<Self>) -> Div {
315 v_flex()
316 .w_full()
317 .child(caption(tr("settings.help.caption_packages")))
318 .when(self.bundles.is_empty(), |section| {
319 section.child(
320 div()
321 .px(px(ROW_INSET))
322 .py(px(8.))
323 .text_size(px(13.))
324 .text_color(theme::text_muted())
325 .child(tr("settings.help.packages_empty")),
326 )
327 })
328 .children(
329 self.bundles
330 .iter()
331 .map(|entry| self.bundle_row(entry, cx))
332 .collect::<Vec<_>>(),
333 )
334 }
335
336 fn preview_body(&self, preview: &FilePreview, cx: &App) -> AnyElement {
337 match preview.kind {
338 PreviewKind::Markdown => {
339 // Full document markdown (headings, tables, lists) — not the light chat dialect.
340 let html = teams_core::Draft::from_markdown(&preview.text).to_html();
341 let blocks =
342 strip_image_placeholders(layout_blocks(&teams_core::html_to_spans(&html)));
343 div()
344 .w_full()
345 .min_w(px(0.))
346 .text_size(px(13.))
347 .text_color(theme::text())
348 .whitespace_normal()
349 .child(render_blocks(&blocks, "settings-help-preview", false, cx))
350 .into_any_element()
351 }
352 PreviewKind::Text => div()
353 .w_full()
354 .min_w(px(0.))
355 .font_family(cx.theme().mono_font_family.clone())
356 .text_size(px(12.))
357 .line_height(px(18.))
358 .text_color(theme::text_muted())
359 .whitespace_normal()
360 .child(preview.text.clone())
361 .into_any_element(),
362 }
363 }
364
365 fn preview_panel(&self, cx: &mut Context<Self>) -> Div {
366 let preview = self.preview.as_ref().expect("preview open");
367 v_flex()
368 .w_full()
369 .h_full()
370 .min_h_0()
371 .gap(px(8.))
372 .px(px(ROW_INSET))
373 .pb(px(ROW_INSET))
374 .child(
375 h_flex()
376 .w_full()
377 .flex_none()
378 .gap(px(8.))
379 .items_center()
380 .child(
381 text_button("settings-help-preview-back", tr("common.close"))
382 .on_click(cx.listener(|this, _, _, cx| this.close_preview(cx))),
383 )
384 .child(
385 div()
386 .flex_1()
387 .min_w(px(0.))
388 .text_size(px(14.))
389 .font_weight(FontWeight::SEMIBOLD)
390 .text_color(theme::text())
391 .child(preview.name.clone()),
392 ),
393 )
394 .when(preview.truncated, |column| {
395 column.child(
396 div()
397 .flex_none()
398 .text_size(px(12.))
399 .text_color(theme::text_muted())
400 .child(tr("settings.help.preview_truncated")),
401 )
402 })
403 .child(
404 div()
405 .id("settings-help-preview-scroll")
406 .w_full()
407 .flex_1()
408 .min_h_0()
409 .p(px(PREVIEW_PADDING))
410 .rounded(px(PREVIEW_RADIUS))
411 .dialog_surface()
412 .overflow_y_scrollbar()
413 .child(self.preview_body(preview, cx)),
414 )
415 }
416
417 fn help_home(&self, cx: &mut Context<Self>) -> Div {
418 let info = self.app.read(cx).client_info();
419 let summary = info.markdown();
420 let copy_summary = summary.clone();
421 let logs_path = info.logs_root.clone();
422 let bundles_path = info.support_bundles_root.clone();
423 let create_app = self.app.clone();
424 let issue_app = self.app.clone();
425 v_flex()
426 .w_full()
427 .gap(px(4.))
428 .child(caption(tr("settings.help.caption_client")))
429 .child(control_row(
430 tr("settings.help.version"),
431 format!("{} ({})", info.version, info.build),
432 div(),
433 ))
434 .child(control_row(
435 tr("settings.help.platform"),
436 format!("{} / {} / {}", info.os, info.arch, info.session),
437 div(),
438 ))
439 .child(control_row(
440 tr("settings.help.locale"),
441 format!("{} · {}", info.ui_locale, info.timezone),
442 div(),
443 ))
444 .child(control_row(
445 tr("settings.help.signed_in"),
446 if info.signed_in {
447 tr("settings.help.yes")
448 } else {
449 tr("settings.help.no")
450 },
451 div(),
452 ))
453 .child({
454 let debug_on = self.app.read(cx).diagnostics_enabled();
455 let toggle_app = self.app.clone();
456 toggle_row(
457 "settings-help-debug-log",
458 tr("settings.help.debug_logging"),
459 if debug_on {
460 tr("settings.help.debug_logging_on_detail")
461 } else {
462 tr("settings.help.debug_logging_off_detail")
463 },
464 theme::text_muted(),
465 debug_on,
466 )
467 .on_click(cx.listener(move |_, _, _, cx| {
468 toggle_app.update(cx, |state, cx| {
469 state.set_diagnostics_enabled(!debug_on, cx);
470 });
471 cx.notify();
472 }))
473 })
474 .child({
475 let clear_app = self.app.clone();
476 control_row(
477 tr("settings.help.clear_logs"),
478 tr("settings.help.clear_logs_detail"),
479 text_button(
480 "settings-help-clear-logs",
481 tr("settings.help.clear_logs_button"),
482 )
483 .on_click(cx.listener(move |_, _, _, cx| {
484 clear_app.update(cx, |state, cx| state.clear_diagnostic_logs(cx));
485 cx.notify();
486 })),
487 )
488 })
489 .child(caption(tr("settings.help.caption_paths")))
490 .child(self.path_row(
491 tr("settings.help.logs_path"),
492 logs_path,
493 "settings-help-copy-logs",
494 "settings-help-open-logs-root",
495 cx,
496 ))
497 .child(self.path_row(
498 tr("settings.help.bundles_path"),
499 bundles_path,
500 "settings-help-copy-bundles",
501 "settings-help-open-bundles-root",
502 cx,
503 ))
504 .child(self.packages_section(cx))
505 .child(caption(tr("settings.help.caption_actions")))
506 .child(
507 div()
508 .text_size(px(13.))
509 .text_color(theme::text_muted())
510 .pb(px(8.))
511 .px(px(ROW_INSET))
512 .child(tr("settings.help.detail")),
513 )
514 .child(control_row(
515 tr("settings.help.create_bundle"),
516 tr("settings.help.create_bundle_detail"),
517 text_button(
518 "settings-help-bundle",
519 tr("settings.help.create_bundle_button"),
520 )
521 .on_click(cx.listener(move |this, _, _, cx| {
522 create_app.update(cx, |state, _| match state.create_support_bundle() {
523 Ok(bundle) => AppState::reveal_support_bundle(&bundle),
524 Err(error) => {
525 app_base::diagnostics::record(format!("support bundle: {error}"))
526 }
527 });
528 this.reload_bundles();
529 cx.notify();
530 })),
531 ))
532 .child(control_row(
533 tr("settings.help.open_issue"),
534 tr("settings.help.open_issue_detail"),
535 text_button("settings-help-issue", tr("settings.help.open_issue_button")).on_click(
536 cx.listener(move |this, _, _, cx| {
537 issue_app.update(cx, |state, cx| {
538 if let Err(error) = state.open_github_issue_with_bundle(cx) {
539 app_base::diagnostics::record(format!("support issue: {error}"));
540 }
541 });
542 this.reload_bundles();
543 cx.notify();
544 }),
545 ),
546 ))
547 .child(control_row(
548 tr("settings.help.copy_summary"),
549 tr("settings.help.copy_summary_detail"),
550 text_button("settings-help-copy", tr("settings.help.copy_button")).on_click(
551 move |_, _, cx| {
552 cx.write_to_clipboard(ClipboardItem::new_string(copy_summary.clone()));
553 },
554 ),
555 ))
556 .child(control_row(
557 tr("settings.help.open_logs"),
558 tr("settings.help.open_logs_detail"),
559 text_button("settings-help-logs", tr("common.open")).on_click(move |_, _, _| {
560 AppState::reveal_logs_folder();
561 }),
562 ))
563 }
564}
565
566struct ReadPreview {
567 text: String,
568 truncated: bool,
569 binary: bool,
570}
571
572fn read_preview_text(path: &Path) -> std::io::Result<ReadPreview> {
573 use std::io::Read as _;
574 let mut file = std::fs::File::open(path)?;
575 let mut buf = vec![0u8; PREVIEW_LIMIT + 1];
576 let read = file.read(&mut buf)?;
577 let truncated = read > PREVIEW_LIMIT;
578 let slice = &buf[..read.min(PREVIEW_LIMIT)];
579 match std::str::from_utf8(slice) {
580 Ok(text) => Ok(ReadPreview {
581 text: text.to_owned(),
582 truncated,
583 binary: false,
584 }),
585 Err(_) => Ok(ReadPreview {
586 text: String::new(),
587 truncated: false,
588 binary: true,
589 }),
590 }
591}
592
593impl Render for HelpView {
594 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
595 let previewing = self.preview.is_some();
596 v_flex()
597 .w_full()
598 .when(previewing, |root| root.h_full().min_h_0())
599 .debug_selector(|| "settings/help".into())
600 .child(if previewing {
601 self.preview_panel(cx)
602 } else {
603 self.help_home(cx)
604 })
605 }
606}