fix(ui): add Properties gradient presets and hide opaque checkerboards

This commit is contained in:
Skyfuller1303 committed 2026-10-11 02:33:20 +02:00
1 parent 1acde98b6b
commit 587bbb0b3b
3 files changed
+133 -9

No files matched your search

+71 -5
View File
@@ -511,12 +511,16 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform
/// A ramp of RGBA stops over a checkerboard in `r`.
fn paint_ramp(p: &egui::Painter, r: Rect, sample: impl Fn(f32) -> [f32; 4]) {
widgets::checker(p, r, 5.0);
let n = 64;
let samples: Vec<_> = (0..=n).map(|k| sample(k as f32 / n as f32)).collect();
// Opaque previews need no transparency background, including at subpixel edges.
let painter = p.with_clip_rect(r);
if samples.iter().any(|c| c[3] < 1.0) {
widgets::checker(&painter, r, 5.0);
}
let mut mesh = egui::Mesh::default();
for k in 0..=n {
for (k, c) in samples.into_iter().enumerate() {
let u = k as f32 / n as f32;
let c = sample(u);
let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
let col = Color32::from_rgba_unmultiplied(q(c[0]), q(c[1]), q(c[2]), q(c[3]));
let x = r.left() + u * r.width();
@@ -528,7 +532,7 @@ fn paint_ramp(p: &egui::Painter, r: Rect, sample: impl Fn(f32) -> [f32; 4]) {
mesh.add_triangle(i - 1, i + 1, i);
}
}
p.add(mesh);
painter.add(mesh);
}
/// Side, in pixels, of a gradient fill layer's thumbnail texture.
@@ -786,11 +790,26 @@ fn preset_stop_color(app: &mut PhotocraftApp, i: usize) {
}
fn stops_editor_for(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
stops_editor(app, ui, sink, f, false);
}
fn stops_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill, picker: bool) {
let key = strip_key(app, sink).with("selection");
if ui.data(|d| d.get_temp::<Marker>(key)).is_none() {
ui.data_mut(|d| d.insert_temp(key, Marker::Color(0)));
}
stop_strip(app, ui, sink, f);
if picker {
ui.horizontal_top(|ui| {
let width = (ui.available_width() - 20.0 - ui.spacing().item_spacing.x).max(120.0);
ui.allocate_ui(vec2(width, 58.0), |ui| stop_strip(app, ui, sink, f));
ui.vertical(|ui| {
ui.add_space(18.0);
preset_browser::layer_picker(app, ui, sink);
});
});
} else {
stop_strip(app, ui, sink, f);
}
// A preset replacement discards drafts owned by the previous stop set.
let generation = ui.data(|d| d.get_temp::<u64>(key.with("generation"))).unwrap_or_default();
ui.push_id(generation, |ui| properties_panel::stop_fields(app, ui, sink));
@@ -1024,6 +1043,53 @@ mod tests {
app
}
#[test]
fn opaque_ramp_has_no_checkerboard_fringe_at_fractional_pixel_edges() {
use egui_kittest::Harness;
for scale in [1.0, 2.0] {
let edge = 20.0 + 0.3 / scale;
let rect = Rect::from_min_size(pos2(edge, edge), vec2(96.0, 20.0));
let Ok(mut h) = std::panic::catch_unwind(|| {
Harness::builder().with_size(vec2(160.0, 80.0)).with_pixels_per_point(scale).wgpu().build_ui(move |ui| {
paint_ramp(ui.painter(), rect, |_| [0.1, 0.25, 0.2, 1.0]);
})
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
h.run_steps(3);
let image = h.render().unwrap();
let background = image.get_pixel((8.0 * scale) as u32, (25.0 * scale) as u32);
let right = (rect.right() * scale).floor() as u32;
for y in ((rect.top() + 2.0) * scale) as u32..((rect.bottom() - 2.0) * scale) as u32 {
let pixel = image.get_pixel(right, y);
for channel in 0..3 {
assert!(pixel[channel].abs_diff(background[channel]) <= 2, "scale={scale} at ({right}, {y}): {pixel:?} vs {background:?}");
}
}
}
}
#[test]
fn genuinely_transparent_ramp_still_shows_checkerboard_at_both_scales() {
use egui_kittest::Harness;
for scale in [1.0, 2.0] {
let Ok(mut h) = std::panic::catch_unwind(|| {
Harness::builder().with_size(vec2(160.0, 80.0)).with_pixels_per_point(scale).wgpu().build_ui(|ui| {
paint_ramp(ui.painter(), Rect::from_min_size(pos2(20.0, 20.0), vec2(96.0, 20.0)), |_| [0.1, 0.25, 0.2, 0.0]);
})
}) else {
eprintln!("skipping: no GPU adapter");
return;
};
h.run_steps(3);
let image = h.render().unwrap();
let a = image.get_pixel((32.0 * scale) as u32, (22.0 * scale) as u32);
let b = image.get_pixel((32.0 * scale) as u32, (27.0 * scale) as u32);
assert!(a[0].abs_diff(b[0]) >= 30, "scale={scale}: {a:?} vs {b:?}");
}
}
fn mods() -> egui::Modifiers {
egui::Modifiers::NONE
}
@@ -32,6 +32,10 @@ fn new_params(app: &PhotocraftApp, sink: Sink) -> Value {
pub(super) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, panel: &str) -> bool {
let sink = target(app);
show_for(app, ui, panel, sink)
}
fn show_for(app: &mut PhotocraftApp, ui: &mut egui::Ui, panel: &str, sink: Sink) -> bool {
let params = new_params(app, sink);
if !crate::preset_panels::gradient_picker(app, ui, panel, matches!(sink, Sink::Layer(_)), params) {
return false;
@@ -54,6 +58,17 @@ pub(super) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, panel: &str) -> b
true
}
pub(super) fn layer_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink) {
let arrow = crate::icons::button(ui, "chevron-down", 20.0, false, "Presets");
arrow.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), tl!("Presets")));
egui::Popup::from_toggle_button_response(&arrow).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| {
ui.set_width(320.0);
if show_for(app, ui, "gradient-properties-presets", sink) {
ui.close();
}
});
}
pub(super) fn swatch(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> bool {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
@@ -14,22 +14,22 @@ const STYLES: [(GradientStyle, &str); 5] = [
pub(super) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
let document = app.session.active().map(|s| s.doc.id);
ui.push_id(("gradient-properties", document, layer.id.0), |ui| {
layer_controls(app, ui, layer);
layer_controls(app, ui, layer, true);
});
}
pub(super) fn editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
let document = app.session.active().map(|s| s.doc.id);
ui.push_id(("gradient-editor-layer", document, layer.id.0), |ui| {
layer_controls(app, ui, layer);
layer_controls(app, ui, layer, false);
});
}
fn layer_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
fn layer_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer, picker: bool) {
if props_layout::section(ui, "gradient-fill-stops", "Gradient")
&& let Some(fill) = current_fill(app, layer.id)
{
stops_editor_for(app, ui, Sink::Layer(layer.id), fill);
stops_editor(app, ui, Sink::Layer(layer.id), fill, picker);
}
options(app, ui, layer);
}
@@ -254,6 +254,49 @@ mod tests {
h.run_steps(3);
}
#[test]
fn properties_dropdown_edits_the_layer_in_both_tool_modes_and_saves_it() {
for classic in [false, true] {
let mut app = app();
app.ui.tool_options.gradient_classic = classic;
let mut h = Harness::builder().with_size(vec2(1000.0, 800.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
ui.set_width(280.0);
panel(ui, app);
},
app,
);
PhotocraftApp::setup_context(&h.ctx, theme::ThemeKind::ProMedium);
h.run_steps(3);
let original = fill(h.state());
let count = h.state().session.active().unwrap().history.past_len();
let arrow = h.get_by_label("Presets").rect();
let strip = h.get_by_label("Gradient stops").rect();
assert!(arrow.left() >= strip.right() && arrow.right() - strip.left() <= 280.0, "{arrow:?} beside {strip:?}");
assert!((arrow.center().y - (strip.top() + 28.0)).abs() < 1.0);
h.get_by_label("Presets").click();
h.run_steps(3);
h.get_by_label("Create new gradient").click();
h.run_steps(3);
let saved = h.state().session.presets.gradients.iter().flat_map(|g| &g.items).find(|p| p.name == "Custom").unwrap();
assert_eq!(saved.opacity, vec![(0.0, 0.2), (1.0, 1.0)]);
assert_eq!(saved.resolve([0.0; 4], [0.0; 4])[0].1, [1.0, 0.0, 0.0, 0.2]);
assert_eq!(fill(h.state()), original);
h.key_press(egui::Key::Escape);
h.run_steps(3);
h.get_by_label("Presets").click();
h.run_steps(3);
h.get_by_label("Foreground to Transparent").click();
h.run_steps(3);
assert!(matches!(fill(h.state()), Fill::Gradient { opacity_stops, .. } if opacity_stops == vec![(0.0, 1.0), (1.0, 0.0)]));
assert_eq!(h.state().session.active().unwrap().history.past_len(), count + 1);
assert_eq!(h.state().session.active().unwrap().doc.layers.len(), 2);
assert!(h.query_by_label("Basics").is_none());
h.state_mut().run("edit.undo", json!({})).unwrap();
assert_eq!(fill(h.state()), original);
}
}
#[test]
fn style_icons_edit_the_selected_layer_with_one_undo_step() {
let mut h = Harness::builder().with_size(vec2(280.0, 800.0)).build_ui_state(panel, app());