mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-29 01:34:37 +01:00
Merge pull request #2634 from EurekaMk2/hide-selection-when-picking-color
Hide the selection frame while color picker controls are used
This commit is contained in:
4 files changed
+222
-3
No files matched your search
@@ -72,6 +72,44 @@ impl FigApp {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the colour picker is being used right now for the selected layers' own look (a
|
||||
/// paint, an effect, a text range or Selection colors): a drag on its value controls (the
|
||||
/// palette, the hue and alpha sliders, a number field, the gradient bar and its stops), the
|
||||
/// keyboard in one of its fields, the eyedropper sampling, or a drag of the canvas handles it
|
||||
/// owns (gradient stops and handles, image crop). Only then does the canvas hide the selection
|
||||
/// frame and a text's coloured selection, so the colour being chosen shows against the page.
|
||||
/// Merely having the picker open, or pressing a button, a menu or a swatch, leaves it visible.
|
||||
pub(crate) fn picker_hides_selection(&self, ctx: &egui::Context) -> bool {
|
||||
let edits_selection = matches!(
|
||||
self.ui.picker,
|
||||
Some(
|
||||
crate::colorpicker::PickerTarget::Paint { .. }
|
||||
| crate::colorpicker::PickerTarget::Effect { .. }
|
||||
| crate::colorpicker::PickerTarget::TextRange { .. }
|
||||
| crate::colorpicker::PickerTarget::SelectionColor { .. }
|
||||
)
|
||||
);
|
||||
if !edits_selection {
|
||||
return false;
|
||||
}
|
||||
// The picker draws after the canvas, so its card comes from the previous frame.
|
||||
let card = self.ui.picker_popover.as_ref();
|
||||
// A drag changes the colour live; a press alone does not. Buttons, menus, swatches and the
|
||||
// paint-type switches are clicks and must not hide the selection while held (they used to,
|
||||
// which flickered it). Dragging the card by its header moves the popover, not the colour.
|
||||
let header_drag = card.map(|c| c.header_drag_id());
|
||||
let dragging = ctx.dragged_id().is_some_and(|id| header_drag != Some(id))
|
||||
&& ctx.input(|i| i.pointer.any_down() && i.pointer.press_origin().is_some_and(|p| card.is_some_and(|c| c.rect.contains(p))));
|
||||
// A value field keeps the keyboard after its click: hide until focus leaves the picker.
|
||||
// Only a text edit counts: every clicked control takes focus in egui, and a focused
|
||||
// palette square or swatch must not keep the selection hidden after the drag.
|
||||
let typing = ctx.text_edit_focused()
|
||||
&& ctx
|
||||
.memory(|m| m.focused())
|
||||
.is_some_and(|id| ctx.read_response(id).is_some_and(|r| card.is_some_and(|c| c.rect.contains_rect(r.rect))));
|
||||
dragging || typing || self.session.tool.pick_only || crate::handles::gradient_dragging(ctx) || crate::handles::crop_dragging(ctx)
|
||||
}
|
||||
|
||||
/// Leaving Design ends a pen path as Escape does and goes back to Move: Prototype and Inspect
|
||||
/// don't draw (#307). Checked as the frame starts (the control channel) and after the panels
|
||||
/// and shortcuts (the mode switcher, the tabs, Shift+E/Shift+D), before the canvas sees more.
|
||||
|
||||
@@ -124,7 +124,10 @@ pub(super) fn overlays(app: &FigApp, painter: &egui::Painter, t: &Tokens) -> Vec
|
||||
}
|
||||
// Selection.
|
||||
let editing = ds.text_edit.clone();
|
||||
if !sel.is_empty() && editing.is_none() && ds.vector_edit.is_none() {
|
||||
// While the colour picker is being used on the selection, hide its frame and handles so the
|
||||
// colour being picked shows on the layer itself (a text's coloured selection would hide it
|
||||
// too).
|
||||
if !sel.is_empty() && editing.is_none() && ds.vector_edit.is_none() && !app.picker_hides_selection(painter.ctx()) {
|
||||
size_label = selection_overlay(app, painter, ds, &doc, &sel, zoom);
|
||||
}
|
||||
if app.session.tool.tool == ToolId::ShapeBuilder
|
||||
@@ -268,12 +271,16 @@ fn text_edit_overlay(app: &FigApp, painter: &egui::Painter, t: &Tokens, doc: &Do
|
||||
let preedit = app.preedit_view();
|
||||
let edoc: &Document = preedit.map(|p| &*p.doc).unwrap_or(doc);
|
||||
let Some(text) = TextOnScreen::of(edoc, te.id) else { return };
|
||||
// While the colour picker is being used on this text, its frame and coloured selection hide
|
||||
// too: the selection colour tinted the very characters whose colour is being chosen. The
|
||||
// caret stays.
|
||||
let picking = app.picker_hides_selection(painter.ctx());
|
||||
let layout = &text.layout;
|
||||
if let Some(q) = node_quad(app, edoc, te.id) {
|
||||
if !picking && let Some(q) = node_quad(app, edoc, te.id) {
|
||||
quad(painter, q, stroke(accent(painter), 1.0));
|
||||
}
|
||||
let (a, b) = (te.caret.min(te.anchor), te.caret.max(te.anchor));
|
||||
if a != b && preedit.is_none() {
|
||||
if a != b && preedit.is_none() && !picking {
|
||||
for q in text.selection(app, a, b) {
|
||||
painter.add(Shape::convex_polygon(q.to_vec(), accent(painter).gamma_multiply(0.3), Stroke::NONE));
|
||||
}
|
||||
|
||||
@@ -372,3 +372,172 @@ fn a_gradient_drag_whose_gesture_closes_under_the_held_button_stops_editing() {
|
||||
u.button(u.app.to_screen(180.0, 200.0), false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `shape` is painted in `col`, as a fill or a stroke.
|
||||
fn painted_in(shape: &egui::Shape, col: egui::Color32) -> bool {
|
||||
use egui::epaint::ColorMode;
|
||||
match shape {
|
||||
egui::Shape::Path(p) => p.fill == col || matches!(p.stroke.color, ColorMode::Solid(c) if c == col),
|
||||
egui::Shape::Rect(r) => r.fill == col || r.stroke.color == col,
|
||||
egui::Shape::LineSegment { stroke, .. } => stroke.color == col,
|
||||
egui::Shape::Circle(c) => c.fill == col || c.stroke.color == col,
|
||||
egui::Shape::Vec(v) => v.iter().any(|s| painted_in(s, col)),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// How many shapes painted in `col` this frame draws inside `area`. On the page those are the
|
||||
/// selection's frame, its handles and a text's coloured selection.
|
||||
fn painted_around(out: &egui::FullOutput, area: egui::Rect, col: egui::Color32) -> usize {
|
||||
out.shapes.iter().filter(|c| painted_in(&c.shape, col) && c.shape.visual_bounding_rect().intersects(area)).count()
|
||||
}
|
||||
|
||||
/// A text layer's box on screen, with room for its handles.
|
||||
fn text_area(u: &Ui, id: NodeId) -> egui::Rect {
|
||||
let b = u.app.session.doc().unwrap().world_bounds(id).unwrap();
|
||||
egui::Rect::from_two_pos(u.app.to_screen(b.x0, b.y0), u.app.to_screen(b.x1, b.y1)).expand(20.0)
|
||||
}
|
||||
|
||||
fn selected_text(s: &mut Session) -> NodeId {
|
||||
let id =
|
||||
NodeId(s.execute("text.create", &json!({"text": "Hello", "x": 300, "y": 200, "props": {"fontSize": 48}})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("select.set", &json!({"ids": [id]})).unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
// The colour picker only hides the selection while it is being used: opening it leaves the frame in
|
||||
// view, and the press/drag on its controls hides it. A text makes this matter: its frame runs
|
||||
// through the glyphs and its selection is tinted.
|
||||
#[test]
|
||||
fn a_selected_texts_frame_hides_only_while_its_colour_picker_is_used() {
|
||||
let mut s = Session::new();
|
||||
let text = selected_text(&mut s);
|
||||
let mut u = Ui::new(s);
|
||||
let area = text_area(&u, text);
|
||||
let accent = crate::theme::canvas_accent(&u.ctx);
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "the selected text's frame shows");
|
||||
|
||||
let target = PickerTarget::Paint { fills: true, index: 0, ids: vec![text] };
|
||||
u.app.open_picker(target.clone(), None);
|
||||
for _ in 0..3 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
assert_eq!(u.app.ui.picker, Some(target.clone()), "the picker is open");
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "an open picker alone leaves the frame in view");
|
||||
|
||||
// A press alone changes nothing; dragging the saturation/value square uses the picker.
|
||||
let square = u.ctx.read_response(egui::Id::unique("cp_sv")).expect("the saturation/value square").rect;
|
||||
let from = square.center();
|
||||
let to = from + egui::vec2(12.0, 8.0);
|
||||
u.button(from, true);
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "holding the palette still leaves the frame");
|
||||
u.frame(vec![egui::Event::PointerMoved(to)]);
|
||||
assert_eq!(painted_around(&u.frame_output(vec![]), area, accent), 0, "dragging the palette hides the frame");
|
||||
u.button(to, false);
|
||||
assert_eq!(u.app.ui.picker, Some(target), "releasing does not close the picker");
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "releasing brings the frame back");
|
||||
|
||||
// A button in the picker (the blend dropdown) is a click, not a value drag.
|
||||
let button = egui::pos2(square.center().x, square.min.y - 30.0);
|
||||
u.button(button, true);
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "holding a picker button leaves the frame");
|
||||
u.button(button, false);
|
||||
}
|
||||
|
||||
// The picker's header is its drag handle: moving the card is not choosing a colour.
|
||||
#[test]
|
||||
fn dragging_the_picker_by_its_header_does_not_hide_the_selection() {
|
||||
let mut s = Session::new();
|
||||
let text = selected_text(&mut s);
|
||||
let mut u = Ui::new(s);
|
||||
let area = text_area(&u, text);
|
||||
let accent = crate::theme::canvas_accent(&u.ctx);
|
||||
u.app.open_picker(PickerTarget::Paint { fills: true, index: 0, ids: vec![text] }, None);
|
||||
for _ in 0..3 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
let header = u.app.ui.picker_popover.as_ref().expect("the picker is open").header.center();
|
||||
let before = u.app.ui.picker_popover.as_ref().unwrap().rect;
|
||||
u.button(header, true);
|
||||
for k in 1..=5 {
|
||||
u.frame(vec![egui::Event::PointerMoved(header - egui::vec2(0.0, 6.0 * k as f32))]);
|
||||
}
|
||||
assert!(u.app.ui.picker_popover.as_ref().unwrap().rect.min.y < before.min.y - 10.0, "the header drag moved the picker");
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "moving the picker leaves the frame in view");
|
||||
u.button(header - egui::vec2(0.0, 30.0), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_texts_coloured_selection_hides_only_while_its_colour_picker_is_used() {
|
||||
let mut s = Session::new();
|
||||
let text = selected_text(&mut s);
|
||||
// Select all of it: the selection highlight the colour picker's own layer would otherwise wear.
|
||||
figcraft_engine::textedit::begin(&mut s, text, None, false).unwrap();
|
||||
let mut u = Ui::new(s);
|
||||
let area = text_area(&u, text);
|
||||
let tint = crate::theme::canvas_accent(&u.ctx).gamma_multiply(0.3);
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, tint) > 0, "the text selection is tinted");
|
||||
|
||||
u.app.open_picker(PickerTarget::Paint { fills: true, index: 0, ids: vec![text] }, None);
|
||||
for _ in 0..3 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, tint) > 0, "an open picker alone leaves it tinted");
|
||||
|
||||
let square = u.ctx.read_response(egui::Id::unique("cp_sv")).expect("the saturation/value square").rect;
|
||||
let from = square.center();
|
||||
let to = from + egui::vec2(10.0, 6.0);
|
||||
u.button(from, true);
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, tint) > 0, "holding the palette still leaves the tint");
|
||||
u.frame(vec![egui::Event::PointerMoved(to)]);
|
||||
assert_eq!(painted_around(&u.frame_output(vec![]), area, tint), 0, "dragging the palette hides the tint");
|
||||
u.button(to, false);
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, tint) > 0, "releasing brings the tint back");
|
||||
}
|
||||
|
||||
// Typing a colour value counts as using the picker even when no mouse button is down.
|
||||
#[test]
|
||||
fn a_focused_colour_field_hides_the_selection_while_it_is_edited() {
|
||||
let mut s = Session::new();
|
||||
let text = selected_text(&mut s);
|
||||
let mut u = Ui::new(s);
|
||||
let area = text_area(&u, text);
|
||||
let accent = crate::theme::canvas_accent(&u.ctx);
|
||||
u.app.open_picker(PickerTarget::Paint { fills: true, index: 0, ids: vec![text] }, None);
|
||||
for _ in 0..3 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "the frame shows while the picker is idle");
|
||||
|
||||
let hex = u.ctx.read_response(egui::Id::unique("cp_hex").with("click")).expect("the Hex field").rect;
|
||||
u.button(hex.center(), true);
|
||||
u.button(hex.center(), false);
|
||||
for _ in 0..2 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
assert_eq!(painted_around(&u.frame_output(vec![]), area, accent), 0, "a focused value field hides the frame");
|
||||
u.frame(vec![egui::Event::Text("0".into())]);
|
||||
assert_eq!(painted_around(&u.frame_output(vec![]), area, accent), 0, "and typing in it keeps the frame hidden");
|
||||
}
|
||||
|
||||
// egui gives every clicked control the keyboard, a palette square and a swatch included. Only a
|
||||
// text field means the picker is being used: a square that keeps focus must not keep the
|
||||
// selection hidden after its drag ends.
|
||||
#[test]
|
||||
fn a_focused_palette_square_does_not_hide_the_selection() {
|
||||
let mut s = Session::new();
|
||||
let text = selected_text(&mut s);
|
||||
let mut u = Ui::new(s);
|
||||
let area = text_area(&u, text);
|
||||
let accent = crate::theme::canvas_accent(&u.ctx);
|
||||
u.app.open_picker(PickerTarget::Paint { fills: true, index: 0, ids: vec![text] }, None);
|
||||
for _ in 0..3 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
// What egui does to a clicked square.
|
||||
u.ctx.memory_mut(|m| m.request_focus(egui::Id::unique("cp_sv")));
|
||||
for _ in 0..2 {
|
||||
u.frame(vec![]);
|
||||
}
|
||||
assert!(painted_around(&u.frame_output(vec![]), area, accent) > 0, "focus on a palette square leaves the frame in view");
|
||||
}
|
||||
@@ -124,6 +124,11 @@ impl Popover {
|
||||
}
|
||||
}
|
||||
|
||||
/// The id of the header's drag handle: a drag there moves the panel, not a value in it.
|
||||
pub fn header_drag_id(&self) -> Id {
|
||||
self.id.with("drag")
|
||||
}
|
||||
|
||||
/// `width` points wide instead of [`WIDTH`].
|
||||
pub fn width(mut self, width: f32) -> Self {
|
||||
self.width = width.max(1.0);
|
||||
|
||||
Reference in new issue
Block a user