refactor(settings): add settings sections methods

This commit is contained in:
Fernando López Guevara
2025-07-29 21:09:33 -03:00
parent 0dcf70bc15
commit 5848f1c355

View File

@@ -130,6 +130,29 @@ pub struct SettingsView<'a> {
img_cache: &'a mut Images, img_cache: &'a mut Images,
} }
fn small_richtext(i18n: &'_ mut Localization, text: &str, comment: &str) -> RichText {
RichText::new(tr!(i18n, text, comment)).text_style(NotedeckTextStyle::Small.text_style())
}
fn settings_group<S>(ui: &mut egui::Ui, title: S, contents: impl FnOnce(&mut egui::Ui))
where
S: Into<String>,
{
Frame::group(ui.style())
.fill(ui.style().visuals.widgets.open.bg_fill)
.inner_margin(10.0)
.show(ui, |ui| {
ui.label(RichText::new(title).text_style(NotedeckTextStyle::Body.text_style()));
ui.separator();
ui.vertical(|ui| {
ui.spacing_mut().item_spacing = vec2(10.0, 10.0);
contents(ui)
});
});
}
impl<'a> SettingsView<'a> { impl<'a> SettingsView<'a> {
pub fn new( pub fn new(
img_cache: &'a mut Images, img_cache: &'a mut Images,
@@ -180,46 +203,25 @@ impl<'a> SettingsView<'a> {
}.to_string() }.to_string()
} }
pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<SettingsAction> { pub fn appearance_section(&mut self, ui: &mut egui::Ui) -> Option<SettingsAction> {
let id = ui.id();
let mut action = None; let mut action = None;
let title = tr!(
Frame::default()
.inner_margin(Margin::symmetric(10, 10))
.show(ui, |ui| {
Frame::group(ui.style())
.fill(ui.style().visuals.widgets.open.bg_fill)
.inner_margin(10.0)
.show(ui, |ui| {
ui.vertical(|ui| {
ui.label(
RichText::new(tr!(
self.i18n, self.i18n,
"Appearance", "Appearance",
"Label for appearance settings section" "Label for appearance settings section",
))
.text_style(NotedeckTextStyle::Body.text_style()),
); );
ui.separator(); settings_group(ui, title, |ui| {
ui.spacing_mut().item_spacing = vec2(10.0, 10.0);
let current_zoom = ui.ctx().zoom_factor(); let current_zoom = ui.ctx().zoom_factor();
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label( ui.label(small_richtext(
RichText::new(tr!(
self.i18n, self.i18n,
"Zoom Level:", "Zoom Level:",
"Label for zoom level, Appearance settings section" "Label for zoom level, Appearance settings section",
)) ));
.text_style(NotedeckTextStyle::Small.text_style()),
);
if ui if ui
.button( .button(RichText::new("-").text_style(NotedeckTextStyle::Small.text_style()))
RichText::new("-")
.text_style(NotedeckTextStyle::Small.text_style()),
)
.clicked() .clicked()
{ {
let new_zoom = (current_zoom - 0.1).max(0.1); let new_zoom = (current_zoom - 0.1).max(0.1);
@@ -232,10 +234,7 @@ impl<'a> SettingsView<'a> {
); );
if ui if ui
.button( .button(RichText::new("+").text_style(NotedeckTextStyle::Small.text_style()))
RichText::new("+")
.text_style(NotedeckTextStyle::Small.text_style()),
)
.clicked() .clicked()
{ {
let new_zoom = (current_zoom + 0.1).min(10.0); let new_zoom = (current_zoom + 0.1).min(10.0);
@@ -243,14 +242,11 @@ impl<'a> SettingsView<'a> {
}; };
if ui if ui
.button( .button(small_richtext(
RichText::new(tr!(
self.i18n, self.i18n,
"Reset", "Reset",
"Label for reset zoom level, Appearance settings section" "Label for reset zoom level, Appearance settings section",
)) ))
.text_style(NotedeckTextStyle::Small.text_style()),
)
.clicked() .clicked()
{ {
action = Some(SettingsAction::SetZoomFactor(1.0)); action = Some(SettingsAction::SetZoomFactor(1.0));
@@ -258,28 +254,23 @@ impl<'a> SettingsView<'a> {
}); });
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label( ui.label(small_richtext(
RichText::new(tr!(
self.i18n, self.i18n,
"Language:", "Language:",
"Label for language, Appearance settings section" "Label for language, Appearance settings section",
)) ));
.text_style(NotedeckTextStyle::Small.text_style()),
);
ComboBox::from_label("") ComboBox::from_label("")
.selected_text(self.get_selected_language_name()) .selected_text(self.get_selected_language_name())
.show_ui(ui, |ui| { .show_ui(ui, |ui| {
for lang in self.i18n.get_available_locales() { for lang in self.i18n.get_available_locales() {
let name = self.i18n let name = self
.i18n
.get_locale_native_name(lang) .get_locale_native_name(lang)
.map(|s| s.to_owned()) .map(|s| s.to_owned())
.unwrap_or_else(|| lang.to_string()); .unwrap_or_else(|| lang.to_string());
if ui if ui
.selectable_value( .selectable_value(self.selected_language, lang.to_string(), name)
self.selected_language,
lang.to_string(),
name,
)
.clicked() .clicked()
{ {
action = Some(SettingsAction::SetLocale(lang.to_owned())) action = Some(SettingsAction::SetLocale(lang.to_owned()))
@@ -289,24 +280,20 @@ impl<'a> SettingsView<'a> {
}); });
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label( ui.label(small_richtext(
RichText::new(tr!(
self.i18n, self.i18n,
"Theme:", "Theme:",
"Label for theme, Appearance settings section" "Label for theme, Appearance settings section",
)) ));
.text_style(NotedeckTextStyle::Small.text_style()),
);
if ui if ui
.selectable_value( .selectable_value(
self.theme, self.theme,
"Light".into(), "Light".into(),
RichText::new(tr!( small_richtext(
self.i18n, self.i18n,
"Light", "Light",
"Label for Theme Light, Appearance settings section" "Label for Theme Light, Appearance settings section",
)) ),
.text_style(NotedeckTextStyle::Small.text_style()),
) )
.clicked() .clicked()
{ {
@@ -316,12 +303,11 @@ impl<'a> SettingsView<'a> {
.selectable_value( .selectable_value(
self.theme, self.theme,
"Dark".into(), "Dark".into(),
RichText::new(tr!( small_richtext(
self.i18n, self.i18n,
"Dark", "Dark",
"Label for Theme Dark, Appearance settings section" "Label for Theme Dark, Appearance settings section",
)) ),
.text_style(NotedeckTextStyle::Small.text_style()),
) )
.clicked() .clicked()
{ {
@@ -329,39 +315,23 @@ impl<'a> SettingsView<'a> {
} }
}); });
}); });
});
ui.add_space(5.0); action
}
Frame::group(ui.style())
.fill(ui.style().visuals.widgets.open.bg_fill)
.inner_margin(10.0)
.show(ui, |ui| {
ui.label(
RichText::new(tr!(
self.i18n,
"Storage",
"Label for storage settings section"
))
.text_style(NotedeckTextStyle::Body.text_style()),
);
ui.separator();
ui.vertical(|ui| {
ui.spacing_mut().item_spacing = vec2(10.0, 10.0);
pub fn storage_section(&mut self, ui: &mut egui::Ui) -> Option<SettingsAction> {
let id = ui.id();
let mut action: Option<SettingsAction> = None;
let title = tr!(self.i18n, "Storage", "Label for storage settings section");
settings_group(ui, title, |ui| {
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {
let static_imgs_size = self let static_imgs_size = self.img_cache.static_imgs.cache_size.lock().unwrap();
.img_cache
.static_imgs
.cache_size
.lock()
.unwrap();
let gifs_size = self.img_cache.gifs.cache_size.lock().unwrap(); let gifs_size = self.img_cache.gifs.cache_size.lock().unwrap();
ui.label( ui.label(
RichText::new(format!("{} {}", RichText::new(format!(
"{} {}",
tr!( tr!(
self.i18n, self.i18n,
"Image cache size:", "Image cache size:",
@@ -370,8 +340,7 @@ impl<'a> SettingsView<'a> {
format_size( format_size(
[static_imgs_size, gifs_size] [static_imgs_size, gifs_size]
.iter() .iter()
.fold(0_u64, |acc, cur| acc .fold(0_u64, |acc, cur| acc + cur.unwrap_or_default())
+ cur.unwrap_or_default())
) )
)) ))
.text_style(NotedeckTextStyle::Small.text_style()), .text_style(NotedeckTextStyle::Small.text_style()),
@@ -379,19 +348,24 @@ impl<'a> SettingsView<'a> {
ui.end_row(); ui.end_row();
if !notedeck::ui::is_compiled_as_mobile() && if !notedeck::ui::is_compiled_as_mobile()
ui.button(RichText::new(tr!(self.i18n, "View folder", "Label for view folder button, Storage settings section")) && ui
.text_style(NotedeckTextStyle::Small.text_style())).clicked() { .button(small_richtext(
self.i18n,
"View folder",
"Label for view folder button, Storage settings section",
))
.clicked()
{
action = Some(SettingsAction::OpenCacheFolder); action = Some(SettingsAction::OpenCacheFolder);
} }
let clearcache_resp = ui.button( let clearcache_resp = ui.button(
RichText::new(tr!( small_richtext(
self.i18n, self.i18n,
"Clear cache", "Clear cache",
"Label for clear cache button, Storage settings section" "Label for clear cache button, Storage settings section",
)) )
.text_style(NotedeckTextStyle::Small.text_style())
.color(Color32::LIGHT_RED), .color(Color32::LIGHT_RED),
); );
@@ -412,55 +386,42 @@ impl<'a> SettingsView<'a> {
confirm_pressed = true; confirm_pressed = true;
} }
if confirm_resp.clicked() || ui.button(tr!( if confirm_resp.clicked()
|| ui
.button(tr!(
self.i18n, self.i18n,
"Cancel", "Cancel",
"Label for cancel clear cache, Storage settings section" "Label for cancel clear cache, Storage settings section"
)).clicked() { ))
.clicked()
{
ui.data_mut(|d| d.insert_temp(id_clearcache, false)); ui.data_mut(|d| d.insert_temp(id_clearcache, false));
} }
}); });
if confirm_pressed { if confirm_pressed {
action = Some(SettingsAction::ClearCacheFolder); action = Some(SettingsAction::ClearCacheFolder);
} else if !confirm_pressed } else if !confirm_pressed && clearcache_resp.clicked_elsewhere() {
&& clearcache_resp.clicked_elsewhere()
{
ui.data_mut(|d| d.insert_temp(id_clearcache, false)); ui.data_mut(|d| d.insert_temp(id_clearcache, false));
} }
}; };
}); });
}); });
});
ui.add_space(5.0); action
}
Frame::group(ui.style()) fn other_options_section(&mut self, ui: &mut egui::Ui) -> Option<SettingsAction> {
.fill(ui.style().visuals.widgets.open.bg_fill) let mut action = None;
.inner_margin(10.0)
.show(ui, |ui| {
ui.label(
RichText::new(tr!(
self.i18n,
"Others",
"Label for others settings section"
))
.text_style(NotedeckTextStyle::Body.text_style()),
);
ui.separator();
ui.vertical(|ui| {
ui.spacing_mut().item_spacing = vec2(10.0, 10.0);
let title = tr!(self.i18n, "Others", "Label for others settings section");
settings_group(ui, title, |ui| {
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {
ui.label( ui.label(small_richtext(
RichText::new(
tr!(
self.i18n, self.i18n,
"Show source client", "Show source client",
"Label for Show source client, others settings section" "Label for Show source client, others settings section",
)) ));
.text_style(NotedeckTextStyle::Small.text_style()),
);
for option in [ for option in [
ShowSourceClientOption::Hide, ShowSourceClientOption::Hide,
@@ -473,8 +434,7 @@ impl<'a> SettingsView<'a> {
.selectable_value( .selectable_value(
self.show_note_client, self.show_note_client,
option, option,
RichText::new(label) RichText::new(label).text_style(NotedeckTextStyle::Small.text_style()),
.text_style(NotedeckTextStyle::Small.text_style()),
) )
.changed() .changed()
{ {
@@ -483,26 +443,57 @@ impl<'a> SettingsView<'a> {
} }
}); });
}); });
});
ui.add_space(10.0); action
}
fn manage_relays_section(&mut self, ui: &mut egui::Ui) -> Option<SettingsAction> {
let mut action = None;
if ui if ui
.add_sized( .add_sized(
[ui.available_width(), 30.0], [ui.available_width(), 30.0],
Button::new( Button::new(small_richtext(
RichText::new(tr!(
self.i18n, self.i18n,
"Configure relays", "Configure relays",
"Label for configure relays, settings section" "Label for configure relays, settings section",
)) )),
.text_style(NotedeckTextStyle::Small.text_style()),
),
) )
.clicked() .clicked()
{ {
action = Some(SettingsAction::OpenRelays); action = Some(SettingsAction::OpenRelays);
} }
action
}
pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<SettingsAction> {
let mut action: Option<SettingsAction> = None;
Frame::default()
.inner_margin(Margin::symmetric(10, 10))
.show(ui, |ui| {
if let Some(new_action) = self.appearance_section(ui) {
action = Some(new_action);
}
ui.add_space(5.0);
if let Some(new_action) = self.storage_section(ui) {
action = Some(new_action);
}
ui.add_space(5.0);
if let Some(new_action) = self.other_options_section(ui) {
action = Some(new_action);
}
ui.add_space(10.0);
if let Some(new_action) = self.manage_relays_section(ui) {
action = Some(new_action);
}
}); });
action action