BlueGit

ict rusty-teams

Files

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

appearance.rs

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