Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gio: Add subclassing support to AppLaunchContext & FileMonitor #1012

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions gio/src/subclass/app_launch_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ffi::c_char;

use crate::{subclass::prelude::*, AppInfo, AppLaunchContext, File};
use glib::{prelude::*, translate::*, GString, List, Variant};

pub trait AppLaunchContextImpl: ObjectImpl {
#[doc(alias = "get_display")]
fn display(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
self.parent_display(info, files)
}
#[doc(alias = "get_startup_notify_id")]
fn startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
self.parent_startup_notify_id(info, files)
}
fn launch_failed(&self, startup_notify_id: &str) {
self.parent_launch_failed(startup_notify_id)
}
fn launch_started(&self, info: &AppInfo, platform_data: &Variant) {
self.parent_launch_started(info, platform_data)
}
fn launched(&self, info: &AppInfo, platform_data: &Variant) {
self.parent_launched(info, platform_data)
}
}

pub trait AppLaunchContextImplExt: ObjectSubclass {
fn parent_display(&self, info: &AppInfo, files: List<File>) -> Option<GString>;
fn parent_startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString>;
fn parent_launch_failed(&self, startup_notify_id: &str);
fn parent_launch_started(&self, info: &AppInfo, platform_data: &Variant);
fn parent_launched(&self, info: &AppInfo, platform_data: &Variant);
}

impl<T: AppLaunchContextImpl> AppLaunchContextImplExt for T {
fn parent_display(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
(*parent_class).get_display.map(|f| {
from_glib_full(f(
self.obj()
.unsafe_cast_ref::<AppLaunchContext>()
.to_glib_none()
.0,
info.to_glib_none().0,
files.as_ptr() as *mut _,
))
})
}
}

fn parent_startup_notify_id(&self, info: &AppInfo, files: List<File>) -> Option<GString> {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
(*parent_class).get_startup_notify_id.map(|f| {
from_glib_full(f(
self.obj()
.unsafe_cast_ref::<AppLaunchContext>()
.to_glib_none()
.0,
info.to_glib_none().0,
files.as_ptr() as *mut _,
))
})
}
}

fn parent_launch_failed(&self, startup_notify_id: &str) {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
if let Some(f) = (*parent_class).launch_failed {
f(
self.obj()
.unsafe_cast_ref::<AppLaunchContext>()
.to_glib_none()
.0,
startup_notify_id.to_glib_none().0,
)
}
}
}

fn parent_launch_started(&self, info: &AppInfo, platform_data: &Variant) {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
if let Some(f) = (*parent_class).launch_started {
f(
self.obj()
.unsafe_cast_ref::<AppLaunchContext>()
.to_glib_none()
.0,
info.to_glib_none().0,
platform_data.to_glib_none().0,
)
}
}
}

fn parent_launched(&self, info: &AppInfo, platform_data: &Variant) {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GAppLaunchContextClass;
if let Some(f) = (*parent_class).launched {
f(
self.obj()
.unsafe_cast_ref::<AppLaunchContext>()
.to_glib_none()
.0,
info.to_glib_none().0,
platform_data.to_glib_none().0,
)
}
}
}
}

unsafe impl<T: AppLaunchContextImpl> IsSubclassable<T> for AppLaunchContext {
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

let klass = class.as_mut();
klass.get_display = Some(app_launch_context_get_display::<T>);
klass.get_startup_notify_id = Some(app_launch_context_get_startup_notify_id::<T>);
klass.launch_failed = Some(app_launch_context_launch_failed::<T>);
klass.launched = Some(app_launch_context_launched::<T>);
klass.launch_started = Some(app_launch_context_launch_started::<T>);
}
}

unsafe extern "C" fn app_launch_context_get_display<T: AppLaunchContextImpl>(
ptr: *mut ffi::GAppLaunchContext,
infoptr: *mut ffi::GAppInfo,
filesptr: *mut glib::ffi::GList,
) -> *mut c_char {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.display(&from_glib_borrow(infoptr), List::from_glib_none(filesptr))
.to_glib_full()
}

unsafe extern "C" fn app_launch_context_get_startup_notify_id<T: AppLaunchContextImpl>(
ptr: *mut ffi::GAppLaunchContext,
infoptr: *mut ffi::GAppInfo,
filesptr: *mut glib::ffi::GList,
) -> *mut c_char {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.startup_notify_id(&from_glib_borrow(infoptr), List::from_glib_none(filesptr))
.to_glib_full()
}

unsafe extern "C" fn app_launch_context_launch_failed<T: AppLaunchContextImpl>(
ptr: *mut ffi::GAppLaunchContext,
startup_id: *const c_char,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.launch_failed(&GString::from_glib_borrow(startup_id))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should maybe be a &GStr?

}

unsafe extern "C" fn app_launch_context_launched<T: AppLaunchContextImpl>(
ptr: *mut ffi::GAppLaunchContext,
infoptr: *mut ffi::GAppInfo,
platform_ptr: *mut glib::ffi::GVariant,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.launched(&from_glib_borrow(infoptr), &from_glib_borrow(platform_ptr))
}

unsafe extern "C" fn app_launch_context_launch_started<T: AppLaunchContextImpl>(
ptr: *mut ffi::GAppLaunchContext,
infoptr: *mut ffi::GAppInfo,
platform_ptr: *mut glib::ffi::GVariant,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.launch_started(&from_glib_borrow(infoptr), &from_glib_borrow(platform_ptr))
}
85 changes: 85 additions & 0 deletions gio/src/subclass/file_monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{subclass::prelude::*, File, FileMonitor, FileMonitorEvent};
use glib::{prelude::*, translate::*};

pub trait FileMonitorImpl: ObjectImpl {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually useful?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today it is not for sure. My intention is to cover up whatever APIs we can manually to have a better understanding of what is needed for an automatically generated of the subclassing code in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, is this actually intended to be subclasses? The API exposed here looks quite incomplete for implementing a new file monitor

fn changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent) {
self.parent_changed(file, other_file, event_type)
}
fn cancel(&self) -> bool {
self.parent_cancel()
}
}

pub trait FileMonitorImplExt: ObjectSubclass {
fn parent_changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent);
fn parent_cancel(&self) -> bool;
}

impl<T: FileMonitorImpl> FileMonitorImplExt for T {
fn parent_changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent) {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GFileMonitorClass;
if let Some(f) = (*parent_class).changed {
f(
self.obj().unsafe_cast_ref::<FileMonitor>().to_glib_none().0,
file.to_glib_none().0,
other_file.to_glib_none().0,
event_type.into_glib(),
)
}
}
}

fn parent_cancel(&self) -> bool {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GFileMonitorClass;
let f = (*parent_class)
.cancel
.expect("No parent class implementation for \"cancel\"");
from_glib(f(self
.obj()
.unsafe_cast_ref::<FileMonitor>()
.to_glib_none()
.0))
}
}
}

unsafe impl<T: FileMonitorImpl> IsSubclassable<T> for FileMonitor {
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

let klass = class.as_mut();
klass.changed = Some(file_monitor_changed::<T>);
klass.cancel = Some(file_monitor_cancel::<T>);
}
}

unsafe extern "C" fn file_monitor_changed<T: FileMonitorImpl>(
ptr: *mut ffi::GFileMonitor,
file: *mut ffi::GFile,
other_file: *mut ffi::GFile,
event: ffi::GFileMonitorEvent,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.changed(
&from_glib_borrow(file),
&from_glib_borrow(other_file),
from_glib(event),
)
}

unsafe extern "C" fn file_monitor_cancel<T: FileMonitorImpl>(
ptr: *mut ffi::GFileMonitor,
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.cancel().into_glib()
}
Loading