Shapes

Shape views draw geometric forms. Nib has five built-in shapes: Rectangle, RoundedRectangle, Circle, Ellipse and Capsule. Custom outlines are built with Path & Shape, and color transitions with Gradients.

A shape fills the frame it is given. It is used in one of three ways:

  • Standalone, as a visible element with an explicit width and height.
  • As the background= of another view, where it takes the size of that view.
  • As the clip_shape= of another view, where it masks the view's content to its outline.
import nib

nib.VStack(
    controls=[
        nib.Circle(fill=nib.Color.BLUE, width=40, height=40),
        nib.Text(
            "Background",
            padding={"horizontal": 12, "vertical": 6},
            background=nib.Capsule(fill=nib.Color.ORANGE),
        ),
        nib.Image(
            src="photo.jpg",
            aspect_ratio=nib.ContentMode.FILL,
            width=60,
            height=60,
            clip_shape=nib.RoundedRectangle(12),
        ),
    ],
    alignment=nib.HorizontalAlignment.LEADING,
    spacing=12,
)

Shared parameters

Every built-in shape accepts these parameters. The sections below list only the parameters specific to each shape.

Parameter Type Default Description
fill str | Color | gradient None The interior. A hex string ("#FF5733"), a named color ("red"), a Color, or a gradient such as nib.LinearGradient(...).
stroke_color str | Color None The outline color, drawn along the shape's edge on top of the fill.
stroke_width float None The outline width in points. The outline is 1 point wide when stroke_color is set without a width.
**modifiers Common view modifiers: width, height, padding, opacity, animation and the rest. See Modifiers.

A shape with only stroke_color draws an outline with an empty interior.

Rectangle

Rectangle

A rectangle that fills its frame. Its corners are sharp by default, rounded uniformly when corner_radius is a float, and rounded per corner when corner_radius is a CornerRadius.

nib.Rectangle(
    fill=None,
    corner_radius=None,
    stroke_color=None,
    stroke_width=None,
    **modifiers,
)
Parameter Type Default Description
corner_radius float | CornerRadius None Corner rounding. A float applies one radius to all corners. A CornerRadius sets each corner. None gives sharp corners.

CornerRadius

nib.CornerRadius holds one radius per corner. Corners left out are 0.

nib.CornerRadius(
    top_left=0,
    top_right=0,
    bottom_left=0,
    bottom_right=0,
)
Factory Description
CornerRadius.all(radius) The same radius on all four corners.
CornerRadius.vertical(top, bottom) One radius for both top corners, another for both bottom corners.
CornerRadius.horizontal(left, right) One radius for both left corners, another for both right corners.
CornerRadius.only(top_left=0, top_right=0, bottom_left=0, bottom_right=0) Only the named corners are rounded.
nib.Rectangle(
    fill=nib.Color.BLUE,
    corner_radius=nib.CornerRadius.vertical(top=12, bottom=0),
    width=200,
    height=80,
)

Basic shapes

rectangle_basic_shapes

import nib


def main(app: nib.App):
    app.title = "Rectangles"
    app.icon = nib.SFSymbol("rectangle")

    def labelled(shape, caption):
        return nib.VStack(
            controls=[
                shape,
                nib.Text(
                    caption, font=nib.Font.CAPTION, foreground_color=nib.Color.SECONDARY
                ),
            ],
            spacing=6,
        )

    app.build(
        nib.HStack(
            controls=[
                labelled(
                    nib.Rectangle(fill=nib.Color.BLUE, width=80, height=80), "fill"
                ),
                labelled(
                    nib.Rectangle(
                        fill=nib.Color.GREEN, corner_radius=14, width=80, height=80
                    ),
                    "corner_radius",
                ),
                labelled(
                    nib.Rectangle(
                        fill="#3a3a3c",
                        corner_radius=8,
                        stroke_color=nib.Color.ORANGE,
                        stroke_width=3,
                        width=80,
                        height=80,
                    ),
                    "stroke",
                ),
            ],
            spacing=16,
            padding=20,
        )
    )


nib.run(main)

Per-corner radius

Round only the top corners to create a card header shape.

rectangle_per_corner_radius

import nib


