LibWeb: Add basic support for <input type=checkbox>

This is implemented entirely inside LibWeb, there is no GUI::CheckBox
widget instantiated, unlike other input types. All input types should
be moved to this new style of implementation.
This commit is contained in:
Andreas Kling 2020-09-11 18:17:39 +02:00
parent d6889ecf35
commit f2431adf47
Notes: sideshowbarker 2024-07-19 02:45:51 +09:00
7 changed files with 195 additions and 1 deletions

View File

@ -133,6 +133,7 @@ set(SOURCES
Layout/LayoutBox.cpp
Layout/LayoutBreak.cpp
Layout/LayoutCanvas.cpp
Layout/LayoutCheckBox.cpp
Layout/LayoutDocument.cpp
Layout/LayoutFrame.cpp
Layout/LayoutImage.cpp

View File

@ -30,9 +30,10 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/LayoutCheckBox.h>
#include <LibWeb/Layout/LayoutWidget.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/InProcessWebView.h>
namespace Web::HTML {
@ -78,6 +79,8 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const CSS::StyleProperti
const_cast<HTMLInputElement*>(this)->dispatch_event(DOM::Event::create("click"));
};
widget = button;
} else if (type() == "checkbox") {
return adopt(*new LayoutCheckBox(document(), *this, move(style)));
} else {
auto& text_box = page_view.add<GUI::TextBox>();
text_box.set_text(value());
@ -99,4 +102,18 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const CSS::StyleProperti
return adopt(*new LayoutWidget(document(), *this, *widget));
}
void HTMLInputElement::set_checked(bool checked)
{
if (m_checked == checked)
return;
m_checked = checked;
if (layout_node())
layout_node()->set_needs_display();
}
bool HTMLInputElement::enabled() const
{
return !has_attribute(HTML::AttributeNames::disabled);
}
}

View File

@ -42,6 +42,14 @@ public:
String type() const { return attribute(HTML::AttributeNames::type); }
String value() const { return attribute(HTML::AttributeNames::value); }
String name() const { return attribute(HTML::AttributeNames::name); }
bool checked() const { return m_checked; }
void set_checked(bool);
bool enabled() const;
private:
bool m_checked { false };
};
}

View File

@ -11,4 +11,5 @@ interface HTMLInputElement : HTMLElement {
[Reflect=dirname] attribute DOMString dirName;
[Reflect=value] attribute DOMString defaultValue;
attribute boolean checked;
}

View File

@ -0,0 +1,105 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGUI/Event.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Font.h>
#include <LibGfx/StylePainter.h>
#include <LibWeb/Layout/LayoutCheckBox.h>
#include <LibWeb/Page/Frame.h>
namespace Web {
LayoutCheckBox::LayoutCheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
: LayoutReplaced(document, element, move(style))
{
}
LayoutCheckBox::~LayoutCheckBox()
{
}
void LayoutCheckBox::layout(LayoutMode layout_mode)
{
set_has_intrinsic_width(true);
set_has_intrinsic_height(true);
set_intrinsic_width(13);
set_intrinsic_height(13);
LayoutReplaced::layout(layout_mode);
}
void LayoutCheckBox::paint(PaintContext& context, PaintPhase phase)
{
if (!is_visible())
return;
LayoutReplaced::paint(context, phase);
if (phase == PaintPhase::Foreground) {
Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), node().enabled(), node().checked(), m_being_pressed);
}
}
void LayoutCheckBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
{
if (button != GUI::MouseButton::Left)
return;
m_being_pressed = true;
set_needs_display();
m_tracking_mouse = true;
frame().event_handler().set_mouse_event_tracking_layout_node(this);
}
void LayoutCheckBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
{
if (!m_tracking_mouse || button != GUI::MouseButton::Left)
return;
bool is_inside = enclosing_int_rect(absolute_rect()).contains(position);
if (is_inside)
node().set_checked(!node().checked());
m_being_pressed = false;
m_tracking_mouse = false;
frame().event_handler().set_mouse_event_tracking_layout_node(nullptr);
}
void LayoutCheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
{
if (!m_tracking_mouse)
return;
bool is_inside = enclosing_int_rect(absolute_rect()).contains(position);
if (m_being_pressed == is_inside)
return;
m_being_pressed = is_inside;
set_needs_display();
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/Layout/LayoutReplaced.h>
namespace Web {
class LayoutCheckBox : public LayoutReplaced {
public:
LayoutCheckBox(DOM::Document&, HTML::HTMLInputElement&, NonnullRefPtr<CSS::StyleProperties>);
virtual ~LayoutCheckBox() override;
virtual void layout(LayoutMode = LayoutMode::Default) override;
virtual void paint(PaintContext&, PaintPhase) override;
const HTML::HTMLInputElement& node() const { return static_cast<const HTML::HTMLInputElement&>(LayoutReplaced::node()); }
HTML::HTMLInputElement& node() { return static_cast<HTML::HTMLInputElement&>(LayoutReplaced::node()); }
private:
virtual const char* class_name() const override { return "LayoutCheckBox"; }
virtual bool is_check_box() const override { return true; }
virtual bool wants_mouse_events() const override { return true; }
virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers) override;
bool m_being_pressed { false };
bool m_tracking_mouse { false };
};
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutCheckBox)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_check_box(); }
AK_END_TYPE_TRAITS()

View File

@ -92,6 +92,7 @@ public:
virtual bool is_table_cell() const { return false; }
virtual bool is_table_row_group() const { return false; }
virtual bool is_break() const { return false; }
virtual bool is_check_box() const { return false; }
bool has_style() const { return m_has_style; }
bool is_inline() const { return m_inline; }