diff options
author | Rafał Babiarz <5783815+Sauler@users.noreply.github.com> | 2022-02-16 19:12:54 +0100 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-02-16 15:34:08 -0500 |
commit | 21e353980f6f79912210620185749b37ad909346 (patch) | |
tree | 26b83af1a7b099303f3f473c0a60b342cdba912c /Userland/Libraries | |
parent | d8388f30c85d601334df70a70bde14e7b46c0074 (diff) | |
download | serenity-21e353980f6f79912210620185749b37ad909346.zip |
LibWeb: Add basic implementation of progress bar element
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLProgressElement.cpp | 57 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLProgressElement.h | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLProgressElement.idl | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Layout/Progress.cpp | 34 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Layout/Progress.h | 25 |
6 files changed, 133 insertions, 3 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index f965b0504d..24ee3aee65 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -235,6 +235,7 @@ set(SOURCES Layout/ListItemBox.cpp Layout/ListItemMarkerBox.cpp Layout/Node.cpp + Layout/Progress.cpp Layout/RadioButton.cpp Layout/ReplacedBox.cpp Layout/SVGBox.cpp diff --git a/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.cpp index 42ffa73628..6162812d8c 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.cpp @@ -1,10 +1,12 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include <LibWeb/HTML/HTMLProgressElement.h> +#include <LibWeb/Layout/Progress.h> +#include <stdlib.h> namespace Web::HTML { @@ -17,4 +19,57 @@ HTMLProgressElement::~HTMLProgressElement() { } +RefPtr<Layout::Node> HTMLProgressElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style) +{ + return adopt_ref(*new Layout::Progress(document(), *this, move(style))); +} + +double HTMLProgressElement::value() const +{ + auto parsed_value = strtod(attribute(HTML::AttributeNames::value).characters(), nullptr); + if (!isfinite(parsed_value) || parsed_value < 0) + return 0; + + return min(parsed_value, max()); +} + +void HTMLProgressElement::set_value(double value) +{ + if (value < 0) + return; + + set_attribute(HTML::AttributeNames::value, String::number(value)); + + if (layout_node()) + layout_node()->set_needs_display(); +} + +double HTMLProgressElement::max() const +{ + auto parsed_value = strtod(attribute(HTML::AttributeNames::max).characters(), nullptr); + if (!isfinite(parsed_value) || parsed_value <= 0) + return 1; + + return parsed_value; +} + +void HTMLProgressElement::set_max(double value) +{ + if (value <= 0) + return; + + set_attribute(HTML::AttributeNames::max, String::number(value)); + + if (layout_node()) + layout_node()->set_needs_display(); +} + +double HTMLProgressElement::position() const +{ + if (!is_determinate()) + return -1; + + return value() / max(); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.h b/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.h index dafc869192..a6a3706467 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,6 +16,19 @@ public: HTMLProgressElement(DOM::Document&, QualifiedName); virtual ~HTMLProgressElement() override; + + virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override; + + double value() const; + void set_value(double); + + double max() const; + void set_max(double value); + + double position() const; + +private: + bool is_determinate() const { return has_attribute(HTML::AttributeNames::value); } }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.idl index cabd26a136..4064f278c9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLProgressElement.idl @@ -2,6 +2,8 @@ interface HTMLProgressElement : HTMLElement { - + [CEReactions] attribute double value; + [CEReactions] attribute double max; + readonly attribute double position; }; diff --git a/Userland/Libraries/LibWeb/Layout/Progress.cpp b/Userland/Libraries/LibWeb/Layout/Progress.cpp new file mode 100644 index 0000000000..fffa16a517 --- /dev/null +++ b/Userland/Libraries/LibWeb/Layout/Progress.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibGfx/Painter.h> +#include <LibGfx/StylePainter.h> +#include <LibWeb/Layout/Progress.h> + +namespace Web::Layout { + +Progress::Progress(DOM::Document& document, HTML::HTMLProgressElement& element, NonnullRefPtr<CSS::StyleProperties> style) + : LabelableNode(document, element, move(style)) +{ + set_intrinsic_height(12); +} + +Progress::~Progress() +{ +} + +void Progress::paint(PaintContext& context, PaintPhase phase) +{ + if (!is_visible()) + return; + + if (phase == PaintPhase::Foreground) { + // FIXME: This does not support floating point value() and max() + Gfx::StylePainter::paint_progressbar(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), 0, dom_node().max(), dom_node().value(), ""); + } +} + +} diff --git a/Userland/Libraries/LibWeb/Layout/Progress.h b/Userland/Libraries/LibWeb/Layout/Progress.h new file mode 100644 index 0000000000..a7dc36495e --- /dev/null +++ b/Userland/Libraries/LibWeb/Layout/Progress.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibWeb/HTML/HTMLProgressElement.h> +#include <LibWeb/Layout/LabelableNode.h> + +namespace Web::Layout { + +class Progress : public LabelableNode { +public: + Progress(DOM::Document&, HTML::HTMLProgressElement&, NonnullRefPtr<CSS::StyleProperties>); + virtual ~Progress() override; + + virtual void paint(PaintContext&, PaintPhase) override; + + const HTML::HTMLProgressElement& dom_node() const { return static_cast<const HTML::HTMLProgressElement&>(LabelableNode::dom_node()); } + HTML::HTMLProgressElement& dom_node() { return static_cast<HTML::HTMLProgressElement&>(LabelableNode::dom_node()); } +}; + +} |