forge
Explore
Log in

Don't you dare go Hollow

Settings sections are one enum (SettingsSection: ALL/id/label) used by the page, Ctrl+K and setting rows

Browse files at this commit

6357a4550eed3ff232b23947c95081b1eaab242fAhmet Kaan GümüşParent: e47e0f7
  • refactor
    • settings sections are one enum (SettingsSection: ALL/id/label) used by the page, Ctrl+K and setting rows
    • pages select sections by id instead of position, so hidden sections (logged out, missing permissions) no longer shift the selection
Show full message
♻️ refactor: settings sections are one enum (SettingsSection: ALL/id/label) used by the page, Ctrl+K and setting rows; pages select sections by id instead of position, so hidden sections (logged out, missing permissions) no longer shift the selection
crates/client/src/gui.rsmodified+1 -1
@@ -904,7 +904,7 @@ impl Yumush {
904 }904 }
905 905
906 fn show_view(&mut self, view: View, window: &mut Window, cx: &mut Context<Self>) {906 fn show_view(&mut self, view: View, window: &mut Window, cx: &mut Context<Self>) {
907- self.chat.section = 0;907+ self.chat.section = None;
908 if self.chat.view == View::Settings && view != View::Settings {908 if self.chat.view == View::Settings && view != View::Settings {
909 self.leave_settings();909 self.leave_settings();
910 }910 }
crates/client/src/gui/chat.rsmodified+2 -2
@@ -173,7 +173,7 @@ pub struct Chat {
173 highlight: Option<(Id, Instant)>,173 highlight: Option<(Id, Instant)>,
174 glide: Option<(Id, gpui_kit::Pixels, Instant)>,174 glide: Option<(Id, gpui_kit::Pixels, Instant)>,
175 pub(super) view: View,175 pub(super) view: View,
176- pub(super) section: usize,176+ pub(super) section: Option<&'static str>,
177 pub(super) username_input: Entity<InputState>,177 pub(super) username_input: Entity<InputState>,
178 pub(super) community_name_input: Entity<InputState>,178 pub(super) community_name_input: Entity<InputState>,
179 pub(super) status_input: Entity<InputState>,179 pub(super) status_input: Entity<InputState>,
@@ -449,7 +449,7 @@ impl Chat {
449 sidebar_collapsed: false,449 sidebar_collapsed: false,
450 sidebar_toggled: None,450 sidebar_toggled: None,
451 view: View::Chat,451 view: View::Chat,
452- section: 0,452+ section: None,
453 username_input: editable_input(w().chat.username, Yumush::save_username, window, cx),453 username_input: editable_input(w().chat.username, Yumush::save_username, window, cx),
454 community_name_input: editable_input(454 community_name_input: editable_input(
455 w().chat.community_name,455 w().chat.community_name,
crates/client/src/gui/community_settings.rsmodified+12 -3
@@ -95,7 +95,8 @@ impl Yumush {
95 let mut sections = Vec::new();95 let mut sections = Vec::new();
96 96
97 if let Some(community) = community.filter(|_| permission::can_rename_community(actor)) {97 if let Some(community) = community.filter(|_| permission::can_rename_community(actor)) {
98- sections.push(Section(98+ sections.push(Section::new(
99+ "profile",
99 w().community_settings.profile,100 w().community_settings.profile,
100 picture_card(101 picture_card(
101 Picture {102 Picture {
@@ -121,6 +122,7 @@ impl Yumush {
121 }122 }
122 if permission::can_rename_community(actor) {123 if permission::can_rename_community(actor) {
123 sections.push(section(124 sections.push(section(
125+ "general",
124 w().community_settings.general,126 w().community_settings.general,
125 palette,127 palette,
126 vec![text_row(128 vec![text_row(
@@ -139,6 +141,7 @@ impl Yumush {
139 }141 }
140 if let Some(community) = community.filter(|_| permission::can_manage_voice(actor)) {142 if let Some(community) = community.filter(|_| permission::can_manage_voice(actor)) {
141 sections.push(section(143 sections.push(section(
144+ "voice",
142 w().community_settings.voice,145 w().community_settings.voice,
143 palette,146 palette,
144 vec![row(147 vec![row(
@@ -177,13 +180,14 @@ impl Yumush {
177 }180 }
178 if !moderation.is_empty() {181 if !moderation.is_empty() {
179 sections.push(section(182 sections.push(section(
183+ "moderation",
180 w().community_settings.moderation,184 w().community_settings.moderation,
181 palette,185 palette,
182 moderation,186 moderation,
183 ));187 ));
184 }188 }
185 if permission::can_delete_community(actor) {189 if permission::can_delete_community(actor) {
186- sections.push(danger(vec![190+ sections.push(danger("danger_zone", vec![
187 delete_control(191 delete_control(
188 "delete_community_button",192 "delete_community_button",
189 w().community_settings.delete_community,193 w().community_settings.delete_community,
@@ -196,7 +200,12 @@ impl Yumush {
196 ]));200 ]));
197 }201 }
198 if sections.is_empty() {202 if sections.is_empty() {
199- sections.push(section(w().community_settings.general, palette, Vec::new()));203+ sections.push(section(
204+ "general",
205+ w().community_settings.general,
206+ palette,
207+ Vec::new(),
208+ ));
200 }209 }
201 210
202 page(211 page(
crates/client/src/gui/jump.rsmodified+11 -12
@@ -18,7 +18,7 @@ use super::{
18 community_icon, community_name,18 community_icon, community_name,
19 parts::{Ink, Text, band, words},19 parts::{Ink, Text, band, words},
20 presence_avatar,20 presence_avatar,
21- settings_page::{SETTINGS_IDS, setting_rows, settings_titles},21+ settings_page::{SettingsSection, setting_rows},
22 style::{Theme, Tokens, my_hue, radius, size, space, stroke, text},22 style::{Theme, Tokens, my_hue, radius, size, space, stroke, text},
23 voice_settings::modes,23 voice_settings::modes,
24 widget::bare_input,24 widget::bare_input,
@@ -32,7 +32,7 @@ pub(super) enum Place {
32 Person(Id),32 Person(Id),
33 Notes,33 Notes,
34 Friends,34 Friends,
35- Settings(usize),35+ Settings(SettingsSection),
36 Setting(usize),36 Setting(usize),
37 Builder(BuilderTab),37 Builder(BuilderTab),
38 NewCommunity,38 NewCommunity,
@@ -96,7 +96,7 @@ impl Place {
96 Place::Person(id) => format!("person:{id}"),96 Place::Person(id) => format!("person:{id}"),
97 Place::Notes => "notes".into(),97 Place::Notes => "notes".into(),
98 Place::Friends => "friends".into(),98 Place::Friends => "friends".into(),
99- Place::Settings(index) => format!("settings:{}", SETTINGS_IDS[index]),99+ Place::Settings(section) => format!("settings:{}", section.id()),
100 Place::Setting(index) => format!("setting:{index}"),100 Place::Setting(index) => format!("setting:{index}"),
101 Place::Builder(tab) => format!("builder:{}", tab.id()),101 Place::Builder(tab) => format!("builder:{}", tab.id()),
102 Place::NewCommunity => "new_community".into(),102 Place::NewCommunity => "new_community".into(),
@@ -181,13 +181,12 @@ impl Yumush {
181 ]181 ]
182 .into_iter()182 .into_iter()
183 .chain(183 .chain(
184- settings_titles()184+ SettingsSection::ALL
185 .into_iter()185 .into_iter()
186- .enumerate()186+ .map(|section| {
187- .map(|(index, title)| {
188 Row::new(187 Row::new(
189- Place::Settings(index),188+ Place::Settings(section),
190- title.to_string(),189+ section.label().to_string(),
191 Glyph::Icon(IconName::Settings2),190 Glyph::Icon(IconName::Settings2),
192 )191 )
193 .hinted(w().jump.hint_settings)192 .hinted(w().jump.hint_settings)
@@ -203,7 +202,7 @@ impl Yumush {
203 label.to_string(),202 label.to_string(),
204 Glyph::Icon(IconName::Settings2),203 Glyph::Icon(IconName::Settings2),
205 )204 )
206- .hinted(settings_titles()[section])205+ .hinted(section.label())
207 }),206 }),
208 )207 )
209 .chain(208 .chain(
@@ -414,13 +413,13 @@ impl Yumush {
414 }413 }
415 Place::Notes => self.open_notes(window, cx),414 Place::Notes => self.open_notes(window, cx),
416 Place::Friends => self.open_friends(window, cx),415 Place::Friends => self.open_friends(window, cx),
417- Place::Settings(index) => {416+ Place::Settings(section) => {
418 self.show_view(View::Settings, window, cx);417 self.show_view(View::Settings, window, cx);
419- self.chat.section = index;418+ self.chat.section = Some(section.id());
420 }419 }
421 Place::Setting(index) => {420 Place::Setting(index) => {
422 self.show_view(View::Settings, window, cx);421 self.show_view(View::Settings, window, cx);
423- self.chat.section = setting_rows()[index].0;422+ self.chat.section = Some(setting_rows()[index].0.id());
424 }423 }
425 Place::Builder(tab) => {424 Place::Builder(tab) => {
426 self.show_view(View::Builder, window, cx);425 self.show_view(View::Builder, window, cx);
crates/client/src/gui/settings_page.rsmodified+111 -62
@@ -29,54 +29,75 @@ use crate::{
29 share::encode::capabilities,29 share::encode::capabilities,
30 };30 };
31 31
32-pub(super) const SETTINGS_IDS: [&str; 5] = [32+#[derive(Clone, Copy, PartialEq)]
33- "profile",33+pub(super) enum SettingsSection {
34- "voice",34+ Profile,
35- "camera_share",35+ Voice,
36- "appearance",36+ CameraShare,
37- "danger_zone",37+ Appearance,
38-];38+ DangerZone,
39-
40-pub(super) fn settings_titles() -> [&'static str; SETTINGS_IDS.len()] {
41- let w = &w().settings_page;
42- [
43- w.profile,
44- w.voice,
45- w.camera_share,
46- w.appearance,
47- w.danger_zone,
48- ]
49 }39 }
50 40
51-pub(super) fn setting_rows() -> [(usize, &'static str); 25] {41+impl SettingsSection {
42+ pub(super) const ALL: [Self; 5] = [
43+ Self::Profile,
44+ Self::Voice,
45+ Self::CameraShare,
46+ Self::Appearance,
47+ Self::DangerZone,
48+ ];
49+
50+ pub(super) fn id(self) -> &'static str {
51+ match self {
52+ Self::Profile => "profile",
53+ Self::Voice => "voice",
54+ Self::CameraShare => "camera_share",
55+ Self::Appearance => "appearance",
56+ Self::DangerZone => "danger_zone",
57+ }
58+ }
59+
60+ pub(super) fn label(self) -> &'static str {
61+ let w = &w().settings_page;
62+ match self {
63+ Self::Profile => w.profile,
64+ Self::Voice => w.voice,
65+ Self::CameraShare => w.camera_share,
66+ Self::Appearance => w.appearance,
67+ Self::DangerZone => w.danger_zone,
68+ }
69+ }
70+}
71+
72+pub(super) fn setting_rows() -> [(SettingsSection, &'static str); 25] {
52 let s = &w().settings_page;73 let s = &w().settings_page;
53 let v = &w().voice_settings;74 let v = &w().voice_settings;
54 [75 [
55- (0, s.name),76+ (SettingsSection::Profile, s.name),
56- (0, s.status),77+ (SettingsSection::Profile, s.status),
57- (0, s.presence),78+ (SettingsSection::Profile, s.presence),
58- (0, s.change_picture),79+ (SettingsSection::Profile, s.change_picture),
59- (4, s.delete_account),80+ (SettingsSection::DangerZone, s.delete_account),
60- (1, v.input_device),81+ (SettingsSection::Voice, v.input_device),
61- (1, v.output_device),82+ (SettingsSection::Voice, v.output_device),
62- (1, v.input_volume),83+ (SettingsSection::Voice, v.input_volume),
63- (1, v.output_volume),84+ (SettingsSection::Voice, v.output_volume),
64- (1, v.mode),85+ (SettingsSection::Voice, v.mode),
65- (1, v.hear_yourself),86+ (SettingsSection::Voice, v.hear_yourself),
66- (1, v.play_sound),87+ (SettingsSection::Voice, v.play_sound),
67- (1, v.sensitivity),88+ (SettingsSection::Voice, v.sensitivity),
68- (1, v.key),89+ (SettingsSection::Voice, v.key),
69- (2, s.camera),90+ (SettingsSection::CameraShare, s.camera),
70- (2, s.screen_share),91+ (SettingsSection::CameraShare, s.screen_share),
71- (2, s.codec),92+ (SettingsSection::CameraShare, s.codec),
72- (2, s.share_audio),93+ (SettingsSection::CameraShare, s.share_audio),
73- (3, s.theme),94+ (SettingsSection::Appearance, s.theme),
74- (3, s.font_size),95+ (SettingsSection::Appearance, s.font_size),
75- (3, s.language),96+ (SettingsSection::Appearance, s.language),
76- (3, s.close_to_tray),97+ (SettingsSection::Appearance, s.close_to_tray),
77- (3, s.launch),98+ (SettingsSection::Appearance, s.launch),
78- (3, s.clock_seconds),99+ (SettingsSection::Appearance, s.clock_seconds),
79- (3, s.your_hue),100+ (SettingsSection::Appearance, s.your_hue),
80 ]101 ]
81 }102 }
82 103
@@ -90,8 +111,9 @@ impl Yumush {
90 let mut sections = Vec::new();111 let mut sections = Vec::new();
91 if let Some(user) = self.user.as_ref() {112 if let Some(user) = self.user.as_ref() {
92 let presence_entity = entity.clone();113 let presence_entity = entity.clone();
93- sections.push(Section(114+ sections.push(Section::new(
94- settings_titles()[0],115+ SettingsSection::Profile.id(),
116+ SettingsSection::Profile.label(),
95 div()117 div()
96 .flex()118 .flex()
97 .flex_col()119 .flex_col()
@@ -166,12 +188,14 @@ impl Yumush {
166 ));188 ));
167 }189 }
168 sections.push(section(190 sections.push(section(
169- settings_titles()[1],191+ SettingsSection::Voice.id(),
192+ SettingsSection::Voice.label(),
170 palette,193 palette,
171 self.voice_rows(entity, palette, cx),194 self.voice_rows(entity, palette, cx),
172 ));195 ));
173- sections.push(Section(196+ sections.push(Section::new(
174- settings_titles()[2],197+ SettingsSection::CameraShare.id(),
198+ SettingsSection::CameraShare.label(),
175 camera_share(self, entity, palette),199 camera_share(self, entity, palette),
176 ));200 ));
177 let mut appearance = vec![201 let mut appearance = vec![
@@ -246,9 +270,14 @@ impl Yumush {
246 hue_picker(Some(style::hue_of(user.id)), entity, palette),270 hue_picker(Some(style::hue_of(user.id)), entity, palette),
247 ));271 ));
248 }272 }
249- sections.push(section(settings_titles()[3], palette, appearance));273+ sections.push(section(
274+ SettingsSection::Appearance.id(),
275+ SettingsSection::Appearance.label(),
276+ palette,
277+ appearance,
278+ ));
250 if self.user.is_some() {279 if self.user.is_some() {
251- sections.push(danger(vec![280+ sections.push(danger(SettingsSection::DangerZone.id(), vec![
252 delete_control(281 delete_control(
253 "delete_account_button",282 "delete_account_button",
254 w().settings_page.delete_account,283 w().settings_page.delete_account,
@@ -603,7 +632,17 @@ pub(super) fn picked_avatar(
603 }632 }
604 }633 }
605 634
606-pub(super) struct Section(pub &'static str, pub AnyElement);635+pub(super) struct Section {
636+ id: &'static str,
637+ title: &'static str,
638+ body: AnyElement,
639+}
640+
641+impl Section {
642+ pub(super) fn new(id: &'static str, title: &'static str, body: AnyElement) -> Self {
643+ Self { id, title, body }
644+ }
645+}
607 646
608 pub(super) fn page(647 pub(super) fn page(
609 id: &'static str,648 id: &'static str,
@@ -611,22 +650,26 @@ pub(super) fn page(
611 palette: Palette,650 palette: Palette,
612 title: impl IntoElement,651 title: impl IntoElement,
613 sections: Vec<Section>,652 sections: Vec<Section>,
614- selected: usize,653+ selected: Option<&'static str>,
615 ) -> impl IntoElement {654 ) -> impl IntoElement {
616- let selected = selected.min(sections.len().saturating_sub(1));655+ let selected = sections
656+ .iter()
657+ .position(|section| Some(section.id) == selected)
658+ .unwrap_or(0);
617 let index: Vec<_> = sections659 let index: Vec<_> = sections
618 .iter()660 .iter()
619 .enumerate()661 .enumerate()
620- .map(|(position, Section(name, _))| {662+ .map(|(position, section)| {
621 let entity = entity.clone();663 let entity = entity.clone();
664+ let key = section.id;
622 super::parts::index_item(665 super::parts::index_item(
623- gpui_kit::ElementId::Name(format!("{id}_index_{name}").into()),666+ gpui_kit::ElementId::Name(format!("{id}_index_{key}").into()),
624- *name,667+ section.title,
625 position == selected,668 position == selected,
626 palette.tokens,669 palette.tokens,
627 move |_, cx| {670 move |_, cx| {
628 entity.update(cx, |yumush, cx| {671 entity.update(cx, |yumush, cx| {
629- yumush.chat.section = position;672+ yumush.chat.section = Some(key);
630 yumush.chat.armed = None;673 yumush.chat.armed = None;
631 cx.notify();674 cx.notify();
632 })675 })
@@ -635,7 +678,7 @@ pub(super) fn page(
635 .into_any_element()678 .into_any_element()
636 })679 })
637 .collect();680 .collect();
638- let Some(Section(name, body)) = sections.into_iter().nth(selected) else {681+ let Some(Section { title: name, body, .. }) = sections.into_iter().nth(selected) else {
639 return frame(id, entity, palette, title, index, Vec::new(), Vec::new());682 return frame(id, entity, palette, title, index, Vec::new(), Vec::new());
640 };683 };
641 684
@@ -744,12 +787,18 @@ pub(super) fn frame(
744 )787 )
745 }788 }
746 789
747-pub(super) fn section(title: &'static str, palette: Palette, rows: Vec<Div>) -> Section {790+pub(super) fn section(
748- Section(title, rows_column(palette, rows))791+ id: &'static str,
792+ title: &'static str,
793+ palette: Palette,
794+ rows: Vec<Div>,
795+) -> Section {
796+ Section::new(id, title, rows_column(palette, rows).into_any_element())
749 }797 }
750 798
751-pub(super) fn danger(actions: Vec<AnyElement>) -> Section {799+pub(super) fn danger(id: &'static str, actions: Vec<AnyElement>) -> Section {
752- Section(800+ Section::new(
801+ id,
753 w().settings_page.danger_zone,802 w().settings_page.danger_zone,
754 div()803 div()
755 .ml(px(-space::SM))804 .ml(px(-space::SM))