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§
Trait Implementations§
source§impl<T> From<Painter<T>> for BackgroundBrush<T>
impl<T> From<Painter<T>> for BackgroundBrush<T>
source§fn from(src: Painter<T>) -> BackgroundBrush<T>
fn from(src: Painter<T>) -> BackgroundBrush<T>
Converts to this type from the input type.
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Painter<T>
impl<T> !Send for Painter<T>
impl<T> !Sync for Painter<T>
impl<T> Unpin for Painter<T>
impl<T> !UnwindSafe for Painter<T>
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> RoundFrom<T> for T
impl<T> RoundFrom<T> for T
§fn round_from(x: T) -> T
fn round_from(x: T) -> T
Performs the conversion.
§impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
§fn round_into(self) -> U
fn round_into(self) -> U
Performs the conversion.