mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-29 08:21:47 +00:00
Tagging: Cache results and only search when a new search phrase is typed
This commit is contained in:
parent
e4892ae467
commit
2e3ee6276d
@ -381,7 +381,7 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
|
||||
let down = i.count_and_consume_key(Modifiers::NONE, Key::ArrowDown);
|
||||
let up = i.count_and_consume_key(Modifiers::NONE, Key::ArrowUp);
|
||||
index += down;
|
||||
index = index.min(app.draft_data.tagging_search_results.saturating_sub(1));
|
||||
index = index.min(app.draft_data.tagging_search_results.len().saturating_sub(1));
|
||||
|
||||
(Some(index.saturating_sub(up)), enter)
|
||||
})
|
||||
@ -391,7 +391,7 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
|
||||
|
||||
// Temporary for debugging:
|
||||
if let Some(tagging) = &app.draft_data.tagging_search_substring {
|
||||
ui.label(format!("TAGGING SEARCH SUBSTRING = {}, results: {}", tagging, app.draft_data.tagging_search_results));
|
||||
ui.label(format!("TAGGING SEARCH SUBSTRING = {}, results: {}", tagging, app.draft_data.tagging_search_results.len()));
|
||||
}
|
||||
|
||||
let text_edit_area = text_edit_multiline!(app, app.draft_data.draft)
|
||||
@ -446,119 +446,135 @@ fn real_posting_area(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
|
||||
|
||||
// show tagging slector tooltip
|
||||
if let Some(search) = &app.draft_data.tagging_search_substring {
|
||||
let search= search.clone(); // need to release borrow of app.
|
||||
let pairs = GLOBALS
|
||||
.people
|
||||
.search_people_to_tag(&search)
|
||||
.unwrap_or(vec![]);
|
||||
app.draft_data.tagging_search_results = pairs.len();
|
||||
if !pairs.is_empty() {
|
||||
area.show(ui.ctx(), |ui|{
|
||||
frame.show(ui, |ui|{
|
||||
egui::ScrollArea::vertical()
|
||||
.max_width(TAGG_WIDTH)
|
||||
.max_height(250.0)
|
||||
.show(ui, |ui|{
|
||||
for (i, pair) in pairs.iter().enumerate() {
|
||||
let avatar = if let Some(avatar) = app.try_get_avatar(ctx, &pair.1) {
|
||||
avatar
|
||||
} else {
|
||||
app.placeholder_avatar.clone()
|
||||
};
|
||||
|
||||
let frame = egui::Frame::none()
|
||||
.rounding( egui::Rounding::ZERO )
|
||||
.inner_margin(
|
||||
egui::Margin::symmetric(10.0,5.0));
|
||||
let mut prepared = frame.begin(ui);
|
||||
|
||||
prepared.content_ui.set_min_width(TAGG_WIDTH);
|
||||
prepared.content_ui.set_max_width(TAGG_WIDTH);
|
||||
prepared.content_ui.set_min_height(27.0);
|
||||
|
||||
let frame_rect = (prepared.frame.inner_margin + prepared.frame.outer_margin)
|
||||
.expand_rect(prepared.content_ui.min_rect());
|
||||
|
||||
let response = ui
|
||||
.interact(frame_rect, ui.auto_id_with(pair.1.as_hex_string()), egui::Sense::click())
|
||||
.on_hover_cursor(egui::CursorIcon::PointingHand);
|
||||
|
||||
// mouse hover moves selected index
|
||||
app.draft_data.tagging_search_selected = if response.hovered() {
|
||||
Some(i)
|
||||
} else {
|
||||
app.draft_data.tagging_search_selected
|
||||
};
|
||||
let is_selected = Some(i) == app.draft_data.tagging_search_selected;
|
||||
response.has_focus();
|
||||
|
||||
{ // render inside of frame using prepared.content_ui
|
||||
let ui = &mut prepared.content_ui;
|
||||
if is_selected { app.theme.on_accent_style(ui.style_mut()) }
|
||||
ui.horizontal(|ui|{
|
||||
ui.add(egui::Image::new(&avatar)
|
||||
.max_size(egui::Vec2 {
|
||||
x: 27.0,
|
||||
y: 27.0,
|
||||
})
|
||||
.maintain_aspect_ratio(true));
|
||||
ui.vertical(|ui|{
|
||||
widgets::truncated_label(ui, RichText::new(&pair.0).small(), TAGG_WIDTH - 33.0);
|
||||
if let Ok(Some(person)) = GLOBALS.storage.read_person(&pair.1) {
|
||||
let mut nip05 = RichText::new(person.nip05().unwrap_or_default()).weak().small();
|
||||
if !person.nip05_valid { nip05 = nip05.strikethrough() }
|
||||
widgets::truncated_label(ui, nip05, TAGG_WIDTH - 33.0);
|
||||
}
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
prepared.frame.fill =
|
||||
if is_selected {
|
||||
app.theme.accent_color()
|
||||
} else {
|
||||
egui::Color32::TRANSPARENT
|
||||
};
|
||||
|
||||
prepared.end(ui);
|
||||
|
||||
if is_selected { response.scroll_to_me(None) }
|
||||
let clicked = response.clicked();
|
||||
if clicked || (enter_key && is_selected) {
|
||||
// remove @ and search text
|
||||
app.draft_data.draft = app.draft_data.draft.trim_end_matches(
|
||||
format!("@{}", search).as_str()).to_string();
|
||||
|
||||
// replace with nostr url
|
||||
let nostr_url: NostrUrl = pair.1.into();
|
||||
app.draft_data.draft.push_str(&format!("{} ", nostr_url));
|
||||
app.draft_data.tag_someone = "".to_owned();
|
||||
|
||||
// mover cursor to end
|
||||
let mut state = output.state.clone();
|
||||
let mut ccrange = CCursorRange::default();
|
||||
ccrange.primary.index = usize::MAX;
|
||||
ccrange.secondary.index = usize::MAX;
|
||||
state.set_ccursor_range(Some(ccrange));
|
||||
state.store(ctx, text_edit_area_id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// if bresp.clicked() {
|
||||
// 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_data.draft.push_str(&format!("{}", nostr_url));
|
||||
// app.draft_data.tag_someone = "".to_owned();
|
||||
// }
|
||||
});
|
||||
});
|
||||
// only do the search when search string changes
|
||||
if app.draft_data.tagging_search_substring != app.draft_data.tagging_search_searched {
|
||||
let pairs = GLOBALS
|
||||
.people
|
||||
.search_people_to_tag(&search)
|
||||
.unwrap_or(vec![]);
|
||||
app.draft_data.tagging_search_searched = Some(search.clone());
|
||||
app.draft_data.tagging_search_results = pairs.to_owned();
|
||||
}
|
||||
} else {
|
||||
// no more search substring, clear results
|
||||
app.draft_data.tagging_search_searched = None;
|
||||
app.draft_data.tagging_search_results.clear();
|
||||
}
|
||||
|
||||
// show search results
|
||||
if !app.draft_data.tagging_search_results.is_empty() {
|
||||
area.show(ui.ctx(), |ui|{
|
||||
frame.show(ui, |ui|{
|
||||
egui::ScrollArea::vertical()
|
||||
.max_width(TAGG_WIDTH)
|
||||
.max_height(250.0)
|
||||
.show(ui, |ui|{
|
||||
// need to clone results to avoid immutable borrow error on app.
|
||||
let pairs = app.draft_data.tagging_search_results.clone();
|
||||
for (i, pair) in pairs.iter().enumerate() {
|
||||
let avatar = if let Some(avatar) = app.try_get_avatar(ctx, &pair.1) {
|
||||
avatar
|
||||
} else {
|
||||
app.placeholder_avatar.clone()
|
||||
};
|
||||
|
||||
let frame = egui::Frame::none()
|
||||
.rounding( egui::Rounding::ZERO )
|
||||
.inner_margin(
|
||||
egui::Margin::symmetric(10.0,5.0));
|
||||
let mut prepared = frame.begin(ui);
|
||||
|
||||
prepared.content_ui.set_min_width(TAGG_WIDTH);
|
||||
prepared.content_ui.set_max_width(TAGG_WIDTH);
|
||||
prepared.content_ui.set_min_height(27.0);
|
||||
|
||||
let frame_rect = (prepared.frame.inner_margin + prepared.frame.outer_margin)
|
||||
.expand_rect(prepared.content_ui.min_rect());
|
||||
|
||||
let response = ui
|
||||
.interact(frame_rect, ui.auto_id_with(pair.1.as_hex_string()), egui::Sense::click())
|
||||
.on_hover_cursor(egui::CursorIcon::PointingHand);
|
||||
|
||||
// mouse hover moves selected index
|
||||
// app.draft_data.tagging_search_selected = if response.hovered() {
|
||||
// Some(i)
|
||||
// } else {
|
||||
// app.draft_data.tagging_search_selected
|
||||
// };
|
||||
let is_selected = Some(i) == app.draft_data.tagging_search_selected;
|
||||
|
||||
{ // render inside of frame using prepared.content_ui
|
||||
let ui = &mut prepared.content_ui;
|
||||
if is_selected { app.theme.on_accent_style(ui.style_mut()) }
|
||||
ui.horizontal(|ui|{
|
||||
ui.add(egui::Image::new(&avatar)
|
||||
.max_size(egui::Vec2 {
|
||||
x: 27.0,
|
||||
y: 27.0,
|
||||
})
|
||||
.maintain_aspect_ratio(true));
|
||||
ui.vertical(|ui|{
|
||||
widgets::truncated_label(ui, RichText::new(&pair.0).small(), TAGG_WIDTH - 33.0);
|
||||
if let Ok(Some(person)) = GLOBALS.storage.read_person(&pair.1) {
|
||||
let mut nip05 = RichText::new(person.nip05().unwrap_or_default()).weak().small();
|
||||
if !person.nip05_valid { nip05 = nip05.strikethrough() }
|
||||
widgets::truncated_label(ui, nip05, TAGG_WIDTH - 33.0);
|
||||
}
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
prepared.frame.fill =
|
||||
if is_selected {
|
||||
app.theme.accent_color()
|
||||
} else {
|
||||
egui::Color32::TRANSPARENT
|
||||
};
|
||||
|
||||
prepared.end(ui);
|
||||
|
||||
if is_selected { response.scroll_to_me(None) }
|
||||
let clicked = response.clicked();
|
||||
if clicked || (enter_key && is_selected) {
|
||||
// remove @ and search text
|
||||
let search = if let Some(search) = app.draft_data.tagging_search_searched.as_ref() {
|
||||
search.clone()
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
app.draft_data.draft = app.draft_data.draft.trim_end_matches(
|
||||
format!("@{}", search).as_str()).to_string();
|
||||
|
||||
// replace with nostr url
|
||||
let nostr_url: NostrUrl = pair.1.into();
|
||||
app.draft_data.draft.push_str(&format!("{} ", nostr_url));
|
||||
app.draft_data.tag_someone = "".to_owned();
|
||||
|
||||
// mover cursor to end
|
||||
let mut state = output.state.clone();
|
||||
let mut ccrange = CCursorRange::default();
|
||||
ccrange.primary.index = usize::MAX;
|
||||
ccrange.secondary.index = usize::MAX;
|
||||
state.set_ccursor_range(Some(ccrange));
|
||||
state.store(ctx, text_edit_area_id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// if bresp.clicked() {
|
||||
// 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_data.draft.push_str(&format!("{}", nostr_url));
|
||||
// app.draft_data.tag_someone = "".to_owned();
|
||||
// }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let is_open = app.draft_data.tagging_search_substring.is_some();
|
||||
|
@ -287,7 +287,8 @@ pub struct DraftData {
|
||||
// If the user is typing a @tag, this is what they typed
|
||||
pub tagging_search_substring: Option<String>,
|
||||
pub tagging_search_selected: Option<usize>,
|
||||
pub tagging_search_results: usize,
|
||||
pub tagging_search_searched: Option<String>,
|
||||
pub tagging_search_results: Vec<(String,PublicKey)>,
|
||||
}
|
||||
|
||||
impl Default for DraftData {
|
||||
@ -306,7 +307,8 @@ impl Default for DraftData {
|
||||
|
||||
tagging_search_substring: None,
|
||||
tagging_search_selected: None,
|
||||
tagging_search_results: 0,
|
||||
tagging_search_searched: None,
|
||||
tagging_search_results: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user