def main(app: nib.App):
    app.title = "Weather card"
    app.icon = nib.SFSymbol("cloud.sun")

    app.build(
        nib.VStack(
            controls=[
                nib.ZStack(
                    controls=[
                        nib.Rectangle(
                            fill="#0a84ff",
                            corner_radius=nib.CornerRadius(
                                top_left=16, top_right=16, bottom_left=0, bottom_right=0
                            ),
                        ),
                        nib.HStack(
                            controls=[
                                nib.SFSymbol(
                                    "sun.max.fill", foreground_color=nib.Color.YELLOW
                                ),
                                nib.Text("Sunny", foreground_color=nib.Color.WHITE),
                            ],
                            spacing=6,
                        ),
                    ],
                    width=220,
                    height=56,
                ),
                nib.ZStack(
                    controls=[
                        nib.Rectangle(
                            fill="#1e293b",
                            corner_radius=nib.CornerRadius.vertical(top=0, bottom=16),
                        ),
                        nib.Text(
                            "24°",
                            font=nib.Font.LARGE_TITLE,
                            foreground_color=nib.Color.WHITE,
                        ),
                    ],
                    width=220,
                    height=90,
                ),
            ],
            spacing=0,
            padding=20,
        )
    )


nib.run(main)

RoundedRectangle

A Rectangle with corner_radius as its required first argument. It accepts the same values, including a CornerRadius. It is the usual choice for background= and clip_shape=.

nib.RoundedRectangle(
    corner_radius,
    fill=None,
    stroke_color=None,
    stroke_width=None,
    **modifiers,
)
Parameter Type Default Description
corner_radius float | CornerRadius required Corner rounding. A float applies one radius to all corners. A CornerRadius sets each corner.

Card background

rounded_rectangle_card_background

import nib


def main(app: nib.App):
    app.title = "Tip"
    app.icon = nib.SFSymbol("lightbulb")

    app.build(
        nib.HStack(
            controls=[
                nib.SFSymbol(
                    "lightbulb.fill",
                    foreground_color=nib.Color.YELLOW,
                    font=nib.Font.system(24),
                ),
                nib.VStack(
                    controls=[
                        nib.Text("Tip", font=nib.Font.HEADLINE),
                        nib.Text(
                            "Press ⌘, to open the settings window.",
                            foreground_color=nib.Color.SECONDARY,
                        ),
                    ],
                    spacing=2,
                    alignment=nib.HorizontalAlignment.LEADING,
                ),
            ],
            spacing=12,
            padding=16,
            background=nib.RoundedRectangle(corner_radius=12, fill="#1e293b"),
            margin=20,
        )
    )


nib.run(main)

Clip shape

rounded_rectangle_clip_shape

import nib


def main(app: nib.App):
    app.title = "Clip"
    app.icon = nib.SFSymbol("scissors")
    app.width = 320

    app.build(
        nib.HStack(
            controls=[
                nib.Image(
                    src="https://picsum.photos/id/237/240/240",
                    aspect_ratio=nib.ContentMode.FILL,
                    clip_shape=nib.RoundedRectangle(corner_radius=18),
                    width=120,
                    height=120,
                ),
                nib.Text(
                    "Clipped",
                    foreground_color=nib.Color.WHITE,
                    padding=14,
                    background=nib.Color.BLUE,
                    clip_shape=nib.RoundedRectangle(corner_radius=10),
                ),
            ],
            spacing=20,
            padding=20,
        )
    )


nib.run(main)

Per-corner radius

rounded_rectangle_per_corner_radius

import nib


def main(app: nib.App):
    app.title = "Tabs"
    app.icon = nib.SFSymbol("rectangle.topthird.inset.filled")
    app.width = 330

    def tab(name, active):
        return nib.Text(
            name,
            foreground_color=nib.Color.WHITE if active else nib.Color.SECONDARY,
            padding={"horizontal": 16, "vertical": 8},
            background=nib.RoundedRectangle(
                corner_radius=nib.CornerRadius.vertical(top=10, bottom=0),
                fill="#0a84ff" if active else "#3a3a3c",
            ),
        )

    app.build(
        nib.VStack(
            controls=[
                nib.HStack(
                    controls=[
                        tab("General", True),
                        tab("Editor", False),
                        tab("Advanced", False),
                    ],
                    spacing=4,
                ),
                nib.RoundedRectangle(
                    corner_radius=nib.CornerRadius.vertical(top=0, bottom=12),
                    fill="#1e293b",
                    width=280,
                    height=90,
                ),
            ],
            spacing=0,
            padding=20,
            alignment=nib.HorizontalAlignment.LEADING,
        )
    )


nib.run(main)

Circle

Circle

