Corrected display error on color picker in windows
Moorly CI / windows-build (push) Successful in 3m7s
Moorly CI / linux-build (push) Successful in 3m42s

This commit is contained in:
Squid committed 2026-11-08 16:35:11 +01:00
1 parent 0c9946d2b4
commit 6915fc2857
2 files changed
+276 -131

No files matched your search

+8 -2
View File
@@ -312,7 +312,13 @@ where
); );
} }
let position = layout.position() + translation; let bounds = layout.bounds();
let anchor = Rectangle {
x: bounds.x + translation.x,
y: bounds.y + translation.y,
width: bounds.width,
height: bounds.height,
};
Some( Some(
ColorPickerOverlay::new( ColorPickerOverlay::new(
@@ -320,7 +326,7 @@ where
self.on_cancel.clone(), self.on_cancel.clone(),
&self.on_submit, &self.on_submit,
self.on_color_change.as_deref(), self.on_color_change.as_deref(),
position, anchor,
&self.class, &self.class,
&mut tree.children[1], &mut tree.children[1],
*viewport, *viewport,
+268 -129
View File
@@ -9,10 +9,11 @@ use crate::{
}; };
use iced_core::{ use iced_core::{
Alignment, Border, Clipboard, Color, Element, Event, Layout, Length, Overlay, Padding, Pixels, Alignment, Background, Border, Clipboard, Color, Element, Event, Gradient, Layout, Length,
Point, Rectangle, Renderer as _, Shell, Size, Text, Vector, Widget, Overlay, Padding, Pixels, Point, Radians, Rectangle, Renderer as _, Shell, Size, Text, Vector,
Widget,
alignment::{Horizontal, Vertical}, alignment::{Horizontal, Vertical},
event, keyboard, event, gradient, keyboard,
layout::{Limits, Node}, layout::{Limits, Node},
mouse::{self, Cursor}, mouse::{self, Cursor},
overlay, renderer, overlay, renderer,
@@ -60,8 +61,8 @@ where
on_submit: &'a dyn Fn(Color) -> Message, on_submit: &'a dyn Fn(Color) -> Message,
/// Optional function that produces a message when the color changes during selection (real-time updates). /// Optional function that produces a message when the color changes during selection (real-time updates).
on_color_change: Option<&'a dyn Fn(Color) -> Message>, on_color_change: Option<&'a dyn Fn(Color) -> Message>,
/// The position of the [`ColorPickerOverlay`]. /// The bounds of the underlay that opened this overlay.
position: Point, anchor: Rectangle,
/// The style of the [`ColorPickerOverlay`]. /// The style of the [`ColorPickerOverlay`].
class: &'a <Theme as style::color_picker::Catalog>::Class<'b>, class: &'a <Theme as style::color_picker::Catalog>::Class<'b>,
/// The reference to the tree holding the state of this overlay. /// The reference to the tree holding the state of this overlay.
@@ -85,7 +86,7 @@ where
on_cancel: Message, on_cancel: Message,
on_submit: &'a dyn Fn(Color) -> Message, on_submit: &'a dyn Fn(Color) -> Message,
on_color_change: Option<&'a dyn Fn(Color) -> Message>, on_color_change: Option<&'a dyn Fn(Color) -> Message>,
position: Point, anchor: Rectangle,
class: &'a <Theme as style::color_picker::Catalog>::Class<'b>, class: &'a <Theme as style::color_picker::Catalog>::Class<'b>,
tree: &'a mut Tree, tree: &'a mut Tree,
viewport: Rectangle, viewport: Rectangle,
@@ -110,7 +111,7 @@ where
.on_press(on_cancel), // Sending a fake message .on_press(on_cancel), // Sending a fake message
on_submit, on_submit,
on_color_change, on_color_change,
position, anchor,
class, class,
tree, tree,
viewport, viewport,
@@ -425,10 +426,7 @@ where
clipboard: &mut dyn Clipboard, clipboard: &mut dyn Clipboard,
shell: &mut Shell<Message>, shell: &mut Shell<Message>,
) -> event::Status { ) -> event::Status {
let Event::Keyboard(keyboard::Event::KeyPressed { let Event::Keyboard(keyboard::Event::KeyPressed { key, modifiers, .. }) = event else {
key, modifiers, ..
}) = event
else {
return event::Status::Ignored; return event::Status::Ignored;
}; };
@@ -738,28 +736,27 @@ where
// ----------- Block 2 ---------------------- // ----------- Block 2 ----------------------
let block2_node = block2_layout(self, renderer, block2_bounds); let block2_node = block2_layout(self, renderer, block2_bounds);
let (width, height) = if bounds.width > bounds.height { // Iced clips an overlay to its layout bounds. Build those bounds from
( // every child instead of approximating them, so no part of the popup
block1_node.size().width + block2_node.size().width + SPACING.0, // + (2.0 * PADDING as f32), // can be drawn outside the clip layer.
block2_node.size().height, let block1_extent = node_extent(&block1_node);
) let block2_extent = node_extent(&block2_node);
} else { let min_x = block1_extent.x.min(block2_extent.x).min(0.0);
( let min_y = block1_extent.y.min(block2_extent.y).min(0.0);
block2_node.size().width, let max_x = (block1_extent.x + block1_extent.width)
block1_node.size().height + block2_node.size().height + SPACING.0, .max(block2_extent.x + block2_extent.width)
) .max(0.0);
}; let max_y = (block1_extent.y + block1_extent.height)
.max(block2_extent.y + block2_extent.height)
.max(0.0);
let size = Size::new(max_x - min_x, max_y - min_y);
let offset = Vector::new(-min_x, -min_y);
let block1_node = block1_node.translate(offset);
let block2_node = block2_node.translate(offset);
let mut node = let position = place_popup(self.anchor, size, bounds, self.viewport);
Node::with_children(Size::new(width, height), vec![block1_node, block2_node]); let mut node = Node::with_children(size, vec![block1_node, block2_node]);
node = node.move_to(position);
// Place the picker to the left of the opening button, top aligned.
let x = (self.position.x - width).max(0.0);
let y = self
.position
.y
.clamp(0.0, (bounds.height - height).max(0.0));
node = node.move_to(Point::new(x, y));
node node
} }
@@ -1127,6 +1124,64 @@ where
} }
} }
/// Returns the bounds of a node including every descendant, relative to the
/// node's parent.
fn node_extent(node: &Node) -> Rectangle {
let mut extent = node.bounds();
for child in node.children() {
let child_extent = node_extent(child);
let child_extent = Rectangle {
x: child_extent.x + node.bounds().x,
y: child_extent.y + node.bounds().y,
..child_extent
};
extent = extent.union(&child_extent);
}
extent
}
/// Places the popup around its anchor, preferring the left side and flipping
/// to the right when the left side does not fit. The result is clamped to the
/// visible viewport so the whole popup stays inside the window.
fn place_popup(anchor: Rectangle, size: Size, window: Size, viewport: Rectangle) -> Point {
let window = Rectangle::new(Point::new(0.0, 0.0), window);
let viewport = viewport.intersection(&window).unwrap_or(window);
let available = if size.width <= viewport.width && size.height <= viewport.height {
viewport
} else {
window
};
let max_x = (available.x + available.width - size.width).max(available.x);
let max_y = (available.y + available.height - size.height).max(available.y);
let left = anchor.x - size.width;
let right = anchor.x + anchor.width;
let x = if left >= available.x {
left
} else if right + size.width <= available.x + available.width {
right
} else {
let left_space = anchor.x - available.x;
let right_space = available.x + available.width - (anchor.x + anchor.width);
if right_space > left_space {
right
} else {
left
}
.clamp(available.x, max_x)
};
let y = anchor.y.clamp(available.y, max_y);
Point::new(x, y)
}
/// Defines the layout of the 1. block of the color picker containing the HSV part. /// Defines the layout of the 1. block of the color picker containing the HSV part.
fn block1_layout<'a, Message, Theme>( fn block1_layout<'a, Message, Theme>(
color_picker: &mut ColorPickerOverlay<'_, '_, Message, Theme>, color_picker: &mut ColorPickerOverlay<'_, '_, Message, Theme>,
@@ -1473,103 +1528,113 @@ fn hsv_color<Message, Theme>(
sat_value_style_state = sat_value_style_state.max(StyleState::Hovered); sat_value_style_state = sat_value_style_state.max(StyleState::Hovered);
} }
let geometry = color_picker.state.sat_value_canvas_cache.draw( let sat_value_bounds = sat_value_layout.bounds();
renderer, let hue_color = Color::from(Hsv::from_hsv(hsv_color.hue, 1.0, 1.0));
sat_value_layout.bounds().size(),
|frame| {
let column_count = frame.width() as u16;
let row_count = frame.height() as u16;
for column in 0..column_count { // Draw the saturation/value square with the regular quad renderer instead
for row in 0..row_count { // of a cached canvas. This avoids the canvas mesh path, which is not
let saturation = f32::from(column) / frame.width(); // rendered reliably on some Windows GPU drivers.
let value = f32::from(row) / frame.height(); let horizontal = Background::Gradient(Gradient::Linear(
gradient::Linear::new(Radians::PI / 2.0)
.add_stop(0.0, Color::WHITE)
.add_stop(1.0, hue_color),
));
frame.fill_rectangle( renderer.fill_quad(
Point::new(f32::from(column), f32::from(row)), renderer::Quad {
Size::new(1.0, 1.0), bounds: sat_value_bounds,
Color::from(Hsv::from_hsv(hsv_color.hue, saturation, value)), ..renderer::Quad::default()
);
}
}
let stroke = Stroke {
style: canvas::Style::Solid(
Hsv {
hue: 0,
saturation: 0.0,
value: 1.0 - hsv_color.value,
}
.into(),
),
width: 3.0,
line_cap: LineCap::Round,
..Stroke::default()
};
// Keep the crosshair inside the map so it stays visible at the
// edges (pure black/white, fully saturated colors, etc.).
let inset = 5.0;
let saturation = (hsv_color.saturation * frame.width())
.clamp(inset, (frame.width() - inset).max(inset));
let value = (hsv_color.value * frame.height())
.clamp(inset, (frame.height() - inset).max(inset));
frame.stroke(
&Path::line(
Point::new(saturation, 0.0),
Point::new(saturation, frame.height()),
),
stroke,
);
frame.stroke(
&Path::line(Point::new(0.0, value), Point::new(frame.width(), value)),
stroke,
);
let marker = Path::circle(Point::new(saturation, value), 4.0);
frame.fill(&marker, Color::WHITE);
frame.stroke(
&marker,
Stroke {
style: canvas::Style::Solid(Color::BLACK),
width: 1.5,
line_cap: LineCap::Round,
..Stroke::default()
},
);
let stroke = Stroke {
style: canvas::Style::Solid(
style_sheet
.get(&sat_value_style_state)
.expect("Style Sheet not found.")
.bar_border_color,
),
width: 2.0,
line_cap: LineCap::Round,
..Stroke::default()
};
frame.stroke(
&Path::rectangle(
Point::new(0.0, 0.0),
Size::new(frame.size().width - 0.0, frame.size().height - 0.0),
),
stroke,
);
}, },
horizontal,
); );
let translation = Vector::new(sat_value_layout.bounds().x, sat_value_layout.bounds().y); let vertical = Background::Gradient(Gradient::Linear(
renderer.with_translation(translation, |renderer| { gradient::Linear::new(Radians::PI)
renderer.draw_geometry(geometry); .add_stop(0.0, Color::from_rgba(0.0, 0.0, 0.0, 0.0))
.add_stop(1.0, Color::BLACK),
));
renderer.fill_quad(
renderer::Quad {
bounds: sat_value_bounds,
..renderer::Quad::default()
},
vertical,
);
// Keep the crosshair inside the map so it stays visible at the edges.
let inset = 5.0;
let saturation = (hsv_color.saturation * sat_value_bounds.width)
.clamp(inset, (sat_value_bounds.width - inset).max(inset));
let value = (hsv_color.value * sat_value_bounds.height)
.clamp(inset, (sat_value_bounds.height - inset).max(inset));
let crosshair_color = Color::from(Hsv {
hue: 0,
saturation: 0.0,
value: 1.0 - hsv_color.value,
}); });
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
Point::new(sat_value_bounds.x + saturation - 1.5, sat_value_bounds.y),
Size::new(3.0, sat_value_bounds.height),
),
..renderer::Quad::default()
},
crosshair_color,
);
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
Point::new(sat_value_bounds.x, sat_value_bounds.y + value - 1.5),
Size::new(sat_value_bounds.width, 3.0),
),
..renderer::Quad::default()
},
crosshair_color,
);
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
Point::new(
sat_value_bounds.x + saturation - 4.0,
sat_value_bounds.y + value - 4.0,
),
Size::new(8.0, 8.0),
),
border: Border {
radius: 4.0.into(),
width: 1.5,
color: Color::BLACK,
},
..renderer::Quad::default()
},
Color::WHITE,
);
renderer.fill_quad(
renderer::Quad {
bounds: sat_value_bounds,
border: Border {
radius: 0.0.into(),
width: 2.0,
color: style_sheet
.get(&sat_value_style_state)
.expect("Style Sheet not found.")
.bar_border_color,
},
..renderer::Quad::default()
},
Color::TRANSPARENT,
);
let hue_layout = hsv_color_children let hue_layout = hsv_color_children
.next() .next()
.expect("Graphics: Layout should have a hue layout"); .expect("Graphics: Layout should have a hue layout");
let mut hue_style_state = StyleState::Active; let mut hue_style_state = StyleState::Active;
if color_picker.state.focus == Focus::Hue { if color_picker.state.focus == Focus::Hue {
hue_style_state = hue_style_state.max(StyleState::Focused); hue_style_state = hue_style_state.max(StyleState::Focused);
@@ -1884,7 +1949,11 @@ fn draw_action_button(
hovered: Color, hovered: Color,
cursor: Cursor, cursor: Cursor,
) { ) {
let color = if cursor.is_over(bounds) { hovered } else { base }; let color = if cursor.is_over(bounds) {
hovered
} else {
base
};
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
@@ -1959,9 +2028,7 @@ fn parse_hex_color(input: &str) -> Option<Color> {
Some(Color::from_rgba8(r, g, b, f32::from(a) / 255.0)) Some(Color::from_rgba8(r, g, b, f32::from(a) / 255.0))
} }
6 | 8 => { 6 | 8 => {
let byte = |range: std::ops::Range<usize>| { let byte = |range: std::ops::Range<usize>| u8::from_str_radix(&hex[range], 16).ok();
u8::from_str_radix(&hex[range], 16).ok()
};
let r = byte(0..2)?; let r = byte(0..2)?;
let g = byte(2..4)?; let g = byte(2..4)?;
@@ -2096,8 +2163,6 @@ pub struct State {
pub(crate) color: Color, pub(crate) color: Color,
/// The color used to initialize [`ColorPickerOverlay`]. /// The color used to initialize [`ColorPickerOverlay`].
pub(crate) initial_color: Color, pub(crate) initial_color: Color,
/// The cache of the sat/value canvas of the [`ColorPickerOverlay`].
pub(crate) sat_value_canvas_cache: canvas::Cache,
/// The cache of the hue canvas of the [`ColorPickerOverlay`]. /// The cache of the hue canvas of the [`ColorPickerOverlay`].
pub(crate) hue_canvas_cache: canvas::Cache, pub(crate) hue_canvas_cache: canvas::Cache,
/// The dragged color bar of the [`ColorPickerOverlay`]. /// The dragged color bar of the [`ColorPickerOverlay`].
@@ -2131,10 +2196,9 @@ impl State {
/// Reset cached canvas when internal state is modified. /// Reset cached canvas when internal state is modified.
/// ///
/// If the color has changed, empty all canvas caches /// If the color has changed, empty the hue canvas cache
/// as they (unfortunately) do not depend on the picker state. /// as it (unfortunately) does not depend on the picker state.
fn clear_cache(&self) { fn clear_cache(&self) {
self.sat_value_canvas_cache.clear();
self.hue_canvas_cache.clear(); self.hue_canvas_cache.clear();
} }
@@ -2227,7 +2291,6 @@ impl Default for State {
Self { Self {
color: default_color, color: default_color,
initial_color: default_color, initial_color: default_color,
sat_value_canvas_cache: canvas::Cache::default(),
hue_canvas_cache: canvas::Cache::default(), hue_canvas_cache: canvas::Cache::default(),
color_bar_dragged: ColorBarDragged::None, color_bar_dragged: ColorBarDragged::None,
focus: Focus::default(), focus: Focus::default(),
@@ -2424,3 +2487,79 @@ impl Focus {
} }
} }
#[cfg(test)]
mod tests {
use super::*;
fn anchor(x: f32, y: f32, width: f32, height: f32) -> Rectangle {
Rectangle::new(Point::new(x, y), Size::new(width, height))
}
#[test]
fn popup_prefers_the_left_side_when_it_fits() {
let position = place_popup(
anchor(700.0, 100.0, 30.0, 24.0),
Size::new(300.0, 200.0),
Size::new(1000.0, 800.0),
Rectangle::new(Point::new(0.0, 0.0), Size::new(1000.0, 800.0)),
);
assert_eq!(position, Point::new(400.0, 100.0));
}
#[test]
fn popup_flips_to_the_right_when_the_left_side_is_too_narrow() {
let position = place_popup(
anchor(100.0, 100.0, 30.0, 24.0),
Size::new(300.0, 200.0),
Size::new(1000.0, 800.0),
Rectangle::new(Point::new(0.0, 0.0), Size::new(1000.0, 800.0)),
);
assert_eq!(position, Point::new(130.0, 100.0));
}
#[test]
fn popup_stays_inside_a_shifted_viewport() {
let viewport = Rectangle::new(Point::new(100.0, 100.0), Size::new(500.0, 400.0));
let left = place_popup(
anchor(550.0, 200.0, 30.0, 24.0),
Size::new(300.0, 200.0),
Size::new(1000.0, 800.0),
viewport,
);
assert_eq!(left, Point::new(250.0, 200.0));
let right = place_popup(
anchor(150.0, 200.0, 30.0, 24.0),
Size::new(300.0, 200.0),
Size::new(1000.0, 800.0),
viewport,
);
assert_eq!(right, Point::new(180.0, 200.0));
}
#[test]
fn popup_is_clamped_when_neither_side_fits() {
let position = place_popup(
anchor(400.0, 700.0, 30.0, 24.0),
Size::new(900.0, 300.0),
Size::new(1000.0, 800.0),
Rectangle::new(Point::new(0.0, 0.0), Size::new(1000.0, 800.0)),
);
assert_eq!(position, Point::new(100.0, 500.0));
}
#[test]
fn node_extent_includes_children_outside_the_node() {
let child = Node::new(Size::new(50.0, 20.0)).move_to(Point::new(-10.0, 5.0));
let node = Node::with_children(Size::new(40.0, 30.0), vec![child]);
assert_eq!(
node_extent(&node),
Rectangle::new(Point::new(-10.0, 0.0), Size::new(50.0, 30.0))
);
}
}