Skip to content

Commit b270e5b

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

File tree

2 files changed

+194
-0
lines changed

2 files changed

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

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)