i18n: make localization context non-global

- Simplify Localization{Context,Manager} to just Localization
- Fixed a bunch of lifetime issueo
- Removed all Arcs and Locks
- Removed globals
  * widgets now need access to &mut Localization for i18n

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-06-29 11:05:31 -07:00
parent d1e222f732
commit 3d4db820b4
47 changed files with 1414 additions and 1166 deletions

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::ui::{Preview, PreviewConfig};
use egui::{Align, Button, CornerRadius, Frame, Id, Layout, Margin, Rgba, RichText, Ui, Vec2};
use enostr::{RelayPool, RelayStatus};
use notedeck::{tr, NotedeckTextStyle, RelayAction};
use notedeck::{tr, Localization, NotedeckTextStyle, RelayAction};
use notedeck_ui::app_images;
use notedeck_ui::{colors::PINK, padding};
use tracing::debug;
@@ -13,6 +13,7 @@ use super::widgets::styled_button;
pub struct RelayView<'a> {
pool: &'a RelayPool,
id_string_map: &'a mut HashMap<Id, String>,
i18n: &'a mut Localization,
}
impl RelayView<'_> {
@@ -26,7 +27,7 @@ impl RelayView<'_> {
ui.horizontal(|ui| {
ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
ui.label(
RichText::new(tr!("Relays", "Label for relay list section"))
RichText::new(tr!(self.i18n, "Relays", "Label for relay list section"))
.text_style(NotedeckTextStyle::Heading2.text_style()),
);
});
@@ -53,10 +54,15 @@ impl RelayView<'_> {
}
impl<'a> RelayView<'a> {
pub fn new(pool: &'a RelayPool, id_string_map: &'a mut HashMap<Id, String>) -> Self {
pub fn new(
pool: &'a RelayPool,
id_string_map: &'a mut HashMap<Id, String>,
i18n: &'a mut Localization,
) -> Self {
RelayView {
pool,
id_string_map,
i18n,
}
}
@@ -65,7 +71,7 @@ impl<'a> RelayView<'a> {
}
/// Show the current relays and return a relay the user selected to delete
fn show_relays(&'a self, ui: &mut Ui) -> Option<String> {
fn show_relays(&mut self, ui: &mut Ui) -> Option<String> {
let mut relay_to_remove = None;
for (index, relay_info) in get_relay_infos(self.pool).iter().enumerate() {
ui.add_space(8.0);
@@ -107,7 +113,7 @@ impl<'a> RelayView<'a> {
relay_to_remove = Some(relay_info.relay_url.to_string());
};
show_connection_status(ui, relay_info.status);
show_connection_status(ui, self.i18n, relay_info.status);
});
});
});
@@ -123,7 +129,7 @@ impl<'a> RelayView<'a> {
match self.id_string_map.get(&id) {
None => {
ui.with_layout(Layout::top_down(Align::Min), |ui| {
let relay_button = add_relay_button();
let relay_button = add_relay_button(self.i18n);
if ui.add(relay_button).clicked() {
debug!("add relay clicked");
self.id_string_map
@@ -151,6 +157,7 @@ impl<'a> RelayView<'a> {
let text_edit = egui::TextEdit::singleline(text_buffer)
.hint_text(
RichText::new(tr!(
self.i18n,
"Enter the relay here",
"Placeholder for relay input field"
))
@@ -163,7 +170,10 @@ impl<'a> RelayView<'a> {
ui.add(text_edit);
ui.add_space(8.0);
if ui
.add_sized(egui::vec2(50.0, 40.0), add_relay_button2(is_enabled))
.add_sized(
egui::vec2(50.0, 40.0),
add_relay_button2(self.i18n, is_enabled),
)
.clicked()
{
self.id_string_map.remove(&id) // remove and return the value
@@ -175,10 +185,10 @@ impl<'a> RelayView<'a> {
}
}
fn add_relay_button() -> Button<'static> {
fn add_relay_button(i18n: &mut Localization) -> Button<'static> {
Button::image_and_text(
app_images::add_relay_image().fit_to_exact_size(Vec2::new(48.0, 48.0)),
RichText::new(tr!("Add relay", "Button label to add a relay"))
RichText::new(tr!(i18n, "Add relay", "Button label to add a relay"))
.size(16.0)
// TODO: this color should not be hard coded. Find some way to add it to the visuals
.color(PINK),
@@ -186,9 +196,9 @@ fn add_relay_button() -> Button<'static> {
.frame(false)
}
fn add_relay_button2(is_enabled: bool) -> impl egui::Widget + 'static {
fn add_relay_button2<'a>(i18n: &'a mut Localization, is_enabled: bool) -> impl egui::Widget + 'a {
move |ui: &mut egui::Ui| -> egui::Response {
let add_text = tr!("Add", "Button label to add a relay");
let add_text = tr!(i18n, "Add", "Button label to add a relay");
let button_widget = styled_button(add_text.as_str(), notedeck_ui::colors::PINK);
ui.add_enabled(is_enabled, button_widget)
}
@@ -219,7 +229,7 @@ fn relay_frame(ui: &mut Ui) -> Frame {
.stroke(ui.style().visuals.noninteractive().bg_stroke)
}
fn show_connection_status(ui: &mut Ui, status: RelayStatus) {
fn show_connection_status(ui: &mut Ui, i18n: &mut Localization, status: RelayStatus) {
let fg_color = match status {
RelayStatus::Connected => ui.visuals().selection.bg_fill,
RelayStatus::Connecting => ui.visuals().warn_fg_color,
@@ -228,9 +238,11 @@ fn show_connection_status(ui: &mut Ui, status: RelayStatus) {
let bg_color = egui::lerp(Rgba::from(fg_color)..=Rgba::BLACK, 0.8).into();
let label_text = match status {
RelayStatus::Connected => tr!("Connected", "Status label for connected relay"),
RelayStatus::Connecting => tr!("Connecting...", "Status label for connecting relay"),
RelayStatus::Disconnected => tr!("Not Connected", "Status label for disconnected relay"),
RelayStatus::Connected => tr!(i18n, "Connected", "Status label for connected relay"),
RelayStatus::Connecting => tr!(i18n, "Connecting...", "Status label for connecting relay"),
RelayStatus::Disconnected => {
tr!(i18n, "Not Connected", "Status label for disconnected relay")
}
};
let frame = Frame::new()
@@ -290,7 +302,7 @@ mod preview {
fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) -> Option<AppAction> {
self.pool.try_recv();
let mut id_string_map = HashMap::new();
RelayView::new(app.pool, &mut id_string_map).ui(ui);
RelayView::new(app.pool, &mut id_string_map, app.i18n).ui(ui);
None
}
}