Skip to main content

Painter

Trait Painter 

Source
pub trait Painter {
    // Required methods
    fn cache_policy(&self) -> CachePolicy;
    fn fill(&mut self, color: Color);
    fn fill_rect(&mut self, bounds: Rect, color: Color);
    fn rect(&mut self, bounds: Rect, color: Color);
    fn line(&mut self, from: Point, to: Point, color: Color);
    fn text(
        &mut self,
        bounds: Rect,
        style: &TextStyle,
        color: Color,
        msg: &str,
    ) -> Size;
    fn line_height(&mut self, style: &TextStyle) -> u32;
    fn text_width(&mut self, style: &TextStyle, msg: &str) -> u32;
    fn image(&mut self, path: &str, bounds: Rect) -> Result<(), Error>;
    fn dither(&mut self, bounds: Rect, first: Color, second: Color);
    fn dither_overlay(&mut self, bounds: Rect, color: Color);
    fn clip(&mut self, bounds: Option<Rect>);
}
Expand description

The drawing operations a Widget’s paint can use. Kept small: only what the helpers in this module need.

Coordinates are relative to the Widget’s own upper left-hand corner. A backend decides where the pixels go (a GPU texture, a memory buffer, the screen with clipping).

Required Methods§

Source

fn cache_policy(&self) -> CachePolicy

Returns how this backend caches painted Widgets.

§Returns

The backend’s CachePolicy.

Source

fn fill(&mut self, color: Color)

Fills the whole drawing surface with a color.

§Arguments
  • color - The fill color.
Source

fn fill_rect(&mut self, bounds: Rect, color: Color)

Fills a rectangle with a color.

§Arguments
  • bounds - The rectangle to fill.
  • color - The fill color.
Source

fn rect(&mut self, bounds: Rect, color: Color)

Draws a one-pixel rectangle outline.

§Arguments
  • bounds - The rectangle to outline.
  • color - The line color.
Source

fn line(&mut self, from: Point, to: Point, color: Color)

Draws a one-pixel line.

§Arguments
  • from - The start point.
  • to - The end point.
  • color - The line color.
Source

fn text( &mut self, bounds: Rect, style: &TextStyle, color: Color, msg: &str, ) -> Size

Draws text within a rectangle, aligned by style, wrapped at the rectangle’s width, and centered vertically when it fits (top-aligned when it doesn’t).

§Arguments
  • bounds - The rectangle to draw within.
  • style - The font and alignment.
  • color - The text color (overrides style.font_color).
  • msg - The text to draw.
§Returns

The size of the text drawn; 0 by 0 if nothing was drawn.

Source

fn line_height(&mut self, style: &TextStyle) -> u32

Returns the height of one line of text in a font.

§Arguments
  • style - The font.
§Returns

The line height in pixels.

Source

fn text_width(&mut self, style: &TextStyle, msg: &str) -> u32

Measures how wide text is on one line, without drawing it.

§Arguments
  • style - The font.
  • msg - The text.
§Returns

The width in pixels.

Source

fn image(&mut self, path: &str, bounds: Rect) -> Result<(), Error>

Draws an image file, scaled to a rectangle.

§Arguments
  • path - Path to the image file.
  • bounds - The rectangle to draw into.
§Returns

Ok(()) once the image is drawn.

§Errors

Returns Error::Image if the image cannot be loaded or drawn.

Source

fn dither(&mut self, bounds: Rect, first: Color, second: Color)

Fills a rectangle with a 50% checkerboard of two colors, for backends and displays with few colors. The pixel at the rectangle’s upper left-hand corner is first.

§Arguments
  • bounds - The rectangle to fill.
  • first - The color of the corner pixel.
  • second - The other color.
Source

fn dither_overlay(&mut self, bounds: Rect, color: Color)

Draws a color over every other pixel of a rectangle (the pixels a dither would draw in first), blending with what is already there and leaving the other pixels untouched. The pixel at the rectangle’s upper left-hand corner is drawn.

§Arguments
  • bounds - The rectangle to draw over.
  • color - The color to blend; its alpha sets the strength.
Source

fn clip(&mut self, bounds: Option<Rect>)

Limits later drawing to a rectangle, or removes the limit.

§Arguments
  • bounds - The rectangle to draw within, or None for no limit.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§