26template <FixedString Name>
27struct ComputedStyleProp {
28 ComputedStyleProp() =
default;
29 ComputedStyleProp(
const ComputedStyleProp&) =
delete;
30 ComputedStyleProp& operator=(
const ComputedStyleProp&) =
delete;
32 operator std::string()
const;
34 StyleValue AsStyleValue()
const {
return StyleValue::Parse(std::string(*
this)); }
36 ultralight::Color AsColor()
const {
return ColorFromStyleText(std::string(*
this)); }
38 friend bool operator==(
const ComputedStyleProp& p, std::string_view v) {
39 return std::string(p) == v;
45struct ComputedStyleStorage {
46 ULDOMElement element =
nullptr;
126#define ULTRALIGHT_DOM_V(ident, cssname) \
127 ULTRALIGHT_DOM_NO_UNIQUE_ADDRESS detail::ComputedStyleProp<cssname> ident;
129#undef ULTRALIGHT_DOM_V
130#define ULTRALIGHT_DOM_V(ident, cssname, canonical) \
131 ULTRALIGHT_DOM_NO_UNIQUE_ADDRESS detail::ComputedStyleProp<cssname> ident;
133#undef ULTRALIGHT_DOM_V
160 other.detail_.element =
nullptr;
171 std::swap(
detail_.element, other.detail_.element);
190 detail::CString n(name);
191 return detail::TakeString(ulDOMElementGetComputedStyleProperty(
detail_.element, n.c_str()));
203 template <detail::FixedString Name>
205 return detail::StyleText(detail::TakeString(
229static_assert(std::is_standard_layout_v<ComputedStyle>,
"ComputedStyle must stay standard-layout");
230static_assert(!std::is_aggregate_v<ComputedStyle>,
"ComputedStyle must not be an aggregate");
233 ULDOMElement handle = element.
raw();
234 return ComputedStyle(handle ? ulCreateDOMElementRef(handle) :
nullptr);
242inline const size_t* ComputedStyleMemberOffsets() {
243 static const size_t offsets[] = {
244#define ULTRALIGHT_DOM_V(ident, cssname) offsetof(ComputedStyle, ident),
246#undef ULTRALIGHT_DOM_V
247#define ULTRALIGHT_DOM_V(ident, cssname, canonical) offsetof(ComputedStyle, ident),
249#undef ULTRALIGHT_DOM_V
254template <FixedString Name>
255ComputedStyleProp<Name>::operator std::string()
const {
256 const ComputedStyle* owner =
reinterpret_cast<const ComputedStyle*
>(
257 reinterpret_cast<const char*
>(
this) - ComputedStyleMemberOffsets()[kStyleIndex<Name>]);
258 return TakeString(ulDOMElementGetComputedStyleProperty(owner->detail_.element, Name.data));
#define ULTRALIGHT_DOM_V(ident, cssname)
Definition ComputedStyle.h:126
bool operator==(const JSRetainPtr< T > &a, const JSRetainPtr< U > &b)
Definition JSRetainPtr.h:169
A read-only view of an element's computed style (getComputedStyle).
Definition ComputedStyle.h:122
~ComputedStyle()
Destroy this view (the element isn't affected).
Definition ComputedStyle.h:178
ComputedStyle(ComputedStyle &&other) noexcept
Move constructor (other becomes empty).
Definition ComputedStyle.h:159
detail::ComputedStyleStorage detail_
Internal storage (not part of the API).
Definition ComputedStyle.h:138
ComputedStyle & operator=(ComputedStyle other) noexcept
Assignment (copies or moves).
Definition ComputedStyle.h:170
ComputedStyle(const ComputedStyle &other)
Copy constructor (both read the same element's style).
Definition ComputedStyle.h:150
ComputedStyle()
Create an empty ComputedStyle (every read returns an empty string).
Definition ComputedStyle.h:143
detail::StyleText operator[](CSSPropertyName< Name >) const
Get a property's computed value using a name checked at compile time (eg, computed[dom::css<"font-siz...
Definition ComputedStyle.h:204
friend ComputedStyle getComputedStyle(const Element &element)
Get an element's computed style (getComputedStyle).
Definition ComputedStyle.h:232
detail::StyleText operator[](std::string_view name) const
Get a property's computed value by name.
Definition ComputedStyle.h:218
std::string getPropertyValue(std::string_view name) const
Get a property's computed value (getPropertyValue).
Definition ComputedStyle.h:189
A handle to an element on a page.
Definition Element.h:142
ULDOMElement raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMElement.h> functions.
Definition Element.h:2006
@ StyleValue
A CSS number with a unit (see dom::StyleValue).
Definition ValueTraits.h:32
Direct C++ access to modify page elements and handle events.
ComputedStyle getComputedStyle(const Element &element)
Get an element's computed style (getComputedStyle).
Definition ComputedStyle.h:232
Root namespace for every public Ultralight type, function, and enumeration.
A CSS property name checked when your code compiles (the type of dom::css).
Definition CSSPropertyName.h:17
static constexpr const char * name()
Get the property's CSS name (null-terminated).
Definition CSSPropertyName.h:26