Skip to content

Commit 32d36cb

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

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
(*parent_class).get_display.map(|f| {
42+
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+
})
51+
}
52+
}
53+
54+
fn parent_startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
55+
unsafe {
56+
let data = T::type_data();
57+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
58+
(*parent_class).get_startup_notify_id.map(|f| {
59+
from_glib_full(f(
60+
self.obj()
61+
.unsafe_cast_ref::<AppLaunchContext>()
62+
.to_glib_none()
63+
.0,
64+
info.to_glib_none().0,
65+
files.as_ptr() as *mut _,
66+
))
67+
})
68+
}
69+
}
70+
71+
fn parent_launch_failed(&self, startup_notify_id: &str) {
72+
unsafe {
73+
let data = T::type_data();
74+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
75+
if let Some(f) = (*parent_class).launch_failed {
76+
f(
77+
self.obj()
78+
.unsafe_cast_ref::<AppLaunchContext>()
79+
.to_glib_none()
80+
.0,
81+
startup_notify_id.to_glib_none().0,
82+
)
83+
}
84+
}
85+
}
86+
87+
fn parent_launch_started(&self, info: &AppInfo, platform_data: &Variant) {
88+
unsafe {
89+
let data = T::type_data();
90+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
91+
if let Some(f) = (*parent_class).launch_started {
92+
f(
93+
self.obj()
94+
.unsafe_cast_ref::<AppLaunchContext>()
95+
.to_glib_none()
96+
.0,
97+
info.to_glib_none().0,
98+
platform_data.to_glib_none().0,
99+
)
100+
}
101+
}
102+
}
103+
104+
fn parent_launched(&self, info: &AppInfo, platform_data: &Variant) {
105+
unsafe {
106+
let data = T::type_data();
107+
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
108+
if let Some(f) = (*parent_class).launched {
109+
f(
110+
self.obj()
111+
.unsafe_cast_ref::<AppLaunchContext>()
112+
.to_glib_none()
113+
.0,
114+
info.to_glib_none().0,
115+
platform_data.to_glib_none().0,
116+
)
117+
}
118+
}
119+
}
120+
}
121+
122+
unsafe impl<T: AppLaunchContextImpl> IsSubclassable<T> for AppLaunchContext {
123+
fn class_init(class: &mut ::glib::Class<Self>) {
124+
Self::parent_class_init::<T>(class);
125+
126+
let klass = class.as_mut();
127+
klass.get_display = Some(app_launch_context_get_display::<T>);
128+
klass.get_startup_notify_id = Some(app_launch_context_get_startup_notify_id::<T>);
129+
klass.launch_failed = Some(app_launch_context_launch_failed::<T>);
130+
klass.launched = Some(app_launch_context_launched::<T>);
131+
klass.launch_started = Some(app_launch_context_launch_started::<T>);
132+
}
133+
}
134+
135+
unsafe extern "C" fn app_launch_context_get_display<T: AppLaunchContextImpl>(
136+
ptr: *mut ffi::GAppLaunchContext,
137+
infoptr: *mut ffi::GAppInfo,
138+
filesptr: *mut glib::ffi::GList,
139+
) -> *mut c_char {
140+
let instance = &*(ptr as *mut T::Instance);
141+
let imp = instance.imp();
142+
143+
imp.display(&from_glib_borrow(infoptr), List::from_glib_none(filesptr))
144+
.to_glib_full()
145+
}
146+
147+
unsafe extern "C" fn app_launch_context_get_startup_notify_id<T: AppLaunchContextImpl>(
148+
ptr: *mut ffi::GAppLaunchContext,
149+
infoptr: *mut ffi::GAppInfo,
150+
filesptr: *mut glib::ffi::GList,
151+
) -> *mut c_char {
152+
let instance = &*(ptr as *mut T::Instance);
153+
let imp = instance.imp();
154+
155+
imp.startup_notify_id(&from_glib_borrow(infoptr), List::from_glib_none(filesptr))
156+
.to_glib_full()
157+
}
158+
159+
unsafe extern "C" fn app_launch_context_launch_failed<T: AppLaunchContextImpl>(
160+
ptr: *mut ffi::GAppLaunchContext,
161+
startup_id: *const c_char,
162+
) {
163+
let instance = &*(ptr as *mut T::Instance);
164+
let imp = instance.imp();
165+
166+
imp.launch_failed(&GString::from_glib_borrow(startup_id))
167+
}
168+
169+
unsafe extern "C" fn app_launch_context_launched<T: AppLaunchContextImpl>(
170+
ptr: *mut ffi::GAppLaunchContext,
171+
infoptr: *mut ffi::GAppInfo,
172+
platform_ptr: *mut glib::ffi::GVariant,
173+
) {
174+
let instance = &*(ptr as *mut T::Instance);
175+
let imp = instance.imp();
176+
177+
imp.launched(&from_glib_borrow(infoptr), &from_glib_borrow(platform_ptr))
178+
}
179+
180+
unsafe extern "C" fn app_launch_context_launch_started<T: AppLaunchContextImpl>(
181+
ptr: *mut ffi::GAppLaunchContext,
182+
infoptr: *mut ffi::GAppInfo,
183+
platform_ptr: *mut glib::ffi::GVariant,
184+
) {
185+
let instance = &*(ptr as *mut T::Instance);
186+
let imp = instance.imp();
187+
188+
imp.launch_started(&from_glib_borrow(infoptr), &from_glib_borrow(platform_ptr))
189+
}

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)