| 1 | use gpui_kit::component::{h_flex, v_flex}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::center::NotificationCenter; |
| 5 | use super::rules::Settings; |
| 6 | use crate::theme; |
| 7 | |
| 8 | type Accessor = fn(&mut Settings) -> &mut bool; |
| 9 | |
| 10 | struct Row { |
| 11 | title: &'static str, |
| 12 | detail: &'static str, |
| 13 | field: Accessor, |
| 14 | } |
| 15 | |
| 16 | const ROWS: [Row; 5] = [ |
| 17 | Row { |
| 18 | title: "Sound", |
| 19 | detail: "Messages and mentions", |
| 20 | field: |settings| &mut settings.sound, |
| 21 | }, |
| 22 | Row { |
| 23 | title: "Mentions only", |
| 24 | detail: "Direct messages and @mentions", |
| 25 | field: |settings| &mut settings.mentions_only, |
| 26 | }, |
| 27 | Row { |
| 28 | title: "Show preview", |
| 29 | detail: "Off: only \"New message\"", |
| 30 | field: |settings| &mut settings.preview, |
| 31 | }, |
| 32 | Row { |
| 33 | title: "Flash taskbar", |
| 34 | detail: "On new messages, until you open the window", |
| 35 | field: |settings| &mut settings.flash, |
| 36 | }, |
| 37 | Row { |
| 38 | title: "Minimize to tray on close", |
| 39 | detail: "The title bar X hides the window", |
| 40 | field: |settings| &mut settings.close_to_tray, |
| 41 | }, |
| 42 | ]; |
| 43 | |
| 44 | pub struct SettingsView { |
| 45 | center: Entity<NotificationCenter>, |
| 46 | _subscription: Subscription, |
| 47 | } |
| 48 | |
| 49 | impl SettingsView { |
| 50 | pub const WIDTH: f32 = 420.; |
| 51 | pub const HEIGHT: f32 = 460.; |
| 52 | |
| 53 | pub fn new(center: Entity<NotificationCenter>, cx: &mut Context<Self>) -> Self { |
| 54 | let subscription = cx.observe(¢er, |_, _, cx| cx.notify()); |
| 55 | SettingsView { |
| 56 | center, |
| 57 | _subscription: subscription, |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn switch(on: bool) -> Div { |
| 63 | div() |
| 64 | .w(px(36.)) |
| 65 | .h(px(20.)) |
| 66 | .flex_none() |
| 67 | .rounded_full() |
| 68 | .relative() |
| 69 | .bg(if on { |
| 70 | theme::accent() |
| 71 | } else { |
| 72 | theme::border_strong() |
| 73 | }) |
| 74 | .child( |
| 75 | div() |
| 76 | .absolute() |
| 77 | .top(px(2.)) |
| 78 | .left(px(if on { 18. } else { 2. })) |
| 79 | .size(px(16.)) |
| 80 | .rounded_full() |
| 81 | .bg(theme::text()), |
| 82 | ) |
| 83 | } |
| 84 | |
| 85 | fn button(label: &'static str) -> Stateful<Div> { |
| 86 | div() |
| 87 | .id(label) |
| 88 | .h(px(32.)) |
| 89 | .px(px(14.)) |
| 90 | .flex() |
| 91 | .items_center() |
| 92 | .rounded(px(8.)) |
| 93 | .border_1() |
| 94 | .border_color(theme::border_strong()) |
| 95 | .cursor_pointer() |
| 96 | .text_size(px(13.)) |
| 97 | .font_weight(FontWeight::SEMIBOLD) |
| 98 | .text_color(theme::text()) |
| 99 | .hover(|button| button.bg(theme::row_hover())) |
| 100 | .child(label) |
| 101 | } |
| 102 | |
| 103 | impl Render for SettingsView { |
| 104 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 105 | let mut current = self.center.read(cx).settings().clone(); |
| 106 | let corner = current.corner; |
| 107 | let rows = ROWS.iter().enumerate().map(|(index, row)| { |
| 108 | let on = *(row.field)(&mut current); |
| 109 | let center = self.center.clone(); |
| 110 | let field = row.field; |
| 111 | h_flex() |
| 112 | .id(("setting", index)) |
| 113 | .w_full() |
| 114 | .px(px(20.)) |
| 115 | .py(px(10.)) |
| 116 | .gap(px(12.)) |
| 117 | .items_center() |
| 118 | .cursor_pointer() |
| 119 | .hover(|row| row.bg(theme::row_hover())) |
| 120 | .on_click(move |_, _, cx| { |
| 121 | center.update(cx, |center, cx| { |
| 122 | center.update_settings(cx, |settings| { |
| 123 | let value = field(settings); |
| 124 | *value = !*value; |
| 125 | }) |
| 126 | }); |
| 127 | }) |
| 128 | .child( |
| 129 | v_flex() |
| 130 | .flex_1() |
| 131 | .child( |
| 132 | div() |
| 133 | .text_size(px(14.)) |
| 134 | .font_weight(FontWeight::SEMIBOLD) |
| 135 | .text_color(theme::text()) |
| 136 | .child(row.title), |
| 137 | ) |
| 138 | .child( |
| 139 | div() |
| 140 | .text_size(px(12.)) |
| 141 | .text_color(theme::text_muted()) |
| 142 | .child(row.detail), |
| 143 | ), |
| 144 | ) |
| 145 | .child(switch(on)) |
| 146 | }); |
| 147 | let position_center = self.center.clone(); |
| 148 | let test_center = self.center.clone(); |
| 149 | v_flex() |
| 150 | .size_full() |
| 151 | .bg(theme::background()) |
| 152 | .pt(px(12.)) |
| 153 | .children(rows) |
| 154 | .child( |
| 155 | h_flex() |
| 156 | .w_full() |
| 157 | .px(px(20.)) |
| 158 | .py(px(10.)) |
| 159 | .gap(px(12.)) |
| 160 | .items_center() |
| 161 | .child( |
| 162 | v_flex() |
| 163 | .flex_1() |
| 164 | .child( |
| 165 | div() |
| 166 | .text_size(px(14.)) |
| 167 | .font_weight(FontWeight::SEMIBOLD) |
| 168 | .text_color(theme::text()) |
| 169 | .child("Position"), |
| 170 | ) |
| 171 | .child( |
| 172 | div() |
| 173 | .text_size(px(12.)) |
| 174 | .text_color(theme::text_muted()) |
| 175 | .child("Corner of the main screen"), |
| 176 | ), |
| 177 | ) |
| 178 | .child(button(corner.label()).on_click(move |_, _, cx| { |
| 179 | position_center.update(cx, |center, cx| { |
| 180 | center.update_settings(cx, |settings| { |
| 181 | settings.corner = settings.corner.next() |
| 182 | }) |
| 183 | }); |
| 184 | })), |
| 185 | ) |
| 186 | .child( |
| 187 | h_flex() |
| 188 | .w_full() |
| 189 | .px(px(20.)) |
| 190 | .pt(px(12.)) |
| 191 | .child(button("Send test notification").on_click(move |_, _, cx| { |
| 192 | test_center.update(cx, |center, cx| center.send_test_notification(cx)); |
| 193 | })), |
| 194 | ) |
| 195 | } |
| 196 | } |