[][src]Struct druid::widget::Painter

pub struct Painter<T>(_);

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

impl<T> Painter<T>[src]

pub fn new(f: impl FnMut(&mut PaintCtx<'_, '_, '_>, &T, &Env) + 'static) -> Self[src]

Create a new Painter with the provided paint fn.

Trait Implementations

impl<T> From<Painter<T>> for BackgroundBrush<T>[src]

impl<T: Data> Widget<T> for Painter<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Painter<T>[src]

impl<T> !Send for Painter<T>[src]

impl<T> !Sync for Painter<T>[src]

impl<T> Unpin for Painter<T>[src]

impl<T> !UnwindSafe for Painter<T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> RoundFrom<T> for T

impl<T, U> RoundInto<U> for T where
    U: RoundFrom<T>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.