BlueGit

ict rusty-teams

Files

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

help.rs

help.rs 21.5 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(text_button(open_id, tr("common.open")).on_click(move |_, _, _| {
210 Self::reveal_path(&open_path);
211 })),
212 )
213 .child(
214 div()
215 .w_full()
216 .min_w(px(0.))
217 .font_family(cx.theme().mono_font_family.clone())
218 .text_size(px(12.))
219 .line_height(px(18.))
220 .text_color(theme::text_muted())
221 .whitespace_normal()
222 .child(path),
223 )
224 }
225
226 fn bundle_row(&self, entry: &SupportBundleEntry, cx: &mut Context<Self>) -> Div {
227 let directory = entry.directory.clone();
228 let toggle_dir = directory.clone();
229 let delete_dir = directory.clone();
230 let expanded = self.expanded.as_ref() == Some(&directory);
231 v_flex()
232 .w_full()
233 .child(
234 h_flex()
235 .w_full()
236 .px(px(ROW_INSET))
237 .py(px(10.))
238 .gap(px(12.))
239 .items_center()
240 .rounded(px(8.))
241 .when(expanded, |row| row.bg(theme::popup_hover()))
242 .child(
243 h_flex()
244 .id(SharedString::from(format!(
245 "settings-help-bundle-{}",
246 entry.name
247 )))
248 .flex_1()
249 .min_w(px(0.))
250 .gap(px(8.))
251 .items_center()
252 .cursor_pointer()
253 .hover(|row| row.bg(theme::popup_hover()))
254 .on_click(cx.listener(move |this, _, _, cx| {
255 this.toggle_expand(toggle_dir.clone(), cx);
256 }))
257 .child(
258 div()
259 .flex_1()
260 .min_w(px(0.))
261 .text_size(px(14.))
262 .font_weight(FontWeight::SEMIBOLD)
263 .text_color(theme::text())
264 .child(entry.name.clone()),
265 ),
266 )
267 .child(
268 text_button(
269 SharedString::from(format!("settings-help-delete-{}", entry.name)),
270 tr("common.delete"),
271 )
272 .on_click(cx.listener(move |this, _, _, cx| {
273 this.delete_bundle(delete_dir.clone(), cx);
274 })),
275 ),
276 )
277 .when(expanded, |column| column.child(self.expanded_panel(cx)))
278 }
279
280 fn expanded_panel(&self, cx: &mut Context<Self>) -> Div {
281 v_flex()
282 .w_full()
283 .px(px(ROW_INSET + 8.))
284 .pb(px(8.))
285 .gap(px(4.))
286 .children(self.expanded_files.iter().map(|file| {
287 let path = file.path.clone();
288 let label = format!("{} · {}", file.name, format_bytes(file.size_bytes));
289 div()
290 .id(SharedString::from(format!(
291 "settings-help-file-{}",
292 file.name
293 )))
294 .w_full()
295 .px(px(8.))
296 .py(px(6.))
297 .rounded(px(6.))
298 .cursor_pointer()
299 .hover(|row| row.bg(theme::popup_hover()))
300 .on_click(cx.listener(move |this, _, _, cx| {
301 this.open_preview(path.clone(), cx);
302 }))
303 .child(
304 div()
305 .text_size(px(12.))
306 .text_color(theme::text_muted())
307 .child(label),
308 )
309 }))
310 }
311
312 fn packages_section(&self, cx: &mut Context<Self>) -> Div {
313 v_flex()
314 .w_full()
315 .child(caption(tr("settings.help.caption_packages")))
316 .when(self.bundles.is_empty(), |section| {
317 section.child(
318 div()
319 .px(px(ROW_INSET))
320 .py(px(8.))
321 .text_size(px(13.))
322 .text_color(theme::text_muted())
323 .child(tr("settings.help.packages_empty")),
324 )
325 })
326 .children(
327 self.bundles
328 .iter()
329 .map(|entry| self.bundle_row(entry, cx))
330 .collect::<Vec<_>>(),
331 )
332 }
333
334 fn preview_body(&self, preview: &FilePreview, cx: &App) -> AnyElement {
335 match preview.kind {
336 PreviewKind::Markdown => {
337 // Full document markdown (headings, tables, lists) — not the light chat dialect.
338 let html = teams_core::Draft::from_markdown(&preview.text).to_html();
339 let blocks =
340 strip_image_placeholders(layout_blocks(&teams_core::html_to_spans(&html)));
341 div()
342 .w_full()
343 .min_w(px(0.))
344 .text_size(px(13.))
345 .text_color(theme::text())
346 .whitespace_normal()
347 .child(render_blocks(&blocks, "settings-help-preview", false, cx))
348 .into_any_element()
349 }
350 PreviewKind::Text => div()
351 .w_full()
352 .min_w(px(0.))
353 .font_family(cx.theme().mono_font_family.clone())
354 .text_size(px(12.))
355 .line_height(px(18.))
356 .text_color(theme::text_muted())
357 .whitespace_normal()
358 .child(preview.text.clone())
359 .into_any_element(),
360 }
361 }
362
363 fn preview_panel(&self, cx: &mut Context<Self>) -> Div {
364 let preview = self.preview.as_ref().expect("preview open");
365 v_flex()
366 .w_full()
367 .h_full()
368 .min_h_0()
369 .gap(px(8.))
370 .px(px(ROW_INSET))
371 .pb(px(ROW_INSET))
372 .child(
373 h_flex()
374 .w_full()
375 .flex_none()
376 .gap(px(8.))
377 .items_center()
378 .child(
379 text_button("settings-help-preview-back", tr("common.close")).on_click(
380 cx.listener(|this, _, _, cx| this.close_preview(cx)),
381 ),
382 )
383 .child(
384 div()
385 .flex_1()
386 .min_w(px(0.))
387 .text_size(px(14.))
388 .font_weight(FontWeight::SEMIBOLD)
389 .text_color(theme::text())
390 .child(preview.name.clone()),
391 ),
392 )
393 .when(preview.truncated, |column| {
394 column.child(
395 div()
396 .flex_none()
397 .text_size(px(12.))
398 .text_color(theme::text_muted())
399 .child(tr("settings.help.preview_truncated")),
400 )
401 })
402 .child(
403 div()
404 .id("settings-help-preview-scroll")
405 .w_full()
406 .flex_1()
407 .min_h_0()
408 .p(px(PREVIEW_PADDING))
409 .rounded(px(PREVIEW_RADIUS))
410 .dialog_surface()
411 .overflow_y_scrollbar()
412 .child(self.preview_body(preview, cx)),
413 )
414 }
415
416 fn help_home(&self, cx: &mut Context<Self>) -> Div {
417 let info = self.app.read(cx).client_info();
418 let summary = info.markdown();
419 let copy_summary = summary.clone();
420 let logs_path = info.logs_root.clone();
421 let bundles_path = info.support_bundles_root.clone();
422 let create_app = self.app.clone();
423 let issue_app = self.app.clone();
424 v_flex()
425 .w_full()
426 .gap(px(4.))
427 .child(caption(tr("settings.help.caption_client")))
428 .child(control_row(
429 tr("settings.help.version"),
430 format!("{} ({})", info.version, info.build),
431 div(),
432 ))
433 .child(control_row(
434 tr("settings.help.platform"),
435 format!("{} / {} / {}", info.os, info.arch, info.session),
436 div(),
437 ))
438 .child(control_row(
439 tr("settings.help.locale"),
440 format!("{} · {}", info.ui_locale, info.timezone),
441 div(),
442 ))
443 .child(control_row(
444 tr("settings.help.signed_in"),
445 if info.signed_in {
446 tr("settings.help.yes")
447 } else {
448 tr("settings.help.no")
449 },
450 div(),
451 ))
452 .child({
453 let debug_on = self.app.read(cx).diagnostics_enabled();
454 let toggle_app = self.app.clone();
455 toggle_row(
456 "settings-help-debug-log",
457 tr("settings.help.debug_logging"),
458 if debug_on {
459 tr("settings.help.debug_logging_on_detail")
460 } else {
461 tr("settings.help.debug_logging_off_detail")
462 },
463 theme::text_muted(),
464 debug_on,
465 )
466 .on_click(cx.listener(move |_, _, _, cx| {
467 toggle_app.update(cx, |state, cx| {
468 state.set_diagnostics_enabled(!debug_on, cx);
469 });
470 cx.notify();
471 }))
472 })
473 .child({
474 let clear_app = self.app.clone();
475 control_row(
476 tr("settings.help.clear_logs"),
477 tr("settings.help.clear_logs_detail"),
478 text_button("settings-help-clear-logs", tr("settings.help.clear_logs_button"))
479 .on_click(cx.listener(move |_, _, _, cx| {
480 clear_app.update(cx, |state, cx| state.clear_diagnostic_logs(cx));
481 cx.notify();
482 })),
483 )
484 })
485 .child(caption(tr("settings.help.caption_paths")))
486 .child(self.path_row(
487 tr("settings.help.logs_path"),
488 logs_path,
489 "settings-help-copy-logs",
490 "settings-help-open-logs-root",
491 cx,
492 ))
493 .child(self.path_row(
494 tr("settings.help.bundles_path"),
495 bundles_path,
496 "settings-help-copy-bundles",
497 "settings-help-open-bundles-root",
498 cx,
499 ))
500 .child(self.packages_section(cx))
501 .child(caption(tr("settings.help.caption_actions")))
502 .child(
503 div()
504 .text_size(px(13.))
505 .text_color(theme::text_muted())
506 .pb(px(8.))
507 .px(px(ROW_INSET))
508 .child(tr("settings.help.detail")),
509 )
510 .child(control_row(
511 tr("settings.help.create_bundle"),
512 tr("settings.help.create_bundle_detail"),
513 text_button("settings-help-bundle", tr("settings.help.create_bundle_button"))
514 .on_click(cx.listener(move |this, _, _, cx| {
515 create_app.update(cx, |state, _| {
516 match state.create_support_bundle() {
517 Ok(bundle) => AppState::reveal_support_bundle(&bundle),
518 Err(error) => app_base::diagnostics::record(format!(
519 "support bundle: {error}"
520 )),
521 }
522 });
523 this.reload_bundles();
524 cx.notify();
525 })),
526 ))
527 .child(control_row(
528 tr("settings.help.open_issue"),
529 tr("settings.help.open_issue_detail"),
530 text_button("settings-help-issue", tr("settings.help.open_issue_button")).on_click(
531 cx.listener(move |this, _, _, cx| {
532 issue_app.update(cx, |state, cx| {
533 if let Err(error) = state.open_github_issue_with_bundle(cx) {
534 app_base::diagnostics::record(format!("support issue: {error}"));
535 }
536 });
537 this.reload_bundles();
538 cx.notify();
539 }),
540 ),
541 ))
542 .child(control_row(
543 tr("settings.help.copy_summary"),
544 tr("settings.help.copy_summary_detail"),
545 text_button("settings-help-copy", tr("settings.help.copy_button")).on_click(
546 move |_, _, cx| {
547 cx.write_to_clipboard(ClipboardItem::new_string(copy_summary.clone()));
548 },
549 ),
550 ))
551 .child(control_row(
552 tr("settings.help.open_logs"),
553 tr("settings.help.open_logs_detail"),
554 text_button("settings-help-logs", tr("common.open")).on_click(move |_, _, _| {
555 AppState::reveal_logs_folder();
556 }),
557 ))
558 }
559}
560
561struct ReadPreview {
562 text: String,
563 truncated: bool,
564 binary: bool,
565}
566
567fn read_preview_text(path: &Path) -> std::io::Result<ReadPreview> {
568 use std::io::Read as _;
569 let mut file = std::fs::File::open(path)?;
570 let mut buf = vec![0u8; PREVIEW_LIMIT + 1];
571 let read = file.read(&mut buf)?;
572 let truncated = read > PREVIEW_LIMIT;
573 let slice = &buf[..read.min(PREVIEW_LIMIT)];
574 match std::str::from_utf8(slice) {
575 Ok(text) => Ok(ReadPreview {
576 text: text.to_owned(),
577 truncated,
578 binary: false,
579 }),
580 Err(_) => Ok(ReadPreview {
581 text: String::new(),
582 truncated: false,
583 binary: true,
584 }),
585 }
586}
587
588impl Render for HelpView {
589 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
590 let previewing = self.preview.is_some();
591 v_flex()
592 .w_full()
593 .when(previewing, |root| root.h_full().min_h_0())
594 .debug_selector(|| "settings/help".into())
595 .child(if previewing {
596 self.preview_panel(cx)
597 } else {
598 self.help_home(cx)
599 })
600 }
601}