Serenity Operating System
at master 107 lines 3.3 kB view raw
1/* 2 * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibCore/Timer.h> 9#include <LibGUI/ColorInput.h> 10#include <LibGUI/ColorPicker.h> 11#include <LibGUI/Painter.h> 12#include <LibGfx/Palette.h> 13 14REGISTER_WIDGET(GUI, ColorInput) 15 16namespace GUI { 17 18ColorInput::ColorInput() 19 : TextEditor(TextEditor::SingleLine) 20{ 21 set_min_size({ 40, 22 }); 22 set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); 23 TextEditor::on_change = [this] { 24 auto parsed_color = Color::from_string(text()); 25 if (parsed_color.has_value()) 26 set_color_internal(parsed_color.value(), AllowCallback::Yes, false); 27 }; 28 29 REGISTER_STRING_PROPERTY("color_picker_title", color_picker_title, set_color_picker_title); 30 REGISTER_BOOL_PROPERTY("has_alpha_channel", has_alpha_channel, set_color_has_alpha_channel); 31} 32 33Gfx::IntRect ColorInput::color_rect() const 34{ 35 auto color_box_padding = 3; 36 auto color_box_size = height() - color_box_padding - color_box_padding; 37 return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size }; 38} 39 40void ColorInput::set_color_internal(Color color, AllowCallback allow_callback, bool change_text) 41{ 42 if (m_color == color) 43 return; 44 m_color = color; 45 if (change_text) 46 set_text(m_color_has_alpha_channel ? color.to_deprecated_string() : color.to_deprecated_string_without_alpha(), AllowCallback::No); 47 update(); 48 if (allow_callback == AllowCallback::Yes && on_change) 49 on_change(); 50} 51 52void ColorInput::set_color(Color color, AllowCallback allow_callback) 53{ 54 set_color_internal(color, allow_callback, true); 55}; 56 57void ColorInput::mousedown_event(MouseEvent& event) 58{ 59 if (event.button() == MouseButton::Primary && color_rect().contains(event.position())) { 60 m_may_be_color_rect_click = true; 61 return; 62 } 63 64 TextEditor::mousedown_event(event); 65} 66 67void ColorInput::mouseup_event(MouseEvent& event) 68{ 69 if (event.button() == MouseButton::Primary) { 70 bool is_color_rect_click = m_may_be_color_rect_click && color_rect().contains(event.position()); 71 m_may_be_color_rect_click = false; 72 if (is_color_rect_click) { 73 auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title); 74 dialog->set_color_has_alpha_channel(m_color_has_alpha_channel); 75 if (dialog->exec() == GUI::Dialog::ExecResult::OK) 76 set_color(dialog->color()); 77 event.accept(); 78 return; 79 } 80 } 81 TextEditor::mouseup_event(event); 82} 83 84void ColorInput::mousemove_event(MouseEvent& event) 85{ 86 if (color_rect().contains(event.position())) { 87 set_override_cursor(Gfx::StandardCursor::Hand); 88 event.accept(); 89 return; 90 } else { 91 set_override_cursor(Gfx::StandardCursor::IBeam); 92 } 93 94 TextEditor::mousemove_event(event); 95} 96 97void ColorInput::paint_event(PaintEvent& event) 98{ 99 TextEditor::paint_event(event); 100 101 Painter painter(*this); 102 painter.add_clip_rect(event.rect()); 103 104 painter.fill_rect(color_rect(), m_color); 105 painter.draw_rect(color_rect(), Color::Black); 106} 107}