Loading

Paste #pi5lseqgv

  1. //! # Basic Subclass example
  2. //!
  3. //! This sample demonstrates how to create a toplevel `window`, set its title, size and
  4. //! position, how to add a `button` to this `window` and how to connect signals with
  5. //! actions.
  6.  
  7. #[macro_use]
  8. extern crate glib;
  9. extern crate gio;
  10. extern crate gtk;
  11.  
  12. use gio::prelude::*;
  13. use gtk::prelude::*;
  14.  
  15. use glib::subclass;
  16. use glib::subclass::prelude::*;
  17. use glib::translate::*;
  18.  
  19. use gio::ApplicationFlags;
  20.  
  21. use std::sync::Mutex;
  22.  
  23. #[derive(Debug)]
  24. struct State {
  25.     some_window: gtk::Window,
  26.     some_label: gtk::Label,
  27. }
  28.  
  29. pub struct SimpleApplicationPrivate {
  30.     state: Mutex<Option<State>>,
  31. }
  32.  
  33. impl ObjectSubclass for SimpleApplicationPrivate {
  34.     const NAME: &'static str = "SimpleApplicationPrivate";
  35.    type ParentType = gtk::Application;
  36.    type Instance = subclass::simple::InstanceStruct<Self>;
  37.    type Class = subclass::simple::ClassStruct<Self>;
  38.  
  39.    glib_object_subclass!();
  40.  
  41.    fn new() -> Self {
  42.        Self { state: Mutex::new(None), }
  43.    }
  44. }
  45.  
  46. impl ObjectImpl for SimpleApplicationPrivate {
  47.    glib_object_impl!();
  48. }
  49.  
  50. impl gio::subclass::prelude::ApplicationImpl for SimpleApplicationPrivate {
  51.    fn activate(&self, app: &gio::Application) {
  52.        let app = app.downcast_ref::<gtk::Application>().unwrap();
  53.        let window = gtk::ApplicationWindow::new(app);
  54.        window.present();
  55.        window.show_all();
  56.        println!("activated!");
  57.    }
  58. }
  59.  
  60. impl gtk::subclass::application::ApplicationImpl for SimpleApplicationPrivate {}
  61.  
  62. glib_wrapper! {
  63.    pub struct SimpleApplication(Object<subclass::simple::InstanceStruct<SimpleApplicationPrivate>, subclass::simple::ClassStruct<SimpleApplicationPrivate>, SimpleApplicationClass>) @extends gio::Application, gtk::Application;
  64.  
  65.    match fn {
  66.        get_type => || SimpleApplicationPrivate::get_type().to_glib(),
  67.    }
  68. }
  69.  
  70. fn main() {
  71.    let app = glib::Object::new(
  72.        SimpleApplication::static_type(),
  73.        &[
  74.            ("application-id", &"org.gtk-rs.SimpleApplication"),
  75.            ("flags", &ApplicationFlags::empty()),
  76.        ],
  77.    )
  78.    .unwrap()
  79.    // .downcast::<gtk::Application>()
  80.    .downcast::<SimpleApplication>()
  81.    .unwrap();
  82.  
  83.    let _priv = SimpleApplicationPrivate::from_instance(&app);
  84.  
  85.    app.set_inactivity_timeout(10000);
  86.  
  87.   let args: Vec<String> = std::env::args().collect();
  88.   ApplicationExtManual::run(&app, &args);
  89. }