Rename PersistState -> StateInMemory

also use IdTypeMap::insert_temp instead of insert_persisted.
The whole conception of using egui memory to share state is probably
going to be changed to a more robust solution in the future.

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-05-22 15:25:58 -04:00
committed by William Casarin
parent 194f41d39c
commit f071d59dae
5 changed files with 24 additions and 24 deletions

View File

@@ -7,8 +7,8 @@ use crate::{
use egui::{Align, Button, Frame, Id, Layout, Margin, RichText, ScrollArea, Sense, Vec2};
use super::global_popup::GlobalPopupType;
use super::persist_state::PERSISTED_ACCOUNT_MANAGEMENT;
use super::profile::preview::SimpleProfilePreview;
use super::state_in_memory::STATE_ACCOUNT_MANAGEMENT;
pub struct AccountManagementView<'a> {
account_manager: AccountManager<'a>,
@@ -49,7 +49,7 @@ impl<'a> AccountManagementView<'a> {
let maybe_remove = self.simple_preview_controller.set_profile_previews(
&self.account_manager,
ui,
PERSISTED_ACCOUNT_MANAGEMENT.get_state(ui.ctx()),
STATE_ACCOUNT_MANAGEMENT.get_state(ui.ctx()),
desktop_account_card_ui(),
);
@@ -66,7 +66,7 @@ impl<'a> AccountManagementView<'a> {
let maybe_remove = self.simple_preview_controller.set_profile_previews(
&self.account_manager,
ui,
PERSISTED_ACCOUNT_MANAGEMENT.get_state(ui.ctx()),
STATE_ACCOUNT_MANAGEMENT.get_state(ui.ctx()),
mobile_account_card_ui(), // closure for creating an account 'card'
);
@@ -104,12 +104,12 @@ impl<'a> AccountManagementView<'a> {
Vec2::new(ui.available_size_before_wrap().x, 32.0),
Layout::left_to_right(egui::Align::Center),
|ui| {
if PERSISTED_ACCOUNT_MANAGEMENT.get_state(ui.ctx()) {
if STATE_ACCOUNT_MANAGEMENT.get_state(ui.ctx()) {
if ui.add(done_account_button()).clicked() {
PERSISTED_ACCOUNT_MANAGEMENT.set_state(ui.ctx(), false);
STATE_ACCOUNT_MANAGEMENT.set_state(ui.ctx(), false);
}
} else if ui.add(edit_account_button()).clicked() {
PERSISTED_ACCOUNT_MANAGEMENT.set_state(ui.ctx(), true);
STATE_ACCOUNT_MANAGEMENT.set_state(ui.ctx(), true);
}
},
);

View File

@@ -3,7 +3,7 @@ use egui::{Align2, CentralPanel, RichText, Vec2, Window};
use crate::Damus;
use super::{
persist_state::{PERSISTED_GLOBAL_POPUP, PERSISTED_SIDE_PANEL},
state_in_memory::{STATE_GLOBAL_POPUP, STATE_SIDE_PANEL},
AccountManagementView, View,
};
@@ -67,8 +67,8 @@ impl<'a> DesktopGlobalPopup<'a> {
let available_size = ui.available_size();
let window_size = available_size - MARGIN;
if let Some(popup) = PERSISTED_SIDE_PANEL.get_state(ctx) {
let mut show_global_popup = PERSISTED_GLOBAL_POPUP.get_state(ctx);
if let Some(popup) = STATE_SIDE_PANEL.get_state(ctx) {
let mut show_global_popup = STATE_GLOBAL_POPUP.get_state(ctx);
if show_global_popup {
overlay_window(&mut show_global_popup, window_size, popup.title()).show(
ctx,
@@ -82,7 +82,7 @@ impl<'a> DesktopGlobalPopup<'a> {
);
// user could have closed the window, set the new state in egui memory
PERSISTED_GLOBAL_POPUP.set_state(ctx, show_global_popup);
STATE_GLOBAL_POPUP.set_state(ctx, show_global_popup);
}
}
});

View File

@@ -4,11 +4,11 @@ pub mod anim;
pub mod global_popup;
pub mod mention;
pub mod note;
pub mod persist_state;
pub mod preview;
pub mod profile;
pub mod relay;
pub mod side_panel;
pub mod state_in_memory;
pub mod username;
pub use account_management::{AccountManagementView, AccountSelectionWidget};

View File

@@ -3,7 +3,7 @@ use egui::{Button, Layout, SidePanel, Vec2};
use crate::ui::global_popup::GlobalPopupType;
use super::{
persist_state::{PERSISTED_GLOBAL_POPUP, PERSISTED_SIDE_PANEL},
state_in_memory::{STATE_GLOBAL_POPUP, STATE_SIDE_PANEL},
View,
};
@@ -32,8 +32,8 @@ impl DesktopSidePanel {
.add_sized(Vec2::new(32.0, 32.0), Button::new("A"))
.clicked()
{
PERSISTED_SIDE_PANEL.set_state(ui.ctx(), Some(GlobalPopupType::AccountManagement));
PERSISTED_GLOBAL_POPUP.set_state(ui.ctx(), true);
STATE_SIDE_PANEL.set_state(ui.ctx(), Some(GlobalPopupType::AccountManagement));
STATE_GLOBAL_POPUP.set_state(ui.ctx(), true);
}
ui.add_space(spacing_amt);
ui.add(settings_button(dark_mode));

View File

@@ -1,39 +1,39 @@
use egui::util::id_type_map::SerializableAny;
use std::any::Any;
use super::global_popup::GlobalPopupType;
/// PersistState is a helper struct for interacting with egui memory persisted data
/// StateInMemory is a helper struct for interacting with egui memory persisted data
#[derive(Clone)]
pub struct PersistState<T: SerializableAny> {
pub struct StateInMemory<T: 'static + Clone + Send> {
id: &'static str,
default_state: T,
}
impl<T: SerializableAny> PersistState<T> {
impl<T: 'static + Any + Clone + Send + Sync> StateInMemory<T> {
pub fn get_state(&self, ctx: &egui::Context) -> T {
ctx.data_mut(|d| {
d.get_persisted(egui::Id::new(self.id))
d.get_temp(egui::Id::new(self.id))
.unwrap_or(self.default_state.clone())
})
}
pub fn set_state(&self, ctx: &egui::Context, new_val: T) {
ctx.data_mut(|d| d.insert_persisted(egui::Id::new(self.id), new_val));
ctx.data_mut(|d| d.insert_temp(egui::Id::new(self.id), new_val));
}
}
pub static PERSISTED_ACCOUNT_MANAGEMENT: PersistState<bool> = PersistState::<bool> {
pub static STATE_ACCOUNT_MANAGEMENT: StateInMemory<bool> = StateInMemory::<bool> {
id: ACCOUNT_MANAGEMENT_VIEW_STATE_ID,
default_state: false,
};
pub static PERSISTED_SIDE_PANEL: PersistState<Option<GlobalPopupType>> =
PersistState::<Option<GlobalPopupType>> {
pub static STATE_SIDE_PANEL: StateInMemory<Option<GlobalPopupType>> =
StateInMemory::<Option<GlobalPopupType>> {
id: SIDE_PANEL_VIEW_STATE_ID,
default_state: None,
};
pub static PERSISTED_GLOBAL_POPUP: PersistState<bool> = PersistState::<bool> {
pub static STATE_GLOBAL_POPUP: StateInMemory<bool> = StateInMemory::<bool> {
id: GLOBAL_POPUP_VIEW_STATE_ID,
default_state: false,
};