Update drawing function of a DrawingArea

杀马特。学长 韩版系。学妹 提交于 2021-01-29 09:23:11

问题


I want to update a cairo drawing inside a DrawingArea. I tried to achieve this by calling DrawingArea::connect_draw(...) with a new function as the parameter. My issue is that it does not replace the original drawing function, but calls both when showing the window. Here is an example

extern crate cairo;
extern crate gio;
extern crate gtk;

use gio::prelude::*;
use gtk::prelude::*;

fn main() {
    let application = gtk::Application::new(Some("com.example"), Default::default())
        .expect("Initialization failed...");

    application.connect_activate(|app| {
        build_ui(app);
    });

    application.run(&vec![]);
}

fn build_ui(application: &gtk::Application) {
    let window = get_window(application);
    let drawing_area = Box::new(gtk::DrawingArea::new)();

    // Set drawing function
    drawing_area.connect_draw(|_, ctx| draw(ctx, 0.5, 2.0));
    // Change drawing function
    drawing_area.connect_draw(|_, ctx| draw(ctx, 0.9, 1.0)); // <-- Why is this not working as expected?

    window.add(&drawing_area);
    window.show_all();
}

fn get_window(application: &gtk::Application) -> gtk::ApplicationWindow {
    let window = gtk::ApplicationWindow::new(application);
    window.set_default_size(500i32, 500i32);

    // Set transparency
    set_visual(&window, None);
    window.connect_screen_changed(set_visual);
    window.set_app_paintable(true);

    window
}

fn draw(ctx: &cairo::Context, param1: f64, param2: f64) -> gtk::Inhibit {
    ctx.scale(500f64, 500f64);

    ctx.set_source_rgba(1.0, 0.2, 0.2, param1);
    ctx.arc(0.5, 0.5, 0.2, 0.0, 3.1414 * param2);
    ctx.fill();

    Inhibit(false)
}

fn set_visual(window: &gtk::ApplicationWindow, _screen: Option<&gdk::Screen>) {
    if let Some(screen) = window.get_screen() {
        if let Some(ref visual) = screen.get_rgba_visual() {
            window.set_visual(Some(visual));
        }
    }
}

I expect half a circle to be shown. Yet the old, full circle is still there, even though I tried to replace connect_draw. How can I properly replace this?


回答1:


Using the connect_draw function attaches another signal handler, but will not replace existing handlers. However, the function returns a SignalHandlerId which you should then be able to use to disonnect your original signal handler using the signal_handler_disconnect function (see https://gtk-rs.org/docs/glib/signal/fn.signal_handler_disconnect.html).



来源:https://stackoverflow.com/questions/58358008/update-drawing-function-of-a-drawingarea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!