user can explicitly close mention hints

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-03-01 18:04:59 -05:00
parent 66b35c5026
commit e3eab0dfa8
2 changed files with 72 additions and 45 deletions

View File

@@ -191,60 +191,77 @@ impl<'a> PostView<'a> {
cursor_index: usize,
textedit_output: &TextEditOutput,
) {
let mut delete_mention = None;
if let Some(mention) = &self.draft.buffer.get_mention(cursor_index) {
if mention.info.mention_type == MentionType::Pending {
let mention_str = self.draft.buffer.get_mention_string(mention);
if ui.ctx().input(|r| r.key_pressed(egui::Key::Escape)) {
delete_mention = Some(mention.index);
} else {
let mention_str = self.draft.buffer.get_mention_string(mention);
if !mention_str.is_empty() {
if let Some(mention_hint) = &mut self.draft.cur_mention_hint {
if mention_hint.index != mention.index {
mention_hint.index = mention.index;
mention_hint.pos = calculate_mention_hints_pos(
textedit_output,
mention.info.start_index,
);
if !mention_str.is_empty() {
if let Some(mention_hint) = &mut self.draft.cur_mention_hint {
if mention_hint.index != mention.index {
mention_hint.index = mention.index;
mention_hint.pos = calculate_mention_hints_pos(
textedit_output,
mention.info.start_index,
);
}
mention_hint.text = mention_str.to_owned();
} else {
self.draft.cur_mention_hint = Some(MentionHint {
index: mention.index,
text: mention_str.to_owned(),
pos: calculate_mention_hints_pos(
textedit_output,
mention.info.start_index,
),
});
}
mention_hint.text = mention_str.to_owned();
} else {
self.draft.cur_mention_hint = Some(MentionHint {
index: mention.index,
text: mention_str.to_owned(),
pos: calculate_mention_hints_pos(
textedit_output,
mention.info.start_index,
),
});
}
}
if let Some(hint) = &self.draft.cur_mention_hint {
let hint_rect = {
let mut hint_rect = self.inner_rect;
hint_rect.set_top(hint.pos.y);
hint_rect
};
if let Some(hint) = &self.draft.cur_mention_hint {
let hint_rect = {
let mut hint_rect = self.inner_rect;
hint_rect.set_top(hint.pos.y);
hint_rect
};
if let Ok(res) = self.ndb.search_profile(txn, mention_str, 10) {
let hint_selection =
SearchResultsView::new(self.img_cache, self.ndb, txn, &res)
if let Ok(res) = self.ndb.search_profile(txn, mention_str, 10) {
let resp = SearchResultsView::new(self.img_cache, self.ndb, txn, &res)
.show_in_rect(hint_rect, ui);
if let Some(hint_index) = hint_selection {
if let Some(pk) = res.get(hint_index) {
let record = self.ndb.get_profile_by_pubkey(txn, pk);
match resp {
ui::search_results::SearchResultsResponse::SelectResult(
selection,
) => {
if let Some(hint_index) = selection {
if let Some(pk) = res.get(hint_index) {
let record = self.ndb.get_profile_by_pubkey(txn, pk);
self.draft.buffer.select_mention_and_replace_name(
mention.index,
get_display_name(record.ok().as_ref()).name(),
Pubkey::new(**pk),
);
self.draft.cur_mention_hint = None;
self.draft.buffer.select_mention_and_replace_name(
mention.index,
get_display_name(record.ok().as_ref()).name(),
Pubkey::new(**pk),
);
self.draft.cur_mention_hint = None;
}
}
}
ui::search_results::SearchResultsResponse::DeleteMention => {
self.draft.buffer.delete_mention(mention.index)
}
}
}
}
}
}
}
if let Some(mention_to_delete) = delete_mention {
self.draft.buffer.delete_mention(mention_to_delete);
}
}
fn focused(&self, ui: &egui::Ui) -> bool {

View File

@@ -17,6 +17,11 @@ pub struct SearchResultsView<'a> {
results: &'a Vec<&'a [u8; 32]>,
}
pub enum SearchResultsResponse {
SelectResult(Option<usize>),
DeleteMention,
}
impl<'a> SearchResultsView<'a> {
pub fn new(
img_cache: &'a mut Images,
@@ -32,8 +37,8 @@ impl<'a> SearchResultsView<'a> {
}
}
fn show(&mut self, ui: &mut egui::Ui, width: f32) -> Option<usize> {
let mut selection = None;
fn show(&mut self, ui: &mut egui::Ui, width: f32) -> SearchResultsResponse {
let mut search_results_selection = None;
ui.vertical(|ui| {
for (i, res) in self.results.iter().enumerate() {
let profile = match self.ndb.get_profile_by_pubkey(self.txn, res) {
@@ -48,15 +53,15 @@ impl<'a> SearchResultsView<'a> {
.add(user_result(&profile, self.img_cache, i, width))
.clicked()
{
selection = Some(i)
search_results_selection = Some(i)
}
}
});
selection
SearchResultsResponse::SelectResult(search_results_selection)
}
pub fn show_in_rect(&mut self, rect: egui::Rect, ui: &mut egui::Ui) -> Option<usize> {
pub fn show_in_rect(&mut self, rect: egui::Rect, ui: &mut egui::Ui) -> SearchResultsResponse {
let widget_id = ui.id().with("search_results");
let area_resp = egui::Area::new(widget_id)
.order(egui::Order::Foreground)
@@ -70,7 +75,7 @@ impl<'a> SearchResultsView<'a> {
.show(ui, |ui| {
let width = rect.width() - (2.0 * inner_margin_size);
let _close_button_resp = {
let close_button_resp = {
let close_button_size = 16.0;
let (close_section_rect, _) = ui.allocate_exact_size(
vec2(width, close_button_size),
@@ -96,7 +101,12 @@ impl<'a> SearchResultsView<'a> {
.auto_shrink(Vec2b::FALSE)
.show(ui, |ui| self.show(ui, width));
ui.advance_cursor_after_rect(rect);
scroll_resp.inner
if close_button_resp {
SearchResultsResponse::DeleteMention
} else {
scroll_resp.inner
}
})
.inner
});