Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.log
/build
/.vscode
/.idea
/.idea
*.swo
32 changes: 29 additions & 3 deletions examples/event_bus/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ pub mod events;
pub mod widget;

use events::{EventBus, Events};
use tmui::{application::Application, application_window::ApplicationWindow, widget::ChildOp};
use tmui::{
application::Application,
application_window::ApplicationWindow,
button::Button,
hbox::HBox,
prelude::{ContainerImplExt, ObjectOperation},
widget::{callbacks::CallbacksRegister, ChildOp},
};
use widget::WidgetEvents;

fn main() {
Expand All @@ -21,9 +28,28 @@ fn main() {
}

fn build_ui(window: &mut ApplicationWindow) {
window.child(WidgetEvents::new());
let w1 = WidgetEvents::new();
let id = w1.id();
let w2 = WidgetEvents::new();

window.register_run_after(|_| {
let mut button = Button::new(Some("Emit event"));
button.register_mouse_released(|_, _| {
EventBus::push(Events::Test);
});

let mut hbox = HBox::new();
hbox.add_child(button);
hbox.add_child(w1);
hbox.add_child(w2);
let hbox_id = hbox.id();
window.child(hbox);

window.register_run_after(move |win| {
EventBus::push(Events::Test);
win.find_id_mut(hbox_id)
.unwrap()
.downcast_mut::<HBox>()
.unwrap()
.remove_children(id);
});
}
12 changes: 11 additions & 1 deletion examples/event_bus/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ impl ObjectSubclass for WidgetEvents {
impl ObjectImpl for WidgetEvents {
fn initialize(&mut self) {
EventBus::register(self);

self.set_background(Color::RED);
self.set_borders(1., 1., 1., 1.);
self.width_request(100);
self.height_request(100);
}

fn on_drop(&mut self) {
info!("widget drop");
EventBus::remove(self);
}
}

Expand All @@ -40,7 +50,7 @@ impl EventHandle for WidgetEvents {

fn handle(&mut self, evt: &Self::Event) {
match evt {
Events::Test => info!("Test events received."),
Events::Test => info!("Test events received. id = {}", self.id()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/remove_demo/child_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl ObjectImpl for ChildWidget {
self.parent_construct();

self.width_request(200);
self.height_request(100);
self.height_request(200);
self.set_background(Color::GREY_LIGHT);

let mut child: Box<Widget> = Object::new(&[]);
Expand Down
26 changes: 24 additions & 2 deletions tlib/src/event_bus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod event;
pub mod event_handle;

use crate::{nonnull_mut, prelude::*};
use crate::{nonnull_mut, nonnull_ref, prelude::*};
use event::{IEvent, IEventType};
use event_handle::EventHandle;
use log::warn;
Expand All @@ -10,7 +10,7 @@ use std::{
collections::VecDeque,
hash::{DefaultHasher, Hasher},
marker::PhantomData,
ptr::NonNull,
ptr::{self, NonNull},
};

pub type RegisterMap<E, T> =
Expand Down Expand Up @@ -49,6 +49,23 @@ impl<E: IEvent<EventType = T>, T: IEventType> InnerEventBus<E, T> {
}
}

#[inline]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn remove(&mut self, handle: *const dyn EventHandle<Event = E, EventType = T>) {
for listen in unsafe { handle.as_ref() }.unwrap().listen() {
let mut hasher = DefaultHasher::default();
listen.hash(&mut hasher);
let k = hasher.finish();

if let Some(hnds) = self.register.get_mut(&k) {
hnds.retain(|h| {
let h = nonnull_ref!(h);
!ptr::eq(handle, h)
});
}
}
}

#[inline]
pub fn push(&mut self, e: E) {
let mut hasher = DefaultHasher::default();
Expand Down Expand Up @@ -124,6 +141,11 @@ macro_rules! event_bus_init {
with_event_bus(|event_bus| event_bus.register(handle))
}

#[inline]
pub fn remove(handle: *const dyn EventHandle<Event = $event, EventType = $event_type>) {
with_event_bus(|event_bus| event_bus.remove(handle))
}

#[inline]
pub fn push(e: $event) {
with_event_bus(|event_bus| event_bus.push(e))
Expand Down
4 changes: 3 additions & 1 deletion tlib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ pub trait ObjectImpl: ObjectImplExt + InnerInitializer + TypeName {
/// Override to register the reflect type info to [`TypeRegistry`] in this function.
fn type_register(&self, type_registry: &mut TypeRegistry) {}

/// Call on drop.
/// Any struct that extends `Object` will automatically implement `Drop` trait.
///
/// To customize the behavior, override on_drop instead.
fn on_drop(&mut self) {}
}

Expand Down
1 change: 1 addition & 0 deletions tmui/src/graphics/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl Board {
self.element_list.borrow_mut().push(NonNull::new(element))
}

#[inline]
pub(crate) fn remove_element(&self, id: ObjectId) {
self.element_list
.borrow_mut()
Expand Down