Skip to content

Commit 47fde97

Browse files
committed
Update to piet 0.1.0 (coregraphics backend)
This moves to using coregraphics for druid on the mac; most importantly this means we have access to a full-featured text API.
1 parent 0d56631 commit 47fde97

File tree

7 files changed

+80
-45
lines changed

7 files changed

+80
-45
lines changed

Cargo.lock

Lines changed: 61 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book_examples/src/custom_widgets_md.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn sized_swatch() -> impl Widget<Color> {
2828
// ANCHOR: background_label
2929
fn background_label() -> impl Widget<Color> {
3030
Label::dynamic(|color: &Color, _| {
31-
let (r, g, b, _) = color.as_rgba_u8();
31+
let (r, g, b, _) = color.as_rgba8();
3232
format!("#{:X}{:X}{:X}", r, g, b)
3333
})
3434
.background(make_color_swatch())

druid-shell/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ x11 = ["xcb", "cairo-sys-rs"]
1616
default-target = "x86_64-pc-windows-msvc"
1717

1818
[dependencies]
19-
piet-common = "0.0.12"
19+
piet-common = "0.1.0"
2020
log = "0.4.8"
2121
lazy_static = "1.0"
2222
time = "0.2.7"
2323
cfg-if = "0.1.10"
2424
# NOTE: if changing, ensure version is compatible with the version in piet
25-
kurbo = "0.5.11"
25+
kurbo = "0.6.0"
2626
# NOTE: This defaults to mimicking std::time on non-wasm targets
2727
instant = { version = "0.1", features = [ "wasm-bindgen" ] }
2828

@@ -38,7 +38,7 @@ gtk-sys = { version = "0.9.0", optional = true }
3838
xcb = { version = "0.9.0", features = ["thread", "xlib_xcb", "randr"], optional = true }
3939

4040
[dev-dependencies]
41-
piet-common = {version = "0.0.12", features = ["png"]}
41+
piet-common = {version = "0.1.0", features = ["png"]}
4242

4343
[target.'cfg(target_os="windows")'.dependencies]
4444
wio = "0.2"
@@ -50,7 +50,8 @@ features = ["d2d1_1", "dwrite", "winbase", "libloaderapi", "errhandlingapi", "wi
5050
[target.'cfg(target_os="macos")'.dependencies]
5151
cocoa = "0.20.0"
5252
objc = "0.2.5"
53-
cairo-rs = { version = "0.8.1", default_features = false }
53+
core-graphics = "0.19"
54+
foreign-types = "0.3.2"
5455

5556
# TODO(x11/dependencies): only use feature "xcb" if using XCB
5657
[target.'cfg(target_os="linux")'.dependencies]

druid-shell/examples/invalidate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct InvalidateTest {
3232
impl InvalidateTest {
3333
fn update_color_and_rect(&mut self) {
3434
let time_since_start = (Instant::now() - self.start_time).as_millis();
35-
let (r, g, b, _) = self.color.as_rgba_u8();
35+
let (r, g, b, _) = self.color.as_rgba8();
3636
self.color = match (time_since_start % 2, time_since_start % 3) {
3737
(0, _) => Color::rgb8(r.wrapping_add(10), g, b),
3838
(_, 0) => Color::rgb8(r, g.wrapping_add(10), b),

druid-shell/src/platform/mac/window.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ use cocoa::base::{id, nil, BOOL, NO, YES};
3131
use cocoa::foundation::{
3232
NSAutoreleasePool, NSInteger, NSPoint, NSRect, NSSize, NSString, NSUInteger,
3333
};
34+
use core_graphics::context::CGContextRef;
35+
use foreign_types::ForeignTypeRef;
3436
use lazy_static::lazy_static;
37+
use log::{error, info};
3538
use objc::declare::ClassDecl;
3639
use objc::rc::WeakPtr;
3740
use objc::runtime::{Class, Object, Sel};
3841
use objc::{class, msg_send, sel, sel_impl};
3942

40-
use cairo::{Context, QuartzSurface};
41-
use log::{error, info};
42-
4343
use crate::kurbo::{Point, Rect, Size, Vec2};
4444
use crate::piet::{Piet, RenderContext};
4545

@@ -574,23 +574,17 @@ extern "C" fn mods_changed(this: &mut Object, _: Sel, nsevent: id) {
574574
extern "C" fn draw_rect(this: &mut Object, _: Sel, dirtyRect: NSRect) {
575575
unsafe {
576576
let context: id = msg_send![class![NSGraphicsContext], currentContext];
577-
// TODO: probably should use a better type than void pointer, but it's not obvious what's best.
578-
// cairo_sys::CGContextRef would be better documentation-wise, but it's a type alias.
579-
let cgcontext: *mut c_void = msg_send![context, CGContext];
580-
// TODO: use width and height from view size
581-
let frame = NSView::frame(this as *mut _);
582-
let width = frame.size.width as u32;
583-
let height = frame.size.height as u32;
577+
//FIXME: when core_graphics is at 0.20, we should be able to use
578+
//core_graphics::sys::CGContextRef as our pointer type.
579+
let cgcontext_ptr: *mut <CGContextRef as ForeignTypeRef>::CType =
580+
msg_send![context, CGContext];
581+
let cgcontext_ref = CGContextRef::from_ptr_mut(cgcontext_ptr);
582+
584583
let rect = Rect::from_origin_size(
585584
(dirtyRect.origin.x, dirtyRect.origin.y),
586585
(dirtyRect.size.width, dirtyRect.size.height),
587586
);
588-
let cairo_surface =
589-
QuartzSurface::create_for_cg_context(cgcontext, width, height).expect("cairo surface");
590-
let mut cairo_ctx = Context::new(&cairo_surface);
591-
cairo_ctx.set_source_rgb(0.0, 0.5, 0.0);
592-
cairo_ctx.paint();
593-
let mut piet_ctx = Piet::new(&mut cairo_ctx);
587+
let mut piet_ctx = Piet::new_y_down(cgcontext_ref);
594588
let view_state: *mut c_void = *this.get_ivar("viewState");
595589
let view_state = &mut *(view_state as *mut ViewState);
596590
let anim = (*view_state).handler.paint(&mut piet_ctx, rect);

druid/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ console_log = "0.1.2"
5151
[dev-dependencies]
5252
float-cmp = { version = "0.6.0", default-features = false }
5353
tempfile = "3.1.0"
54-
piet-common = {version = "0.0.12", features = ["png"]}
54+
piet-common = {version = "0.1.0", features = ["png"]}

druid/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
441441
let text_rect = Rect::from_origin_size(origin, text_size);
442442

443443
ctx.fill(text_rect, &border_color);
444-
let (r, g, b, _) = border_color.as_rgba_u8();
444+
let (r, g, b, _) = border_color.as_rgba8();
445445
let avg = (r as u32 + g as u32 + b as u32) / 3;
446446
let text_color = if avg < 128 {
447447
Color::WHITE

0 commit comments

Comments
 (0)