Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce account switcher #106

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::relay_pool_manager::RelayPoolManager;
use crate::route::Route;
use crate::timeline;
use crate::timeline::{MergeKind, NoteRef, Timeline, ViewFilter};
use crate::ui;
use crate::ui::{DesktopSidePanel, RelayView, SidePanelAction, View};
use crate::ui::{self, AccountSelectionWidget};
use crate::ui::{DesktopSidePanel, RelayView, View};
use crate::Result;
use egui_nav::{Nav, NavAction};
use enostr::RelayPool;
Expand Down Expand Up @@ -58,6 +58,7 @@ pub struct Damus {
pub account_manager: AccountManager,

frame_history: crate::frame_history::FrameHistory,
pub show_account_switcher: bool,
}

fn relay_setup(pool: &mut RelayPool, ctx: &egui::Context) {
Expand Down Expand Up @@ -726,6 +727,7 @@ impl Damus {
),
//compose: "".to_string(),
frame_history: FrameHistory::default(),
show_account_switcher: false,
}
}

Expand All @@ -752,6 +754,7 @@ impl Damus {
ndb: Ndb::new(data_path.as_ref().to_str().expect("db path ok"), &config).expect("ndb"),
account_manager: AccountManager::new(None, crate::key_storage::KeyStorage::None),
frame_history: FrameHistory::default(),
show_account_switcher: false,
}
}

Expand Down Expand Up @@ -983,6 +986,9 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) {

main_panel(&ctx.style(), app.is_mobile()).show(ctx, |ui| {
ui.spacing_mut().item_spacing.x = 0.0;
if app.show_account_switcher {
AccountSelectionWidget::ui(app, ui);
}
if need_scroll {
egui::ScrollArea::horizontal().show(ui, |ui| {
timelines_view(ui, panel_sizes, app, app.timelines.len());
Expand All @@ -1004,10 +1010,8 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, timelines: us

if side_panel.response.clicked() {
info!("clicked {:?}", side_panel.action);
if let SidePanelAction::Account = side_panel.action {
app.timelines[0].routes.push(Route::ManageAccount);
}
}
DesktopSidePanel::perform_action(app, side_panel.action);
});

for timeline_ind in 0..timelines {
Expand Down
84 changes: 61 additions & 23 deletions src/ui/account_switcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,67 @@ impl AccountSelectionWidget {
if app.is_mobile() {
Self::show_mobile(ui);
} else {
Self::show(app, ui);
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.account_manager.remove_account(_index)
}
AccountSelectAction::SelectAccount { _index } => {
app.show_account_switcher = false;
app.account_manager.select_account(_index);
}
AccountSelectAction::OpenAccountManagement => {
app.show_account_switcher = false;
// TODO: push account management to global popup router
}
}
}

fn show(app: &mut Damus, ui: &mut egui::Ui) -> AccountSelectResponse {
fn show(app: &mut Damus, ui: &mut egui::Ui) -> (AccountSelectResponse, egui::Response) {
let mut res = AccountSelectResponse::default();
let mut selected_index = app.account_manager.get_selected_account_index();

Frame::none().outer_margin(8.0).show(ui, |ui| {
res = top_section_widget(ui);
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.account_manager.get_account(_index) {
ui.add_space(8.0);
if Self::handle_sign_out(&app.ndb, ui, account) {
res.action = Some(AccountSelectAction::RemoveAccount { _index })
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.account_manager.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);
});
ui.add_space(8.0);
})
.response;

res
(res, response)
}

fn handle_sign_out(ndb: &Ndb, ui: &mut egui::Ui, account: &UserAccount) -> bool {
Expand Down Expand Up @@ -144,6 +172,16 @@ fn account_switcher_card_ui(
.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 =
Expand Down Expand Up @@ -221,7 +259,7 @@ mod previews {

impl View for AccountSelectionPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
AccountSelectionWidget::show(&mut self.app, ui);
AccountSelectionWidget::ui(&mut self.app, ui);
}
}

Expand Down
48 changes: 35 additions & 13 deletions src/ui/side_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'a> DesktopSidePanel<'a> {
let settings_resp = ui.add(settings_button(dark_mode));
let column_resp = ui.add(add_column_button(dark_mode));

if pfp_resp.clicked() || pfp_resp.hovered() {
if pfp_resp.clicked() {
egui::InnerResponse::new(SidePanelAction::Account, pfp_resp)
} else if settings_resp.clicked() || settings_resp.hovered() {
egui::InnerResponse::new(SidePanelAction::Settings, settings_resp)
Expand All @@ -71,8 +71,22 @@ impl<'a> DesktopSidePanel<'a> {
}

fn pfp_button(&mut self, ui: &mut egui::Ui) -> egui::Response {
profile_preview_controller::show_with_selected_pfp(self.app, ui, show_pfp());
add_button_to_ui(ui, no_account_pfp())
if let Some(resp) =
profile_preview_controller::show_with_selected_pfp(self.app, ui, show_pfp())
{
resp
} else {
add_button_to_ui(ui, no_account_pfp())
}
}

pub fn perform_action(app: &mut Damus, action: SidePanelAction) {
match action {
SidePanelAction::Panel => {} // TODO
SidePanelAction::Account => app.show_account_switcher = !app.show_account_switcher,
SidePanelAction::Settings => {} // TODO
SidePanelAction::Columns => (), // TODO
}
}
}

Expand Down Expand Up @@ -109,9 +123,11 @@ fn add_column_button(dark_mode: bool) -> egui::Button<'static> {

mod preview {

use egui_extras::{Size, StripBuilder};

use crate::{
test_data,
ui::{Preview, PreviewConfig},
ui::{AccountSelectionWidget, Preview, PreviewConfig},
};

use super::*;
Expand All @@ -129,15 +145,21 @@ mod preview {

impl View for DesktopSidePanelPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
let _selected_account = self
.app
.account_manager
.get_selected_account()
.map(|x| x.pubkey.bytes());

let mut panel = DesktopSidePanel::new(&mut self.app);

DesktopSidePanel::panel().show(ui.ctx(), |ui| panel.ui(ui));
StripBuilder::new(ui)
.size(Size::exact(40.0))
.sizes(Size::remainder(), 0)
.clip(true)
.horizontal(|mut strip| {
strip.cell(|ui| {
let mut panel = DesktopSidePanel::new(&mut self.app);
let response = panel.show(ui);
DesktopSidePanel::perform_action(&mut self.app, response.action);
});
});

if self.app.show_account_switcher {
AccountSelectionWidget::ui(&mut self.app, ui);
}
}
}

Expand Down
Loading