diff --git a/src/ui/dm_chat_list.rs b/src/ui/dm_chat_list.rs index 5e3deab1..aadcb845 100644 --- a/src/ui/dm_chat_list.rs +++ b/src/ui/dm_chat_list.rs @@ -56,11 +56,12 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr .clicked() { app.set_page(Page::Feed(FeedKind::DmChat(channeldata.dm_channel.clone()))); - app.replying_to = None; - app.draft_repost = None; app.show_post_area = true; - app.draft_dm_channel = Some(channeldata.dm_channel); app.draft_needs_focus = false; + + app.draft_data.replying_to = None; + app.draft_data.repost = None; + app.draft_data.dm_channel = Some(channeldata.dm_channel); } }); ui.add_space(20.0); diff --git a/src/ui/feed/note/mod.rs b/src/ui/feed/note/mod.rs index dda31ec6..551a0ff0 100644 --- a/src/ui/feed/note/mod.rs +++ b/src/ui/feed/note/mod.rs @@ -658,10 +658,10 @@ fn render_note_inner( .on_hover_text("Repost") .clicked() { - app.draft_repost = Some(note.event.id); - app.replying_to = None; - app.draft_dm_channel = None; app.show_post_area = true; + app.draft_data.repost = Some(note.event.id); + app.draft_data.replying_to = None; + app.draft_data.dm_channel = None; } ui.add_space(24.0); @@ -675,8 +675,10 @@ fn render_note_inner( .on_hover_text("Quote") .clicked() { - if !app.draft.ends_with(' ') && !app.draft.is_empty() { - app.draft.push(' '); + if !app.draft_data.draft.ends_with(' ') + && !app.draft_data.draft.is_empty() + { + app.draft_data.draft.push(' '); } let event_pointer = EventPointer { id: note.event.id, @@ -694,10 +696,13 @@ fn render_note_inner( kind: None, }; let nostr_url: NostrUrl = event_pointer.into(); - app.draft.push_str(&format!("{}", nostr_url)); - app.draft_repost = None; - app.replying_to = None; - app.draft_dm_channel = None; + app.draft_data + .draft + .push_str(&format!("{}", nostr_url)); + app.draft_data.repost = None; + app.draft_data.replying_to = None; + app.draft_data.dm_channel = None; + app.show_post_area = true; app.draft_needs_focus = true; } @@ -721,16 +726,17 @@ fn render_note_inner( .on_hover_text("Reply") .clicked() { - app.replying_to = + app.draft_data.replying_to = if note.event.kind.is_direct_message_related() { None } else { Some(note.event.id) }; - app.draft_repost = None; - app.show_post_area = true; - app.draft_dm_channel = + app.draft_data.repost = None; + app.draft_data.dm_channel = DmChannel::from_event(¬e.event, None); + + app.show_post_area = true; app.draft_needs_focus = true; } diff --git a/src/ui/feed/post.rs b/src/ui/feed/post.rs index 0ffb0fda..41402d1c 100644 --- a/src/ui/feed/post.rs +++ b/src/ui/feed/post.rs @@ -105,7 +105,7 @@ pub(in crate::ui) fn posting_area( fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui: &mut Ui) { // Maybe render post we are replying to or reposting - if let Some(id) = app.replying_to.or(app.draft_repost) { + if let Some(id) = app.draft_data.replying_to.or(app.draft_data.repost) { ScrollArea::vertical() .max_height(200.0) .override_scroll_delta(Vec2 { @@ -132,7 +132,7 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram let mut send_now: bool = false; - if app.draft_repost.is_none() { + if app.draft_data.repost.is_none() { // Text area let theme = app.settings.theme; let mut layouter = |ui: &Ui, text: &str, wrap_width: f32| { @@ -141,40 +141,40 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram ui.fonts(|f| f.layout_job(layout_job)) }; - if app.include_subject && app.replying_to.is_none() { + if app.draft_data.include_subject && app.draft_data.replying_to.is_none() { ui.horizontal(|ui| { ui.label("Subject: "); ui.add( - text_edit_line!(app, app.subject) + text_edit_line!(app, app.draft_data.subject) .hint_text("Type subject here") .desired_width(f32::INFINITY), ); }); } - if app.include_content_warning { + if app.draft_data.include_content_warning { ui.horizontal(|ui| { ui.label("Content Warning: "); ui.add( - text_edit_line!(app, app.content_warning) + text_edit_line!(app, app.draft_data.content_warning) .hint_text("Type content warning here") .desired_width(f32::INFINITY), ); }); } - if let Some(dm_channel) = &app.draft_dm_channel { + if let Some(dm_channel) = &app.draft_data.dm_channel { ui.label(format!("DIRECT MESSAGE TO: {}", dm_channel.name())); ui.label("WARNING: DMs currently have security weaknesses and the more DMs you send, the easier it is to crack your keypair."); } let draft_response = ui.add( - text_edit_multiline!(app, app.draft) + text_edit_multiline!(app, app.draft_data.draft) .id_source("compose_area") .hint_text("Type your message here") .desired_width(f32::INFINITY) .lock_focus(true) - .interactive(app.draft_repost.is_none()) + .interactive(app.draft_data.repost.is_none()) .layouter(&mut layouter), ); if app.draft_needs_focus { @@ -182,7 +182,7 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram app.draft_needs_focus = false; } - if draft_response.has_focus() && !app.draft.is_empty() { + if draft_response.has_focus() && !app.draft_data.draft.is_empty() { let modifiers = if cfg!(target_os = "macos") { Modifiers { command: true, @@ -205,73 +205,78 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram ui.horizontal(|ui| { if ui.button("Cancel").clicked() { - app.clear_post(); + app.draft_data = Default::default(); + app.show_post_area = false; + app.draft_needs_focus = false; } ui.with_layout(Layout::right_to_left(Align::TOP), |ui| { ui.add_space(12.0); - let send_label = if app.draft_repost.is_some() { + let send_label = if app.draft_data.repost.is_some() { "Repost" } else { "Send" }; if ui.button(send_label).clicked() - && (!app.draft.is_empty() || app.draft_repost.is_some()) + && (!app.draft_data.draft.is_empty() || app.draft_data.repost.is_some()) { send_now = true; } - if app.draft_repost.is_none() { + if app.draft_data.repost.is_none() { ui.add( - text_edit_line!(app, app.tag_someone) + text_edit_line!(app, app.draft_data.tag_someone) .desired_width(100.0) .hint_text("@username"), ); - if !app.tag_someone.is_empty() { + if !app.draft_data.tag_someone.is_empty() { let pairs = GLOBALS .people - .search_people_to_tag(&app.tag_someone) + .search_people_to_tag(&app.draft_data.tag_someone) .unwrap_or(vec![]); if !pairs.is_empty() { ui.menu_button("@", |ui| { for pair in pairs { if ui.button(pair.0).clicked() { - if !app.draft.ends_with(' ') && !app.draft.is_empty() { - app.draft.push(' '); + if !app.draft_data.draft.ends_with(' ') + && !app.draft_data.draft.is_empty() + { + app.draft_data.draft.push(' '); } let nostr_url: NostrUrl = pair.1.into(); - app.draft.push_str(&format!("{}", nostr_url)); - app.tag_someone = "".to_owned(); + app.draft_data.draft.push_str(&format!("{}", nostr_url)); + app.draft_data.tag_someone = "".to_owned(); } } }); } } - if app.include_subject { + if app.draft_data.include_subject { if ui.button("Remove Subject").clicked() { - app.include_subject = false; - app.subject = "".to_owned(); + app.draft_data.include_subject = false; + app.draft_data.subject = "".to_owned(); } - } else if app.replying_to.is_none() && ui.button("Add Subject").clicked() { - app.include_subject = true; + } else if app.draft_data.replying_to.is_none() && ui.button("Add Subject").clicked() + { + app.draft_data.include_subject = true; } - if app.include_content_warning { + if app.draft_data.include_content_warning { if ui.button("Remove Content Warning").clicked() { - app.include_content_warning = false; - app.content_warning = "".to_owned(); + app.draft_data.include_content_warning = false; + app.draft_data.content_warning = "".to_owned(); } } else if ui.button("Add Content Warning").clicked() { - app.include_content_warning = true; + app.draft_data.include_content_warning = true; } // Emoji picker ui.menu_button(RichText::new("😀▼").size(14.0), |ui| { if let Some(emoji) = crate::ui::components::emoji_picker(ui) { - app.draft.push(emoji); + app.draft_data.draft.push(emoji); } }); } @@ -280,52 +285,55 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram if send_now { let mut tags: Vec = Vec::new(); - if app.include_content_warning { + if app.draft_data.include_content_warning { tags.push(Tag::ContentWarning { - warning: app.content_warning.clone(), + warning: app.draft_data.content_warning.clone(), trailing: Vec::new(), }); } if let Some(delegatee_tag) = GLOBALS.delegation.get_delegatee_tag() { tags.push(delegatee_tag); } - if app.include_subject { + if app.draft_data.include_subject { tags.push(Tag::Subject { - subject: app.subject.clone(), + subject: app.draft_data.subject.clone(), trailing: Vec::new(), }); } - match app.replying_to { + match app.draft_data.replying_to { Some(replying_to_id) => { let _ = GLOBALS.to_overlord.send(ToOverlordMessage::Post( - app.draft.clone(), + app.draft_data.draft.clone(), tags, Some(replying_to_id), - app.draft_dm_channel.clone(), + app.draft_data.dm_channel.clone(), )); } None => { - if let Some(event_id) = app.draft_repost { + if let Some(event_id) = app.draft_data.repost { let _ = GLOBALS .to_overlord .send(ToOverlordMessage::Repost(event_id)); } else { let _ = GLOBALS.to_overlord.send(ToOverlordMessage::Post( - app.draft.clone(), + app.draft_data.draft.clone(), tags, None, - app.draft_dm_channel.clone(), + app.draft_data.dm_channel.clone(), )); } } } - app.clear_post(); + + app.draft_data = Default::default(); + app.show_post_area = false; + app.draft_needs_focus = false; } // List tags that will be applied // FIXME: list tags from parent event too in case of reply // FIXME: tag handling in overlord::post() needs to move back here so the user can control this - for (i, bech32) in NostrBech32::find_all_in_string(&app.draft) + for (i, bech32) in NostrBech32::find_all_in_string(&app.draft_data.draft) .iter() .enumerate() { diff --git a/src/ui/help/theme.rs b/src/ui/help/theme.rs index 805c2997..346410b9 100644 --- a/src/ui/help/theme.rs +++ b/src/ui/help/theme.rs @@ -1,10 +1,10 @@ use super::GossipUi; -use crate::ui::HighlightType; use crate::ui::feed::NoteRenderData; +use crate::ui::HighlightType; use eframe::egui; -use egui::{Color32, Context, Frame, Margin, RichText, Ui}; use egui::text::LayoutJob; use egui::widget_text::WidgetText; +use egui::{Color32, Context, Frame, Margin, RichText, Ui}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Background { @@ -88,36 +88,38 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr ui.heading("Input Background"); inner(app, ui, Background::Input); }); - } fn inner(app: &mut GossipUi, ui: &mut Ui, background: Background) { let theme = app.settings.theme.clone(); let accent = RichText::new("accent").color(theme.accent_color()); - let accent_complementary = RichText::new("accent complimentary (indirectly used)").color(theme.accent_complementary_color()); + let accent_complementary = RichText::new("accent complimentary (indirectly used)") + .color(theme.accent_complementary_color()); line(ui, accent); line(ui, accent_complementary); if background == Background::Input { - for (ht,txt) in [(HighlightType::Nothing, "nothing"), - (HighlightType::PublicKey, "public key"), - (HighlightType::Event, "event"), - (HighlightType::Relay, "relay"), - (HighlightType::Hyperlink, "hyperlink")] - { + for (ht, txt) in [ + (HighlightType::Nothing, "nothing"), + (HighlightType::PublicKey, "public key"), + (HighlightType::Event, "event"), + (HighlightType::Relay, "relay"), + (HighlightType::Hyperlink, "hyperlink"), + ] { let mut highlight_job = LayoutJob::default(); highlight_job.append( &format!("highlight text format for {}", txt), 0.0, - theme.highlight_text_format(ht) + theme.highlight_text_format(ht), ); line(ui, WidgetText::LayoutJob(highlight_job)); } } if background == Background::Note || background == Background::HighlightedNote { - let warning_marker = RichText::new("warning marker").color(theme.warning_marker_text_color()); + let warning_marker = + RichText::new("warning marker").color(theme.warning_marker_text_color()); line(ui, warning_marker); let notice_marker = RichText::new("notice marker").color(theme.notice_marker_text_color()); @@ -130,7 +132,7 @@ fn inner(app: &mut GossipUi, ui: &mut Ui, background: Background) { crate::ui::widgets::break_anywhere_hyperlink_to( ui, "https://hyperlink.example.com", - "https://hyperlink.example.com" + "https://hyperlink.example.com", ); }); } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 05c11c60..f57f7574 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -187,6 +187,34 @@ pub enum HighlightType { Hyperlink, } +pub struct DraftData { + pub draft: String, + pub repost: Option, + pub tag_someone: String, + pub include_subject: bool, + pub subject: String, + pub include_content_warning: bool, + pub content_warning: String, + pub dm_channel: Option, + pub replying_to: Option, +} + +impl Default for DraftData { + fn default() -> DraftData { + DraftData { + draft: "".to_owned(), + repost: None, + tag_someone: "".to_owned(), + include_subject: false, + subject: "".to_owned(), + include_content_warning: false, + content_warning: "".to_owned(), + dm_channel: None, + replying_to: None, + } + } +} + struct GossipUi { #[cfg(feature = "video-ffmpeg")] audio_device: Option, @@ -247,18 +275,20 @@ struct GossipUi { // User entry: posts show_post_area: bool, - draft: String, draft_needs_focus: bool, - draft_repost: Option, unlock_needs_focus: bool, + draft_data: DraftData, + /* + draft: String, + repost: Option, tag_someone: String, include_subject: bool, subject: String, include_content_warning: bool, content_warning: String, - draft_dm_channel: Option, + dm_channel: Option, replying_to: Option, - + */ // User entry: metadata editing_metadata: bool, metadata: Metadata, @@ -467,17 +497,9 @@ impl GossipUi { media_hide_list: HashSet::new(), media_full_width_list: HashSet::new(), show_post_area: false, - draft: "".to_owned(), draft_needs_focus: false, - draft_repost: None, unlock_needs_focus: false, - tag_someone: "".to_owned(), - include_subject: false, - subject: "".to_owned(), - include_content_warning: false, - content_warning: "".to_owned(), - draft_dm_channel: None, - replying_to: None, + draft_data: DraftData::default(), editing_metadata: false, metadata: Metadata::new(), delegatee_tag_str: "".to_owned(), @@ -560,26 +582,6 @@ impl GossipUi { } self.page = page; } - - fn clear_post(&mut self) { - self.draft = "".to_owned(); - self.draft_repost = None; - self.tag_someone = "".to_owned(); - self.include_subject = false; - self.subject = "".to_owned(); - self.replying_to = None; - self.draft_dm_channel = None; - self.include_content_warning = false; - self.content_warning = "".to_owned(); - - if let Page::Feed(FeedKind::DmChat(_)) = self.page { - self.show_post_area = true; - self.draft_needs_focus = true; - } else { - self.show_post_area = false; - self.draft_needs_focus = false; - } - } } impl eframe::App for GossipUi { @@ -824,9 +826,9 @@ impl eframe::App for GossipUi { if response.clicked() { self.show_post_area = true; if let Page::Feed(FeedKind::DmChat(channel)) = &self.page { - self.draft_dm_channel = Some(channel.clone()); + self.draft_data.dm_channel = Some(channel.clone()); } else { - self.draft_dm_channel = None; + self.draft_data.dm_channel = None; } if GLOBALS.signer.is_ready() { self.draft_needs_focus = true; @@ -974,10 +976,10 @@ impl GossipUi { if GLOBALS.signer.is_ready() { if ui.button("Send DM").clicked() { let channel = DmChannel::new(&[person.pubkey]); - app.replying_to = None; - app.draft_repost = None; + app.draft_data.replying_to = None; + app.draft_data.repost = None; app.show_post_area = true; - app.draft_dm_channel = Some(channel.clone()); + app.draft_data.dm_channel = Some(channel.clone()); app.draft_needs_focus = true; app.set_page(Page::Feed(FeedKind::DmChat(channel))); }