relm4/typed_view/mod.rs
1//! Typed views.
2
3pub mod column;
4pub mod grid;
5mod iterator;
6pub mod list;
7mod selection_ext;
8
9pub use self::selection_ext::RelmSelectionExt;
10use gtk::{glib, prelude::Cast};
11use std::{
12 cell::{Ref, RefMut},
13 cmp::Ordering,
14 marker::PhantomData,
15};
16
17pub use self::iterator::TypedIterator;
18
19/// Sorting function used for views.
20pub type OrdFn<T> = Option<Box<dyn Fn(&T, &T) -> Ordering>>;
21
22struct Filter {
23 filter: gtk::CustomFilter,
24 model: gtk::FilterListModel,
25}
26/// And item of a [`list::TypedListView`].
27///
28/// The interface is very similar to [`std::cell::RefCell`].
29/// Ownership is calculated at runtime, so you have to borrow the
30/// value explicitly which might panic if done incorrectly.
31#[derive(Debug, Clone)]
32pub struct TypedListItem<T> {
33 inner: glib::BoxedAnyObject,
34 _ty: PhantomData<*const T>,
35}
36
37impl<T: 'static> TypedListItem<T> {
38 fn new(inner: glib::BoxedAnyObject) -> Self {
39 Self {
40 inner,
41 _ty: PhantomData,
42 }
43 }
44
45 // rustdoc-stripper-ignore-next
46 /// Immutably borrows the wrapped value.
47 ///
48 /// The borrow lasts until the returned `Ref` exits scope. Multiple
49 /// immutable borrows can be taken out at the same time.
50 ///
51 /// # Panics
52 ///
53 /// Panics if the value is currently mutably borrowed.
54 ///
55 /// For a non-panicking variant, use
56 /// [`try_borrow`](#method.try_borrow).
57 #[must_use]
58 pub fn borrow(&self) -> Ref<'_, T> {
59 self.inner.borrow()
60 }
61
62 // rustdoc-stripper-ignore-next
63 /// Mutably borrows the wrapped value.
64 ///
65 /// The borrow lasts until the returned `RefMut` or all `RefMut`s derived
66 /// from it exit scope. The value cannot be borrowed while this borrow is
67 /// active.
68 ///
69 /// # Panics
70 ///
71 /// Panics if the value is currently borrowed.
72 ///
73 /// For a non-panicking variant, use
74 /// [`try_borrow_mut`](#method.try_borrow_mut).
75 #[must_use]
76 pub fn borrow_mut(&self) -> RefMut<'_, T> {
77 self.inner.borrow_mut()
78 }
79}
80
81fn get_value<T: 'static>(obj: &glib::Object) -> Ref<'_, T> {
82 let wrapper = obj.downcast_ref::<glib::BoxedAnyObject>().unwrap();
83 wrapper.borrow()
84}
85
86fn get_mut_value<T: 'static>(obj: &glib::Object) -> RefMut<'_, T> {
87 let wrapper = obj.downcast_ref::<glib::BoxedAnyObject>().unwrap();
88 wrapper.borrow_mut()
89}