Skip to content

Commit

Permalink
LibWeb: Support accent-color for range input and progress element
Browse files Browse the repository at this point in the history
Fixes #466
  • Loading branch information
simonkrauter authored and AtkinsSJ committed Jul 15, 2024
1 parent 5c315b5 commit 9df8e1f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
4 changes: 0 additions & 4 deletions Userland/Libraries/LibWeb/CSS/Default.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ input[type=range]::-webkit-slider-thumb {
height: 16px;
transform: translateX(-50%);
border-radius: 50%;
background-color: AccentColor;
outline: 1px solid rgba(0, 0, 0, 0.5);
z-index: 1;
}
Expand Down Expand Up @@ -143,9 +142,6 @@ progress::-webkit-progress-bar {
background-color: Background;
border: 1px solid rgba(0, 0, 0, 0.5);
}
progress::-webkit-progress-value {
background-color: AccentColor;
}

/* 15.3.1 Hidden elements
* https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements
Expand Down
16 changes: 15 additions & 1 deletion Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,6 @@ void HTMLInputElement::create_range_input_shadow_tree()
display: block;
position: absolute;
height: 100%;
background-color: AccentColor;
)~~~"_string));
MUST(slider_runnable_track->append_child(*m_range_progress_element));

Expand Down Expand Up @@ -1087,6 +1086,21 @@ void HTMLInputElement::create_range_input_shadow_tree()
add_event_listener_without_options(UIEvents::EventNames::mousedown, DOM::IDLEventListener::create(realm(), mousedown_callback));
}

void HTMLInputElement::computed_css_values_changed()
{
auto palette = document().page().palette();
auto accent_color = palette.color(ColorRole::Accent).to_string();

auto accent_color_property = computed_css_values()->property(CSS::PropertyID::AccentColor);
if (accent_color_property->has_color())
accent_color = accent_color_property->to_string();

if (m_range_progress_element)
MUST(m_range_progress_element->style_for_bindings()->set_property(CSS::PropertyID::BackgroundColor, accent_color));
if (m_slider_thumb)
MUST(m_slider_thumb->style_for_bindings()->set_property(CSS::PropertyID::BackgroundColor, accent_color));
}

void HTMLInputElement::user_interaction_did_change_input_value()
{
// https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLInputElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class HTMLInputElement final

// ^DOM::Element
virtual i32 default_tab_index_value() const override;
virtual void computed_css_values_changed() override;

// https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):dimension-attributes
virtual bool supports_dimension_attributes() const override { return type_state() == TypeAttributeState::ImageButton; }
Expand Down
15 changes: 15 additions & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLProgressElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
*/

#include <LibWeb/Bindings/HTMLProgressElementPrototype.h>
#include <LibWeb/CSS/StyleProperties.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/HTMLProgressElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Page/Page.h>

namespace Web::HTML {

Expand Down Expand Up @@ -119,4 +121,17 @@ void HTMLProgressElement::update_progress_value_element()
MUST(m_progress_value_element->style_for_bindings()->set_property(CSS::PropertyID::Width, MUST(String::formatted("{}%", position() * 100))));
}

void HTMLProgressElement::computed_css_values_changed()
{
auto palette = document().page().palette();
auto accent_color = palette.color(ColorRole::Accent).to_string();

auto accent_color_property = computed_css_values()->property(CSS::PropertyID::AccentColor);
if (accent_color_property->has_color())
accent_color = accent_color_property->to_string();

if (m_progress_value_element)
MUST(m_progress_value_element->style_for_bindings()->set_property(CSS::PropertyID::BackgroundColor, accent_color));
}

}
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLProgressElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class HTMLProgressElement final : public HTMLElement {

// ^DOM::Node
virtual bool is_html_progress_element() const final { return true; }
virtual void computed_css_values_changed() override;

virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Expand Down

0 comments on commit 9df8e1f

Please sign in to comment.