2
2
mod imp;
3
3
4
4
pub use self :: imp:: PinnedFuture ;
5
- use gtk:: glib:: {
6
- self ,
5
+ use gtk:: {
6
+ glib:: { self , subclass:: prelude:: * } ,
7
+ prelude:: * ,
7
8
subclass:: prelude:: * ,
8
- translate:: { from_glib_borrow, ToGlibPtr } ,
9
9
} ;
10
- use gtk:: { prelude:: * , subclass:: prelude:: * } ;
11
10
12
11
glib:: wrapper! {
13
12
pub struct BaseButton ( ObjectSubclass <imp:: BaseButton >)
14
13
@extends gtk:: Widget , gtk:: Button ;
15
14
}
16
15
16
+ impl BaseButton {
17
+ pub fn new ( ) -> Self {
18
+ glib:: Object :: new ( & [ ] ) . expect ( "Failed to create BaseButton" )
19
+ }
20
+ }
21
+
22
+ impl Default for BaseButton {
23
+ fn default ( ) -> Self {
24
+ Self :: new ( )
25
+ }
26
+ }
27
+
17
28
/// Public trait that implements our functions for everything that derives from BaseButton
18
29
pub trait BaseButtonExt {
19
- fn sync_method ( & self , extra_text : Box < Option < String > > ) ;
30
+ fn sync_method ( & self , extra_text : Option < String > ) ;
20
31
fn async_method ( & self ) -> PinnedFuture ;
21
32
}
22
33
23
34
/// We call into imp::BaseButton_$method_name for each function. These will retrieve the
24
35
/// correct class (the base class for the BaseButton or the derived class for DerivedButton)
25
36
/// and call the correct implementation of the function.
26
37
impl < O : IsA < BaseButton > > BaseButtonExt for O {
27
- fn sync_method ( & self , extra_text : Box < Option < String > > ) {
28
- unsafe {
29
- imp:: base_button_sync_method ( self . as_ref ( ) . to_glib_none ( ) . 0 , Box :: into_raw ( extra_text) )
30
- }
38
+ fn sync_method ( & self , extra_text : Option < String > ) {
39
+ unsafe { imp:: base_button_sync_method ( self . upcast_ref :: < BaseButton > ( ) , extra_text) }
31
40
}
32
41
33
42
fn async_method ( & self ) -> PinnedFuture {
34
- unsafe { imp:: base_button_async_method ( self . as_ref ( ) . to_glib_none ( ) . 0 ) }
43
+ unsafe { imp:: base_button_async_method ( self . upcast_ref :: < BaseButton > ( ) ) }
35
44
}
36
45
}
37
46
38
47
/// The BaseButtonImpl that each derived private struct has to implement. See derived_button/imp.rs for how
39
48
/// to override functions.
40
49
pub trait BaseButtonImpl : ButtonImpl + ObjectImpl + ' static {
41
- fn sync_method ( & self , obj : & BaseButton , extra_text : Box < Option < String > > ) {
50
+ fn sync_method ( & self , obj : & BaseButton , extra_text : Option < String > ) {
42
51
self . parent_sync_method ( obj, extra_text)
43
52
}
44
53
45
54
fn async_method ( & self , obj : & BaseButton ) -> PinnedFuture {
46
55
self . parent_async_method ( obj)
47
56
}
57
+ }
48
58
49
- fn parent_sync_method ( & self , obj : & BaseButton , extra_text : Box < Option < String > > ) {
59
+ pub trait BaseButtonImplExt : ObjectSubclass {
60
+ fn parent_sync_method ( & self , obj : & BaseButton , extra_text : Option < String > ) ;
61
+ fn parent_async_method ( & self , obj : & BaseButton ) -> PinnedFuture ;
62
+ }
63
+
64
+ impl < T : BaseButtonImpl > BaseButtonImplExt for T {
65
+ fn parent_sync_method ( & self , obj : & BaseButton , extra_text : Option < String > ) {
50
66
unsafe {
51
67
let data = Self :: type_data ( ) ;
52
68
let parent_class = data. as_ref ( ) . parent_class ( ) as * mut imp:: BaseButtonClass ;
53
69
if let Some ( ref f) = ( * parent_class) . sync_method {
54
- f ( obj. to_glib_none ( ) . 0 , Box :: into_raw ( extra_text) )
70
+ f ( obj, extra_text)
55
71
} else {
56
72
unimplemented ! ( )
57
73
}
@@ -63,7 +79,7 @@ pub trait BaseButtonImpl: ButtonImpl + ObjectImpl + 'static {
63
79
let data = Self :: type_data ( ) ;
64
80
let parent_class = data. as_ref ( ) . parent_class ( ) as * mut imp:: BaseButtonClass ;
65
81
if let Some ( ref f) = ( * parent_class) . async_method {
66
- f ( obj. to_glib_none ( ) . 0 )
82
+ f ( obj)
67
83
} else {
68
84
unimplemented ! ( )
69
85
}
@@ -74,7 +90,7 @@ pub trait BaseButtonImpl: ButtonImpl + ObjectImpl + 'static {
74
90
/// Make the BaseButton subclassable
75
91
unsafe impl < T : BaseButtonImpl > IsSubclassable < T > for BaseButton {
76
92
fn class_init ( class : & mut glib:: Class < Self > ) {
77
- <gtk:: Widget as IsSubclassable < T > >:: class_init ( class) ;
93
+ <gtk:: Button as IsSubclassable < T > >:: class_init ( class. upcast_ref_mut ( ) ) ;
78
94
79
95
let klass = class. as_mut ( ) ;
80
96
klass. sync_method = Some ( sync_method_trampoline :: < T > ) ;
@@ -86,25 +102,21 @@ unsafe impl<T: BaseButtonImpl> IsSubclassable<T> for BaseButton {
86
102
}
87
103
}
88
104
89
- // Virtual method default implementation trampolines
90
- unsafe extern "C" fn sync_method_trampoline < T : ObjectSubclass > (
91
- this : * mut imp:: BaseButtonInstance ,
92
- extra_text : * mut Option < String > ,
93
- ) where
94
- T : BaseButtonImpl ,
105
+ // Virtual method implementation trampolines
106
+ unsafe fn sync_method_trampoline < T > ( this : & BaseButton , extra_text : Option < String > )
107
+ where
108
+ T : ObjectSubclass + BaseButtonImpl ,
95
109
{
96
- let instance = & * ( this as * const T :: Instance ) ;
110
+ let instance = & * ( this as * const _ as * const T :: Instance ) ;
97
111
let imp = instance. impl_ ( ) ;
98
- imp. sync_method ( & from_glib_borrow ( this) , Box :: from_raw ( extra_text) )
112
+ imp. sync_method ( this, extra_text)
99
113
}
100
114
101
- unsafe extern "C" fn async_method_trampoline < T : ObjectSubclass > (
102
- this : * mut imp:: BaseButtonInstance ,
103
- ) -> PinnedFuture
115
+ unsafe fn async_method_trampoline < T > ( this : & BaseButton ) -> PinnedFuture
104
116
where
105
- T : BaseButtonImpl ,
117
+ T : ObjectSubclass + BaseButtonImpl ,
106
118
{
107
- let instance = & * ( this as * const T :: Instance ) ;
119
+ let instance = & * ( this as * const _ as * const T :: Instance ) ;
108
120
let imp = instance. impl_ ( ) ;
109
- imp. async_method ( & from_glib_borrow ( this) )
121
+ imp. async_method ( this)
110
122
}
0 commit comments