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
35use 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
242294mod 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}
0 commit comments