Skip to content

Commit

Permalink
examples: Document/Replace deprecated APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Oct 16, 2023
1 parent 84bfe7c commit 275fc44
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
36 changes: 5 additions & 31 deletions examples/gif_paintable/gif_paintable_window/imp.rs
Original file line number Diff line number Diff line change
@@ -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<gtk::Picture>,
pub dialog: gtk::FileChooserNative,
}

#[glib::object_subclass]
Expand All @@ -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}");
};
},
);
}
Expand Down
21 changes: 21 additions & 0 deletions examples/gif_paintable/gif_paintable_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ impl GifPaintableWindow {
glib::Object::builder().property("application", app).build()
}

pub async fn open_file(&self) -> Result<(), Box<dyn std::error::Error>> {
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::<gtk::FileFilter>();
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<dyn std::error::Error>> {
let paintable = GifPaintable::default();
let (bytes, _) = file.load_contents(gio::Cancellable::NONE)?;
Expand Down
3 changes: 3 additions & 0 deletions examples/list_box_model/list_box_row/imp.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
3 changes: 3 additions & 0 deletions examples/list_box_model/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 275fc44

Please sign in to comment.