| 1 | use gpui_kit::component::{h_flex, v_flex}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::rows::{Choice, caption, control_row, select, text_button}; |
| 5 | use crate::notify::NotificationCenter; |
| 6 | use crate::notify::sounds::{Ringtone, RingtoneCallType}; |
| 7 | |
| 8 | pub fn render(center: &Entity<NotificationCenter>, cx: &App) -> Div { |
| 9 | let ringtones = center.read(cx).settings().sounds.ringtones; |
| 10 | let rows = RingtoneCallType::ALL |
| 11 | .into_iter() |
| 12 | .filter(|call_type| { |
| 13 | *call_type == RingtoneCallType::Regular || ringtones.get(*call_type) != Ringtone::Ring |
| 14 | }) |
| 15 | .map(|call_type| ringtone_row(center, call_type, ringtones.get(call_type))); |
| 16 | v_flex().w_full().child(caption("RINGTONES")).children(rows) |
| 17 | } |
| 18 | |
| 19 | fn ringtone_row( |
| 20 | center: &Entity<NotificationCenter>, |
| 21 | call_type: RingtoneCallType, |
| 22 | current: Ringtone, |
| 23 | ) -> Div { |
| 24 | let choices = Ringtone::ALL |
| 25 | .iter() |
| 26 | .map(|ringtone| { |
| 27 | let (center, ringtone) = (center.clone(), *ringtone); |
| 28 | Choice::new(ringtone.label(), ringtone == current, move |cx| { |
| 29 | center.update(cx, |center, cx| { |
| 30 | center.update_settings(cx, |settings| { |
| 31 | settings.sounds.ringtones.set(call_type, ringtone) |
| 32 | }); |
| 33 | center.preview_sound(ringtone.file_name(), cx); |
| 34 | }) |
| 35 | }) |
| 36 | }) |
| 37 | .collect(); |
| 38 | let (id, preview_id) = ringtone_ids(call_type); |
| 39 | let preview_center = center.clone(); |
| 40 | control_row( |
| 41 | call_type.label(), |
| 42 | "", |
| 43 | h_flex() |
| 44 | .gap(px(8.)) |
| 45 | .items_center() |
| 46 | .child(select(id, current.label(), false, choices)) |
| 47 | .child( |
| 48 | text_button(preview_id, "Preview").on_click(move |_, _, cx| { |
| 49 | preview_center.update(cx, |center, cx| { |
| 50 | center.preview_sound(current.file_name(), cx) |
| 51 | }) |
| 52 | }), |
| 53 | ), |
| 54 | ) |
| 55 | } |
| 56 | |
| 57 | fn ringtone_ids(call_type: RingtoneCallType) -> (&'static str, &'static str) { |
| 58 | match call_type { |
| 59 | RingtoneCallType::Regular => ("ringtone-regular", "ringtone-regular-preview"), |
| 60 | RingtoneCallType::CallGroup => ("ringtone-call-group", "ringtone-call-group-preview"), |
| 61 | RingtoneCallType::CallQueue => ("ringtone-call-queue", "ringtone-call-queue-preview"), |
| 62 | RingtoneCallType::Delegate => ("ringtone-delegate", "ringtone-delegate-preview"), |
| 63 | } |
| 64 | } |