BlueGit

ict rusty-teams

Files

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

appearance.rs

appearance.rs 9.2 KiB Download Raw
1use gpui_kit::component::{h_flex, v_flex};
2use gpui_kit::prelude::FluentBuilder as _;
3use gpui_kit::*;
4
5use super::rows::{Choice, ROW_INSET, caption, control_row, select_with_width, titled, toggle_row};
6use crate::active_appearance;
7use crate::active_locale;
8use crate::app_state::AppState;
9use crate::appearance::{AppIcon, ThemeChoice};
10use crate::i18n::tr;
11use crate::theme::{self, Palette};
12use crate::views::title_bar::app_icon_image;
13use app_base::ui_locale::LocalePreference;
14
15const TILE_WIDTH: f32 = 168.;
16const TILE_PADDING: f32 = 8.;
17const TILE_GAP: f32 = 12.;
18const TILE_RADIUS: f32 = 12.;
19const PREVIEW_HEIGHT: f32 = 96.;
20const PREVIEW_RADIUS: f32 = TILE_RADIUS - TILE_PADDING;
21const PREVIEW_ICON_SIZE: f32 = 48.;
22const RADIO_SIZE: f32 = 16.;
23const RADIO_DOT_SIZE: f32 = 8.;
24const THEME_SELECT_WIDTH: f32 = 240.;
25const LANGUAGE_SELECT_WIDTH: f32 = 240.;
26
27fn language_label(pref: LocalePreference) -> String {
28 match pref {
29 LocalePreference::Auto => tr("settings.appearance.language.follow_system"),
30 LocalePreference::English => tr("settings.appearance.language.english"),
31 LocalePreference::German => tr("settings.appearance.language.german"),
32 LocalePreference::Dutch => tr("settings.appearance.language.dutch"),
33 }
34}
35fn app_icon_detail() -> String {
36 #[cfg(windows)]
37 {
38 tr("settings.appearance.app_icon_detail_windows")
39 }
40 #[cfg(not(windows))]
41 {
42 tr("settings.appearance.app_icon_detail")
43 }
44}
45
46pub fn render(app: &Entity<AppState>, cx: &App) -> Div {
47 let current = active_appearance::current(cx);
48 let locale_pref = active_locale::current_preference(cx);
49 let language_choices = LocalePreference::ALL
50 .into_iter()
51 .map(|pref| {
52 let app = app.clone();
53 Choice::new(language_label(pref), pref == locale_pref, move |cx| {
54 let store = app.read(cx).store.clone();
55 active_locale::set_preference(pref, &store, cx);
56 })
57 })
58 .collect();
59 let scheme_tiles = theme::SCHEMES.into_iter().map(|scheme| {
60 let app = app.clone();
61 tile(
62 SharedString::from(format!("settings-color-scheme-{}", scheme.id)),
63 scheme.name,
64 scheme.id == current.color_scheme.id,
65 scheme_preview(scheme.palette(theme::shade())),
66 )
67 .on_click(move |_, _, cx| {
68 let store = app.read(cx).store.clone();
69 active_appearance::set_color_scheme(scheme, &store, cx);
70 })
71 });
72 let icon_tiles = AppIcon::ALL.into_iter().map(|icon| {
73 let app = app.clone();
74 tile(
75 SharedString::from(format!("settings-app-icon-{}", icon.key())),
76 icon.label(),
77 icon == current.app_icon,
78 icon_preview(icon),
79 )
80 .on_click(move |_, _, cx| {
81 let store = app.read(cx).store.clone();
82 active_appearance::set_app_icon(icon, &store, cx);
83 })
84 });
85 let theme_choices = ThemeChoice::ALL
86 .into_iter()
87 .map(|choice| {
88 let app = app.clone();
89 Choice::new(choice.label(), choice == current.theme, move |cx| {
90 let store = app.read(cx).store.clone();
91 active_appearance::set_theme(choice, &store, cx);
92 })
93 })
94 .collect();
95 v_flex()
96 .w_full()
97 .child(caption(&tr("settings.caption.language")))
98 .child(control_row(
99 &tr("settings.appearance.language.title"),
100 &tr("settings.appearance.language.detail"),
101 select_with_width(
102 "settings-language",
103 language_label(locale_pref),
104 LANGUAGE_SELECT_WIDTH,
105 false,
106 language_choices,
107 ),
108 ))
109 .child(caption(tr("settings.caption.regional_format")))
110 .children(super::regional::rows(app, cx))
111 .child(caption(tr("settings.caption.theme")))
112 .child(control_row(
113 tr("settings.appearance.theme"),
114 tr("settings.appearance.theme_detail"),
115 select_with_width(
116 "settings-theme",
117 current.theme.label(),
118 THEME_SELECT_WIDTH,
119 false,
120 theme_choices,
121 ),
122 ))
123 .when(app_base::kvt_window::available(), |view| {
124 let store = app.clone();
125 let on = crate::kvt::window_controls::enabled(cx);
126 view.child(toggle_row(
127 "settings-window-controls",
128 tr("kvt.window.title"),
129 tr("kvt.window.detail"),
130 theme::text_muted(),
131 on,
132 )
133 .on_click(move |_, window, cx| {
134 let store = store.read(cx).store.clone();
135 crate::kvt::window_controls::set_enabled(!on, &store, window, cx);
136 }))
137 })
138 .child(choice_group(
139 tr("settings.appearance.color_scheme"),
140 tr("settings.appearance.color_scheme_detail"),
141 scheme_tiles,
142 ))
143 .child(caption(tr("settings.caption.app_icon")))
144 .child(choice_group(
145 tr("settings.appearance.app_icon"),
146 app_icon_detail(),
147 icon_tiles,
148 ))
149}
150
151fn choice_group(
152 title: impl Into<SharedString>,
153 detail: impl Into<SharedString>,
154 tiles: impl Iterator<Item = Stateful<Div>>,
155) -> Div {
156 v_flex()
157 .w_full()
158 .px(px(ROW_INSET))
159 .py(px(12.))
160 .gap(px(12.))
161 .child(titled(title, detail, theme::text_muted()))
162 .child(
163 h_flex()
164 .w_full()
165 .flex_wrap()
166 .gap(px(TILE_GAP))
167 .children(tiles),
168 )
169}
170
171fn tile(id: SharedString, label: &'static str, selected: bool, preview: Div) -> Stateful<Div> {
172 v_flex()
173 .id(id)
174 .w(px(TILE_WIDTH))
175 .flex_none()
176 .p(px(TILE_PADDING))
177 .gap(px(TILE_PADDING))
178 .rounded(px(TILE_RADIUS))
179 .bg(theme::surface_raised())
180 .cursor_pointer()
181 .hover(|tile| tile.bg(theme::popup_hover()))
182 .child(preview)
183 .child(
184 h_flex()
185 .w_full()
186 .px(px(TILE_PADDING / 2.))
187 .pb(px(TILE_PADDING / 2.))
188 .items_center()
189 .justify_between()
190 .child(
191 div()
192 .text_size(px(14.))
193 .line_height(px(20.))
194 .text_color(theme::text())
195 .child(label),
196 )
197 .child(radio(selected)),
198 )
199}
200
201fn radio(selected: bool) -> Div {
202 div()
203 .size(px(RADIO_SIZE))
204 .flex_none()
205 .flex()
206 .items_center()
207 .justify_center()
208 .rounded_full()
209 .border_1()
210 .border_color(if selected {
211 theme::accent()
212 } else {
213 theme::text_muted()
214 })
215 .when(selected, |radio| {
216 radio.child(
217 div()
218 .size(px(RADIO_DOT_SIZE))
219 .rounded_full()
220 .bg(theme::accent()),
221 )
222 })
223}
224
225fn preview_frame(background: Hsla) -> Div {
226 div()
227 .w_full()
228 .h(px(PREVIEW_HEIGHT))
229 .rounded(px(PREVIEW_RADIUS))
230 .overflow_hidden()
231 .bg(background)
232}
233
234fn scheme_preview(palette: &Palette) -> Div {
235 let fill = |hex: u32| theme::color(hex);
236 let bubble = |hex: u32, width: f32| div().w(px(width)).h(px(12.)).rounded(px(6.)).bg(fill(hex));
237 preview_frame(fill(palette.background)).child(
238 h_flex()
239 .size_full()
240 .p(px(6.))
241 .gap(px(6.))
242 .child(
243 v_flex()
244 .w(px(14.))
245 .h_full()
246 .gap(px(4.))
247 .items_center()
248 .child(
249 div()
250 .size(px(10.))
251 .rounded(px(3.))
252 .bg(fill(palette.surface_raised)),
253 )
254 .child(div().size(px(10.)).rounded(px(3.)).bg(fill(palette.accent))),
255 )
256 .child(
257 v_flex()
258 .flex_1()
259 .h_full()
260 .p(px(8.))
261 .gap(px(6.))
262 .rounded(px(6.))
263 .bg(fill(palette.surface))
264 .child(h_flex().child(bubble(palette.bubble_other, 64.)))
265 .child(
266 h_flex()
267 .justify_end()
268 .child(bubble(palette.bubble_own, 52.)),
269 )
270 .child(h_flex().child(bubble(palette.bubble_other, 40.)))
271 .child(
272 h_flex()
273 .justify_end()
274 .child(div().size(px(12.)).rounded_full().bg(fill(palette.accent))),
275 ),
276 ),
277 )
278}
279
280fn icon_preview(icon: AppIcon) -> Div {
281 preview_frame(theme::background())
282 .flex()
283 .items_center()
284 .justify_center()
285 .child(img(app_icon_image(icon)).size(px(PREVIEW_ICON_SIZE)))
286}