[−][src]Struct druid::widget::Painter
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]
pub fn from(src: Painter<T>) -> BackgroundBrush<T>
[src]
impl<T: Data> Widget<T> for Painter<T>
[src]
pub fn event(&mut self, _: &mut EventCtx<'_, '_>, _: &Event, _: &mut T, _: &Env)
[src]
pub fn lifecycle(
&mut self,
_: &mut LifeCycleCtx<'_, '_>,
_: &LifeCycle,
_: &T,
_: &Env
)
[src]
&mut self,
_: &mut LifeCycleCtx<'_, '_>,
_: &LifeCycle,
_: &T,
_: &Env
)
pub fn update(&mut self, ctx: &mut UpdateCtx<'_, '_>, old: &T, new: &T, _: &Env)
[src]
pub fn layout(
&mut self,
_ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
_: &T,
_: &Env
) -> Size
[src]
&mut self,
_ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
_: &T,
_: &Env
) -> Size
pub fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, env: &Env)
[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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> RoundFrom<T> for T
pub fn round_from(x: T) -> T
impl<T, U> RoundInto<U> for T where
U: RoundFrom<T>,
U: RoundFrom<T>,
pub fn round_into(self) -> U
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]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,