color-w4: net change of 2 commits

- color-w4: written from its sheet
- color-w4: repair 1
This commit is contained in:
bflatastic
2026-11-21 21:27:03 +07:00
parent ca7c44d1c0
commit 5d9d3c28fd
2 changed files with 139 additions and 50 deletions
+98 -50
View File
@@ -15,7 +15,7 @@ use craft_cms::convert::CmykConverter;
use craft_keymap::Mods;
use craft_theme::Srgba;
use craft_ui::buttons::Button;
use craft_ui::choice::{ChoiceWords, Segmented, Swatch, SwatchFace};
use craft_ui::choice::{ChoiceWords, RING_SHARE, Segmented, Swatch, SwatchFace, SwatchStyle, paint_ring};
use craft_ui::contract::{
Access, Activation, Bind, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, Entry, Outcome, Overlay, OverlayUi, Overlays as _, Pages,
PaintUi, PartsCx, Pointer as _, PopupKind, Props as _, Remember as _, Role, Show as _, ShowContainer as _, WordSetCx as _,
@@ -27,7 +27,9 @@ use craft_ui::primitives::textures::CachedTexture;
use craft_ui::primitives::{ContentPainterCx, FacePaint, PaintShape, StrokeWidth};
use craft_ui::runtime::env::{Env, Label};
use craft_ui::runtime::requests::ColorRequest;
use craft_ui::style::{ComponentStyle, DataColor, FocusLook, StyleColor, StyleCx};
use craft_ui::style::{
ComponentStyle, DataColor, Flags, FocusLook, State, StateMix, StateSet, StyleColor, StyleCx, SurfaceEdge, SurfaceStyle,
};
use egui::{Color32, Pos2, Rect, Response, Ui, pos2, vec2};
use super::popover::compact_picker;
@@ -103,9 +105,9 @@ pub struct GradientPreview {
impl GradientPreview {
/// The preview shows `face` in the paint well.
#[must_use]
pub fn face(self, face: FacePaint) -> Self {
let _ = face;
todo!("wg.W47")
pub fn face(mut self, face: FacePaint) -> Self {
self.face = Some(face);
self
}
/// A preview through `stops`, not reversed.
@@ -221,6 +223,23 @@ pub struct PaintChipStyle {
none_fill: StyleColor,
/// The No Color diagonal.
none_mark: StyleColor,
/// The swatch look the ring is painted with.
swatch: SwatchStyle,
}
impl PaintChipStyle {
/// The look of the chip's well, as a ring or filled.
pub(crate) fn well_look(&self, ring: bool) -> WellLook {
WellLook {
none_fill: self.none_fill,
none_mark: self.none_mark,
edge: self.edge,
height: self.height,
radius: self.radius,
ring,
swatch: self.swatch.clone(),
}
}
}
/// The last solid colour the chip held: a switch back to Solid starts from it.
@@ -271,7 +290,6 @@ impl Outcome for PaintChipOutput {
}
/// The look of a paint well: the chip's own look fields.
#[allow(dead_code)] // P3(color-w4): wg.W47
pub(crate) struct WellLook {
pub(crate) none_fill: StyleColor,
pub(crate) none_mark: StyleColor,
@@ -282,15 +300,62 @@ pub(crate) struct WellLook {
/// Whether the well shows its colour as a ring.
pub(crate) ring: bool,
/// The swatch look the ring is painted with.
pub(crate) swatch: craft_ui::choice::SwatchStyle,
pub(crate) swatch: SwatchStyle,
}
/// One paint-well face for `PaintChip` and `SwatchPair`: none, solid, a gradient face or ramp, a pattern or a picture;
/// a face paints over the checker.
#[allow(dead_code)] // P3(color-w4): wg.W47
///
/// A ring well shows a solid paint through [`paint_ring`]; any other paint fills the well and the hole inside the band
/// is filled with the surface the well sits on.
pub(crate) fn paint_well(ui: &mut PaintUi<'_>, cx: &Cx<'_, impl craft_ui::contract::ContentPainter>, well: Rect, paint: &Paint, style: &WellLook) {
let _ = (ui, cx, well, paint, style);
todo!("wg.W47")
if let (true, Paint::Solid(color)) = (style.ring, paint) {
let state = StateSet::new(State::Rest, Flags::empty(), StateMix::new(0.0, 0.0, 0.0));
paint_ring(&ui.draw(), well, *color, &style.swatch, &state, style.radius);
return;
}
let shape = PaintShape::Rect(well, style.radius);
let painter = ui.content_painter(cx);
match paint {
Paint::Solid(color) => {
if color.a < 1.0 {
painter.checker(well);
}
ui.draw().data_fill(&shape, DataColor::new(Color32::from(*color)));
}
Paint::None => {
let draw = ui.draw();
draw.fill(&shape, style.none_fill);
let inner = well.shrink(style.height * DIAGONAL_INSET_IN_HEIGHTS);
let (from, to) = if cx.flow.is_rtl() { (inner.right_bottom(), inner.left_top()) } else { (inner.left_bottom(), inner.right_top()) };
draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Medium, style.none_mark);
}
Paint::Gradient(preview) => {
painter.checker(well);
if let Some(face) = &preview.face {
face.paint(&painter, well);
} else if !preview.stops.is_empty() {
painter.ramp(well, egui::CornerRadius::ZERO, &ramp_stops(preview, cx.flow.is_rtl()), Axis::X);
}
}
Paint::Pattern(image) | Paint::Picture(image) => {
painter.checker(well);
match image.texture {
Some(texture) => paint_image(&painter, well, texture, matches!(paint, Paint::Pattern(_))),
None => ui.draw().fill(&shape, style.none_fill),
}
}
}
if style.ring {
let ground = ui.with_style(cx.resolved.variant, |scope| SurfaceStyle {
edge: SurfaceEdge::None,
shadow: None,
..scope.surface(cx.env.surface())
});
ui.draw().surface_styled(well.shrink(well.width() * RING_SHARE), &ground);
} else {
ui.draw().stroke(&shape, StrokeWidth::Thin, style.edge);
}
}
/// A paint chip, a control high and 1.5 times as wide.
@@ -343,6 +408,8 @@ pub struct PaintChip<'a> {
ring: bool,
/// The request of the Color Picker… button, for this pass.
pick_color: Option<ColorRequest>,
/// The modifiers of this pass's click on a chip without a popover.
clicked: Option<Mods>,
}
impl<'a> PaintChip<'a> {
@@ -366,6 +433,7 @@ impl<'a> PaintChip<'a> {
popover: true,
ring: false,
pick_color: None,
clicked: None,
}
.label(label)
}
@@ -475,6 +543,7 @@ impl Component for PaintChip<'_> {
edge: cx.color(colors.line.control_edge),
none_fill: cx.color(colors.surface.sunken),
none_mark: cx.color(colors.status.bad.solid),
swatch: Swatch::style(cx, Swatch::new(Label::default(), Srgba::BLACK).style_key()),
}
}
@@ -487,7 +556,10 @@ impl Component for PaintChip<'_> {
}
fn parts(&mut self, rect: egui::Rect, cx: &mut PartsCx<'_, Self>) {
let _ = cx.part(WELL, rect).toggles("picker");
let part = cx.part(WELL, rect);
if self.popover {
let _ = part.toggles("picker");
}
}
fn bind(&mut self) -> Option<craft_ui::contract::BindMut<'_, Paint>> {
@@ -500,10 +572,14 @@ impl Component for PaintChip<'_> {
let color = *color;
cx.memory::<LastSolid>().0 = Some(color);
}
let activation = cx.activation().filter(|_: &Activation| cx.resolved.enabled);
if !self.popover {
self.clicked = activation.map(|activation| activation.mods);
return;
}
let popup = cx.popup("picker");
let activated = cx.activation().filter(|_: &Activation| cx.resolved.enabled).is_some();
let open = popup.is_open(cx);
if activated {
if activation.is_some() {
if open {
popup.close(cx);
} else {
@@ -513,6 +589,9 @@ impl Component for PaintChip<'_> {
}
fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) {
if !self.popover {
return;
}
let popup = cx.popup("picker");
let words = cx.words::<PaintWords>();
let name = cx.label();
@@ -561,11 +640,10 @@ impl Component for PaintChip<'_> {
let groups: &[SwatchGroup] = groups;
let rows = swatch_rows(groups, &swatch_words);
let colors: HashMap<&str, &SwatchColor> =
// P3(color-w4): co.K28
groups.iter().flat_map(|g| g.entries.iter().filter_map(|e| e.color().map(|c| (e.name.as_str(), c)))).collect();
let out = PresetBrowser::new(words.swatches.clone(), state.reborrowed(), &rows)
.toolbar(false)
.row_menu(PresetRowMenu::Off) // P3(color-w4): ap.AP5
.row_menu(PresetRowMenu::Off)
.default_open(DefaultOpen::None)
.room_height(Env::of(ui).tokens.size.control.sm * SWATCH_ROWS)
.thumb(|painter: &ContentPainterCx<'_>, rect: Rect, info: ThumbInfo<'_, SwatchKey>| {
@@ -629,51 +707,21 @@ impl Component for PaintChip<'_> {
Some(paint) => access.value(value_text(&words, paint)),
None => access.value(cx.words::<ChoiceWords>().swatch_mixed.as_str()),
};
access.has_popup(PopupKind::Dialog);
if self.popover {
access.has_popup(PopupKind::Dialog);
}
}
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let style = cx.style;
let well = cx.rect;
let shape = PaintShape::Rect(well, style.radius);
let Some(paint) = cx.value().cloned() else {
ui.face(WELL, Swatch::new(cx.resolved.label.to_owned(), Mixed::<Srgba>::Mixed));
return;
};
let painter = ui.content_painter(cx);
match &paint {
Paint::Solid(color) => {
if color.a < 1.0 {
painter.checker(well);
}
ui.draw().data_fill(&shape, DataColor::new(Color32::from(*color)));
}
Paint::None => {
let draw = ui.draw();
draw.fill(&shape, style.none_fill);
let inner = well.shrink(style.height * DIAGONAL_INSET_IN_HEIGHTS);
let (from, to) = if cx.flow.is_rtl() { (inner.right_bottom(), inner.left_top()) } else { (inner.left_bottom(), inner.right_top()) };
draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Medium, style.none_mark);
}
Paint::Gradient(preview) => {
painter.checker(well);
if !preview.stops.is_empty() {
painter.ramp(well, egui::CornerRadius::ZERO, &ramp_stops(preview, cx.flow.is_rtl()), Axis::X);
}
}
Paint::Pattern(image) | Paint::Picture(image) => {
painter.checker(well);
match image.texture {
Some(texture) => paint_image(&painter, well, texture, matches!(paint, Paint::Pattern(_))),
None => ui.draw().fill(&shape, style.none_fill),
}
}
}
ui.draw().stroke(&shape, StrokeWidth::Thin, style.edge);
paint_well(ui, cx, cx.rect, &paint, &cx.style.well_look(self.ring));
}
fn finish(self, cx: Cx<'_, Self>) -> PaintChipOutput {
PaintChipOutput { edit: cx.edit_response(), screen_pick: self.screen_pick, pick_color: self.pick_color, clicked: None }
PaintChipOutput { edit: cx.edit_response(), screen_pick: self.screen_pick, pick_color: self.pick_color, clicked: self.clicked }
}
}
@@ -5,7 +5,9 @@ use craft_behavior::Mixed;
use craft_theme::Srgba;
use craft_ui::contract::Role;
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _, Validity};
use craft_ui::display::{Shade, Text};
use craft_ui::layout_components::Stack;
use craft_ui::primitives::FacePaint;
use craft_ui::patterns::presets::PresetState;
use craft_ui::runtime::env::Message;
use craft_ui::testing::{Drive, Fixture, StoryData, StoryOutput, story};
@@ -58,6 +60,45 @@ fn paint_chip(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
first.unwrap_or_default()
}
/// Host-picker mode: a click opens nothing and reports the modifiers held.
#[story(PaintChip<'static>, covers = [], drive = Drive::Never)]
fn paint_chip_host_picker(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let (held, clicks) = data.value("state", (Paint::Solid(BLUE), 0u32));
let shown = PaintChip::new("Fill", Bind::bound(held), BLUE).popover(false).show(ui);
if shown.clicked.is_some() {
*clicks += 1;
}
Text::new(format!("Clicks: {clicks}")).shade(Shade::Muted).show(ui);
shown.into()
}
/// The chip as a ring round an open middle, for a solid and a gradient paint.
#[story(PaintChip<'static>, covers = [], drive = Drive::Never)]
fn paint_chip_ring(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let gradient = GradientPreview::new(vec![(0.0, BLUE), (1.0, Srgba::rgb(1.0, 1.0, 1.0))]);
let paints = data.value("paints", [Paint::Solid(BLUE), Paint::Gradient(gradient)]);
let mut first = None;
Stack::new().show(ui, |ui, _| {
for (index, (label, paint)) in ["Stroke", "Gradient stroke"].into_iter().zip(paints.iter_mut()).enumerate() {
let shown = PaintChip::new(label, Bind::bound(paint), BLUE).ring(true).show(ui);
if index == 0 {
first = Some(StoryOutput::from(shown));
}
}
});
first.unwrap_or_default()
}
/// A gradient whose face is the host's painter: one coloured rect over the checker.
#[story(PaintChip<'static>, covers = [], drive = Drive::Never)]
fn paint_chip_gradient_face(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let face = FacePaint::new(1, |painter, rect| {
painter.painter.rect_filled(rect.shrink2(egui::vec2(rect.width() / 4.0, 0.0)), 0.0, egui::Color32::from_rgb(230, 120, 30));
});
let preview = GradientPreview::new(vec![(0.0, BLUE), (1.0, Srgba::rgb(1.0, 1.0, 1.0))]).face(face);
chip(ui, data, Paint::Gradient(preview))
}
#[story(PaintChip<'static>, covers = [Mixed], drive = Drive::Never)]
fn paint_chip_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let held = data.value("mixed", Mixed::<Paint>::Mixed);