BlueGit

ict rusty-teams

Files

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

appearance.rs

appearance.rs 8.6 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};
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 .child(choice_group(
124 tr("settings.appearance.color_scheme"),
125 tr("settings.appearance.color_scheme_detail"),
126 scheme_tiles,
127 ))
128 .child(caption(tr("settings.caption.app_icon")))
129 .child(choice_group(
130 tr("settings.appearance.app_icon"),
131 app_icon_detail(),
132 icon_tiles,
133 ))
134}
135
136fn choice_group(
137 title: impl Into<SharedString>,
138 detail: impl Into<SharedString>,
139 tiles: impl Iterator<Item = Stateful<Div>>,
140) -> Div {
141 v_flex()
142 .w_full()
143 .px(px(ROW_INSET))
144 .py(px(12.))
145 .gap(px(12.))
146 .child(titled(title, detail, theme::text_muted()))
147 .child(
148 h_flex()
149 .w_full()
150 .flex_wrap()
151 .gap(px(TILE_GAP))
152 .children(tiles),
153 )
154}
155
156fn tile(id: SharedString, label: &'static str, selected: bool, preview: Div) -> Stateful<Div> {
157 v_flex()
158 .id(id)
159 .w(px(TILE_WIDTH))
160 .flex_none()
161 .p(px(TILE_PADDING))
162 .gap(px(TILE_PADDING))
163 .rounded(px(TILE_RADIUS))
164 .bg(theme::surface_raised())
165 .cursor_pointer()
166 .hover(|tile| tile.bg(theme::popup_hover()))
167 .child(preview)
168 .child(
169 h_flex()
170 .w_full()
171 .px(px(TILE_PADDING / 2.))
172 .pb(px(TILE_PADDING / 2.))
173 .items_center()
174 .justify_between()
175 .child(
176 div()
177 .text_size(px(14.))
178 .line_height(px(20.))
179 .text_color(theme::text())
180 .child(label),
181 )
182 .child(radio(selected)),
183 )
184}
185
186fn radio(selected: bool) -> Div {
187 div()
188 .size(px(RADIO_SIZE))
189 .flex_none()
190 .flex()
191 .items_center()
192 .justify_center()
193 .rounded_full()
194 .border_1()
195 .border_color(if selected {
196 theme::accent()
197 } else {
198 theme::text_muted()
199 })
200 .when(selected, |radio| {
201 radio.child(
202 div()
203 .size(px(RADIO_DOT_SIZE))
204 .rounded_full()
205 .bg(theme::accent()),
206 )
207 })
208}
209
210fn preview_frame(background: Hsla) -> Div {
211 div()
212 .w_full()
213 .h(px(PREVIEW_HEIGHT))
214 .rounded(px(PREVIEW_RADIUS))
215 .overflow_hidden()
216 .bg(background)
217}
218
219fn scheme_preview(palette: &Palette) -> Div {
220 let fill = |hex: u32| theme::color(hex);
221 let bubble = |hex: u32, width: f32| div().w(px(width)).h(px(12.)).rounded(px(6.)).bg(fill(hex));
222 preview_frame(fill(palette.background)).child(
223 h_flex()
224 .size_full()
225 .p(px(6.))
226 .gap(px(6.))
227 .child(
228 v_flex()
229 .w(px(14.))
230 .h_full()
231 .gap(px(4.))
232 .items_center()
233 .child(
234 div()
235 .size(px(10.))
236 .rounded(px(3.))
237 .bg(fill(palette.surface_raised)),
238 )
239 .child(div().size(px(10.)).rounded(px(3.)).bg(fill(palette.accent))),
240 )
241 .child(
242 v_flex()
243 .flex_1()
244 .h_full()
245 .p(px(8.))
246 .gap(px(6.))
247 .rounded(px(6.))
248 .bg(fill(palette.surface))
249 .child(h_flex().child(bubble(palette.bubble_other, 64.)))
250 .child(
251 h_flex()
252 .justify_end()
253 .child(bubble(palette.bubble_own, 52.)),
254 )
255 .child(h_flex().child(bubble(palette.bubble_other, 40.)))
256 .child(
257 h_flex()
258 .justify_end()
259 .child(div().size(px(12.)).rounded_full().bg(fill(palette.accent))),
260 ),
261 ),
262 )
263}
264
265fn icon_preview(icon: AppIcon) -> Div {
266 preview_frame(theme::background())
267 .flex()
268 .items_center()
269 .justify_center()
270 .child(img(app_icon_image(icon)).size(px(PREVIEW_ICON_SIZE)))
271}