mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 02:58:12 +00:00
LibGUI: Allow editing ColorInput widgets as text
You can now enter a specific color as #rrggbb instead of clicking your way through the color picker. If you still want the color picker, just click the little color rect in the widget and we'll bring up a ColorPicker. For a visual cue that this rect is interactive, we use a hover hand cursor when hovering the rect.
This commit is contained in:
parent
033a4aee50
commit
bc305a16ae
2 changed files with 46 additions and 53 deletions
|
@ -25,9 +25,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCore/Timer.h>
|
#include <LibCore/Timer.h>
|
||||||
|
#include <LibGUI/ColorInput.h>
|
||||||
#include <LibGUI/ColorPicker.h>
|
#include <LibGUI/ColorPicker.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGUI/ColorInput.h>
|
|
||||||
#include <LibGfx/Palette.h>
|
#include <LibGfx/Palette.h>
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
@ -35,83 +35,78 @@ namespace GUI {
|
||||||
ColorInput::ColorInput()
|
ColorInput::ColorInput()
|
||||||
: TextEditor(TextEditor::SingleLine)
|
: TextEditor(TextEditor::SingleLine)
|
||||||
{
|
{
|
||||||
set_readonly(true);
|
TextEditor::on_change = [this] {
|
||||||
|
auto parsed_color = Color::from_string(text());
|
||||||
|
if (parsed_color.has_value())
|
||||||
|
set_color_without_changing_text(parsed_color.value());
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ColorInput::~ColorInput()
|
ColorInput::~ColorInput()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorInput::set_color(Color color)
|
Gfx::Rect ColorInput::color_rect() const
|
||||||
{
|
{
|
||||||
|
auto color_box_padding = 3;
|
||||||
|
auto color_box_size = height() - color_box_padding - color_box_padding;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (m_color == color)
|
||||||
|
return;
|
||||||
m_color = color;
|
m_color = color;
|
||||||
set_text(color.to_string());
|
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
|
||||||
if (on_change)
|
if (on_change)
|
||||||
on_change();
|
on_change();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColorInput::set_color(Color color)
|
||||||
|
{
|
||||||
|
if (m_color == color)
|
||||||
|
return;
|
||||||
|
set_text(color.to_string());
|
||||||
};
|
};
|
||||||
|
|
||||||
void ColorInput::mousedown_event(MouseEvent& event)
|
void ColorInput::mousedown_event(MouseEvent& event)
|
||||||
{
|
{
|
||||||
if (event.button() == MouseButton::Left) {
|
if (event.button() == MouseButton::Left) {
|
||||||
if (is_enabled()) {
|
if (is_enabled() && color_rect().contains(event.position())) {
|
||||||
m_being_pressed = true;
|
auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
|
||||||
update();
|
if (dialog->exec() == GUI::Dialog::ExecOK)
|
||||||
|
set_color(dialog->color());
|
||||||
|
event.accept();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget::mousedown_event(event);
|
TextEditor::mousedown_event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorInput::mouseup_event(MouseEvent& event)
|
void ColorInput::mousemove_event(MouseEvent& event)
|
||||||
{
|
{
|
||||||
if (event.button() == MouseButton::Left) {
|
if (color_rect().contains(event.position())) {
|
||||||
if (is_enabled()) {
|
window()->set_override_cursor(StandardCursor::Hand);
|
||||||
bool was_being_pressed = m_being_pressed;
|
event.accept();
|
||||||
m_being_pressed = false;
|
return;
|
||||||
update();
|
} else {
|
||||||
if (was_being_pressed)
|
window()->set_override_cursor(StandardCursor::IBeam);
|
||||||
click();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Widget::mouseup_event(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ColorInput::enter_event(Core::Event&)
|
TextEditor::mousemove_event(event);
|
||||||
{
|
|
||||||
ASSERT(window());
|
|
||||||
window()->set_override_cursor(StandardCursor::Arrow);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorInput::paint_event(PaintEvent& event)
|
void ColorInput::paint_event(PaintEvent& event)
|
||||||
{
|
{
|
||||||
// Set cursor color to base color and stop timer. FIXME: Find better way to hide cursor.
|
|
||||||
auto pal = palette();
|
|
||||||
pal.set_color(ColorRole::TextCursor, palette().base());
|
|
||||||
set_palette(pal);
|
|
||||||
stop_timer();
|
|
||||||
|
|
||||||
TextEditor::paint_event(event);
|
TextEditor::paint_event(event);
|
||||||
|
|
||||||
auto color_box_padding = 3;
|
|
||||||
auto color_box_size = event.rect().height() - color_box_padding - color_box_padding;
|
|
||||||
|
|
||||||
Painter painter(*this);
|
Painter painter(*this);
|
||||||
painter.fill_rect({ event.rect().width() - color_box_size - color_box_padding , color_box_padding, color_box_size, color_box_size}, m_color);
|
painter.add_clip_rect(event.rect());
|
||||||
}
|
|
||||||
|
|
||||||
void ColorInput::click()
|
painter.fill_rect(color_rect(), m_color);
|
||||||
{
|
painter.draw_rect(color_rect(), Color::Black);
|
||||||
if (!is_enabled())
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
|
|
||||||
if (dialog->exec() == GUI::Dialog::ExecOK) {
|
|
||||||
auto tmp = dialog->color();
|
|
||||||
set_color(tmp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,24 +41,22 @@ public:
|
||||||
void set_color(Color);
|
void set_color(Color);
|
||||||
Color color() { return m_color; }
|
Color color() { return m_color; }
|
||||||
|
|
||||||
void set_color_picker_title(String title) { m_color_picker_title = title; }
|
void set_color_picker_title(String title) { m_color_picker_title = move(title); }
|
||||||
String color_picker_title() { return m_color_picker_title; }
|
String color_picker_title() { return m_color_picker_title; }
|
||||||
|
|
||||||
Function<void()> on_change;
|
Function<void()> on_change;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void mousedown_event(MouseEvent&) override;
|
virtual void mousedown_event(MouseEvent&) override;
|
||||||
virtual void mouseup_event(MouseEvent&) override;
|
virtual void mousemove_event(MouseEvent&) override;
|
||||||
virtual void enter_event(Core::Event&) override;
|
|
||||||
virtual void paint_event(PaintEvent&) override;
|
virtual void paint_event(PaintEvent&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void click();
|
Gfx::Rect color_rect() const;
|
||||||
|
void set_color_without_changing_text(Color);
|
||||||
|
|
||||||
Color m_color;
|
Color m_color;
|
||||||
String m_color_picker_title { "Select Color" };
|
String m_color_picker_title { "Select color" };
|
||||||
|
|
||||||
bool m_being_pressed { false };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue