Merge pull request #9353 from dotCooCoo/feat/doc-info-click-copy

Document Info: a click on a value copies it
This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 19:31:48 +07:00
commit e3a641c388
2 files changed
+104 -5

No files matched your search

+31 -5
View File
@@ -31,13 +31,32 @@ pub struct DocInfoView {
}
/// A `label: value` row. `label` is shown as given: translate an interface label first, a name
/// stays as it is.
pub(crate) fn row(ui: &mut Ui, label: &str, value: String) {
/// stays as it is. Whether the value was clicked to copy it: a click with no key held, other than
/// the second or third click of a double or triple click.
pub(crate) fn row(ui: &mut Ui, label: &str, value: &str) -> bool {
let mut clicked = false;
Row::new().fill_width().align(RowAlign::SpaceBetween).wrap(true).show(ui, |ui, _| {
Text::new(label.to_string()).shade(Shade::Muted).show(ui);
// The value can be selected and copied (#8432).
SelectableText::new(label.to_string(), &value).show(ui);
// The value can be selected and copied, and a click copies all of it (#8432). A drag selects
// and a Shift-click extends the selection, so neither copies.
let shown = SelectableText::new(label.to_string(), value).show(ui);
// The click's own position and keys: a move or a key change after the release in the same
// frame leaves the pointer and the held keys elsewhere. A frame with more than one release
// copies nothing, since the click flags below do not say which release they describe.
let area = shown.interact_rect.intersect(ui.clip_rect());
let primary = egui::PointerButton::Primary;
let release = ui.input(|i| {
let mut releases = i.events.iter().filter_map(|e| match e {
egui::Event::PointerButton { pos, button, pressed: false, modifiers } if *button == primary => Some((*pos, *modifiers)),
_ => None,
});
let release = releases.next().filter(|_| releases.next().is_none());
let single = i.pointer.button_clicked(primary) && !i.pointer.button_double_clicked(primary) && !i.pointer.button_triple_clicked(primary);
release.filter(|_| single)
});
clicked = release.is_some_and(|(pos, keys)| keys.is_none() && area.contains(pos) && ui.ctx().layer_id_at(pos) == Some(ui.layer_id()));
});
clicked
}
/// `document.info` with its categories, fetched again only when the document, the scope or the
@@ -64,6 +83,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
return;
};
let DocInfoView { selection_only: sel_only, category } = app.ui.doc_info.clone();
let mut copy = None;
Stack::new().show(ui, |ui, _| {
let sections = i["sections"].as_array().map_or(&[][..], Vec::as_slice);
let shown = sections.iter().filter(|s| category.is_empty() || s["id"] == category.as_str());
@@ -92,11 +112,17 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
Text::new(super::label_or_name(label, !named.contains(&k)).to_string()).shade(Shade::Muted).show(ui);
} else {
let shown = shown_value(&i, id, k, (label, value), named.contains(&k)).unwrap_or_else(|| value.to_string());
row(ui, super::label_or_name(label, !named.contains(&k)), shown);
if row(ui, super::label_or_name(label, !named.contains(&k)), &shown) {
copy = Some(shown);
}
}
}
}
});
if let Some(text) = copy {
app.clipboard_out = Some(text);
app.status("Copied the value");
}
}
/// A Brushes row's value in the interface language: the brush's type (`Art Brush`), or that the
+73
View File
@@ -380,6 +380,79 @@ fn copy_and_cut_copy_the_text_selected_in_a_panel_label() {
assert!(copied(&after).iter().any(|t| t.starts_with("<?xml")), "Copy copied the art");
}
/// #8432: a click on a Document Info value copies the whole value and says so in the status bar, and
/// leaves the art. A double click copies it once, and a drag across the value or a Shift-click on
/// it copies nothing. The click's own position and keys count, not where the pointer and the keys
/// are at the end of its frame.
#[test]
fn a_click_on_a_document_info_value_copies_it() {
let mut a = App::new();
let out = show_document_info(&mut a);
let length = "234 pt";
let at = text_rect(&out, length).center();
// Each step's frames, and what they copied.
let copies = |a: &mut App, frames: Vec<Vec<Event>>| frames.into_iter().flat_map(|events| copied(&a.frame(events))).collect::<Vec<_>>();
let (status, sets) = (a.app.ui.status.clone(), a.app.ui.status_sets);
let clicked = copies(&mut a, vec![vec![Event::PointerMoved(at)], vec![click(at, true)], vec![click(at, false)], vec![]]);
assert_eq!(clicked, [length], "a click copies the value, once");
assert!(a.app.ui.status_sets != sets && a.app.ui.status != status, "the status bar says it was copied");
assert_eq!(a.selected(), 1, "the art stays selected");
assert!(a.app.session.clipboard.is_empty(), "no art copied");
a.time += 1.0;
let twice = copies(&mut a, vec![vec![click(at, true)], vec![click(at, false)], vec![click(at, true)], vec![click(at, false)], vec![]]);
assert_eq!(twice, [length], "a double click copies the value once");
a.time += 1.0;
let (from, to) = (text_rect(&out, length).left_center() + vec2(1.0, 0.0), text_rect(&out, length).right_center() - vec2(1.0, 0.0));
let mut drag = vec![vec![Event::PointerMoved(from)], vec![click(from, true)]];
drag.extend((1..=4).map(|k| vec![Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]));
drag.extend([vec![click(to, false)], vec![]]);
assert!(copies(&mut a, drag).is_empty(), "a drag selects and copies nothing");
a.time += 1.0;
let shift = Event::ModifiersChanged(Modifiers::SHIFT);
let shift_click =
|at: Pos2, pressed: bool| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::SHIFT };
let extended = copies(
&mut a,
vec![
vec![shift.clone(), Event::PointerMoved(at)],
vec![shift_click(at, true)],
vec![shift_click(at, false)],
vec![Event::ModifiersChanged(Modifiers::NONE)],
],
);
assert!(extended.is_empty(), "a Shift-click copies nothing");
a.time += 1.0;
// Shift let go in the frame of the release: the click was still a Shift-click.
let let_go = copies(
&mut a,
vec![
vec![shift, Event::PointerMoved(at)],
vec![shift_click(at, true)],
vec![shift_click(at, false), Event::ModifiersChanged(Modifiers::NONE)],
vec![],
],
);
assert!(let_go.is_empty(), "a Shift-click copies nothing when Shift is let go in the same frame");
a.time += 1.0;
// Released on the length, then moved onto the Paths count in the same frame.
let there = text_rect(&out, "2").center();
let moved =
copies(&mut a, vec![vec![Event::PointerMoved(at)], vec![click(at, true)], vec![click(at, false), Event::PointerMoved(there)], vec![]]);
assert_eq!(moved, [length], "a click copies the value it was released on, not the one the pointer moved to");
a.time += 1.0;
// A click on the Paths count, then a drag across the length, in one frame.
let (from, to) = (text_rect(&out, length).left_center() + vec2(1.0, 0.0), text_rect(&out, length).right_center() - vec2(1.0, 0.0));
let both = copies(
&mut a,
vec![
vec![Event::PointerMoved(there)],
vec![click(there, true), click(there, false), Event::PointerMoved(from), click(from, true), Event::PointerMoved(to), click(to, false)],
vec![],
],
);
assert!(!both.iter().any(|t| t == length), "a drag after a click in the same frame does not copy the dragged value");
}
/// Drag across a label in a window of its own (a floating panel's), selecting its text there. The
/// main window's labels then copy nothing on Copy or Cut, which run on the art a pass later.
fn select_label_text_in_another_window(a: &mut App) {