Skip to content

Instantly share code, notes, and snippets.

@jneem
Last active March 27, 2020 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneem/4fa5d0bc3d9bca4d79961b56b4cc29fd to your computer and use it in GitHub Desktop.
Save jneem/4fa5d0bc3d9bca4d79961b56b4cc29fd to your computer and use it in GitHub Desktop.
Rectangle overflow
[package]
name = "druid-rectangle"
version = "0.1.0"
authors = ["Joe Neeman <joeneeman@gmail.com>"]
edition = "2018"
[dependencies]
druid = { git = "https://github.com/xi-editor/druid.git" }
use druid::widget::prelude::*;
use druid::widget::Align;
use druid::{AppLauncher, Color, Env, LocalizedString, Point, Rect, Widget, WindowDesc};
const WINDOW_TITLE: LocalizedString<f64> = LocalizedString::new("Hello World!");
fn main() {
let main_window = WindowDesc::new(build_root_widget)
.title(WINDOW_TITLE)
.window_size((400.0, 400.0));
AppLauncher::with_window(main_window)
.launch(1e7)
.expect("Failed to launch application");
}
fn build_root_widget() -> impl Widget<f64> {
Align::centered(RectangleWidget)
}
struct RectangleWidget;
impl Widget<f64> for RectangleWidget {
fn event(&mut self, _: &mut EventCtx, _: &Event, _: &mut f64, _: &Env) {}
fn lifecycle(&mut self, _: &mut LifeCycleCtx, _: &LifeCycle, _: &f64, _: &Env) {}
fn update(&mut self, _: &mut UpdateCtx, _: &f64, _: &f64, _: &Env) {}
fn layout(&mut self, _: &mut LayoutCtx, bc: &BoxConstraints, _: &f64, _: &Env) -> Size {
bc.max()
}
fn paint(&mut self, ctx: &mut PaintCtx, width: &f64, _env: &Env) {
let size = ctx.size();
let widget_rect = Rect::from_origin_size(Point::ORIGIN, size);
let draw_rect = Rect::from_origin_size((0.0, 50.0), (*width, size.height - 100.0))
.to_rounded_rect(50.0);
ctx.with_save(move |ctx| {
ctx.clip(widget_rect);
ctx.fill(draw_rect, &Color::rgb8(0, 0, 255));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment