BlueGit

ict rusty-teams

Files

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

help.rs

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