BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / translation_line.rs

translation_line.rs

cdded127 History
translation_line.rs 3.3 KiB Download Raw
1use std::rc::Rc;
2
3use gpui_kit::component::h_flex;
4use gpui_kit::*;
5
6use crate::rows::MessageRow;
7use crate::theme;
8use crate::translation::TranslationLine;
9
10const LINE_SIZE: f32 = 11.;
11const LINE_GAP: f32 = 10.;
12
13pub type LineAction = Rc<dyn Fn(&mut App)>;
14
15#[derive(Clone)]
16pub struct TranslationActions {
17 pub translate: LineAction,
18 pub never: Option<LineAction>,
19}
20
21fn line_link(id: ElementId, label: String, color: Hsla, action: LineAction) -> Stateful<Div> {
22 div()
23 .id(id)
24 .cursor_pointer()
25 .text_color(color)
26 .hover(|link| link.underline())
27 .child(label)
28 .on_click(move |_, _, cx| {
29 cx.stop_propagation();
30 action(cx)
31 })
32}
33
34pub fn translation_line(
35 row: &MessageRow,
36 index: usize,
37 actions: Option<&TranslationActions>,
38) -> Option<AnyElement> {
39 let line = row.translation.as_ref()?;
40 let link_id = |name: &str| ElementId::Name(format!("translation-{name}-{index}").into());
41 let base = h_flex()
42 .gap(px(LINE_GAP))
43 .text_size(px(LINE_SIZE))
44 .text_color(theme::text_muted());
45 let element = match line {
46 TranslationLine::Offer { language, .. } => base
47 .children(actions.map(|actions| {
48 line_link(
49 link_id("translate"),
50 "Translate".to_owned(),
51 theme::accent_text(),
52 actions.translate.clone(),
53 )
54 }))
55 .children(actions.and_then(|actions| {
56 actions.never.clone().map(|never| {
57 line_link(
58 link_id("never"),
59 format!("Never translate {language}"),
60 theme::text_muted(),
61 never,
62 )
63 })
64 })),
65 TranslationLine::Working => base.child("Translating..."),
66 TranslationLine::Failed => {
67 base.child("Couldn't translate.")
68 .children(actions.map(|actions| {
69 line_link(
70 link_id("retry"),
71 "Retry".to_owned(),
72 theme::accent_text(),
73 actions.translate.clone(),
74 )
75 }))
76 }
77 TranslationLine::Translated {
78 language,
79 showing_original,
80 } => {
81 let summary = match (language, showing_original) {
82 (_, true) => "Showing original".to_owned(),
83 (Some(language), false) => format!("Translated from {language}"),
84 (None, false) => "Translated".to_owned(),
85 };
86 let toggle = if *showing_original {
87 "See translation"
88 } else {
89 "See original"
90 };
91 base.child(
92 h_flex()
93 .gap(px(4.))
94 .child(summary)
95 .child("\u{b7}")
96 .children(actions.map(|actions| {
97 line_link(
98 link_id("toggle"),
99 toggle.to_owned(),
100 theme::accent_text(),
101 actions.translate.clone(),
102 )
103 })),
104 )
105 }
106 };
107 Some(element.into_any_element())
108}