context_menu.rs (1330B)
1 /// Context menu helpers (paste, etc) 2 use egui_winit::clipboard::Clipboard; 3 4 #[derive(Copy, Clone, Eq, PartialEq)] 5 pub enum PasteBehavior { 6 Clear, 7 Append, 8 } 9 10 fn handle_paste(clipboard: &mut Clipboard, input: &mut String, paste_behavior: PasteBehavior) { 11 if let Some(text) = clipboard.get() { 12 // if called with clearing_input_context, then we clear before 13 // we paste. Useful for certain fields like passwords, etc 14 match paste_behavior { 15 PasteBehavior::Clear => input.clear(), 16 PasteBehavior::Append => {} 17 } 18 input.push_str(&text); 19 } 20 } 21 22 pub fn input_context( 23 response: &egui::Response, 24 clipboard: &mut Clipboard, 25 input: &mut String, 26 paste_behavior: PasteBehavior, 27 ) { 28 response.context_menu(|ui| { 29 if ui.button("Paste").clicked() { 30 handle_paste(clipboard, input, paste_behavior); 31 ui.close_menu(); 32 } 33 34 if ui.button("Copy").clicked() { 35 clipboard.set_text(input.to_owned()); 36 ui.close_menu(); 37 } 38 39 if ui.button("Cut").clicked() { 40 clipboard.set_text(input.to_owned()); 41 input.clear(); 42 ui.close_menu(); 43 } 44 }); 45 46 if response.middle_clicked() { 47 handle_paste(clipboard, input, paste_behavior) 48 } 49 }