Skip to content

Commit 3909fb8

Browse files
committed
wip switch to profile pictures in header
Signed-off-by: William Casarin <[email protected]>
1 parent de1fec3 commit 3909fb8

File tree

2 files changed

+119
-75
lines changed

2 files changed

+119
-75
lines changed

src/nav.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,14 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> RenderNavRe
249249
.returning(app.columns_mut().column_mut(col).router_mut().returning)
250250
.id_source(egui::Id::new(col_id))
251251
.show_mut(ui, |ui, render_type, nav| match render_type {
252-
NavUiType::Title => NavTitle::new(&app.columns, nav.routes_arr()).show(ui),
252+
NavUiType::Title => NavTitle::new(
253+
&app.ndb,
254+
&mut app.img_cache,
255+
&app.columns,
256+
app.accounts.get_selected_account().map(|a| &a.pubkey),
257+
nav.routes_arr(),
258+
)
259+
.show(ui),
253260
NavUiType::Body => render_nav_body(ui, app, nav.routes().last().expect("top"), col),
254261
});
255262

src/ui/column/header.rs

+111-74
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
use crate::{
2-
app_style::{get_font_size, NotedeckTextStyle},
2+
app_style::NotedeckTextStyle,
33
column::Columns,
4-
fonts::NamedFontFamily,
4+
imgcache::ImageCache,
55
nav::RenderNavAction,
66
route::Route,
7-
ui::anim::{AnimationHelper, ICON_EXPANSION_MULTIPLE},
7+
timeline::{TimelineId, TimelineRoute},
8+
ui::{
9+
self,
10+
anim::{AnimationHelper, ICON_EXPANSION_MULTIPLE},
11+
},
812
};
913

10-
use egui::{pos2, Color32, Stroke};
14+
use egui::{pos2, RichText, Stroke};
15+
use enostr::Pubkey;
16+
use nostrdb::{Ndb, Transaction};
1117

1218
pub struct NavTitle<'a> {
19+
ndb: &'a Ndb,
20+
img_cache: &'a mut ImageCache,
1321
columns: &'a Columns,
22+
deck_author: Option<&'a Pubkey>,
1423
routes: &'a [Route],
1524
}
1625

