Skip to main content

kitchen_shell/
people_view.rs

1//! The frame's People: the Settings section that adds, picks and removes people, and what the
2//! title bar's person button lists ([`menu_items`]). See [`crate::people`].
3
4use gpui_kit::assets::IconName;
5use gpui_kit::component::button::{Button, ButtonVariants as _};
6use gpui_kit::component::input::{Input, InputState};
7use gpui_kit::component::{Selectable as _, Sizable as _, h_flex, v_flex};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::{
10    AnyElement, AppContext as _, Context, Entity, IntoElement, ParentElement as _, SharedString, Styled as _, Window,
11    div, px,
12};
13use kitchen_ui::theme::{self, color};
14use kitchen_ui::widgets::{icon_button, row};
15use serde_json::json;
16
17use crate::command::MenuItem;
18use crate::people::{self, Person};
19
20/// The title bar's person menu: each person (the one working checked), a guest, then People….
21pub fn menu_items() -> Vec<MenuItem> {
22    let current = people::current().map(|p| p.name);
23    let mut items: Vec<MenuItem> = people::people()
24        .into_iter()
25        .map(|p| MenuItem::Run {
26            title: p.name.clone().into(),
27            icon: Some(IconName::CircleUser),
28            command: "people.use".into(),
29            args: json!({ "name": p.name }),
30            checked: current.as_deref() == Some(p.name.as_str()),
31        })
32        .collect();
33    items.push(MenuItem::Run {
34        title: "Guest (no name recorded)".into(),
35        icon: Some(IconName::CircleUser),
36        command: "people.use".into(),
37        args: json!({ "name": null }),
38        checked: current.is_none(),
39    });
40    items.push(MenuItem::Separator);
41    items.push(MenuItem::command("people.manage"));
42    items
43}
44
45/// The People section of Settings.
46pub struct PeopleSettings {
47    name: Entity<InputState>,
48    affiliation: Entity<InputState>,
49    orcid: Entity<InputState>,
50    /// What the last change said (a refusal).
51    error: Option<SharedString>,
52}
53
54impl PeopleSettings {
55    /// The section, its form empty.
56    pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
57        Self {
58            name: cx.new(|cx| InputState::new(window, cx).placeholder("Name, as it is recorded")),
59            affiliation: cx.new(|cx| InputState::new(window, cx).placeholder("Lab or institution (optional)")),
60            orcid: cx.new(|cx| InputState::new(window, cx).placeholder("0000-0000-0000-0000 (optional)")),
61            error: None,
62        }
63    }
64
65    fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
66        let value = |i: &Entity<InputState>, cx: &Context<Self>| i.read(cx).value().to_string();
67        let person = Person {
68            name: value(&self.name, cx),
69            affiliation: value(&self.affiliation, cx),
70            orcid: value(&self.orcid, cx),
71        };
72        match people::add(person, true) {
73            Ok(()) => {
74                self.error = None;
75                for input in [&self.name, &self.affiliation, &self.orcid] {
76                    input.update(cx, |i, cx| i.set_value("", window, cx));
77                }
78            }
79            Err(e) => self.error = Some(e.into()),
80        }
81        cx.notify();
82    }
83
84    fn change(&mut self, done: Result<(), String>, cx: &mut Context<Self>) {
85        self.error = done.err().map(Into::into);
86        cx.notify();
87    }
88
89    /// The section's body.
90    pub fn body(&mut self, cx: &mut Context<Self>) -> AnyElement {
91        let current = people::current().map(|p| p.name);
92        let everyone = people::people();
93        let list = v_flex().gap(px(theme::XS)).children(everyone.into_iter().enumerate().map(|(i, p)| {
94            let working = current.as_deref() == Some(p.name.as_str());
95            let about = [p.affiliation.as_str(), p.orcid.as_str()]
96                .into_iter()
97                .filter(|s| !s.is_empty())
98                .collect::<Vec<_>>()
99                .join(" · ");
100            let (use_name, remove_name) = (p.name.clone(), p.name.clone());
101            h_flex()
102                .gap(px(theme::SM))
103                .p(px(theme::XS))
104                .rounded(px(theme::RADIUS))
105                .when(working, |d| d.bg(color::control()))
106                .child(
107                    v_flex()
108                        .flex_1()
109                        .min_w_0()
110                        .child(div().text_sm().truncate().child(p.name.clone()))
111                        .when(!about.is_empty(), |d| {
112                            d.child(div().text_xs().text_color(color::text_muted()).truncate().child(about))
113                        }),
114                )
115                .child(
116                    Button::new(SharedString::from(format!("people-use-{i}")))
117                        .small()
118                        .ghost()
119                        .label(if working { "Working" } else { "Work as" })
120                        .selected(working)
121                        .on_click(
122                            cx.listener(move |this, _, _, cx| this.change(people::use_person(Some(&use_name)), cx)),
123                        ),
124                )
125                .child(
126                    icon_button(
127                        SharedString::from(format!("people-remove-{i}")),
128                        IconName::Delete,
129                        format!("Forget {} (what is recorded under the name stays)", p.name),
130                    )
131                    .on_click(cx.listener(move |this, _, _, cx| this.change(people::remove(&remove_name), cx))),
132                )
133        }));
134        let guest = Button::new("people-guest")
135            .small()
136            .ghost()
137            .label(if current.is_none() { "Working as a guest" } else { "Work as a guest" })
138            .selected(current.is_none())
139            .on_click(cx.listener(|this, _, _, cx| this.change(people::use_person(None), cx)));
140        // Two groups, further apart than their rows: who there is, and adding someone
141        let add = v_flex()
142            .gap(px(theme::SM))
143            .pt(px(theme::LG))
144            .border_t_1()
145            .border_color(color::border())
146            .child(div().text_sm().font_weight(gpui_kit::FontWeight::SEMIBOLD).child("Add a person"))
147            .child(row("Name", div().w_full().child(Input::new(&self.name).small())))
148            .child(row("Affiliation", div().w_full().child(Input::new(&self.affiliation).small())))
149            .child(row("ORCID iD", div().w_full().child(Input::new(&self.orcid).small())))
150            .children(self.error.clone().map(|e| div().text_xs().text_color(color::danger()).child(e)))
151            .child(
152                h_flex().justify_end().child(
153                    Button::new("people-add")
154                        .small()
155                        .primary()
156                        .icon(IconName::Plus)
157                        .label("Add and work as")
158                        .on_click(cx.listener(|this, _, window, cx| this.add(window, cx))),
159                ),
160            );
161        v_flex()
162            .gap(px(theme::LG))
163            .child(div().max_w(px(560.)).text_xs().text_color(color::text_muted()).child("What you do is recorded under the name of the person working: in the app's activity log and in files you save (a curation, a run's record), which may be shared. A guest is recorded as \u{201c}guest\u{201d}, with no name."))
164            .child(v_flex().gap(px(theme::SM)).child(list).child(h_flex().child(guest)))
165            .child(add)
166            .into_any_element()
167    }
168}