Hello,
I try to use plotters-cairo with plotters-rs and gtk-rs in a gtk4 app. I made a minimal example to showcase the problem:
Cargo.toml
name = "cairotest"
version = "0.1.0"
edition = "2021"
[dependencies]
gtk = { version = "0.5.2", package = "gtk4", features = ["v4_8"] }
plotters = "0.3.4"
plotters-cairo = {git = "https://github.com/plotters-rs/plotters-cairo.git",rev ="51326436fc6a001a2084327a3c58d6f365be858c" }
main.rs
use plotters::backend::DrawingBackend;
use plotters::drawing::IntoDrawingArea;
use plotters_cairo::CairoBackend;
fn main() {
let app = Application::builder().application_id("lambda").build();
let builder = gtk::Builder::from_file("file.ui");
let window = builder.object::<gtk::Window>("MainWindow").unwrap();
window.set_application(Some(&app));
window.set_title(Some("Cairo test App"));
let drawing_area: gtk::DrawingArea = builder.object("MainDrawingArea").unwrap();
drawing_area.set_draw_func(move |widget, cr, w, h| {
let backend = CairoBackend::new(cr, (w as u32, h as u32)).unwrap();
do_with_backend(backend);
});
}
fn do_with_backend<'a, DB>(backend: DB)
where
DB: DrawingBackend + 'a,
{
let drawing_area = backend.into_drawing_area();
todo!()
}
At compile time I get the 2 followings errors:
Error 1
--> src/main.rs:18:41
|
18 | let backend = CairoBackend::new(cr, (w as u32, h as u32)).unwrap();
| ----------------- ^^ expected struct `cairo::context::Context`, found struct `gtk4::cairo::Context`
| |
| arguments to this function are incorrect
|
= note: struct `gtk4::cairo::Context` and struct `cairo::context::Context` have similar names, but are actually distinct types
note: struct `gtk4::cairo::Context` is defined in crate `cairo`
--> /home/joceran/.cargo/registry/src/github.com-1ecc6299db9ec823/cairo-rs-0.16.3/src/context.rs:72:1
|
72 | pub struct Context(ptr::NonNull<cairo_t>);
| ^^^^^^^^^^^^^^^^^^
note: struct `cairo::context::Context` is defined in crate `cairo`
--> /home/joceran/.cargo/registry/src/github.com-1ecc6299db9ec823/cairo-rs-0.15.12/src/context.rs:72:1
|
72 | pub struct Context(ptr::NonNull<cairo_t>);
| ^^^^^^^^^^^^^^^^^^
= note: perhaps two different versions of crate `cairo` are being used?
note: associated function defined here
--> /home/joceran/.cargo/git/checkouts/plotters-cairo-14b56b08bd22bd2d/5132643/src/backend.rs:70:12
|
70 | pub fn new(context: &'a CairoContext, (w, h): (u32, u32)) -> Result<Self, CairoError> {
| ^^^
It looks like Gtk4 cairo::Context isn't the same as the cairo::Context referred by plotters-cairo.
Error 2
error[E0277]: the trait bound `CairoBackend<'_>: DrawingBackend` is not satisfied
--> src/main.rs:20:25
|
20 | do_with_backend(backend);
| --------------- ^^^^^^^ the trait `DrawingBackend` is not implemented for `CairoBackend<'_>`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `DrawingBackend`:
BitMapBackend<'a, P>
SVGBackend<'a>
note: required by a bound in `do_with_backend`
--> src/main.rs:26:9
|
24 | fn do_with_backend<'a, DB>(backend: DB)
| --------------- required by a bound in this
25 | where
26 | DB: DrawingBackend + 'a,
| ^^^^^^^^^^^^^^ required by this bound in `do_with_backend`
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `cairotest` due to 2 previous errors
The DrawingBackend is in scope and I checked cairo-plotters backend.rs : DrawingBackend trait is implemented but somehow is unreachable.
I suppose that plotters-rs and cairo-plotters aren't compatible with gtk4 for now, or I am just mistaken and do something wrong?