M3: the Design panel's header names the selected component or set (#2347)

This commit is contained in:
bflatastic
2026-11-19 03:59:15 +07:00
parent 964e7da7ec
commit e1b96dc5c8
4 changed files with 101 additions and 7 deletions
@@ -0,0 +1,46 @@
//! The Design panel's header with one component or component set selected, driven through egui
//! headless: it names the selected one ("Knapp"), not only its type (#2347). Other layers keep their
//! type ("Rectangle").
use figcraft_doc::NodeId;
use figcraft_engine::Session;
use serde_json::json;
use crate::export_preview_tests::labelled;
use crate::layers_drag_tests::Ui;
fn id(v: serde_json::Value) -> NodeId {
NodeId(v["id"].as_u64().unwrap())
}
/// The text painted in the right panel's header strip (the top of the Design tab).
fn header(u: &mut Ui) -> Vec<String> {
let right = 1440.0 - u.app.ui.right_width;
labelled(u).into_iter().filter(|(_, at)| at.x > right && at.y < 190.0).map(|(t, _)| t).collect()
}
#[test]
fn the_header_names_the_selected_component_or_set() {
let mut s = Session::new();
let f = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 80, "height": 40, "name": "Knapp"})).unwrap());
let c = id(s.execute("component.create", &json!({"ids": [f]})).unwrap());
let r = id(s.execute("shape.rectangle", &json!({"x": 200, "y": 0, "width": 40, "height": 40})).unwrap());
s.execute("select.set", &json!({"ids": [c]})).unwrap();
let mut u = Ui::new(s);
let shown = header(&mut u);
assert!(shown.iter().any(|t| t == "Knapp"), "the component's name: {shown:?}");
assert!(!shown.iter().any(|t| t == "Component"), "not only its type: {shown:?}");
// As a set (Add variant), it is the set's name.
u.app.run("component.addVariant", json!({"id": c}));
let set = u.app.session.doc().unwrap().node(c).unwrap().parent.unwrap();
u.app.run("node.set", json!({"ids": [set], "props": {"name": "Knappar"}}));
u.app.run("select.set", json!({"ids": [set]}));
let shown = header(&mut u);
assert!(shown.iter().any(|t| t == "Knappar"), "the set's name: {shown:?}");
// Another layer still shows its type.
u.app.run("select.set", json!({"ids": [r]}));
let shown = header(&mut u);
assert!(shown.iter().any(|t| t == "Rectangle"), "{shown:?}");
}
+2
View File
@@ -78,6 +78,8 @@ mod colorpicker_selection_tests;
#[cfg(test)]
mod comments_tests;
#[cfg(test)]
mod component_header_tests;
#[cfg(test)]
mod component_props_reorder_tests;
#[cfg(test)]
mod component_set_narrow_tests;
+51 -6
View File
@@ -17,7 +17,7 @@ use crate::i18n::{tr, trf, trn};
use crate::icons::{self, Icon};
use crate::list_drag::{ListDrag, move_flags, remove_flags};
use crate::style_picker::{self, StyleSlot};
use crate::theme::{Tokens, regular, semibold};
use crate::theme::{COMPONENT, Tokens, regular, semibold};
use crate::widgets::{self, Edit, FIELD_H, FieldEdit, Lead, PAD};
use crate::{FigApp, RightTab};
@@ -410,11 +410,36 @@ fn page_section(app: &mut FigApp, ui: &mut Ui, doc: &Document) {
crate::export_panel::page_section(app, ui, doc, page);
}
/// One component or component set selected: it goes by its name, after its icon, in the header;
/// its type shows on hover (#2347).
fn named_component<'a>(doc: &'a Document, sel: &[NodeId]) -> Option<(&'a Node, Icon)> {
let [id] = sel else { return None };
let n = doc.node(*id)?;
match n.kind {
NodeKind::Component { .. } => Some((n, Icon::Component)),
NodeKind::ComponentSet { .. } => Some((n, Icon::ComponentSet)),
_ => None,
}
}
/// The header label's hover: a component's type (and its name when cut short), else the whole
/// label when cut short.
fn label_hover(resp: egui::Response, named: Option<&Node>, shown: &str, label: &str) -> egui::Response {
match named {
Some(n) if shown != label => resp.on_hover_text(format!("{} · {label}", tr(n.kind.label()))),
Some(n) => resp.on_hover_text(tr(n.kind.label())),
None if shown != label => resp.on_hover_text(label),
None => resp,
}
}
/// Draw the selection header: type name (or Mixed), frame/group conversion, orientation
/// swap and frame presets, component/mask/boolean quick actions, and instance details.
fn header(app: &mut FigApp, ui: &mut Ui, doc: &Document, sel: &[NodeId]) {
let t = Tokens::get(ui.ctx());
let named = named_component(doc, sel);
let label = match common(doc, sel, |n| n.kind.label()) {
_ if named.is_some() => named.map(|(n, _)| n.name.clone()).unwrap_or_default(),
Some(l) if sel.len() == 1 => tr(l).to_string(),
// The plural ("Frames") is a key of its own.
Some(l) => tr(&format!("{l}s")).to_string(),
@@ -438,16 +463,36 @@ fn header(app: &mut FigApp, ui: &mut Ui, doc: &Document, sel: &[NodeId]) {
// Keep the type menu and frame actions together. A short label must not leave
// the orientation icon floating in the middle of the header.
// The handoff icon also fits beside the frame controls at the panel minimum.
let max_label_width = if is_frame { (w - 202.0).clamp(40.0, 120.0) } else { 92.0 };
let shown = if is_frame || is_group { widgets::elide_font(ui, &label, max_label_width, semibold(13.0)) } else { label.clone() };
let label_width = if is_frame || is_group { ui.painter().layout_no_wrap(shown.clone(), semibold(13.0), t.text).size().x + 28.0 } else { 120.0 };
// A name keeps clear of the three quick actions on the right.
let max_label_width = if is_frame {
(w - 202.0).clamp(40.0, 120.0)
} else if named.is_some() {
(w - 132.0).clamp(40.0, 200.0)
} else {
92.0
};
let elided = is_frame || is_group || named.is_some();
let shown = if elided { widgets::elide_font(ui, &label, max_label_width, semibold(13.0)) } else { label.clone() };
let text_width = || ui.painter().layout_no_wrap(shown.clone(), semibold(13.0), t.text).size().x;
let label_width = match named {
Some(_) => text_width() + 38.0,
None if elided => text_width() + 28.0,
None => 120.0,
};
let lr = Rect::from_min_size(pos2(r.min.x + 8.0, r.center().y - 12.0), vec2(label_width, 24.0));
let resp = ui.interact(lr, ui.scope_id().with("typedd"), Sense::click());
if resp.hovered() {
ui.painter().rect_filled(lr, CornerRadius::same(5), t.hover);
}
let resp = if shown == label { resp } else { resp.on_hover_text(&label) };
let label_rect = ui.painter().text(pos2(lr.min.x + 8.0, lr.center().y), Align2::LEFT_CENTER, &shown, semibold(13.0), t.text);
let resp = label_hover(resp, named.map(|(n, _)| n), &shown, &label);
let text_x = match named {
Some((_, icon)) => {
icons::paint(ui.painter(), Rect::from_center_size(pos2(lr.min.x + 14.0, lr.center().y), vec2(14.0, 14.0)), icon, COMPONENT);
lr.min.x + 26.0
}
None => lr.min.x + 8.0,
};
let label_rect = ui.painter().text(pos2(text_x, lr.center().y), Align2::LEFT_CENTER, &shown, semibold(13.0), t.text);
if is_frame || is_group {
icons::paint(
ui.painter(),
+2 -1
View File
File diff suppressed because one or more lines are too long