A circle centered in its frame. Its diameter is the smaller side of the frame, so it keeps a 1:1 aspect ratio.

nib.Circle(
    fill=None,
    stroke_color=None,
    stroke_width=None,
    trim_from=None,
    trim_to=None,
    rotation=None,
    **modifiers,
)
Parameter Type Default Description
trim_from float None Start of the drawn arc, from 0.0 to 1.0.
trim_to float None End of the drawn arc, from 0.0 to 1.0.
rotation float None Rotation in degrees, clockwise.

Trimmed circles

Setting trim_from or trim_to to anything other than a full circle (0.0 to 1.0) draws an arc. Position 0.0 is 12 o'clock and the arc runs clockwise, so trim_from=0.0, trim_to=0.25 draws the quarter from 12 to 3 o'clock. rotation turns the arc from there: rotation=90 moves the start to 3 o'clock.

A trimmed circle is drawn as a stroke with round caps, not filled. Its color is stroke_color, or fill when no stroke color is set, and its thickness is stroke_width.

A progress ring is two circles of the same size in a ZStack: a full circle with only stroke_color as the track, and a trimmed circle with the same stroke_width on top. The track needs stroke_color rather than fill, since a full circle with fill is a solid disc. To show a value between 0 and 1, pass trim_from=0.0 and the value as trim_to.

nib.ZStack(
    controls=[
        nib.Circle(stroke_color="#3a3a3c", stroke_width=8, width=80, height=80),
        nib.Circle(
            stroke_color=nib.Color.GREEN,
            stroke_width=8,
            trim_from=0.0,
            trim_to=0.6,
            width=80,
            height=80,
        ),
        nib.Text("60%", font=nib.Font.HEADLINE),
    ]
)

Basic circles

circle_basic_circles

import nib


def main(app: nib.App):
    app.title = "Status"
    app.icon = nib.SFSymbol("circle.fill")
    app.height = 240

    def dot(color, name):
        return nib.HStack(
            controls=[nib.Circle(fill=color, width=12, height=12), nib.Text(name)],
            spacing=8,
        )

    app.build(
        nib.VStack(
            controls=[
                nib.HStack(
                    controls=[
                        nib.Circle(fill=nib.Color.BLUE, width=56, height=56),
                        nib.Circle(
                            fill=nib.Color.RED,
                            stroke_color=nib.Color.WHITE,
                            stroke_width=3,
                            width=56,
                            height=56,
                        ),
                        nib.Circle(
                            stroke_color=nib.Color.GREEN,
                            stroke_width=4,
                            width=56,
                            height=56,
                        ),
                    ],
                    spacing=16,
                ),
                nib.Divider(),
                dot(nib.Color.GREEN, "Online"),
                dot(nib.Color.ORANGE, "Away"),
                dot(nib.Color.GRAY, "Offline"),
            ],
            spacing=10,
            padding=20,
            alignment=nib.HorizontalAlignment.LEADING,
        )
    )


nib.run(main)

Progress ring

Use trim_from and trim_to to create a progress indicator. Each ring starts at 12 o'clock.

circle_progress_ring

import nib


def main(app: nib.App):
    app.title = "Goals"
    app.icon = nib.SFSymbol("figure.walk")

    def ring(name, progress, color):
        return nib.VStack(
            controls=[
                nib.ZStack(
                    controls=[
                        nib.Circle(
                            stroke_color="#3a3a3c", stroke_width=8, width=80, height=80
                        ),
                        nib.Circle(
                            stroke_color=color,
                            stroke_width=8,
                            trim_from=0.0,
                            trim_to=progress,
                            width=80,
                            height=80,
                        ),
                        nib.Text(f"{int(progress * 100)}%", font=nib.Font.HEADLINE),
                    ]
                ),
                nib.Text(
                    name, font=nib.Font.CAPTION, foreground_color=nib.Color.SECONDARY
                ),
            ],
            spacing=6,
        )

    app.build(
        nib.HStack(
            controls=[
                ring("Move", 0.75, nib.Color.RED),
                ring("Exercise", 0.4, nib.Color.GREEN),
                ring("Stand", 0.9, nib.Color.CYAN),
            ],
            spacing=20,
            padding=20,
        )
    )


nib.run(main)

For a standard ring indicator with a value and a label, see Gauge.

Ellipse

Ellipse

An oval that stretches to fill its frame, with independent width and height. With equal width and height it is a circle. Ellipse has no parameters beyond the shared ones.

nib.Ellipse(
    fill=None,
    stroke_color=None,
    stroke_width=None,
    **modifiers,
)

Basic ellipses

ellipse_basic_ellipses

import nib


def main(app: nib.App):
    app.title = "Ellipses"
    app.icon = nib.SFSymbol("oval")
    app.height = 300

    app.build(
        nib.VStack(
            controls=[
                nib.Ellipse(fill=nib.Color.BLUE, width=160, height=70),
                nib.Ellipse(
                    fill=nib.Color.ORANGE,
                    stroke_color=nib.Color.WHITE,
                    stroke_width=3,
                    width=120,
                    height=60,
                ),
                nib.HStack(
                    controls=[
                        nib.Ellipse(fill=nib.Color.PURPLE, width=50, height=100),
                        nib.Ellipse(
                            stroke_color=nib.Color.GREEN,
                            stroke_width=3,
                            width=50,
                            height=100,
                        ),
                    ],
                    spacing=16,
                ),
            ],
            spacing=14,
            padding=20,
        )
    )


nib.run(main)

Ellipse as background

Use an Ellipse as a decorative background for text badges.

ellipse_ellipse_background

import nib


def main(app: nib.App):
    app.title = "Labels"
    app.icon = nib.SFSymbol("tag")

    def pill(text, color):
        return nib.Text(
            text,
            font=nib.Font.CAPTION,
            foreground_color=nib.Color.WHITE,
            padding={"horizontal": 14, "vertical": 6},
            background=nib.Ellipse(fill=color),
        )

    app.build(
        nib.HStack(
            controls=[
                pill("Bug", nib.Color.RED),
                pill("Feature", nib.Color.BLUE),
                pill("Docs", nib.Color.GREEN),
            ],
            spacing=10,
            padding=20,
        )
    )


nib.run(main)

Capsule

Capsule

A rectangle whose shorter sides are fully rounded. The ends are semicircles with a radius of half the shorter dimension: half the height for a wide capsule, half the width for a tall one. A Rectangle with corner_radius keeps a fixed radius whatever its size. Capsule has no parameters beyond the shared ones.

nib.Capsule(
    fill=None,
    stroke_color=None,
    stroke_width=None,
    **modifiers,
)

Basic capsule shapes

capsule_basic_capsule_shapes

import nib


def main(app: nib.App):
    app.title = "Capsules"
    app.icon = nib.SFSymbol("capsule")
    app.height = 270

    app.build(
        nib.VStack(
            controls=[
                nib.Capsule(fill=nib.Color.BLUE, width=160, height=44),
                nib.Capsule(
                    fill=nib.Color.PURPLE,
                    stroke_color=nib.Color.WHITE,
                    stroke_width=2,
                    width=120,
                    height=36,
                ),
                nib.HStack(
                    controls=[
                        nib.Capsule(fill=nib.Color.GREEN, width=30, height=100),
                        nib.Capsule(
                            stroke_color=nib.Color.ORANGE,
                            stroke_width=3,
                            width=30,
                            height=100,
                        ),
                    ],
                    spacing=16,
                ),
            ],
            spacing=14,
            padding=20,
        )
    )


nib.run(main)

Tag / badge with capsule background

Use a Capsule as the background for text to create pill-shaped labels.

capsule_tag_badge_capsule

import nib


def main(app: nib.App):
    app.title = "Store"
    app.icon = nib.SFSymbol("bag")

    def badge(text, color):
        return nib.Text(
            text,
            font=nib.Font.CAPTION,
            foreground_color=nib.Color.WHITE,
            padding={"horizontal": 10, "vertical": 4},
            background=nib.Capsule(fill=color),
        )

    app.build(
        nib.VStack(
            controls=[
                nib.HStack(
                    controls=[
                        nib.Text("Headphones", font=nib.Font.HEADLINE),
                        nib.Spacer(),
                        badge("NEW", nib.Color.RED),
                    ]
                ),
                nib.HStack(
                    controls=[
                        nib.Text("Keyboard", font=nib.Font.HEADLINE),
                        nib.Spacer(),
                        badge("SALE", nib.Color.GREEN),
                    ]
                ),
                nib.HStack(
                    controls=[
                        nib.Text("Monitor", font=nib.Font.HEADLINE),
                        nib.Spacer(),
                        badge("HOT", nib.Color.ORANGE),
                    ]
                ),
            ],
            spacing=12,
            padding=20,
            width=280,
        )
    )


nib.run(main)