Skip to content

Commit

Permalink
Handlers UI: Move add to dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
bu5hm4nn committed Oct 23, 2024
1 parent 147bb1a commit 6165c9d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 32 deletions.
119 changes: 89 additions & 30 deletions gossip-bin/src/ui/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::f32;

use super::{widgets, GossipUi};
use eframe::egui::{self};
use eframe::egui::{self, vec2, RichText};
use egui::{Context, Ui};
use gossip_lib::comms::ToOverlordMessage;
use gossip_lib::{Handler, HandlerKey, HandlersTable, Table, GLOBALS};
Expand All @@ -8,49 +10,106 @@ use nostr_types::{EventKind, NAddr, PublicKey};
pub struct Handlers {
/// Handler that is open for detailed view, if any
detail: Option<PublicKey>,
/// Is the add-handler dialog open?
add_dialog: bool,
/// Entry text for add-dialog
add_naddr: String,
/// Any errors while entering naddr
add_err: Option<String>,
}

impl Default for Handlers {
fn default() -> Self {
Self { detail: None }
Self {
detail: None,
add_dialog: false,
add_naddr: "".to_owned(),
add_err: None,
}
}
}

pub(super) fn update_all_kinds(app: &mut GossipUi, ctx: &Context, ui: &mut Ui) {
// If we end up in this overview, we clear any detail view
app.handlers.detail.take();
fn add_dialog(ui: &mut Ui, app: &mut GossipUi) {
const DLG_SIZE: egui::Vec2 = vec2(400.0, 260.0);
let dlg_response = widgets::modal_popup(ui.ctx(), vec2(400.0, 0.0), DLG_SIZE, true, |ui| {
ui.heading("Import a handler via nevent");
ui.add_space(8.0);

widgets::page_header(ui, "External Event Handlers", |_ui| ());
ui.label("To add a new handler, paste its corresponding naddr here:");
ui.add_space(12.0);

ui.label("Import a handler via nevent");
let response = ui.add(text_edit_line!(app, app.handler_naddr).hint_text("naddr1..."));
let mut go: bool = false;
if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
go = true;
}
if ui.button("Import").clicked() {
go = true;
}
if go {
if app.handler_naddr.starts_with("nostr:") {
app.handler_naddr = app.handler_naddr[6..].to_owned();
}
let response = widgets::TextEdit::singleline(&app.theme, &mut app.handlers.add_naddr)
.desired_width(f32::INFINITY)
.hint_text("naddr1...")
.with_paste()
.show(ui);
let mut go: bool = false;

match NAddr::try_from_bech32_string(&app.handler_naddr) {
Ok(naddr) => {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::FetchNAddr(naddr));
ui.add_space(12.0);

ui.horizontal(|ui| {
if let Some(err) = &app.handlers.add_err {
ui.label(RichText::new(err).color(app.theme.warning_marker_text_color()));
} else {
ui.label("");
}
Err(_) => {
GLOBALS
.status_queue
.write()
.write("Invalid naddr".to_string());

ui.with_layout(egui::Layout::right_to_left(Default::default()), |ui| {
if widgets::Button::primary(&app.theme, "Import")
.show(ui)
.clicked()
{
go = true;
}
});
});
if response.response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
go = true;
}
if go {
if app.handlers.add_naddr.starts_with("nostr:") {
app.handlers.add_naddr = app.handlers.add_naddr[6..].to_owned();
}

match NAddr::try_from_bech32_string(&app.handlers.add_naddr) {
Ok(naddr) => {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::FetchNAddr(naddr));
app.handlers.add_naddr = "".to_string();
app.handlers.add_dialog = false;
app.handlers.add_err = None;
}
Err(_) => {
app.handlers.add_err = Some("Invalid naddr".to_owned());
}
}
}
app.handler_naddr = "".to_string();
});

if dlg_response.inner.clicked() {
app.handlers.add_dialog = false;
app.handlers.add_err = None;
}
}

pub(super) fn update_all_kinds(app: &mut GossipUi, ctx: &Context, ui: &mut Ui) {
// If we end up in this overview, we clear any detail view
app.handlers.detail.take();

if app.handlers.add_dialog {
add_dialog(ui, app);
}

// render page
widgets::page_header(ui, "External Event Handlers", |ui| {
if widgets::Button::primary(&app.theme, "Import Handler")
.show(ui)
.clicked()
{
app.handlers.add_dialog = true;
}
});

app.vert_scroll_area().show(ui, |ui| {
let data = GLOBALS
Expand Down
2 changes: 0 additions & 2 deletions gossip-bin/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ struct GossipUi {
nostr_connect_name: String,
nostr_connect_relay1: String,
nostr_connect_relay2: String,
handler_naddr: String,

// Collapsed threads
collapsed: Vec<Id>,
Expand Down Expand Up @@ -771,7 +770,6 @@ impl GossipUi {
nostr_connect_name: "".to_owned(),
nostr_connect_relay1: "".to_owned(),
nostr_connect_relay2: "".to_owned(),
handler_naddr: "".to_owned(),
collapsed: vec![],
opened: HashSet::new(),
visible_note_ids: vec![],
Expand Down

0 comments on commit 6165c9d

Please sign in to comment.