Skip to content

Commit bb25fd4

Browse files
kernelkindjb55
authored andcommitted
AccountSelectionWidget
Will be useful for selecting an account for the 'Add Column' view Signed-off-by: kernelkind <[email protected]> Signed-off-by: William Casarin <[email protected]>
1 parent e9c3596 commit bb25fd4

File tree

4 files changed

+173
-37
lines changed

4 files changed

+173
-37
lines changed

src/account_manager.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ impl<'a> SimpleProfilePreviewController<'a> {
5353

5454
to_remove
5555
}
56+
57+
pub fn view_profile_previews(
58+
&mut self,
59+
account_manager: &'a AccountManager<'a>,
60+
ui: &mut egui::Ui,
61+
add_preview_ui: fn(ui: &mut egui::Ui, preview: SimpleProfilePreview, index: usize) -> bool,
62+
) -> Option<usize> {
63+
let mut clicked_at: Option<usize> = None;
64+
65+
for i in 0..account_manager.num_accounts() {
66+
if let Some(account) = account_manager.get_account(i) {
67+
if let Ok(txn) = Transaction::new(self.ndb) {
68+
let profile = self
69+
.ndb
70+
.get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes());
71+
72+
if let Ok(profile) = profile {
73+
let preview = SimpleProfilePreview::new(&profile, self.img_cache);
74+
75+
if add_preview_ui(ui, preview, i) {
76+
clicked_at = Some(i)
77+
}
78+
}
79+
}
80+
}
81+
}
82+
83+
clicked_at
84+
}
5685
}
5786

5887
/// The interface for managing the user's accounts.

src/ui/account_management.rs

Lines changed: 140 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use egui::{Align, Align2, Button, Frame, Layout, Margin, RichText, ScrollArea, Vec2, Window};
1+
use egui::{
2+
Align, Align2, Button, Frame, Id, Layout, Margin, RichText, ScrollArea, Sense, Vec2, Window,
3+
};
24

35
use crate::{
4-
account_manager::{AccountManager, SimpleProfilePreviewController},
6+
account_manager::{AccountManager, SimpleProfilePreviewController, UserAccount},
57
app_style::NotedeckTextStyle,
68
ui::{self, Preview, View},
79
};
@@ -237,6 +239,56 @@ fn delete_button(_dark_mode: bool) -> egui::Button<'static> {
237239
egui::Button::image(egui::Image::new(img_data).max_width(30.0)).frame(true)
238240
}
239241

