1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:18:12 +00:00

LibWeb: Add basic implementation of progress bar element

This commit is contained in:
Rafał Babiarz 2022-02-16 19:12:54 +01:00 committed by Tim Flynn
parent d8388f30c8
commit 21e353980f
6 changed files with 133 additions and 3 deletions

View file

@ -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

View file

@ -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();
}
}

View file

@ -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); }
};
}

View file

@ -2,6 +2,8 @@
interface HTMLProgressElement : HTMLElement {
[CEReactions] attribute double value;
[CEReactions] attribute double max;
readonly attribute double position;
};

View file

@ -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(), "");
}
}
}

View file

@ -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()); }
};
}