Skip to content

Commit

Permalink
Pfp integration to side panel
Browse files Browse the repository at this point in the history
Signed-off-by: kernelkind <[email protected]>
  • Loading branch information
kernelkind committed May 27, 2024
1 parent 709818a commit 1b0de88
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 30 deletions.
15 changes: 13 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::notecache::NoteCache;
use crate::relay_pool_manager::create_wakeup;
use crate::timeline;
use crate::timeline::{NoteRef, Timeline};
use crate::ui::profile::SimpleProfilePreviewController;
use crate::ui::{is_mobile, DesktopGlobalPopup, DesktopSidePanel, View};
use crate::Result;

Expand Down Expand Up @@ -702,7 +703,11 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) {

if app.timelines.len() == 1 {
DesktopSidePanel::panel().show(ctx, |ui| {
DesktopSidePanel::inner(ui);
DesktopSidePanel::new(
&mut app.account_manager,
SimpleProfilePreviewController::new(&app.ndb, &mut app.img_cache),
)
.inner(ui);
});
main_panel(&ctx.style()).show(ctx, |ui| {
DesktopGlobalPopup::new(app).ui(ui);
Expand Down Expand Up @@ -731,7 +736,13 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, timelines: us
.sizes(sizes, timelines)
.clip(true)
.horizontal(|mut strip| {
strip.cell(crate::ui::DesktopSidePanel::inner);
strip.cell(|ui| {
DesktopSidePanel::new(
&mut app.account_manager,
SimpleProfilePreviewController::new(&app.ndb, &mut app.img_cache),
)
.inner(ui)
});

for timeline_ind in 0..timelines {
strip.cell(|ui| timeline::timeline_view(ui, app, timeline_ind));
Expand Down
7 changes: 5 additions & 2 deletions src/ui/global_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> DesktopGlobalPopup<'a> {
mod preview {
use crate::{
test_data,
ui::{DesktopSidePanel, Preview, View},
ui::{profile::SimpleProfilePreviewController, DesktopSidePanel, Preview, View},
Damus,
};

Expand Down Expand Up @@ -157,7 +157,10 @@ mod preview {

impl View for GlobalPopupPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
let mut panel = DesktopSidePanel::new();
let mut panel = DesktopSidePanel::new(
&mut self.app.account_manager,
SimpleProfilePreviewController::new(&self.app.ndb, &mut self.app.img_cache),
);
DesktopSidePanel::panel().show(ui.ctx(), |ui| panel.ui(ui));
DesktopGlobalPopup::new(&mut self.app).ui(ui);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/profile/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn get_display_name<'a>(profile: &'a ProfileRecord<'a>) -> DisplayName<'a> {
}
}

fn get_profile_url<'a>(profile: &'a ProfileRecord<'a>) -> &'a str {
pub fn get_profile_url<'a>(profile: &'a ProfileRecord<'a>) -> &'a str {
if let Some(url) = profile.record().profile().and_then(|p| p.picture()) {
url
} else {
Expand Down
24 changes: 19 additions & 5 deletions src/ui/profile/profile_preview_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::{
ui::state_in_memory::STATE_ACCOUNT_SWITCHER, DisplayName,
};

use super::preview::{get_display_name, SimpleProfilePreview};
use super::{
preview::{get_display_name, get_profile_url, SimpleProfilePreview},
ProfilePic,
};

pub struct SimpleProfilePreviewController<'a> {
ndb: &'a Ndb,
Expand Down Expand Up @@ -117,7 +120,7 @@ impl<'a> SimpleProfilePreviewController<'a> {
}

pub fn show_with_nickname(
&'a self,
&self,
ui: &mut egui::Ui,
key: &Pubkey,
ui_element: fn(ui: &mut egui::Ui, username: &DisplayName) -> egui::Response,
Expand All @@ -133,10 +136,21 @@ impl<'a> SimpleProfilePreviewController<'a> {
}

pub fn show_with_pfp(
&'a self,
&mut self,
ui: &mut egui::Ui,
key: &Pubkey,
ui_element: fn(ui: &mut egui::Ui, preview: SimpleProfilePreview) -> egui::Response,
) {
ui_element: fn(ui: &mut egui::Ui, pfp: ProfilePic) -> egui::Response,
) -> Option<egui::Response> {
if let Ok(txn) = Transaction::new(self.ndb) {
let profile = self.ndb.get_profile_by_pubkey(&txn, key.bytes());

if let Ok(profile) = profile {
return Some(ui_element(
ui,
ProfilePic::new(self.img_cache, get_profile_url(&profile)),
));
}
}
None
}
}
91 changes: 71 additions & 20 deletions src/ui/side_panel.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
use egui::{Button, Layout, SidePanel, Vec2};
use egui::{Button, Layout, SidePanel, Vec2, Widget};

use crate::ui::global_popup::GlobalPopupType;
use crate::{account_manager::AccountManager, ui::global_popup::GlobalPopupType};

use super::{
profile::SimpleProfilePreviewController,
state_in_memory::{STATE_ACCOUNT_SWITCHER, STATE_SIDE_PANEL},
View,
ProfilePic, View,
};

#[derive(Default)]
pub struct DesktopSidePanel {}
pub struct DesktopSidePanel<'a> {
account_manager: &'a mut AccountManager,
simple_preview_controller: SimpleProfilePreviewController<'a>,
}

static ID: &str = "left panel";

impl View for DesktopSidePanel {
impl<'a> View for DesktopSidePanel<'a> {
fn ui(&mut self, ui: &mut egui::Ui) {
DesktopSidePanel::inner(ui);
self.inner(ui);
}
}

impl DesktopSidePanel {
pub fn new() -> Self {
DesktopSidePanel::default()
impl<'a> DesktopSidePanel<'a> {
pub fn new(
account_manager: &'a mut AccountManager,
simple_preview_controller: SimpleProfilePreviewController<'a>,
) -> Self {
DesktopSidePanel {
account_manager,
simple_preview_controller,
}
}

pub fn inner(ui: &mut egui::Ui) {
pub fn inner(&mut self, ui: &mut egui::Ui) {
let dark_mode = ui.ctx().style().visuals.dark_mode;
let spacing_amt = 16.0;
ui.with_layout(Layout::bottom_up(egui::Align::Center), |ui| {
ui.add_space(spacing_amt);
if ui
.add_sized(Vec2::new(32.0, 32.0), Button::new("A"))
.clicked()
{
if self.pfp_button(ui).clicked() {
STATE_SIDE_PANEL.set_state(ui.ctx(), Some(GlobalPopupType::AccountSwitcher));
let previous_val = STATE_ACCOUNT_SWITCHER.get_state(ui.ctx());
STATE_ACCOUNT_SWITCHER.set_state(ui.ctx(), !previous_val);
Expand All @@ -44,18 +50,49 @@ impl DesktopSidePanel {
});
}

fn pfp_button(&mut self, ui: &mut egui::Ui) -> egui::Response {
if let Some(selected_account) = self.account_manager.get_selected_account() {
if let Some(response) = self.simple_preview_controller.show_with_pfp(
ui,
&selected_account.key.pubkey,
show_pfp(),
) {
return response;
}
}

add_button_to_ui(ui, no_account_pfp())
}

pub fn panel() -> SidePanel {
egui::SidePanel::left(ID).resizable(false).exact_width(40.0)
}
}

fn show_pfp() -> fn(ui: &mut egui::Ui, pfp: ProfilePic) -> egui::Response {
|ui, pfp| {
let response = pfp.ui(ui);
ui.allocate_rect(response.rect, egui::Sense::click())
}
}

fn settings_button(dark_mode: bool) -> egui::Button<'static> {
let _ = dark_mode;
let img_data = egui::include_image!("../../assets/icons/settings_dark_4x.png");

egui::Button::image(egui::Image::new(img_data).max_width(32.0)).frame(false)
}

fn add_button_to_ui(ui: &mut egui::Ui, button: Button) -> egui::Response {
ui.add_sized(Vec2::new(32.0, 32.0), button)
}

fn no_account_pfp() -> Button<'static> {
Button::new("A")
.rounding(20.0)
.min_size(Vec2::new(38.0, 38.0))
}

fn add_column_button(dark_mode: bool) -> egui::Button<'static> {
let _ = dark_mode;
let img_data = egui::include_image!("../../assets/icons/add_column_dark_4x.png");
Expand All @@ -64,26 +101,40 @@ fn add_column_button(dark_mode: bool) -> egui::Button<'static> {
}

mod preview {
use crate::ui::Preview;
use nostrdb::Ndb;

use crate::{imgcache::ImageCache, test_data, ui::Preview};

use super::*;

pub struct DesktopSidePanelPreview {}
pub struct DesktopSidePanelPreview {
account_manager: AccountManager,
ndb: Ndb,
img_cache: ImageCache,
}

impl DesktopSidePanelPreview {
fn new() -> Self {
DesktopSidePanelPreview {}
let (account_manager, ndb, img_cache) = test_data::get_accmgr_and_ndb_and_imgcache();
DesktopSidePanelPreview {
account_manager,
ndb,
img_cache,
}
}
}

impl View for DesktopSidePanelPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
let mut panel = DesktopSidePanel::new();
let mut panel = DesktopSidePanel::new(
&mut self.account_manager,
SimpleProfilePreviewController::new(&self.ndb, &mut self.img_cache),
);
DesktopSidePanel::panel().show(ui.ctx(), |ui| panel.ui(ui));
}
}

impl Preview for DesktopSidePanel {
impl<'a> Preview for DesktopSidePanel<'a> {
type Prev = DesktopSidePanelPreview;

fn preview() -> Self::Prev {
Expand Down

0 comments on commit 1b0de88

Please sign in to comment.