BlueGit

ict rusty-teams

Files

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

appearance.rs

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