Skip to content

Commit 21b661f

Browse files
gio: Add AppLaunchContext subclassing support
1 parent 1fddbfd commit 21b661f

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use std::ffi::c_char;
4+
5+
use crate::{subclass::prelude::*, AppInfo, AppLaunchContext, File};
6+
use glib::{prelude::*, translate::*, GString, List, Variant};
7+
8+
pub trait AppLaunchContextImpl: ObjectImpl {
9+
#[doc(alias = "get_display")]
10+
fn display(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
11+
self.parent_display(info, files)
12+
}
13+
#[doc(alias = "get_startup_notify_id")]
14+
fn startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
15+
self.parent_startup_notify_id(info, files)
16+
}
17+
fn launch_failed(&self, startup_notify_id: &str) {
18+
self.parent_launch_failed(startup_notify_id)
19+
}
20+
fn launch_started(&self, info: &AppInfo, platform_data: &Variant) {
21+
self.parent_launch_started(info, platform_data)
22+
}
23+
fn launched(&self, info: &AppInfo, platform_data: &Variant) {
24+
self.parent_launched(info, platform_data)
25+
}
26+
}
27+
28+
pub trait AppLaunchContextImplExt: ObjectSubclass {
29+
fn parent_display(&self, info: &AppInfo, files: List<File>) -> Option<GString>;
30+
fn parent_startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString>;
31+
fn parent_launch_failed(&self, startup_notify_id: &str);
32+
fn parent_launch_started(&self, info: &AppInfo, platform_data: &Variant);
33+
fn parent_launched(&self, info: &AppInfo, platform_data: &Variant);
34+
}
35+
36+
impl<T: AppLaunchContextImpl> AppLaunchContextImplExt for T {
37+
fn parent_display(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
38+
unsafe {
39+
let data = T::type_data();
40+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
41+
if let Some(f) = (*parent_class).get_display {
42+
Some(from_glib_full(f(
43+
self.obj()
44+
.unsafe_cast_ref::<AppLaunchContext>()
45+
.to_glib_none()
46+
.0,
47+
info.to_glib_none().0,
48+
files.as_ptr() as *mut _,
49+
)))
50+
} else {
51+
None
52+
}
53+
}
54+
}
55+
56+
fn parent_startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
57+
unsafe {
58+
let data = T::type_data();
59+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
60+
if let Some(f) = (*parent_class).get_startup_notify_id {
61+
Some(from_glib_full(f(
62+
self.obj()
63+
.unsafe_cast_ref::<AppLaunchContext>()
64+
.to_glib_none()
65+
.0,
66+
info.to_glib_none().0,
67+
files.as_ptr() as *mut _,
68+
)))
69+
} else {
70+
None
71+
}
72+
}
73+
}
74+
75+
fn parent_launch_failed(&self, startup_notify_id: &str) {
76+
unsafe {
77+
let data = T::type_data();
78+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
79+
if let Some(f) = (*parent_class).launch_failed {
80+
f(
81+
self.obj()
82+
.unsafe_cast_ref::<AppLaunchContext>()
83+
.to_glib_none()
84+
.0,
85+
startup_notify_id.to_glib_none().0,
86+
)
87+
}
88+
}
89+
}
90+
91+
fn parent_launch_started(&self, info: &AppInfo, platform_data: &Variant) {
92+
unsafe {
93+
let data = T::type_data();
94+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
95+
if let Some(f) = (*parent_class).launch_started {
96+
f(
97+
self.obj()
98+
.unsafe_cast_ref::<AppLaunchContext>()
99+
.to_glib_none()
100+
.0,
101+
info.to_glib_none().0,
102+
platform_data.to_glib_none().0,
103+
)
104+
}
105+
}
106+
}
107+
108+
fn parent_launched(&self, info: &AppInfo, platform_data: &Variant) {
109+
unsafe {
110+
let data = T::type_data();
111+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
112+
if let Some(f) = (*parent_class).launched {
113+
f(
114+
self.obj()
115+
.unsafe_cast_ref::<AppLaunchContext>()
116+
.to_glib_none()
117+
.0,
118+
info.to_glib_none().0,
119+
platform_data.to_glib_none().0,
120+
)
121+
}
122+
}
123+
}
124+
}
125+
126+
unsafe impl<T: AppLaunchContextImpl> IsSubclassable<T> for AppLaunchContext {
127+
fn class_init(class: &mut ::glib::Class<Self>) {
128+
Self::parent_class_init::<T>(class);
129+
130+
let klass = class.as_mut();
131+
klass.get_display = Some(app_launch_context_get_display::<T>);
132+
klass.get_startup_notify_id = Some(app_launch_context_get_startup_notify_id::<T>);
133+
klass.launch_failed = Some(app_launch_context_launch_failed::<T>);
134+
klass.launched = Some(app_launch_context_launched::<T>);
135+
klass.launch_started = Some(app_launch_context_launch_started::<T>);
136+
}
137+
}
138+
139+
unsafe extern "C" fn app_launch_context_get_display<T: AppLaunchContextImpl>(
140+
ptr: *mut ffi::GAppLaunchContext,
141+
infoptr: *mut ffi::GAppInfo,
142+
filesptr: *mut glib::ffi::GList,
143+
) -> *mut c_char {
144+
let instance = &*(ptr as *mut T::Instance);
145+
let imp = instance.imp();
146+
147+
imp.display(&from_glib_borrow(infoptr), List::from_glib_none(filesptr))
148+
.to_glib_full()
149+
}
150+
151+
unsafe extern "C" fn app_launch_context_get_startup_notify_id<T: AppLaunchContextImpl>(
152+
ptr: *mut ffi::GAppLaunchContext,
153+
infoptr: *mut ffi::GAppInfo,
154+
filesptr: *mut glib::ffi::GList,
155+
) -> *mut c_char {
156+
let instance = &*(ptr as *mut T::Instance);
157+
let imp = instance.imp();
158+
159+
imp.startup_notify_id(&from_glib_borrow(infoptr), List::from_glib_none(filesptr))
160+
.to_glib_full()
161+
}
162+
163+
unsafe extern "C" fn app_launch_context_launch_failed<T: AppLaunchContextImpl>(
164+
ptr: *mut ffi::GAppLaunchContext,
165+
startup_id: *const c_char,
166+
) {
167+
let instance = &*(ptr as *mut T::Instance);
168+
let imp = instance.imp();
169+
170+
imp.launch_failed(&GString::from_glib_borrow(startup_id))
171+
}
172+
173+
unsafe extern "C" fn app_launch_context_launched<T: AppLaunchContextImpl>(
174+
ptr: *mut ffi::GAppLaunchContext,
175+
infoptr: *mut ffi::GAppInfo,
176+
platform_ptr: *mut glib::ffi::GVariant,
177+
) {
178+
let instance = &*(ptr as *mut T::Instance);
179+
let imp = instance.imp();
180+
181+
imp.launched(&from_glib_borrow(infoptr), &from_glib_borrow(platform_ptr))
182+
}
183+
184+
unsafe extern "C" fn app_launch_context_launch_started<T: AppLaunchContextImpl>(
185+
ptr: *mut ffi::GAppLaunchContext,
186+
infoptr: *mut ffi::GAppInfo,
187+
platform_ptr: *mut glib::ffi::GVariant,
188+
) {
189+
let instance = &*(ptr as *mut T::Instance);
190+
let imp = instance.imp();
191+
192+
imp.launch_started(&from_glib_borrow(infoptr), &from_glib_borrow(platform_ptr))
193+
}

gio/src/subclass/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
mod action_group;
44
mod action_map;
5+
mod app_launch_context;
56
mod application;
67
mod async_initable;
78
mod initable;
@@ -20,6 +21,7 @@ pub mod prelude {
2021
pub use super::{
2122
action_group::{ActionGroupImpl, ActionGroupImplExt},
2223
action_map::{ActionMapImpl, ActionMapImplExt},
24+
app_launch_context::{AppLaunchContextImpl, AppLaunchContextImplExt},
2325
application::{ApplicationImpl, ApplicationImplExt},
2426
async_initable::{AsyncInitableImpl, AsyncInitableImplExt},
2527
initable::{InitableImpl, InitableImplExt},

0 commit comments

Comments
 (0)