From 21e353980f6f79912210620185749b37ad909346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Babiarz?= <5783815+Sauler@users.noreply.github.com> Date: Wed, 16 Feb 2022 19:12:54 +0100 Subject: LibWeb: Add basic implementation of progress bar element --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Libraries/LibWeb/HTML/HTMLProgressElement.cpp | 57 +++++++++++++++++++++- .../Libraries/LibWeb/HTML/HTMLProgressElement.h | 15 +++++- .../Libraries/LibWeb/HTML/HTMLProgressElement.idl | 4 +- Userland/Libraries/LibWeb/Layout/Progress.cpp | 34 +++++++++++++ Userland/Libraries/LibWeb/Layout/Progress.h | 25 ++++++++++ 6 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 Userland/Libraries/LibWeb/Layout/Progress.cpp create mode 100644 Userland/Libraries/LibWeb/Layout/Progress.h (limited to 'Userland/Libraries') 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 +#include +#include namespace Web::HTML { @@ -17,4 +19,57 @@ HTMLProgressElement::~HTMLProgressElement() { } +RefPtr HTMLProgressElement::create_layout_node(NonnullRefPtr 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 create_layout_node(NonnullRefPtr) 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 +#include +#include + +namespace Web::Layout { + +Progress::Progress(DOM::Document& document, HTML::HTMLProgressElement& element, NonnullRefPtr 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 +#include + +namespace Web::Layout { + +class Progress : public LabelableNode { +public: + Progress(DOM::Document&, HTML::HTMLProgressElement&, NonnullRefPtr); + virtual ~Progress() override; + + virtual void paint(PaintContext&, PaintPhase) override; + + const HTML::HTMLProgressElement& dom_node() const { return static_cast(LabelableNode::dom_node()); } + HTML::HTMLProgressElement& dom_node() { return static_cast(LabelableNode::dom_node()); } +}; + +} -- cgit v1.2.3