Skip to content

Commit

Permalink
Add copy paste context menu to text inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Jul 7, 2024
1 parent 9ba2e84 commit 7d5b8f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/elements/icon_source.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
addon::Addon,
lockbox::Lockbox,
render_util::{enum_combo, impl_static_variants},
render_util::{enum_combo, impl_static_variants, input_text_context_menu},
texture_manager::TextureManager,
};
use nexus::imgui::{ComboBoxFlags, TextureId, Ui};
Expand Down Expand Up @@ -115,6 +115,7 @@ impl IconSource {
.hint("https://wiki.guildwars2.com/...")
.auto_select_all(true)
.build();
input_text_context_menu(ui, "##urlctx", url);
ui.same_line();
if ui.button("Load") {
self.load();
Expand Down
3 changes: 2 additions & 1 deletion src/elements/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{RenderState, TextAlign, TextDecoration};
use crate::{
component_wise::ComponentWise,
context::{Context, ContextUpdate},
render_util::{draw_text_bg, helper, input_float_with_format},
render_util::{draw_text_bg, helper, input_float_with_format, input_text_context_menu},
traits::{Render, RenderOptions, TreeLeaf},
trigger::{ActiveBuff, BuffTrigger},
};
Expand Down Expand Up @@ -115,6 +115,7 @@ impl RenderOptions for Text {
ui.input_text_multiline("##text", &mut self.text, [0.0, 3.0 * ui.text_line_height()])
.allow_tab_input(true)
.build();
input_text_context_menu(ui, "##textctx", &mut self.text);
ui.same_line();
ui.text("Text"); // own label to fix helper position
ui.same_line();
Expand Down
32 changes: 23 additions & 9 deletions src/render_util/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::item_context_menu;
use crate::util::decode_skill;
use nexus::imgui::{sys, InputTextFlags, Ui};
use nexus::imgui::{sys, InputTextFlags, MenuItem, Ui};
use std::ffi::CString;

pub fn input_u32(
Expand Down Expand Up @@ -54,22 +55,35 @@ pub fn input_size(x: &mut f32, y: &mut f32) {
}

pub fn input_buff_id(ui: &Ui, label: impl AsRef<str>, id: &mut u32, flags: InputTextFlags) -> bool {
let label = label.as_ref();
let mut text = id.to_string(); // TODO: switch to faster int/float to string conversion libraries
if ui
let changed = ui
.input_text(label, &mut text)
.flags(flags | InputTextFlags::CALLBACK_RESIZE)
.build()
{
.build();
if changed {
if let Ok(new) = text.parse() {
*id = new;
} else if let Some(new) = decode_skill(text.trim()) {
*id = new;
}
true
} else {
false
}
input_text_context_menu(ui, format!("##{label}ctx"), &mut text);
changed
}

#[cfg(test)]
mod tests {}
pub fn input_text_context_menu(ui: &Ui, id: impl Into<String>, text: &mut String) {
item_context_menu(id, || {
if MenuItem::new("Copy").build(ui) {
ui.set_clipboard_text(&text);
}

let clipboard = ui.clipboard_text();
if MenuItem::new("Paste")
.enabled(clipboard.is_some())
.build(ui)
{
*text = clipboard.expect("pasting without clipboard text");
}
});
}

0 comments on commit 7d5b8f6

Please # to comment.