返回 CodeWhale
chrome.rs
1 //! Gallery entries for package Chrome: the heading, tabs, toggle and
2 //! segmented control. (Dialog and sheet are in `surface`, keymap hints in
3 //! `hints`, next to the components they extend.)
4
5 use ratatui::{buffer::Buffer, layout::Rect};
6
7 use super::Entry;
8 use crate::{Heading, Paint, Segmented, SegmentedState, Tab, Tabs, TabsState, Theme, Toggle};
9
10 fn row(area: Rect, y: u16, height: u16) -> Rect {
11 Rect {
12 y: area.y.saturating_add(y),
13 height,
14 ..area
15 }
16 .intersection(area)
17 }
18
19 fn heading(area: Rect, buf: &mut Buffer, theme: &Theme) {
20 Heading::new("Settings")
21 .meta("saved to this project")
22 .paint(row(area, 0, 1), buf, theme);
23 Heading::new("Appearance")
24 .section()
25 .meta("3 changed")
26 .paint(row(area, 2, 2), buf, theme);
27 Heading::new("Defaults")
28 .sub()
29 .paint(row(area, 4, 1), buf, theme);
30 Heading::new("A page title far too long for a narrow terminal to hold")
31 .meta("12 items")
32 .paint(row(area, 6, 1), buf, theme);
33 }
34
35 fn tab_items() -> Vec<Tab> {
36 vec![
37 Tab::new("General"),
38 Tab::new("Appearance"),
39 Tab::new("Agents").badge(3),
40 Tab::new("Connections"),
41 Tab::new("Privacy"),
42 Tab::new("Advanced"),
43 ]
44 }
45
46 fn tabs(area: Rect, buf: &mut Buffer, theme: &Theme) {
47 let items = tab_items();
48 Tabs::new(&items, TabsState::new(2))
49 .focused(true)
50 .paint(row(area, 0, 2), buf, theme);
51 // A narrow strip scrolls to keep its selection and marks what it hides.
52 let narrow = Rect {
53 width: area.width.min(34),
54 ..area
55 };
56 Tabs::new(&items, TabsState::new(4)).paint(row(narrow, 3, 2), buf, theme);
57 // Given one row there is no room for an underline: the selection is
58 // bracketed instead.
59 Tabs::new(&items[..3], TabsState::new(1)).paint(row(narrow, 6, 1), buf, theme);
60 }
61
62 fn toggle(area: Rect, buf: &mut Buffer, theme: &Theme) {
63 let rows = [
64 Toggle::new("Show tool details", true),
65 Toggle::new("Reduced motion", false).focused(true),
66 Toggle::new("Share usage data", false).disabled("set by your admin"),
67 Toggle::new("Show reasoning as it streams in the transcript", true),
68 ];
69 for (i, toggle) in rows.iter().enumerate() {
70 toggle.paint(row(area, i as u16, 1), buf, theme);
71 }
72 }
73
74 fn segmented(area: Rect, buf: &mut Buffer, theme: &Theme) {
75 let motion = ["Full", "Reduced", "Still"];
76 Segmented::new(motion, SegmentedState::new(1)).paint(row(area, 0, 1), buf, theme);
77 Segmented::new(["Light", "Dark", "System"], SegmentedState::new(0))
78 .focused(true)
79 .paint(row(area, 1, 1), buf, theme);
80 Segmented::new(motion, SegmentedState::new(2))
81 .disabled("set by your admin")
82 .paint(row(area, 2, 1), buf, theme);
83 // Too narrow for every option: the current one and its place.
84 let narrow = Rect {
85 width: area.width.min(18),
86 ..area
87 };
88 Segmented::new(motion, SegmentedState::new(1)).paint(row(narrow, 4, 1), buf, theme);
89 }
90
91 pub(crate) fn entries() -> Vec<Entry> {
92 vec![
93 Entry {
94 name: "heading",
95 width: 64,
96 height: 7,
97 draw: heading,
98 },
99 Entry {
100 name: "tabs",
101 width: 56,
102 height: 7,
103 draw: tabs,
104 },
105 Entry {
106 name: "toggle",
107 width: 48,
108 height: 4,
109 draw: toggle,
110 },
111 Entry {
112 name: "segmented",
113 width: 40,
114 height: 5,
115 draw: segmented,
116 },
117 ]
118 }
119
119 lines RUST