242+
pub struct AccountSelectionWidget<'a> {
243+
account_manager: AccountManager<'a>,
244+
simple_preview_controller: SimpleProfilePreviewController<'a>,
245+
}
246+
247+
impl<'a> AccountSelectionWidget<'a> {
248+
fn ui(&'a mut self, ui: &mut egui::Ui) -> Option<&'a UserAccount> {
249+
let mut result: Option<&'a UserAccount> = None;
250+
scroll_area().show(ui, |ui| {
251+
ui.horizontal_wrapped(|ui| {
252+
let clicked_at = self.simple_preview_controller.view_profile_previews(
253+
&self.account_manager,
254+
ui,
255+
|ui, preview, index| {
256+
let resp = ui.add_sized(preview.dimensions(), |ui: &mut egui::Ui| {
257+
simple_preview_frame(ui)
258+
.show(ui, |ui| {
259+
ui.vertical_centered(|ui| {
260+
ui.add(preview);
261+
});
262+
})
263+
.response
264+
});
265+
266+
ui.interact(resp.rect, Id::new(index), Sense::click())
267+
.clicked()
268+
},
269+
);
270+
271+
if let Some(index) = clicked_at {
272+
result = self.account_manager.get_account(index);
273+
};
274+
});
275+
});
276+
result
277+
}
278+
}
279+
280+
impl<'a> AccountSelectionWidget<'a> {
281+
pub fn new(
282+
account_manager: AccountManager<'a>,
283+
simple_preview_controller: SimpleProfilePreviewController<'a>,
284+
) -> Self {
285+
AccountSelectionWidget {
286+
account_manager,
287+
simple_preview_controller,
288+
}
289+
}
290+
}
291+
240292
// PREVIEWS
241293

242294
mod preview {
@@ -246,51 +298,59 @@ mod preview {
246298
use super::*;
247299
use crate::key_storage::KeyStorage;
248300
use crate::relay_generation::RelayGenerator;
249-
use crate::{account_manager::UserAccount, imgcache::ImageCache, test_data};
301+
use crate::{imgcache::ImageCache, test_data};
250302
use std::path::Path;
251303

304+
const ACCOUNT_HEXES: [&str; 10] = [
305+
"3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681",
306+
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
307+
"bd1e19980e2c91e6dc657e92c25762ca882eb9272d2579e221f037f93788de91",
308+
"5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
309+
"4c96d763eb2fe01910f7e7220b7c7ecdbe1a70057f344b9f79c28af080c3ee30",
310+
"edf16b1dd61eab353a83af470cc13557029bff6827b4cb9b7fc9bdb632a2b8e6",
311+
"3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681",
312+
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
313+
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
314+
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
315+
];
316+
252317
pub struct AccountManagementPreview {
253318
accounts: Vec<UserAccount>,
254319
ndb: Ndb,
255320
img_cache: ImageCache,
256321
edit_mode: bool,
257322
}
258323

259-
impl AccountManagementPreview {
260-
fn new() -> Self {
261-
let account_hexes = [
262-
"3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681",
263-
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
264-
"bd1e19980e2c91e6dc657e92c25762ca882eb9272d2579e221f037f93788de91",
265-
"5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
266-
"4c96d763eb2fe01910f7e7220b7c7ecdbe1a70057f344b9f79c28af080c3ee30",
267-
"edf16b1dd61eab353a83af470cc13557029bff6827b4cb9b7fc9bdb632a2b8e6",
268-
"3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681",
269-
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
270-
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
271-
"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
272-
];
273-
274-
let accounts: Vec<UserAccount> = account_hexes
275-
.iter()
276-
.map(|account_hex| {
277-
let key = Keys::from_public_key(PublicKey::from_hex(account_hex).unwrap());
324+
fn get_accounts() -> Vec<UserAccount> {
325+
ACCOUNT_HEXES
326+
.iter()
327+
.map(|account_hex| {
328+
let key = Keys::from_public_key(PublicKey::from_hex(account_hex).unwrap());
278329

279-
UserAccount {
280-
key,
281-
relays: test_data::sample_pool(),
282-
}
283-
})
284-
.collect();
330+
UserAccount {
331+
key,
332+
relays: test_data::sample_pool(),
333+
}
334+
})
335+
.collect()
336+
}
285337

286-
let mut config = Config::new();
287-
config.set_ingester_threads(2);
338+
fn get_ndb_and_img_cache() -> (Ndb, ImageCache) {
339+
let mut config = Config::new();
340+
config.set_ingester_threads(2);
288341

289-
let db_dir = Path::new(".");
290-
let path = db_dir.to_str().unwrap();
291-
let ndb = Ndb::new(path, &config).expect("ndb");
292-
let imgcache_dir = db_dir.join("cache/img");
293-
let img_cache = ImageCache::new(imgcache_dir);
342+
let db_dir = Path::new(".");
343+
let path = db_dir.to_str().unwrap();
344+
let ndb = Ndb::new(path, &config).expect("ndb");
345+
let imgcache_dir = db_dir.join("cache/img");
346+
let img_cache = ImageCache::new(imgcache_dir);
347+
(ndb, img_cache)
348+
}
349+
350+
impl AccountManagementPreview {
351+
fn new() -> Self {
352+
let accounts = get_accounts();
353+
let (ndb, img_cache) = get_ndb_and_img_cache();
294354

295355
AccountManagementPreview {
296356
accounts,
@@ -325,4 +385,49 @@ mod preview {
325385
AccountManagementPreview::new()
326386
}
327387
}
388+
389+
pub struct AccountSelectionPreview {
390+
accounts: Vec<UserAccount>,
391+
ndb: Ndb,
392+
img_cache: ImageCache,
393+
}
394+
395+
impl AccountSelectionPreview {
396+
fn new() -> Self {
397+
let accounts = get_accounts();
398+
let (ndb, img_cache) = get_ndb_and_img_cache();
399+
AccountSelectionPreview {
400+
accounts,
401+
ndb,
402+
img_cache,
403+
}
404+
}
405+
}
406+
407+
impl View for AccountSelectionPreview {
408+
fn ui(&mut self, ui: &mut egui::Ui) {
409+
let account_manager = AccountManager::new(
410+
&mut self.accounts,
411+
KeyStorage::None,
412+
RelayGenerator::Constant,
413+
);
414+
415+
let mut widget = AccountSelectionWidget::new(
416+
account_manager,
417+
SimpleProfilePreviewController::new(&self.ndb, &mut self.img_cache),
418+
);
419+
420+
if let Some(account) = widget.ui(ui) {
421+
println!("User made selection: {:?}", account.key);
422+
}
423+
}
424+
}
425+
426+
impl<'a> Preview for AccountSelectionWidget<'a> {
427+
type Prev = AccountSelectionPreview;
428+
429+
fn preview() -> Self::Prev {
430+
AccountSelectionPreview::new()
431+
}
432+
}
328433
}

src/ui/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod profile;
88
pub mod relay;
99
pub mod username;
1010

11-
pub use account_management::AccountManagementView;
11+
pub use account_management::{AccountManagementView, AccountSelectionWidget};
1212
pub use mention::Mention;
1313
pub use note::Note;
1414
pub use preview::{Preview, PreviewApp};

src/ui_preview/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use notedeck::app_creation::{
33
};
44
use notedeck::ui::account_login_view::AccountLoginView;
55
use notedeck::ui::{
6-
AccountManagementView, Preview, PreviewApp, ProfilePic, ProfilePreview, RelayView,
6+
AccountManagementView, AccountSelectionWidget, Preview, PreviewApp, ProfilePic, ProfilePreview,
7+
RelayView,
78
};
89
use std::env;
910

@@ -86,5 +87,6 @@ async fn main() {
8687
ProfilePreview,
8788
ProfilePic,
8889
AccountManagementView,
90+
AccountSelectionWidget,
8991
);
9092
}

0 commit comments

Comments
 (0)