Skip to content

Commit

Permalink
Fix ColorPickerButton close popup on mouse click
Browse files Browse the repository at this point in the history
Fixes #91813
  • Loading branch information
pafuent committed Oct 22, 2024
1 parent b3bcb2d commit 589b679
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,14 @@ void ColorPickerButton::_modal_closed() {
void ColorPickerButton::pressed() {
_update_picker();

// Checking if the popup was open before, so we can keep it closed instead of reopening it.
// Popups get closed when it's clicked outside of them.
if (popup_was_open) {
// Reset popup_was_open value.
popup_was_open = popup->is_visible();
return;
}

Size2 minsize = popup->get_contents_minimum_size();
float viewport_height = get_viewport_rect().size.y;

Expand All @@ -2094,6 +2102,19 @@ void ColorPickerButton::pressed() {
}
}

void ColorPickerButton::gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());

Ref<InputEventMouseButton> mouse_button = p_event;
bool ui_accept = p_event->is_action("ui_accept", true) && !p_event->is_echo();
bool mouse_left_pressed = mouse_button.is_valid() && mouse_button->get_button_index() == MouseButton::LEFT && mouse_button->is_pressed();
if (mouse_left_pressed || ui_accept) {
popup_was_open = popup && popup->is_visible();
}

BaseButton::gui_input(p_event);
}

void ColorPickerButton::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_DRAW: {
Expand Down
2 changes: 2 additions & 0 deletions scene/gui/color_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class ColorPickerButton : public Button {
ColorPicker *picker = nullptr;
Color color;
bool edit_alpha = true;
bool popup_was_open = false;

struct ThemeCache {
Ref<StyleBox> normal_style;
Expand All @@ -408,6 +409,7 @@ class ColorPickerButton : public Button {
protected:
void _notification(int);
static void _bind_methods();
virtual void gui_input(const Ref<InputEvent> &p_event) override;

public:
void set_pick_color(const Color &p_color);
Expand Down

0 comments on commit 589b679

Please sign in to comment.