Skip to content

Commit

Permalink
can left click note more options button
Browse files Browse the repository at this point in the history
egui doesn't support custom buttons so
`stationary_arbitrary_menu_button` had to be hacked together

Signed-off-by: kernelkind <[email protected]>
  • Loading branch information
kernelkind committed Sep 19, 2024
1 parent 6f5f090 commit ad319b6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/ui/note/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
notecache::{CachedNote, NoteCache},
ui::{self, View},
};
use egui::{Align, Id, Label, Layout, Response, RichText, Sense};
use egui::{menu::BarState, Align, Id, InnerResponse, Label, Layout, Response, RichText, Sense};
use enostr::NoteId;
use nostrdb::{Ndb, Note, NoteKey, NoteReply, Transaction};

Expand Down Expand Up @@ -412,7 +412,7 @@ impl<'a> NoteView<'a> {
if use_options_button {
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
let more_options_resp = more_options_button(ui, note_key, 8.0);
options_context_menu(more_options_resp)
options_context_menu(ui, more_options_resp)
})
.inner
} else {
Expand Down Expand Up @@ -728,10 +728,13 @@ fn more_options_button(ui: &mut egui::Ui, note_key: NoteKey, max_height: f32) ->
response
}

fn options_context_menu(more_options_button_resp: egui::Response) -> Option<NoteOptionSelection> {
fn options_context_menu(
ui: &mut egui::Ui,
more_options_button_resp: egui::Response,
) -> Option<NoteOptionSelection> {
let mut selected_option: Option<NoteOptionSelection> = None;

more_options_button_resp.context_menu(|ui| {
stationary_arbitrary_menu_button(ui, more_options_button_resp, |ui| {
ui.set_max_width(200.0);
if ui.button("Copy text").clicked() {
selected_option = Some(NoteOptionSelection::CopyText);
Expand All @@ -746,5 +749,20 @@ fn options_context_menu(more_options_button_resp: egui::Response) -> Option<Note
ui.close_menu();
}
});

selected_option
}

fn stationary_arbitrary_menu_button<R>(
ui: &mut egui::Ui,
button_response: egui::Response,
add_contents: impl FnOnce(&mut egui::Ui) -> R,
) -> InnerResponse<Option<R>> {
let bar_id = ui.id();
let mut bar_state = BarState::load(ui.ctx(), bar_id);

let inner = bar_state.bar_menu(&button_response, add_contents);

bar_state.store(ui.ctx(), bar_id);
InnerResponse::new(inner.map(|r| r.inner), button_response)
}

0 comments on commit ad319b6

Please sign in to comment.