Skip to content

Commit c8b844c

Browse files
authored
Use the new Popup api for the color picker button (#7137)
The color picker popup is now aligned to the bottom left edge (instead of the bottom right), but I think this makes more sense and is aligned with ComboBox etc. It also gets the new nice auto positioning. * closes #5832 * [x] I have followed the instructions in the PR template
1 parent 0152a87 commit c8b844c

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

crates/egui/src/widgets/color_picker.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::util::fixed_cache::FixedCache;
44
use crate::{
5-
epaint, lerp, remap_clamp, Area, Context, DragValue, Frame, Id, Key, Order, Painter, Response,
6-
Sense, Ui, UiKind, Widget as _, WidgetInfo, WidgetType,
5+
epaint, lerp, remap_clamp, Context, DragValue, Id, Painter, Popup, PopupCloseBehavior,
6+
Response, Sense, Ui, Widget as _, WidgetInfo, WidgetType,
77
};
88
use epaint::{
99
ecolor::{Color32, Hsva, HsvaGamma, Rgba},
@@ -496,35 +496,17 @@ pub fn color_edit_button_hsva(ui: &mut Ui, hsva: &mut Hsva, alpha: Alpha) -> Res
496496
button_response = button_response.on_hover_text("Click to edit color");
497497
}
498498

499-
if button_response.clicked() {
500-
ui.memory_mut(|mem| mem.toggle_popup(popup_id));
501-
}
502-
503499
const COLOR_SLIDER_WIDTH: f32 = 275.0;
504500

505-
// TODO(lucasmerlin): Update this to use new Popup struct
506-
if ui.memory(|mem| mem.is_popup_open(popup_id)) {
507-
ui.memory_mut(|mem| mem.keep_popup_open(popup_id));
508-
let area_response = Area::new(popup_id)
509-
.kind(UiKind::Picker)
510-
.order(Order::Foreground)
511-
.fixed_pos(button_response.rect.max)
512-
.show(ui.ctx(), |ui| {
513-
ui.spacing_mut().slider_width = COLOR_SLIDER_WIDTH;
514-
Frame::popup(ui.style()).show(ui, |ui| {
515-
if color_picker_hsva_2d(ui, hsva, alpha) {
516-
button_response.mark_changed();
517-
}
518-
});
519-
})
520-
.response;
521-
522-
if !button_response.clicked()
523-
&& (ui.input(|i| i.key_pressed(Key::Escape)) || area_response.clicked_elsewhere())
524-
{
525-
ui.memory_mut(|mem| mem.close_popup(popup_id));
526-
}
527-
}
501+
Popup::menu(&button_response)
502+
.id(popup_id)
503+
.close_behavior(PopupCloseBehavior::CloseOnClickOutside)
504+
.show(|ui| {
505+
ui.spacing_mut().slider_width = COLOR_SLIDER_WIDTH;
506+
if color_picker_hsva_2d(ui, hsva, alpha) {
507+
button_response.mark_changed();
508+
}
509+
});
528510

529511
button_response
530512
}

0 commit comments

Comments
 (0)