mirror of
https://github.com/RGBCube/serenity
synced 2025-06-30 03:12:12 +00:00
LibGUI: Add AllowCallback parameter to ColorInput::set_color()
The `TextEditor::on_change` callback now only fires if the user types in the box, or `set_text()` is called with `AllowCallback::Yes`. Previously that callback was what set `m_color`, so I've rearranged things a little so that the color still updates regardless of what source the color came from.
This commit is contained in:
parent
91230ff28d
commit
5fd0140772
2 changed files with 9 additions and 9 deletions
|
@ -23,7 +23,7 @@ ColorInput::ColorInput()
|
||||||
TextEditor::on_change = [this] {
|
TextEditor::on_change = [this] {
|
||||||
auto parsed_color = Color::from_string(text());
|
auto parsed_color = Color::from_string(text());
|
||||||
if (parsed_color.has_value())
|
if (parsed_color.has_value())
|
||||||
set_color_without_changing_text(parsed_color.value());
|
set_color_internal(parsed_color.value(), AllowCallback::Yes, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_STRING_PROPERTY("color_picker_title", color_picker_title, set_color_picker_title);
|
REGISTER_STRING_PROPERTY("color_picker_title", color_picker_title, set_color_picker_title);
|
||||||
|
@ -37,21 +37,21 @@ Gfx::IntRect ColorInput::color_rect() const
|
||||||
return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size };
|
return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size };
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorInput::set_color_without_changing_text(Color color)
|
void ColorInput::set_color_internal(Color color, AllowCallback allow_callback, bool change_text)
|
||||||
{
|
{
|
||||||
if (m_color == color)
|
if (m_color == color)
|
||||||
return;
|
return;
|
||||||
m_color = color;
|
m_color = color;
|
||||||
|
if (change_text)
|
||||||
|
set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha(), AllowCallback::No);
|
||||||
update();
|
update();
|
||||||
if (on_change)
|
if (allow_callback == AllowCallback::Yes && on_change)
|
||||||
on_change();
|
on_change();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorInput::set_color(Color color)
|
void ColorInput::set_color(Color color, AllowCallback allow_callback)
|
||||||
{
|
{
|
||||||
if (m_color == color)
|
set_color_internal(color, allow_callback, true);
|
||||||
return;
|
|
||||||
set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha());
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void ColorInput::mousedown_event(MouseEvent& event)
|
void ColorInput::mousedown_event(MouseEvent& event)
|
||||||
|
|
|
@ -21,7 +21,7 @@ public:
|
||||||
bool has_alpha_channel() const { return m_color_has_alpha_channel; }
|
bool has_alpha_channel() const { return m_color_has_alpha_channel; }
|
||||||
void set_color_has_alpha_channel(bool has_alpha) { m_color_has_alpha_channel = has_alpha; }
|
void set_color_has_alpha_channel(bool has_alpha) { m_color_has_alpha_channel = has_alpha; }
|
||||||
|
|
||||||
void set_color(Color);
|
void set_color(Color, AllowCallback = AllowCallback::Yes);
|
||||||
Color color() { return m_color; }
|
Color color() { return m_color; }
|
||||||
|
|
||||||
void set_color_picker_title(String title) { m_color_picker_title = move(title); }
|
void set_color_picker_title(String title) { m_color_picker_title = move(title); }
|
||||||
|
@ -39,7 +39,7 @@ private:
|
||||||
ColorInput();
|
ColorInput();
|
||||||
|
|
||||||
Gfx::IntRect color_rect() const;
|
Gfx::IntRect color_rect() const;
|
||||||
void set_color_without_changing_text(Color);
|
void set_color_internal(Color, AllowCallback, bool change_text);
|
||||||
|
|
||||||
Color m_color;
|
Color m_color;
|
||||||
String m_color_picker_title { "Select color" };
|
String m_color_picker_title { "Select color" };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue