Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Allow for GtkApplication to be subclasses
Browse files Browse the repository at this point in the history
This expands further upon the gio MR that does the same
for GApplictiona.

gtk-rs/gio#209
  • Loading branch information
alatiera committed May 12, 2019
1 parent 54d7de7 commit 21f3a4e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -37,6 +37,7 @@ purge-lgpl-docs = ["gtk-rs-lgpl-docs", "gdk/purge-lgpl-docs"]
embed-lgpl-docs = ["gtk-rs-lgpl-docs", "gdk/embed-lgpl-docs"]
dox = ["gdk/dox", "gtk-sys/dox"]
futures = ["futures-preview", "fragile", "gio/futures"]
subclassing = ["glib/subclassing", "gio/subclassing"]

[target.'cfg(target_os = "macos")'.build-dependencies]
cc = "^1.0"
Expand Down
1 change: 1 addition & 0 deletions Gir.toml
Expand Up @@ -426,6 +426,7 @@ status = "generate"
name = "Gtk.Application"
status = "generate"
trait_name = "GtkApplicationExt"
subclassing = true
manual_traits = ["gio::ApplicationExtManual"]
[[object.function]]
name = "new"
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Expand Up @@ -272,6 +272,10 @@ mod tree_store;
mod widget;
mod window;

#[macro_use]
#[cfg(feature = "subclassing")]
pub mod subclass;

pub mod prelude;

pub use auto::*;
Expand Down
81 changes: 81 additions & 0 deletions src/subclass/application.rs
@@ -0,0 +1,81 @@
use gtk_sys;

use glib::translate::*;

use glib::subclass::prelude::*;

use Application;
use ApplicationClass;
use Window;

pub trait ApplicationImpl: ApplicationImplExt + gio::subclass::prelude::ApplicationImpl + 'static {
fn window_added(&self, application: &Application, window: &Window) {
self.parent_window_added(application, window)
}

fn window_removed(&self, application: &Application, window: &Window) {
self.parent_window_removed(application, window)
}
}

pub trait ApplicationImplExt {
fn parent_window_added(&self, application: &Application, window: &Window);
fn parent_window_removed(&self, application: &Application, window: &Window);
}

impl<T: ApplicationImpl + ObjectImpl> ApplicationImplExt for T {
fn parent_window_added(&self, application: &Application, window: &Window) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut gtk_sys::GtkApplicationClass;
let f = (*parent_class)
.window_added
.expect("No parent class implementation for \"window_added\"");
f(application.to_glib_none().0, window.to_glib_none().0)
}
}

fn parent_window_removed(&self, application: &Application, window: &Window) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut gtk_sys::GtkApplicationClass;
let f = (*parent_class)
.window_removed
.expect("No parent class implementation for \"window_added\"");
f(application.to_glib_none().0, window.to_glib_none().0)
}
}
}

unsafe impl<T: ObjectSubclass + ApplicationImpl> IsSubclassable<T> for ApplicationClass {
fn override_vfuncs(&mut self) {
<gio::ApplicationClass as IsSubclassable<T>>::override_vfuncs(self);
unsafe {
let klass = &mut *(self as *mut Self as *mut gtk_sys::GtkApplicationClass);
klass.window_added = Some(application_window_added::<T>);
klass.window_removed = Some(application_window_removed::<T>);
}
}
}

unsafe extern "C" fn application_window_added<T: ObjectSubclass>(ptr: *mut gtk_sys::GtkApplication, wptr: *mut gtk_sys::GtkWindow)
where
T: ApplicationImpl,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Application = from_glib_borrow(ptr);

imp.window_added(&wrap, &from_glib_borrow(wptr))
}

unsafe extern "C" fn application_window_removed<T: ObjectSubclass>(ptr: *mut gtk_sys::GtkApplication, wptr: *mut gtk_sys::GtkWindow)
where
T: ApplicationImpl,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Application = from_glib_borrow(ptr);

imp.window_removed(&wrap, &from_glib_borrow(wptr))
}
5 changes: 5 additions & 0 deletions src/subclass/mod.rs
@@ -0,0 +1,5 @@
// Copyright 2019, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

pub mod application;

0 comments on commit 21f3a4e

Please sign in to comment.