Module pixel_widgets::style[][src]

Expand description

Styling system Style in pixel-widgets is either defined in .pwss (pixel-widgets stylesheets) files or in code. The file variant uses a format that is syntactically similar to CSS.

How to use styles

Styles can be loaded or created when building your Ui, as Ui::new accepts a TryInto<Style> argument. Usually, you’ll pass a StyleBuilder here. The StyleBuilder has functionality to define styles and to load .pwss files.

.pwss example

column {
    align-horizontal: center;
}
button {
    background: #444;
    padding: 5;
}
button:hover {
    background: #666;
}
button:pressed {
    background: #222;
}
button:hover > text {
    color: #f00;
}
text {
    text-size: 24;
}

This example sets a few properties on some of the widgets. Just try it out with the examples in the examples directory and see for yourself what the effect is.

Syntax

Each pwss file contains a collection of rules. Rules are a group of declarations that are applied to selected widgets.

Rules

A selector has the following format:

<selector> <selector> ... {
    <property>: <value>;
    <property>: <value>;
    ...
}

The first line expects some selectors. These will be used to apply the rule to the desired widgets. Entering multiple selectors like in this example will look for a button inside a column inside a window.

window column button {
    background: @button.png;
}

Selectors

This table describes the supported selectors

selectorexampledescription
**selects all widgets
widgettextselects all text widgets
.class.fancyselects all widgets that have the class “fancy”
.. widget.fancy textselects all text widgets that are a descendant of a “fancy” classed widget
>widget.fancy > textselects all text widgets that are a direct child of a “fancy” classed widget
+widget.fancy + textselects all widgets that follow directly after a “fancy” classed widget
~widget.fancy ~ textselects all widgets that follow after a “fancy” classed widget
:statebutton:hoverselects all buttons that are hovered by the mouse
:nth-child(n)text:nth-child(2)selects text widgets that are the third child of their parent
:nth-last-child(n)text:nth-last-child(2)selects text widgets that are the third child of their parent, counted from the last widget
:nth-child(odd)text:nth-child(odd)selects text widgets that are an odd child of their parent
:nth-child(even)text:nth-child(even)selects text widgets that are an even child of their parent
:not(selector)button:not(:pressed)selects button widgets that are not pressed
:only-childcolumn > *:only-childselects the only child of a column when the column has only one child

Properties

The interior of a rule consists of a number of declarations. These declarations are what specifies style. A declaration starts with a property, and each property has it’s own associated format. Take a look at the table to see what properties exist.

keydescriptionformat
widthwidget widthsize
heightwidget heightsize
backgroundBackground for the widget that full covers the layout rectbackground
paddingAmount of padding to use on each side of the contentrectangle
padding-leftAmount of padding to use on the left side of the contentnumber
padding-rightAmount of padding to use on the right side of the contentnumber
padding-topAmount of padding to use on the top side of the contentnumber
padding-bottomAmount of padding to use on the bottom side of the contentnumber
marginAmount of margin to use on each side of the widgetrectangle
margin-leftAmount of margin to use on the left side of the widget`number
margin-rightAmount of margin to use on the right side of the widgetnumber
margin-topAmount of margin to use on the top side of the widgetnumber`
margin-bottomAmount of margin to use on the bottom side of the widgetnumber
fontFont to use for text renderingurl
colorColor to use for foreground drawing, including textcolor
text-sizeSize of textnumber
text-wrapWrapping strategy for texttextwrap
layout-directionLayout direction for widgets that support itdirection
align-horizontalhow to align children horizontallyalign
align-verticalhow to align children verticallyalign

Value syntax

TypeSyntaxNotes
color#rgb
#rgba
#rrggbb
#rrggbbaa
Examples:
#fff
#ff00ff
url"filename"An url in between quotes
"image.png"
"font.ttf"
numberfloating point literalA number, such as 2.0 or 42
background<url>
<color>
image(<url>, <color>)
patch(<url>, <color>)
none
If a url ends with .9.png it will be resolved 9 patch.
If your 9 slice doesn’t end with .9.png, use patch.
rectangle<num>
<num> <num>
<num> <num> <num>
<num> <num> <num> <num>
all sides
top/bottom, right/left
top, ht/left, bottom<br>top, right, bottom, left`
textwrapno-wrap
wrap
word-wrap
size<number>
fill(<number>)
exact(<number>)
shrink
Just a number resolves to exact
directiontop-to-bottom
left-to-right
right-to-left
bottom-to-top
alignbegin
center
end

Modules

Style building tools

Structs

Container for all styling data.

A fully resolved stylesheet, passed by reference to Widget::draw. Contains the resolved values of all possible style properties.

Enums

A style property and it’s value

Errors that can be encountered while loading a stylesheet

A selector that selects widgets that match some property.

Widget name as used in a Selector.

Widget states