mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 13:55:06 +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 <LibGUI/ColorInput.h>
|
||||
#include <LibGUI/ColorPicker.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/ColorInput.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
|
||||
namespace GUI {
|
||||
|
@ -35,83 +35,78 @@ namespace GUI {
|
|||
ColorInput::ColorInput()
|
||||
: 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()
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
set_text(color.to_string());
|
||||
|
||||
update();
|
||||
|
||||
if (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)
|
||||
{
|
||||
if (event.button() == MouseButton::Left) {
|
||||
if (is_enabled()) {
|
||||
m_being_pressed = true;
|
||||
update();
|
||||
if (is_enabled() && color_rect().contains(event.position())) {
|
||||
auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
|
||||
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 (is_enabled()) {
|
||||
bool was_being_pressed = m_being_pressed;
|
||||
m_being_pressed = false;
|
||||
update();
|
||||
if (was_being_pressed)
|
||||
click();
|
||||
}
|
||||
if (color_rect().contains(event.position())) {
|
||||
window()->set_override_cursor(StandardCursor::Hand);
|
||||
event.accept();
|
||||
return;
|
||||
} else {
|
||||
window()->set_override_cursor(StandardCursor::IBeam);
|
||||
}
|
||||
Widget::mouseup_event(event);
|
||||
}
|
||||
|
||||
void ColorInput::enter_event(Core::Event&)
|
||||
{
|
||||
ASSERT(window());
|
||||
window()->set_override_cursor(StandardCursor::Arrow);
|
||||
TextEditor::mousemove_event(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);
|
||||
|
||||
auto color_box_padding = 3;
|
||||
auto color_box_size = event.rect().height() - color_box_padding - color_box_padding;
|
||||
|
||||
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()
|
||||
{
|
||||
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);
|
||||
}
|
||||
painter.fill_rect(color_rect(), m_color);
|
||||
painter.draw_rect(color_rect(), Color::Black);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue