Struct druid::widget::Painter

source ·
pub struct Painter<T>(_);
Expand description

A widget that only handles painting.

This is useful in a situation where layout is controlled elsewhere and you do not need to handle events, but you would like to customize appearance.

When is paint called?

The Painter widget will call its paint method anytime its Data is changed. If you would like it to repaint at other times (such as when hot or active state changes) you will need to call request_paint further up the tree, perhaps in a Controller widget.

Examples

Changing background color based on some part of data:

use druid::{Env, PaintCtx,Rect, RenderContext};
use druid::widget::Painter;

struct MyData { is_enabled: bool }

let my_painter = Painter::new(|ctx, data: &MyData, env| {
    let bounds = ctx.size().to_rect();
    if data.is_enabled {
        ctx.fill(bounds, &env.get(ENABLED_BG_COLOR));
    } else {

        ctx.fill(bounds, &env.get(DISABLED_BG_COLOR));
    }
});

Using painter to make a simple widget that will draw a selected color

use druid::{Color, Env, PaintCtx,Rect, RenderContext};
use druid::widget::Painter;

const CORNER_RADIUS: f64 = 4.0;
const STROKE_WIDTH: f64 = 2.0;

let colorwell: Painter<Color> = Painter::new(|ctx, data: &Color, env| {
    // Shrink the bounds a little, to ensure that our stroke remains within
    // the paint bounds.
    let bounds = ctx.size().to_rect().inset(-STROKE_WIDTH / 2.0);
    let rounded = bounds.to_rounded_rect(CORNER_RADIUS);
    ctx.fill(rounded, data);
    ctx.stroke(rounded, &env.get(druid::theme::PRIMARY_DARK), STROKE_WIDTH);
});

Implementations§

Create a new Painter with the provided paint fn.

Trait Implementations§

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Performs the conversion.
Performs the conversion.
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more