From 275fc44793b58fded1c3261a8994de5d793ce086 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Mon, 16 Oct 2023 18:04:12 +0200 Subject: [PATCH] examples: Document/Replace deprecated APIs --- .../gif_paintable/gif_paintable_window/imp.rs | 36 +++---------------- .../gif_paintable/gif_paintable_window/mod.rs | 21 +++++++++++ examples/list_box_model/list_box_row/imp.rs | 3 ++ examples/list_box_model/main.rs | 3 ++ 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/examples/gif_paintable/gif_paintable_window/imp.rs b/examples/gif_paintable/gif_paintable_window/imp.rs index e4668dc28cd2..33c3b256520f 100644 --- a/examples/gif_paintable/gif_paintable_window/imp.rs +++ b/examples/gif_paintable/gif_paintable_window/imp.rs @@ -1,11 +1,10 @@ -use gtk::{glib, prelude::*, subclass::prelude::*}; +use gtk::{glib, subclass::prelude::*}; -#[derive(Debug, gtk::CompositeTemplate)] +#[derive(Default, Debug, gtk::CompositeTemplate)] #[template(file = "gif_paintable_window.ui")] pub struct GifPaintableWindow { #[template_child] pub picture: TemplateChild, - pub dialog: gtk::FileChooserNative, } #[glib::object_subclass] @@ -14,40 +13,15 @@ impl ObjectSubclass for GifPaintableWindow { type Type = super::GifPaintableWindow; type ParentType = gtk::ApplicationWindow; - fn new() -> Self { - let gif_filter = gtk::FileFilter::new(); - gif_filter.add_mime_type("image/gif"); - gif_filter.set_name(Some("GIF Image")); - - let dialog = gtk::FileChooserNative::builder() - .title("Open File") - .action(gtk::FileChooserAction::Open) - .accept_label("Open") - .cancel_label("Cancel") - .modal(true) - .build(); - - dialog.add_filter(&gif_filter); - - Self { - dialog, - picture: TemplateChild::default(), - } - } - fn class_init(klass: &mut Self::Class) { klass.bind_template(); klass.install_action_async( "win.open", None, |win, _action_name, _action_target| async move { - let dialog = &win.imp().dialog; - dialog.set_transient_for(Some(&win)); - if dialog.run_future().await == gtk::ResponseType::Accept { - if let Err(error) = win.set_file(dialog.file().unwrap()) { - println!("Error loading the GIF: {error}"); - } - } + if let Err(error) = win.open_file().await { + println!("Error loading the GIF: {error}"); + }; }, ); } diff --git a/examples/gif_paintable/gif_paintable_window/mod.rs b/examples/gif_paintable/gif_paintable_window/mod.rs index 86a5851cfc28..c39944eb462f 100644 --- a/examples/gif_paintable/gif_paintable_window/mod.rs +++ b/examples/gif_paintable/gif_paintable_window/mod.rs @@ -15,6 +15,27 @@ impl GifPaintableWindow { glib::Object::builder().property("application", app).build() } + pub async fn open_file(&self) -> Result<(), Box> { + let gif_filter = gtk::FileFilter::new(); + gif_filter.add_mime_type("image/gif"); + gif_filter.set_name(Some("GIF Image")); + + let filters = gio::ListStore::new::(); + filters.append(&gif_filter); + + let dialog = gtk::FileDialog::builder() + .title("Open File") + .accept_label("Open") + .modal(true) + .filters(&filters) + .build(); + + let file = dialog.open_future(Some(self)).await?; + self.set_file(file)?; + + Ok(()) + } + fn set_file(&self, file: gio::File) -> Result<(), Box> { let paintable = GifPaintable::default(); let (bytes, _) = file.load_contents(gio::Cancellable::NONE)?; diff --git a/examples/list_box_model/list_box_row/imp.rs b/examples/list_box_model/list_box_row/imp.rs index 108cee681907..ff74d00d817e 100644 --- a/examples/list_box_model/list_box_row/imp.rs +++ b/examples/list_box_model/list_box_row/imp.rs @@ -1,3 +1,6 @@ +// gtk::Dialog was deprecated and applications are supposed +// to use plain gtk::Window and structure it however they wish. +#![allow(deprecated)] use std::cell::RefCell; use gtk::{ diff --git a/examples/list_box_model/main.rs b/examples/list_box_model/main.rs index 71924452e537..cb11b7ab017d 100644 --- a/examples/list_box_model/main.rs +++ b/examples/list_box_model/main.rs @@ -1,3 +1,6 @@ +// gtk::Dialog was deprecated and applications are supposed +// to use plain gtk::Window and structure it however they wish. +#![allow(deprecated)] mod list_box_row; mod model; pub mod row_data;