add RoutableWidgetState conception

holds the routes for an arbitrary widget

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-09-06 18:31:50 -04:00
committed by William Casarin
parent df4e331d33
commit ee0029268f
4 changed files with 49 additions and 18 deletions

View File

@@ -13,7 +13,8 @@ use crate::key_storage::KeyStorageType;
use crate::note::NoteRef;
use crate::notecache::{CachedNote, NoteCache};
use crate::relay_pool_manager::RelayPoolManager;
use crate::route::Route;
use crate::routable_widget_state::RoutableWidgetState;
use crate::route::{ManageAccountRoute, Route};
use crate::subscriptions::{SubKind, Subscriptions};
use crate::thread::{DecrementResult, Threads};
use crate::timeline::{Timeline, TimelineKind, TimelineSource, ViewFilter};
@@ -49,6 +50,7 @@ pub struct Damus {
pub pool: RelayPool,
pub columns: Columns,
pub account_management_view_state: RoutableWidgetState<ManageAccountRoute>,
pub ndb: Ndb,
pub unknown_ids: UnknownIds,
pub drafts: Drafts,
@@ -697,6 +699,7 @@ impl Damus {
accounts,
frame_history: FrameHistory::default(),
show_account_switcher: false,
account_management_view_state: RoutableWidgetState::default(),
}
}
@@ -739,6 +742,7 @@ impl Damus {
accounts: AccountManager::new(None, KeyStorageType::None),
frame_history: FrameHistory::default(),
show_account_switcher: false,
account_management_view_state: RoutableWidgetState::default(),
}
}
@@ -949,11 +953,6 @@ fn render_nav(show_postbox: bool, col: usize, app: &mut Damus, ui: &mut egui::Ui
None
}
Route::ManageAccount => {
ui.label("account management view");
None
}
Route::Relays => {
let manager = RelayPoolManager::new(&mut app.pool);
RelayView::new(manager).ui(ui);

View File

@@ -27,6 +27,7 @@ mod post;
mod profile;
pub mod relay_pool_manager;
mod result;
mod routable_widget_state;
mod route;
mod subscriptions;
mod test_data;

View File

@@ -0,0 +1,26 @@
#[derive(Default)]
pub struct RoutableWidgetState<R: Clone> {
routes: Vec<R>,
}
impl<R: Clone> RoutableWidgetState<R> {
pub fn route_to(&mut self, route: R) {
self.routes.push(route);
}
pub fn clear(&mut self) {
self.routes.clear();
}
pub fn go_back(&mut self) {
self.routes.pop();
}
pub fn top(&self) -> Option<R> {
self.routes.last().cloned()
}
pub fn get_routes(&self) -> Vec<R> {
self.routes.clone()
}
}

View File

@@ -1,23 +1,31 @@
use egui::{Response, RichText};
use egui::RichText;
use enostr::NoteId;
use std::fmt::{self};
use strum_macros::Display;
use crate::{ui::AccountManagementView, Damus};
use crate::ui::{
account_login_view::AccountLoginResponse, account_management::AccountManagementViewResponse,
};
/// App routing. These describe different places you can go inside Notedeck.
#[derive(Clone, Debug)]
pub enum Route {
Timeline(String),
ManageAccount,
Thread(NoteId),
Reply(NoteId),
Relays,
}
#[derive(Clone, Debug, Default, Display)]
pub enum ManageAccountRoute {
#[default]
AccountManagement,
AddAccount,
}
impl fmt::Display for Route {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Route::ManageAccount => write!(f, "Manage Account"),
Route::Timeline(name) => write!(f, "{}", name),
Route::Thread(_id) => write!(f, "Thread"),
Route::Reply(_id) => write!(f, "Reply"),
@@ -27,16 +35,8 @@ impl fmt::Display for Route {
}
impl Route {
pub fn show_global_popup(&self, app: &mut Damus, ui: &mut egui::Ui) -> Option<Response> {
match self {
Route::ManageAccount => AccountManagementView::ui(app, ui),
_ => None,
}
}
pub fn title(&self) -> RichText {
match self {
Route::ManageAccount => RichText::new("Manage Account").size(24.0),
Route::Thread(_) => RichText::new("Thread"),
Route::Reply(_) => RichText::new("Reply"),
Route::Relays => RichText::new("Relays"),
@@ -44,3 +44,8 @@ impl Route {
}
}
}
pub enum ManageAcountRouteResponse {
AccountManagement(AccountManagementViewResponse),
AddAccount(AccountLoginResponse),
}