From ab80931e7f604d7f540b9bd0b59887630fd9e483 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sun, 15 Jan 2023 16:04:26 +1300 Subject: [PATCH] Nav --- src/ui/feed.rs | 41 ++++++++----------------- src/ui/help/mod.rs | 6 ++-- src/ui/mod.rs | 66 ++++++++++++++++++++++++++++++++++++----- src/ui/people/mod.rs | 6 +--- src/ui/people/person.rs | 3 +- 5 files changed, 77 insertions(+), 45 deletions(-) diff --git a/src/ui/feed.rs b/src/ui/feed.rs index 920abd1c..ec253038 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -9,7 +9,7 @@ use egui::{ Separator, Stroke, TextEdit, TextStyle, Ui, Vec2, }; use linkify::{LinkFinder, LinkKind}; -use nostr_types::{Event, EventKind, Id, IdHex, PublicKeyHex, Tag}; +use nostr_types::{Event, EventKind, Id, IdHex, Tag}; struct FeedPostParams { id: Id, @@ -19,8 +19,7 @@ struct FeedPostParams { } pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui: &mut Ui) { - let mut feed_kind = GLOBALS.feed.get_feed_kind(); - app.page = Page::Feed(feed_kind.clone()); + let feed_kind = GLOBALS.feed.get_feed_kind(); // Feed Page Selection ui.horizontal(|ui| { @@ -31,10 +30,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram )) .clicked() { - feed_kind = FeedKind::General; - app.page = Page::Feed(feed_kind.clone()); - GLOBALS.feed.set_feed_to_general(); - GLOBALS.events.clear_new(); + app.set_page(Page::Feed(FeedKind::General)); } ui.separator(); if ui @@ -44,10 +40,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram )) .clicked() { - feed_kind = FeedKind::Replies; - app.page = Page::Feed(feed_kind.clone()); - GLOBALS.feed.set_feed_to_replies(); - GLOBALS.events.clear_new(); + app.set_page(Page::Feed(FeedKind::Replies)); } if matches!(feed_kind.clone(), FeedKind::Thread(..)) { ui.separator(); @@ -76,7 +69,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram ui.horizontal(|ui| { ui.label("You need to "); if ui.link("setup an identity").clicked() { - app.page = Page::Relays; + app.set_page(Page::Relays); } ui.label(" to see any replies to that identity."); }); @@ -103,7 +96,7 @@ fn posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui ui.horizontal(|ui| { ui.label("You need to "); if ui.link("setup your identity").clicked() { - app.page = Page::You; + app.set_page(Page::You); } ui.label(" to post."); }); @@ -111,7 +104,7 @@ fn posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui ui.horizontal(|ui| { ui.label("You need to "); if ui.link("choose relays").clicked() { - app.page = Page::Relays; + app.set_page(Page::Relays); } ui.label(" to post."); }); @@ -460,7 +453,7 @@ fn render_post_actual( ) .clicked() { - set_person_view(app, &event.pubkey.into()); + app.set_page(Page::Person(event.pubkey.into())); }; // Everything else next @@ -481,8 +474,7 @@ fn render_post_actual( // the parent might not exist and that would leave us // stranded, but we KNOW the child exists, and it's // ancestors will render when they become available. - GLOBALS.feed.set_feed_to_thread(event.id); - app.page = Page::Feed(FeedKind::Thread(event.id)); + app.set_page(Page::Feed(FeedKind::Thread(event.id))); }; ui.reset_style(); } @@ -496,8 +488,7 @@ fn render_post_actual( ui.with_layout(Layout::right_to_left(Align::TOP), |ui| { ui.menu_button(RichText::new("≡").size(28.0), |ui| { if !is_main_event && ui.button("View Thread").clicked() { - GLOBALS.feed.set_feed_to_thread(event.id); - app.page = Page::Feed(FeedKind::Thread(event.id)); + app.set_page(Page::Feed(FeedKind::Thread(event.id))); } if ui.button("Copy ID").clicked() { ui.output().copied_text = event.id.as_hex_string(); @@ -514,8 +505,7 @@ fn render_post_actual( if !is_main_event && ui.button("➤").on_hover_text("View Thread").clicked() { - GLOBALS.feed.set_feed_to_thread(event.id); - app.page = Page::Feed(FeedKind::Thread(event.id)); + app.set_page(Page::Feed(FeedKind::Thread(event.id))); } ui.label( @@ -629,15 +619,14 @@ fn render_content(app: &mut GossipUi, ui: &mut Ui, tag_re: ®ex::Regex, event: None => format!("@{}", GossipUi::hex_pubkey_short(pubkey)), }; if ui.link(&nam).clicked() { - set_person_view(app, pubkey); + app.set_page(Page::Person(pubkey.to_owned())); }; } Tag::Event { id, .. } => { let idhex: IdHex = (*id).into(); let nam = format!("#{}", GossipUi::hex_id_short(&idhex)); if ui.link(&nam).clicked() { - GLOBALS.feed.set_feed_to_thread(*id); - app.page = Page::Feed(FeedKind::Thread(*id)); + app.set_page(Page::Feed(FeedKind::Thread(*id))); }; } Tag::Hashtag(s) => { @@ -661,10 +650,6 @@ fn render_content(app: &mut GossipUi, ui: &mut Ui, tag_re: ®ex::Regex, event: } } -fn set_person_view(app: &mut GossipUi, pubkeyhex: &PublicKeyHex) { - app.page = Page::Person(pubkeyhex.to_owned()); -} - fn thin_red_separator(ui: &mut Ui) { let mut style = ui.style_mut(); style.visuals.widgets.noninteractive.bg_stroke = Stroke { diff --git a/src/ui/help/mod.rs b/src/ui/help/mod.rs index b20550c7..4772cb32 100644 --- a/src/ui/help/mod.rs +++ b/src/ui/help/mod.rs @@ -54,7 +54,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra ui.horizontal_wrapped(|ui| { ui.label("To get started, go to the"); if ui.link("People > Follow Someone New").clicked() { - app.page = Page::PeopleFollow; + app.set_page(Page::PeopleFollow); } ui.label("page and add people to follow. If you don't know anybody, you can follow me at NIP-05 DNS ID mike@mikedilger.com and you can find other people through me (posts I reply to or quote)."); }); @@ -90,7 +90,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra ui.horizontal_wrapped(|ui| { ui.label("On the"); if ui.link("You").clicked() { - app.page = Page::You; + app.set_page(Page::You); } ui.label("page you can setup your identity. If you are new, you should just press \"Generate\" and you are good to go. Otherwise you can import a private key in hex or bech32 format, although it isn't very secure to cut-n-paste and display your private key, so it will mark your key security as \"weak\". Eventually you'll be able to import your password-protected private key from a nostr relay."); }); @@ -115,7 +115,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra ui.horizontal_wrapped(|ui| { ui.label("Go to the"); if ui.link("Relays").clicked() { - app.page = Page::Relays; + app.set_page(Page::Relays); } ui.label("page and tick a half dozen relays that you intend to post to. If your webserver serves a nostr.json file, you can follow NIP-05 and use the same relays in that file."); }); diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e792d6c2..65ebaecd 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -53,7 +53,7 @@ pub fn run() -> Result<(), Error> { Ok(()) } -#[derive(PartialEq)] +#[derive(Debug, Clone, PartialEq)] enum Page { Feed(FeedKind), PeopleList, @@ -70,6 +70,7 @@ enum Page { struct GossipUi { next_frame: Instant, page: Page, + history: Vec, about: About, icon: TextureHandle, placeholder_avatar: TextureHandle, @@ -143,6 +144,7 @@ impl GossipUi { GossipUi { next_frame: Instant::now(), page: Page::Feed(FeedKind::General), + history: vec![], about: crate::about::about(), icon: icon_texture_handle, placeholder_avatar: placeholder_avatar_texture_handle, @@ -164,6 +166,45 @@ impl GossipUi { tag_re: regex::Regex::new(r"(\#\[\d+\])").unwrap(), } } + + fn set_page(&mut self, page: Page) { + if self.page != page { + tracing::debug!("PUSHING HISTORY: {:?}", &self.page); + self.history.push(self.page.clone()); + self.set_page_inner(page); + } + } + + fn back(&mut self) { + if let Some(page) = self.history.pop() { + tracing::debug!("POPPING HISTORY: {:?}", &page); + self.set_page_inner(page); + } else { + tracing::debug!("HISTORY STUCK ON NONE"); + } + } + + fn set_page_inner(&mut self, page: Page) { + // Setting the page often requires some associated actions: + match &page { + Page::Feed(FeedKind::General) => { + GLOBALS.feed.set_feed_to_general(); + GLOBALS.events.clear_new(); + } + Page::Feed(FeedKind::Replies) => { + GLOBALS.feed.set_feed_to_replies(); + GLOBALS.events.clear_new(); + } + Page::Feed(FeedKind::Thread(id)) => { + GLOBALS.feed.set_feed_to_thread(*id); + } + Page::Feed(FeedKind::Person(pubkey)) => { + GLOBALS.feed.set_feed_to_person(pubkey.to_owned()); + } + _ => {} + } + self.page = page; + } } impl eframe::App for GossipUi { @@ -186,6 +227,17 @@ impl eframe::App for GossipUi { egui::TopBottomPanel::top("menu").show(ctx, |ui| { ui.horizontal(|ui| { + let len = self.history.len(); + let back_label_text = RichText::new(format!("< Back {}", len)); + let label = if self.history.is_empty() { + Label::new(back_label_text.weak()) + } else { + Label::new(back_label_text).sense(Sense::click()) + }; + if ui.add(label).clicked() { + self.back(); + } + ui.separator(); if ui .add(SelectableLabel::new( matches!(self.page, Page::Feed(_)), @@ -193,7 +245,7 @@ impl eframe::App for GossipUi { )) .clicked() { - self.page = Page::Feed(FeedKind::General); + self.set_page(Page::Feed(FeedKind::General)); GLOBALS.events.clear_new(); } ui.separator(); @@ -206,21 +258,21 @@ impl eframe::App for GossipUi { )) .clicked() { - self.page = Page::PeopleList; + self.set_page(Page::PeopleList); } ui.separator(); if ui .add(SelectableLabel::new(self.page == Page::You, "You")) .clicked() { - self.page = Page::You; + self.set_page(Page::You); } ui.separator(); if ui .add(SelectableLabel::new(self.page == Page::Relays, "Relays")) .clicked() { - self.page = Page::Relays; + self.set_page(Page::Relays); } ui.separator(); if ui @@ -230,7 +282,7 @@ impl eframe::App for GossipUi { )) .clicked() { - self.page = Page::Settings; + self.set_page(Page::Settings); } ui.separator(); if ui @@ -242,7 +294,7 @@ impl eframe::App for GossipUi { )) .clicked() { - self.page = Page::HelpHelp; + self.set_page(Page::HelpHelp); } ui.separator(); }); diff --git a/src/ui/people/mod.rs b/src/ui/people/mod.rs index 71c05e49..76aad4eb 100644 --- a/src/ui/people/mod.rs +++ b/src/ui/people/mod.rs @@ -84,7 +84,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra ) .clicked() { - set_person_view(app, person); + app.set_page(Page::Person(person.pubkey.clone())); }; ui.vertical(|ui| { @@ -112,7 +112,3 @@ fn get_name(person: &DbPerson) -> String { GossipUi::hex_pubkey_short(&person.pubkey) } } - -fn set_person_view(app: &mut GossipUi, person: &DbPerson) { - app.page = Page::Person(person.pubkey.clone()); -} diff --git a/src/ui/people/person.rs b/src/ui/people/person.rs index 26a54338..ad665590 100644 --- a/src/ui/people/person.rs +++ b/src/ui/people/person.rs @@ -65,8 +65,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra } if ui.button("VIEW THEIR FEED").clicked() { - GLOBALS.feed.set_feed_to_person(pubkeyhex.clone()); - app.page = Page::Feed(FeedKind::Person(pubkeyhex.clone())); + app.set_page(Page::Feed(FeedKind::Person(pubkeyhex.clone()))); } }