From 79a447239a4d124a9d2b8a0e4d1e7760c433c1fc Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 16 Sep 2024 16:35:29 -0700 Subject: [PATCH] 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, );