1726
impl<'a> NavTitle<'a> {
18-
pub fn new(columns: &'a Columns, routes: &'a [Route]) -> Self {
19-
NavTitle { columns, routes }
27+
pub fn new(
28+
ndb: &'a Ndb,
29+
img_cache: &'a mut ImageCache,
30+
columns: &'a Columns,
31+
deck_author: Option<&'a Pubkey>,
32+
routes: &'a [Route],
33+
) -> Self {
34+
NavTitle {
35+
ndb,
36+
img_cache,
37+
columns,
38+
deck_author,
39+
routes,
40+
}
2041
}
2142

2243
pub fn show(&mut self, ui: &mut egui::Ui) -> Option<RenderNavAction> {
@@ -34,7 +55,6 @@ impl<'a> NavTitle<'a> {
3455
) -> Option<RenderNavAction> {
3556
let icon_width = 32.0;
3657
let padding_external = 16.0;
37-
let padding_internal = 8.0;
3858
let has_back = prev(self.routes).is_some();
3959

4060
let (spacing_rect, titlebar_rect) = allocated_response
@@ -43,7 +63,8 @@ impl<'a> NavTitle<'a> {
4363

4464
ui.advance_cursor_after_rect(spacing_rect);
4565

46-
let (titlebar_resp, back_button_resp) = if has_back {
66+
// TODO: what are we doing with titlebar_response ?
67+
let (_titlebar_resp, back_button_resp) = if has_back {
4768
let (button_rect, titlebar_rect) = titlebar_rect.split_left_right_at_x(
4869
allocated_response.rect.left() + icon_width + padding_external,
4970
);
@@ -55,20 +76,7 @@ impl<'a> NavTitle<'a> {
5576
(allocated_response, None)
5677
};
5778

58-
self.title(
59-
ui,
60-
self.routes.last().unwrap(),
61-
titlebar_resp.rect,
62-
icon_width,
63-
if has_back {
64-
padding_internal
65-
} else {
66-
padding_external
67-
},
68-
);
69-
70-
let delete_button_resp =
71-
self.delete_column_button(ui, titlebar_resp, icon_width, padding_external);
79+
let delete_button_resp = self.title(ui, self.routes.last().unwrap());
7280

7381
if delete_button_resp.clicked() {
7482
Some(RenderNavAction::RemoveColumn)
@@ -118,13 +126,7 @@ impl<'a> NavTitle<'a> {
118126
helper.take_animation_response()
119127
}
120128

121-
fn delete_column_button(
122-
&self,
123-
ui: &mut egui::Ui,
124-
allocation_response: egui::Response,
125-
icon_width: f32,
126-
padding: f32,
127-
) -> egui::Response {
129+
fn delete_column_button(&self, ui: &mut egui::Ui, icon_width: f32) -> egui::Response {
128130
let img_size = 16.0;
129131
let max_size = icon_width * ICON_EXPANSION_MULTIPLE;
130132

@@ -135,20 +137,8 @@ impl<'a> NavTitle<'a> {
135137
};
136138
let img = egui::Image::new(img_data).max_width(img_size);
137139

138-
let button_rect = {
139-
let titlebar_rect = allocation_response.rect;
140-
let titlebar_width = titlebar_rect.width();
141-
let titlebar_center = titlebar_rect.center();
142-
let button_center_y = titlebar_center.y;
143-
let button_center_x =
144-
titlebar_center.x + (titlebar_width / 2.0) - (max_size / 2.0) - padding;
145-
egui::Rect::from_center_size(
146-
pos2(button_center_x, button_center_y),
147-
egui::vec2(max_size, max_size),
148-
)
149-
};
150-
151-
let helper = AnimationHelper::new_from_rect(ui, "delete-column-button", button_rect);
140+
let helper =
141+
AnimationHelper::new(ui, "delete-column-button", egui::vec2(max_size, max_size));
152142

153143
let cur_img_size = helper.scale_1d_pos_min_max(0.0, img_size);
154144

@@ -160,42 +150,89 @@ impl<'a> NavTitle<'a> {
160150
animation_resp
161151
}
162152

163-
fn title(
164-
&mut self,
165-
ui: &mut egui::Ui,
166-
top: &Route,
167-
titlebar_rect: egui::Rect,
168-
icon_width: f32,
169-
padding: f32,
170-
) {
171-
let painter = ui.painter_at(titlebar_rect);
172-
173-
let font = egui::FontId::new(
174-
get_font_size(ui.ctx(), &NotedeckTextStyle::Body),
175-
egui::FontFamily::Name(NamedFontFamily::Bold.as_str().into()),
176-
);
153+
fn pubkey_pfp<'txn, 'me>(
154+
&'me mut self,
155+
txn: &'txn Transaction,
156+
pubkey: &[u8; 32],
157+
pfp_size: f32,
158+
) -> Option<ui::ProfilePic<'me, 'txn>> {
159+
self.ndb
160+
.get_profile_by_pubkey(txn, pubkey)
161+
.as_ref()
162+
.ok()
163+
.and_then(move |p| {
164+
Some(ui::ProfilePic::from_profile(self.img_cache, p)?.size(pfp_size))
165+
})
166+
}
177167

178-
let max_title_width = titlebar_rect.width() - icon_width - padding * 2.;
168+
fn timeline_pfp(&mut self, ui: &mut egui::Ui, id: TimelineId, pfp_size: f32) {
169+
let txn = Transaction::new(self.ndb).unwrap();
170+
171+
if let Some(pfp) = self
172+
.columns
173+
.find_timeline(id)
174+
.and_then(|tl| tl.kind.pubkey_source())
175+
.and_then(|pksrc| self.deck_author.map(|da| pksrc.to_pubkey(da)))
176+
.and_then(|pk| self.pubkey_pfp(&txn, pk.bytes(), pfp_size))
177+
{
178+
ui.add(pfp);
179+
} else {
180+
ui.add(
181+
ui::ProfilePic::new(self.img_cache, ui::ProfilePic::no_pfp_url()).size(pfp_size),
182+
);
183+
}
184+
}
179185

180-
let title_galley = ui.fonts(|f| {
181-
f.layout(
182-
top.title(self.columns).to_string(),
183-
font,
184-
ui.visuals().text_color(),
185-
max_title_width,
186-
)
187-
});
186+
fn title_pfp(&mut self, ui: &mut egui::Ui, top: &Route) {
187+
let pfp_size = 32.0;
188+
match top {
189+
Route::Timeline(tlr) => match tlr {
190+
TimelineRoute::Timeline(tlid) => {
191+
self.timeline_pfp(ui, *tlid, pfp_size);
192+
}
193+
194+
TimelineRoute::Thread(_note_id) => {}
195+
TimelineRoute::Reply(_note_id) => {}
196+
TimelineRoute::Quote(_note_id) => {}
197+
198+
TimelineRoute::Profile(pubkey) => {
199+
let txn = Transaction::new(self.ndb).unwrap();
200+
if let Some(pfp) = self.pubkey_pfp(&txn, pubkey.bytes(), pfp_size) {
201+
ui.add(pfp);
202+
} else {
203+
ui.add(
204+
ui::ProfilePic::new(self.img_cache, ui::ProfilePic::no_pfp_url())
205+
.size(pfp_size),
206+
);
207+
}
208+
}
209+
},
188210

189-
let pos = {
190-
let titlebar_center = titlebar_rect.center();
191-
let text_height = title_galley.rect.height();
211+
Route::Accounts(_as) => {}
212+
Route::ComposeNote => {}
213+
Route::AddColumn(_add_col_route) => {}
214+
Route::Support => {}
215+
Route::Relays => {}
216+
}
217+
}
192218

193-
let galley_pos_x = titlebar_rect.left() + padding;
194-
let galley_pos_y = titlebar_center.y - (text_height / 2.);
195-
pos2(galley_pos_x, galley_pos_y)
196-
};
219+
fn title(&mut self, ui: &mut egui::Ui, top: &Route) -> egui::Response {
220+
ui.horizontal(|ui| {
221+
ui.spacing_mut().item_spacing.x = 10.0;
222+
223+
self.title_pfp(ui, top);
224+
225+
ui.label(
226+
RichText::new(top.title(self.columns))
227+
.text_style(NotedeckTextStyle::Body.text_style()),
228+
);
197229

198-
painter.galley(pos, title_galley, Color32::WHITE);
230+
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
231+
self.delete_column_button(ui, 32.0)
232+
})
233+
.inner
234+
})
235+
.inner
199236
}
200237
}
201238

0 commit comments

Comments
 (0)