From 52a7ed53ece3cde4e8606757f8ea5e5b0c789da7 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 16 Sep 2024 16:28:56 -0700 Subject: [PATCH 1/4] accounts: use column nav for account management Signed-off-by: William Casarin --- src/actionbar.rs | 1 - src/app.rs | 18 +++++++++++++----- src/route.rs | 1 + src/ui/side_panel.rs | 25 ++++++++++++++++++++----- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/actionbar.rs b/src/actionbar.rs index 128909b..7bbd0ea 100644 --- a/src/actionbar.rs +++ b/src/actionbar.rs @@ -40,7 +40,6 @@ fn open_thread( ) -> Option { { router.route_to(Route::thread(NoteId::new(selected_note.to_owned()))); - router.navigating = true; } let root_id = crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note); diff --git a/src/app.rs b/src/app.rs index 9fe6a2e..0fcd34f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,7 @@ use crate::{ app_creation::setup_cc, app_style::user_requested_visuals_change, args::Args, - column::Columns, + column::{Column, Columns}, draft::Drafts, error::{Error, FilterError}, filter, @@ -14,6 +14,7 @@ use crate::{ nav, note::NoteRef, notecache::{CachedNote, NoteCache}, + route::Route, subscriptions::{SubKind, Subscriptions}, thread::Threads, timeline::{Timeline, TimelineKind, ViewFilter}, @@ -962,11 +963,18 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, columns: usiz let rect = ui.available_rect_before_wrap(); let side_panel = DesktopSidePanel::new(app).show(ui); - if side_panel.response.clicked() { - info!("clicked {:?}", side_panel.action); - } + let router = if let Some(router) = + app.columns.columns_mut().get_mut(0).map(|c: &mut Column| c.router_mut()) + { + router + } else { + // TODO(jb55): Maybe we should have an empty column route? + let columns = app.columns.columns_mut(); + columns.push(Column::new(vec![Route::accounts()])); + columns[0].router_mut() + }; - DesktopSidePanel::perform_action(app, side_panel.action); + DesktopSidePanel::perform_action(router, side_panel.action); // vertical sidebar line ui.painter().vline( diff --git a/src/route.rs b/src/route.rs index 259132c..6be6fc3 100644 --- a/src/route.rs +++ b/src/route.rs @@ -68,6 +68,7 @@ impl Router { } pub fn route_to(&mut self, route: R) { + self.navigating = true; self.routes.push(route); } diff --git a/src/ui/side_panel.rs b/src/ui/side_panel.rs index dc9de6b..bc2f811 100644 --- a/src/ui/side_panel.rs +++ b/src/ui/side_panel.rs @@ -1,6 +1,11 @@ use egui::{Button, Layout, SidePanel, Vec2, Widget}; -use crate::{ui::profile_preview_controller, Damus}; +use crate::{ + column::Column, + route::{Route, Router}, + ui::profile_preview_controller, + Damus, +}; use super::{ProfilePic, View}; @@ -80,10 +85,13 @@ impl<'a> DesktopSidePanel<'a> { } } - pub fn perform_action(app: &mut Damus, action: SidePanelAction) { + pub fn perform_action(router: &mut Router, action: SidePanelAction) { match action { SidePanelAction::Panel => {} // TODO - SidePanelAction::Account => app.show_account_switcher = !app.show_account_switcher, + SidePanelAction::Account => { + router.route_to(Route::accounts()); + //app.show_account_switcher = !app.show_account_switcher, + } SidePanelAction::Settings => {} // TODO SidePanelAction::Columns => (), // TODO } @@ -138,7 +146,10 @@ mod preview { impl DesktopSidePanelPreview { fn new() -> Self { - let app = test_data::test_app(); + let mut app = test_data::test_app(); + app.columns + .columns_mut() + .push(Column::new(vec![Route::accounts()])); DesktopSidePanelPreview { app } } } @@ -153,7 +164,11 @@ mod preview { strip.cell(|ui| { let mut panel = DesktopSidePanel::new(&mut self.app); let response = panel.show(ui); - DesktopSidePanel::perform_action(&mut self.app, response.action); + + DesktopSidePanel::perform_action( + self.app.columns.columns_mut()[0].router_mut(), + response.action, + ); }); }); From 79a447239a4d124a9d2b8a0e4d1e7760c433c1fc Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 16 Sep 2024 16:35:29 -0700 Subject: [PATCH 2/4] cleanup: remove account switcher widget we don't need this anymore Signed-off-by: William Casarin --- src/actionbar.rs | 4 +- src/app.rs | 13 +- src/ui/account_switcher.rs | 272 ------------------------------------- src/ui/mod.rs | 2 - src/ui/side_panel.rs | 4 +- src/ui_preview/main.rs | 6 +- 6 files changed, 10 insertions(+), 291 deletions(-) delete mode 100644 src/ui/account_switcher.rs diff --git a/src/actionbar.rs b/src/actionbar.rs index 7bbd0ea..ca452b6 100644 --- a/src/actionbar.rs +++ b/src/actionbar.rs @@ -38,9 +38,7 @@ fn open_thread( threads: &mut Threads, selected_note: &[u8; 32], ) -> Option { - { - router.route_to(Route::thread(NoteId::new(selected_note.to_owned()))); - } + router.route_to(Route::thread(NoteId::new(selected_note.to_owned()))); let root_id = crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note); let thread_res = threads.thread_mut(ndb, txn, root_id); diff --git a/src/app.rs b/src/app.rs index 0fcd34f..2c5c3af 100644 --- a/src/app.rs +++ b/src/app.rs @@ -18,7 +18,7 @@ use crate::{ subscriptions::{SubKind, Subscriptions}, thread::Threads, timeline::{Timeline, TimelineKind, ViewFilter}, - ui::{self, AccountSelectionWidget, DesktopSidePanel}, + ui::{self, DesktopSidePanel}, unknowns::UnknownIds, view_state::ViewState, Result, @@ -65,7 +65,6 @@ pub struct Damus { pub debug: bool, pub since_optimize: bool, pub textmode: bool, - pub show_account_switcher: bool, } fn relay_setup(pool: &mut RelayPool, ctx: &egui::Context) { @@ -698,7 +697,6 @@ impl Damus { ndb, accounts, frame_history: FrameHistory::default(), - show_account_switcher: false, view_state: ViewState::default(), } } @@ -777,7 +775,6 @@ impl Damus { ndb: Ndb::new(data_path.as_ref().to_str().expect("db path ok"), &config).expect("ndb"), accounts: AccountManager::new(None, KeyStorageType::None), frame_history: FrameHistory::default(), - show_account_switcher: false, view_state: ViewState::default(), } } @@ -942,7 +939,6 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) { main_panel(&ctx.style(), ui::is_narrow(ctx)).show(ctx, |ui| { ui.spacing_mut().item_spacing.x = 0.0; - AccountSelectionWidget::ui(app, ui); if need_scroll { egui::ScrollArea::horizontal().show(ui, |ui| { timelines_view(ui, panel_sizes, app, app.columns.columns().len()); @@ -963,8 +959,11 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, columns: usiz let rect = ui.available_rect_before_wrap(); let side_panel = DesktopSidePanel::new(app).show(ui); - let router = if let Some(router) = - app.columns.columns_mut().get_mut(0).map(|c: &mut Column| c.router_mut()) + let router = if let Some(router) = app + .columns + .columns_mut() + .get_mut(0) + .map(|c: &mut Column| c.router_mut()) { router } else { diff --git a/src/ui/account_switcher.rs b/src/ui/account_switcher.rs deleted file mode 100644 index 6454894..0000000 --- a/src/ui/account_switcher.rs +++ /dev/null @@ -1,272 +0,0 @@ -use crate::{ - colors::PINK, profile::DisplayName, ui, ui::profile_preview_controller, - user_account::UserAccount, Damus, Result, -}; - -use nostrdb::Ndb; - -use egui::{ - Align, Button, Color32, Frame, Id, Image, Layout, Margin, RichText, Rounding, ScrollArea, - Sense, Vec2, -}; - -use super::profile::preview::SimpleProfilePreview; - -pub struct AccountSelectionWidget {} - -enum AccountSelectAction { - RemoveAccount { _index: usize }, - SelectAccount { _index: usize }, -} - -#[derive(Default)] -struct AccountSelectResponse { - action: Option, -} - -impl AccountSelectionWidget { - pub fn ui(app: &mut Damus, ui: &mut egui::Ui) { - if !app.show_account_switcher { - return; - } - - if ui::is_narrow(ui.ctx()) { - Self::show_mobile(ui); - } else { - account_switcher_window(&mut app.show_account_switcher.clone()).show( - ui.ctx(), - |ui: &mut egui::Ui| { - let (account_selection_response, response) = Self::show(app, ui); - if let Some(action) = account_selection_response.action { - Self::perform_action(app, action); - } - response - }, - ); - } - } - - fn perform_action(app: &mut Damus, action: AccountSelectAction) { - match action { - AccountSelectAction::RemoveAccount { _index } => { - app.accounts_mut().remove_account(_index) - } - AccountSelectAction::SelectAccount { _index } => { - app.show_account_switcher = false; - app.accounts_mut().select_account(_index); - } - } - } - - fn show(app: &mut Damus, ui: &mut egui::Ui) -> (AccountSelectResponse, egui::Response) { - let mut res = AccountSelectResponse::default(); - let mut selected_index = app.accounts().get_selected_account_index(); - - let response = Frame::none() - .outer_margin(8.0) - .show(ui, |ui| { - res = top_section_widget(ui); - - scroll_area().show(ui, |ui| { - if let Some(_index) = Self::show_accounts(app, ui) { - selected_index = Some(_index); - res.action = Some(AccountSelectAction::SelectAccount { _index }); - } - }); - ui.add_space(8.0); - ui.add(add_account_button()); - - if let Some(_index) = selected_index { - if let Some(account) = app.accounts().get_account(_index) { - ui.add_space(8.0); - if Self::handle_sign_out(app.ndb(), ui, account) { - res.action = Some(AccountSelectAction::RemoveAccount { _index }) - } - } - } - - ui.add_space(8.0); - }) - .response; - - (res, response) - } - - fn handle_sign_out(ndb: &Ndb, ui: &mut egui::Ui, account: &UserAccount) -> bool { - if let Ok(response) = Self::sign_out_button(ndb, ui, account) { - response.clicked() - } else { - false - } - } - - fn show_mobile(ui: &mut egui::Ui) -> egui::Response { - let _ = ui; - todo!() - } - - fn show_accounts(app: &mut Damus, ui: &mut egui::Ui) -> Option { - profile_preview_controller::view_profile_previews(app, ui, account_switcher_card_ui) - } - - fn sign_out_button( - ndb: &Ndb, - ui: &mut egui::Ui, - account: &UserAccount, - ) -> Result { - profile_preview_controller::show_with_nickname( - ndb, - ui, - account.pubkey.bytes(), - |ui: &mut egui::Ui, username: &DisplayName| { - let img_data = egui::include_image!("../../assets/icons/signout_icon_4x.png"); - let img = Image::new(img_data).fit_to_exact_size(Vec2::new(16.0, 16.0)); - let button = egui::Button::image_and_text( - img, - RichText::new(format!(" Sign out @{}", username.username())) - .color(PINK) - .size(16.0), - ) - .frame(false); - - ui.add(button) - }, - ) - } -} - -fn account_switcher_card_ui( - ui: &mut egui::Ui, - preview: SimpleProfilePreview, - width: f32, - is_selected: bool, - index: usize, -) -> bool { - let resp = ui.add_sized(Vec2::new(width, 50.0), |ui: &mut egui::Ui| { - Frame::none() - .show(ui, |ui| { - ui.add_space(8.0); - ui.horizontal(|ui| { - if is_selected { - Frame::none() - .rounding(Rounding::same(8.0)) - .inner_margin(Margin::same(8.0)) - .fill(Color32::from_rgb(0x45, 0x1B, 0x59)) - .show(ui, |ui| { - ui.add(preview); - ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - ui.add(selection_widget()); - }); - }); - } else { - ui.add_space(8.0); - ui.add(preview); - } - }); - }) - .response - }); - - ui.interact(resp.rect, Id::new(index), Sense::click()) - .clicked() -} - -fn account_switcher_window(open: &'_ mut bool) -> egui::Window<'_> { - egui::Window::new("account switcher") - .title_bar(false) - .collapsible(false) - .anchor(egui::Align2::LEFT_BOTTOM, Vec2::new(4.0, -44.0)) - .fixed_size(Vec2::new(360.0, 406.0)) - .open(open) - .movable(false) -} - -fn selection_widget() -> impl egui::Widget { - |ui: &mut egui::Ui| { - let img_data: egui::ImageSource = - egui::include_image!("../../assets/icons/select_icon_3x.png"); - let img = Image::new(img_data).max_size(Vec2::new(16.0, 16.0)); - ui.add(img) - } -} - -fn top_section_widget(ui: &mut egui::Ui) -> AccountSelectResponse { - ui.horizontal(|ui| { - let resp = AccountSelectResponse::default(); - - ui.allocate_ui_with_layout( - Vec2::new(ui.available_size_before_wrap().x, 32.0), - Layout::left_to_right(egui::Align::Center), - |ui| ui.add(account_switcher_title()), - ); - - ui.allocate_ui_with_layout( - Vec2::new(ui.available_size_before_wrap().x, 32.0), - Layout::right_to_left(egui::Align::Center), - |ui| { - if ui.add(manage_accounts_button()).clicked() { - // resp.action = Some(AccountSelectAction::OpenAccountManagement); TODO implement after temporary column impl is finished - } - }, - ); - - resp - }) - .inner -} - -fn manage_accounts_button() -> egui::Button<'static> { - Button::new(RichText::new("Manage").color(PINK).size(16.0)).frame(false) -} - -fn account_switcher_title() -> impl egui::Widget { - |ui: &mut egui::Ui| ui.label(RichText::new("Account switcher").size(20.0).strong()) -} - -fn scroll_area() -> ScrollArea { - egui::ScrollArea::vertical() - .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden) - .auto_shrink([false; 2]) -} - -fn add_account_button() -> egui::Button<'static> { - let img_data = egui::include_image!("../../assets/icons/plus_icon_4x.png"); - let img = Image::new(img_data).fit_to_exact_size(Vec2::new(16.0, 16.0)); - Button::image_and_text(img, RichText::new(" Add account").size(16.0).color(PINK)).frame(false) -} - -mod previews { - - use crate::{ - test_data, - ui::{Preview, PreviewConfig, View}, - Damus, - }; - - use super::AccountSelectionWidget; - - pub struct AccountSelectionPreview { - app: Damus, - } - - impl AccountSelectionPreview { - fn new() -> Self { - let app = test_data::test_app(); - AccountSelectionPreview { app } - } - } - - impl View for AccountSelectionPreview { - fn ui(&mut self, ui: &mut egui::Ui) { - AccountSelectionWidget::ui(&mut self.app, ui); - } - } - - impl Preview for AccountSelectionWidget { - type Prev = AccountSelectionPreview; - - fn preview(_cfg: PreviewConfig) -> Self::Prev { - AccountSelectionPreview::new() - } - } -} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 7da506a..8b829e5 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,6 +1,5 @@ pub mod account_login_view; pub mod account_management; -pub mod account_switcher; pub mod anim; pub mod mention; pub mod note; @@ -13,7 +12,6 @@ pub mod timeline; pub mod username; pub use account_management::AccountsView; -pub use account_switcher::AccountSelectionWidget; pub use mention::Mention; pub use note::{NoteResponse, NoteView, PostReplyView, PostView}; pub use preview::{Preview, PreviewApp, PreviewConfig}; diff --git a/src/ui/side_panel.rs b/src/ui/side_panel.rs index bc2f811..c5c5c8a 100644 --- a/src/ui/side_panel.rs +++ b/src/ui/side_panel.rs @@ -135,7 +135,7 @@ mod preview { use crate::{ test_data, - ui::{AccountSelectionWidget, Preview, PreviewConfig}, + ui::{Preview, PreviewConfig}, }; use super::*; @@ -171,8 +171,6 @@ mod preview { ); }); }); - - AccountSelectionWidget::ui(&mut self.app, ui); } } diff --git a/src/ui_preview/main.rs b/src/ui_preview/main.rs index 577338b..a9804a6 100644 --- a/src/ui_preview/main.rs +++ b/src/ui_preview/main.rs @@ -2,9 +2,8 @@ use notedeck::app_creation::{ generate_mobile_emulator_native_options, generate_native_options, setup_cc, }; use notedeck::ui::{ - account_login_view::AccountLoginView, account_management::AccountsView, AccountSelectionWidget, - DesktopSidePanel, PostView, Preview, PreviewApp, PreviewConfig, ProfilePic, ProfilePreview, - RelayView, + account_login_view::AccountLoginView, account_management::AccountsView, DesktopSidePanel, + PostView, Preview, PreviewApp, PreviewConfig, ProfilePic, ProfilePreview, RelayView, }; use std::env; @@ -101,7 +100,6 @@ async fn main() { ProfilePreview, ProfilePic, AccountsView, - AccountSelectionWidget, DesktopSidePanel, PostView, ); From fce82b2b6da6423dfe5a0a7fbd3364d6110dfd0e Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 16 Sep 2024 16:53:37 -0700 Subject: [PATCH 3/4] nav: fix accounts nav animations also make nav go backward when clicking the account switch button if we already are navigating to accounts Signed-off-by: William Casarin --- src/nav.rs | 4 ++-- src/route.rs | 11 +++++++++++ src/ui/side_panel.rs | 13 +++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/nav.rs b/src/nav.rs index 11536f0..d278400 100644 --- a/src/nav.rs +++ b/src/nav.rs @@ -11,6 +11,7 @@ use crate::{ use egui_nav::{Nav, NavAction}; pub fn render_nav(show_postbox: bool, col: usize, app: &mut Damus, ui: &mut egui::Ui) { + // TODO(jb55): clean up this router_mut mess by using Router in egui-nav directly let nav_response = Nav::new(app.columns().column(col).router().routes().clone()) .navigating(app.columns_mut().column_mut(col).router_mut().navigating) .returning(app.columns_mut().column_mut(col).router_mut().returning) @@ -67,7 +68,7 @@ pub fn render_nav(show_postbox: bool, col: usize, app: &mut Damus, ui: &mut egui } if let Some(NavAction::Returned) = nav_response.action { - let r = app.columns_mut().column_mut(col).router_mut().go_back(); + let r = app.columns_mut().column_mut(col).router_mut().pop(); if let Some(Route::Timeline(TimelineRoute::Thread(id))) = r { thread_unsubscribe( &app.ndb, @@ -77,7 +78,6 @@ pub fn render_nav(show_postbox: bool, col: usize, app: &mut Damus, ui: &mut egui id.bytes(), ); } - app.columns_mut().column_mut(col).router_mut().returning = false; } else if let Some(NavAction::Navigated) = nav_response.action { app.columns_mut().column_mut(col).router_mut().navigating = false; } diff --git a/src/route.rs b/src/route.rs index 6be6fc3..2f2f33b 100644 --- a/src/route.rs +++ b/src/route.rs @@ -72,10 +72,21 @@ impl Router { self.routes.push(route); } + /// Go back, start the returning process pub fn go_back(&mut self) -> Option { + if self.returning || self.routes.len() == 1 { + return None; + } + self.returning = true; + self.routes.get(self.routes.len() - 2).cloned() + } + + /// Pop a route, should only be called on a NavRespose::Returned reseponse + pub fn pop(&mut self) -> Option { if self.routes.len() == 1 { return None; } + self.returning = false; self.routes.pop() } diff --git a/src/ui/side_panel.rs b/src/ui/side_panel.rs index c5c5c8a..8d3a02d 100644 --- a/src/ui/side_panel.rs +++ b/src/ui/side_panel.rs @@ -1,6 +1,7 @@ use egui::{Button, Layout, SidePanel, Vec2, Widget}; use crate::{ + account_manager::AccountsRoute, column::Column, route::{Route, Router}, ui::profile_preview_controller, @@ -89,8 +90,16 @@ impl<'a> DesktopSidePanel<'a> { match action { SidePanelAction::Panel => {} // TODO SidePanelAction::Account => { - router.route_to(Route::accounts()); - //app.show_account_switcher = !app.show_account_switcher, + if router + .routes() + .iter() + .any(|&r| r == Route::Accounts(AccountsRoute::Accounts)) + { + // return if we are already routing to accounts + router.go_back(); + } else { + router.route_to(Route::accounts()); + } } SidePanelAction::Settings => {} // TODO SidePanelAction::Columns => (), // TODO From 945ccde8188a05e4637bca36c514c5359fb5d95f Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 16 Sep 2024 17:09:32 -0700 Subject: [PATCH 4/4] ui: hook up relay management view Signed-off-by: William Casarin --- src/app.rs | 4 +++- src/route.rs | 4 ++++ src/ui/side_panel.rs | 9 ++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 2c5c3af..1e92dc2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -973,7 +973,9 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, columns: usiz columns[0].router_mut() }; - DesktopSidePanel::perform_action(router, side_panel.action); + if side_panel.response.clicked() { + DesktopSidePanel::perform_action(router, side_panel.action); + } // vertical sidebar line ui.painter().vline( diff --git a/src/route.rs b/src/route.rs index 2f2f33b..99e0ac8 100644 --- a/src/route.rs +++ b/src/route.rs @@ -27,6 +27,10 @@ impl Route { } } + pub fn relays() -> Self { + Route::Relays + } + pub fn thread(thread_root: NoteId) -> Self { Route::Timeline(TimelineRoute::Thread(thread_root)) } diff --git a/src/ui/side_panel.rs b/src/ui/side_panel.rs index 8d3a02d..8a890d2 100644 --- a/src/ui/side_panel.rs +++ b/src/ui/side_panel.rs @@ -101,7 +101,14 @@ impl<'a> DesktopSidePanel<'a> { router.route_to(Route::accounts()); } } - SidePanelAction::Settings => {} // TODO + SidePanelAction::Settings => { + if router.routes().iter().any(|&r| r == Route::Relays) { + // return if we are already routing to accounts + router.go_back(); + } else { + router.route_to(Route::relays()); + } + } SidePanelAction::Columns => (), // TODO } }