Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit 4d40ac2

Browse files
Merge pull request #804 from nacho/nacho/toggle_button
Allow subclassing ToggleButton and MenuButton
2 parents 6f27d77 + 8345776 commit 4d40ac2

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

gtk/src/subclass/menu_button.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use glib::subclass::prelude::*;
4+
5+
use super::toggle_button::ToggleButtonImpl;
6+
use crate::MenuButton;
7+
8+
pub trait MenuButtonImpl: MenuButtonImplExt + ToggleButtonImpl {}
9+
10+
pub trait MenuButtonImplExt: ObjectSubclass {}
11+
12+
impl<T: MenuButtonImpl> MenuButtonImplExt for T {}
13+
14+
unsafe impl<T: MenuButtonImpl> IsSubclassable<T> for MenuButton {
15+
fn class_init(class: &mut glib::Class<Self>) {
16+
Self::parent_class_init::<T>(class);
17+
}
18+
}

gtk/src/subclass/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ pub mod header_bar;
2424
pub mod icon_view;
2525
pub mod list_box;
2626
pub mod list_box_row;
27+
pub mod menu_button;
2728
#[cfg(any(gdk_backend = "x11", feature = "dox"))]
2829
pub mod plug;
2930
pub mod scrolled_window;
3031
#[cfg(any(gdk_backend = "x11", feature = "dox"))]
3132
pub mod socket;
3233
pub mod stack;
34+
pub mod toggle_button;
3335
pub mod tree_view;
3436
pub mod widget;
3537
pub mod window;
@@ -64,12 +66,14 @@ pub mod prelude {
6466
pub use super::icon_view::{IconViewImpl, IconViewImplExt};
6567
pub use super::list_box::{ListBoxImpl, ListBoxImplExt};
6668
pub use super::list_box_row::{ListBoxRowImpl, ListBoxRowImplExt};
69+
pub use super::menu_button::MenuButtonImpl;
6770
#[cfg(any(gdk_backend = "x11", feature = "dox"))]
6871
pub use super::plug::{PlugImpl, PlugImplExt};
6972
pub use super::scrolled_window::{ScrolledWindowImpl, ScrolledWindowImplExt};
7073
#[cfg(any(gdk_backend = "x11", feature = "dox"))]
7174
pub use super::socket::{SocketImpl, SocketImplExt};
7275
pub use super::stack::StackImpl;
76+
pub use super::toggle_button::ToggleButtonImpl;
7377
pub use super::tree_view::TreeViewImpl;
7478
pub use super::widget::{
7579
CompositeTemplate, TemplateChild, WidgetClassSubclassExt, WidgetImpl, WidgetImplExt,

gtk/src/subclass/toggle_button.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use glib::subclass::prelude::*;
4+
use glib::translate::*;
5+
use glib::Cast;
6+
7+
use super::button::ButtonImpl;
8+
use crate::ToggleButton;
9+
10+
pub trait ToggleButtonImpl: ToggleButtonImplExt + ButtonImpl {
11+
fn toggled(&self) {
12+
self.parent_toggled()
13+
}
14+
}
15+
16+
pub trait ToggleButtonImplExt: ObjectSubclass {
17+
fn parent_toggled(&self);
18+
}
19+
20+
impl<T: ToggleButtonImpl> ToggleButtonImplExt for T {
21+
fn parent_toggled(&self) {
22+
unsafe {
23+
let data = T::type_data();
24+
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkToggleButtonClass;
25+
if let Some(f) = (*parent_class).toggled {
26+
f(self
27+
.obj()
28+
.unsafe_cast_ref::<ToggleButton>()
29+
.to_glib_none()
30+
.0)
31+
}
32+
}
33+
}
34+
}
35+
36+
unsafe impl<T: ToggleButtonImpl> IsSubclassable<T> for ToggleButton {
37+
fn class_init(class: &mut glib::Class<Self>) {
38+
Self::parent_class_init::<T>(class);
39+
40+
let klass = class.as_mut();
41+
klass.toggled = Some(button_toggled::<T>);
42+
}
43+
}
44+
45+
unsafe extern "C" fn button_toggled<T: ToggleButtonImpl>(ptr: *mut ffi::GtkToggleButton) {
46+
let instance = &*(ptr as *mut T::Instance);
47+
let imp = instance.imp();
48+
49+
imp.toggled()
50+
}

0 commit comments

Comments
 (0)