add input context menu helper
We are going to want this in more places Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
49
crates/notedeck_ui/src/context_menu.rs
Normal file
49
crates/notedeck_ui/src/context_menu.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
/// Context menu helpers (paste, etc)
|
||||
use egui_winit::clipboard::Clipboard;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub enum PasteBehavior {
|
||||
Clear,
|
||||
Append,
|
||||
}
|
||||
|
||||
fn handle_paste(clipboard: &mut Clipboard, input: &mut String, paste_behavior: PasteBehavior) {
|
||||
if let Some(text) = clipboard.get() {
|
||||
// if called with clearing_input_context, then we clear before
|
||||
// we paste. Useful for certain fields like passwords, etc
|
||||
match paste_behavior {
|
||||
PasteBehavior::Clear => input.clear(),
|
||||
PasteBehavior::Append => {}
|
||||
}
|
||||
input.push_str(&text);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input_context(
|
||||
response: &egui::Response,
|
||||
clipboard: &mut Clipboard,
|
||||
input: &mut String,
|
||||
paste_behavior: PasteBehavior,
|
||||
) {
|
||||
response.context_menu(|ui| {
|
||||
if ui.button("Paste").clicked() {
|
||||
handle_paste(clipboard, input, paste_behavior);
|
||||
ui.close_menu();
|
||||
}
|
||||
|
||||
if ui.button("Copy").clicked() {
|
||||
clipboard.set_text(input.to_owned());
|
||||
ui.close_menu();
|
||||
}
|
||||
|
||||
if ui.button("Cut").clicked() {
|
||||
clipboard.set_text(input.to_owned());
|
||||
input.clear();
|
||||
ui.close_menu();
|
||||
}
|
||||
});
|
||||
|
||||
if response.middle_clicked() {
|
||||
handle_paste(clipboard, input, paste_behavior)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ pub mod blur;
|
||||
pub mod colors;
|
||||
pub mod constants;
|
||||
pub mod contacts;
|
||||
pub mod context_menu;
|
||||
pub mod gif;
|
||||
pub mod icons;
|
||||
pub mod images;
|
||||
|
||||
Reference in New Issue
Block a user