This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for GtkApplication to be subclasses
This expands further upon the gio MR that does the same for GApplictiona. gtk-rs/gio#209
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |