Skip to content

Commit 2fb69cd

Browse files
gio: Add FileMonitor subclassing support
1 parent b270e5b commit 2fb69cd

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

gio/src/subclass/file_monitor.rs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::{subclass::prelude::*, File, FileMonitor, FileMonitorEvent};
4+
use glib::{prelude::*, translate::*};
5+
6+
pub trait FileMonitorImpl: ObjectImpl {
7+
fn changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent) {
8+
self.parent_changed(file, other_file, event_type)
9+
}
10+
fn cancel(&self) -> bool {
11+
self.parent_cancel()
12+
}
13+
}
14+
15+
pub trait FileMonitorImplExt: ObjectSubclass {
16+
fn parent_changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent);
17+
fn parent_cancel(&self) -> bool;
18+
}
19+
20+
impl<T: FileMonitorImpl> FileMonitorImplExt for T {
21+
fn parent_changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent) {
22+
unsafe {
23+
let data = T::type_data();
24+
let parent_class = data.as_ref().parent_class() as *mut ffi::GFileMonitorClass;
25+
if let Some(f) = (*parent_class).changed {
26+
f(
27+
self.obj().unsafe_cast_ref::<FileMonitor>().to_glib_none().0,
28+
file.to_glib_none().0,
29+
other_file.to_glib_none().0,
30+
event_type.into_glib(),
31+
)
32+
}
33+
}
34+
}
35+
36+
fn parent_cancel(&self) -> bool {
37+
unsafe {
38+
let data = T::type_data();
39+
let parent_class = data.as_ref().parent_class() as *mut ffi::GFileMonitorClass;
40+
let f = (*parent_class)
41+
.cancel
42+
.expect("No parent class implementation for \"cancel\"");
43+
from_glib(f(self
44+
.obj()
45+
.unsafe_cast_ref::<FileMonitor>()
46+
.to_glib_none()
47+
.0))
48+
}
49+
}
50+
}
51+
52+
unsafe impl<T: FileMonitorImpl> IsSubclassable<T> for FileMonitor {
53+
fn class_init(class: &mut ::glib::Class<Self>) {
54+
Self::parent_class_init::<T>(class);
55+
56+
let klass = class.as_mut();
57+
klass.changed = Some(file_monitor_changed::<T>);
58+
klass.cancel = Some(file_monitor_cancel::<T>);
59+
}
60+
}
61+
62+
unsafe extern "C" fn file_monitor_changed<T: FileMonitorImpl>(
63+
ptr: *mut ffi::GFileMonitor,
64+
file: *mut ffi::GFile,
65+
other_file: *mut ffi::GFile,
66+
event: ffi::GFileMonitorEvent,
67+
) {
68+
let instance = &*(ptr as *mut T::Instance);
69+
let imp = instance.imp();
70+
71+
imp.changed(
72+
&from_glib_borrow(file),
73+
&from_glib_borrow(other_file),
74+
from_glib(event),
75+
)
76+
}
77+
78+
unsafe extern "C" fn file_monitor_cancel<T: FileMonitorImpl>(
79+
ptr: *mut ffi::GFileMonitor,
80+
) -> glib::ffi::gboolean {
81+
let instance = &*(ptr as *mut T::Instance);
82+
let imp = instance.imp();
83+
84+
imp.cancel().into_glib()
85+
}

gio/src/subclass/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod action_map;
55
mod app_launch_context;
66
mod application;
77
mod async_initable;
8+
mod file_monitor;
89
mod initable;
910
mod input_stream;
1011
mod io_stream;
@@ -24,6 +25,7 @@ pub mod prelude {
2425
app_launch_context::{AppLaunchContextImpl, AppLaunchContextImplExt},
2526
application::{ApplicationImpl, ApplicationImplExt},
2627
async_initable::{AsyncInitableImpl, AsyncInitableImplExt},
28+
file_monitor::{FileMonitorImpl, FileMonitorImplExt},
2729
initable::{InitableImpl, InitableImplExt},
2830
input_stream::{InputStreamImpl, InputStreamImplExt},
2931
io_stream::{IOStreamImpl, IOStreamImplExt},

0 commit comments

Comments
 (0)