diff --git a/examples/remove_demo/main.rs b/examples/remove_demo/main.rs index 257aed0..0111dc5 100644 --- a/examples/remove_demo/main.rs +++ b/examples/remove_demo/main.rs @@ -1,5 +1,6 @@ pub mod child_widget; pub mod remove_widget; +pub mod stack_widget; use remove_widget::RemoveWidget; use tmui::{application::Application, application_window::ApplicationWindow, prelude::*}; diff --git a/examples/remove_demo/remove_widget.rs b/examples/remove_demo/remove_widget.rs index 3d0382c..0380129 100644 --- a/examples/remove_demo/remove_widget.rs +++ b/examples/remove_demo/remove_widget.rs @@ -6,7 +6,7 @@ use tmui::{ widget::WidgetImpl, }; -use crate::child_widget::ChildWidget; +use crate::{child_widget::ChildWidget, stack_widget::StackWidget}; #[extends(Widget, Layout(VBox))] #[derive(Childrenable)] @@ -15,6 +15,8 @@ pub struct RemoveWidget { top: Box, #[children] bottom: Box, + #[children] + stack: Box, to_remove: ObjectId, widget_id: ObjectId, @@ -34,8 +36,10 @@ impl ObjectImpl for RemoveWidget { self.set_vexpand(true); let mut button_1 = Button::new(Some("Remove Left")); let mut button_2 = Button::new(Some("Remove Right")); + let mut button_3 = Button::new(Some("Remove Stack")); button_1.width_request(100); button_2.width_request(100); + button_3.width_request(100); connect!( button_1, mouse_pressed(), @@ -48,8 +52,15 @@ impl ObjectImpl for RemoveWidget { self, remove_right_pressed(MouseEvent) ); + connect!( + button_3, + mouse_pressed(), + self, + remove_stack_widget(MouseEvent) + ); self.top.add_child(button_1); self.top.add_child(button_2); + self.top.add_child(button_3); self.bottom.add_child(Label::new(Some("Label 1"))); let label2 = Label::new(Some("Label 2")); @@ -84,4 +95,8 @@ impl RemoveWidget { .unwrap() .remove_child(); } + + pub fn remove_stack_widget(&mut self, _: MouseEvent) { + self.stack.remove_index(1); + } } diff --git a/examples/remove_demo/stack_widget.rs b/examples/remove_demo/stack_widget.rs new file mode 100644 index 0000000..1410ed9 --- /dev/null +++ b/examples/remove_demo/stack_widget.rs @@ -0,0 +1,38 @@ +use tmui::{ + prelude::*, + tlib::object::{ObjectImpl, ObjectSubclass}, + widget::WidgetImpl, +}; + +#[extends(Widget, Layout(Stack))] +pub struct StackWidget {} + +impl ObjectSubclass for StackWidget { + const NAME: &'static str = "StackWidget"; +} + +impl ObjectImpl for StackWidget { + fn construct(&mut self) { + self.parent_construct(); + + self.width_request(100); + self.height_request(100); + + let mut w1: Box = Object::new(&[]); + let mut w2: Box = Object::new(&[]); + + w1.set_vexpand(true); + w1.set_hexpand(true); + w1.set_background(Color::RED); + + w2.set_vexpand(true); + w2.set_hexpand(true); + w2.set_background(Color::BLUE); + + self.add_child(w1); + self.add_child(w2); + self.switch(); + } +} + +impl WidgetImpl for StackWidget {} diff --git a/macros/src/stack.rs b/macros/src/stack.rs index 72a2e25..0c4792b 100644 --- a/macros/src/stack.rs +++ b/macros/src/stack.rs @@ -18,11 +18,19 @@ pub(crate) fn generate_stack_add_child() -> syn::Result syn::Result { Ok(quote! { if let Some(index) = self.container.children.iter().position(|w| w.id() == id) { - if self.current_index == self.container.children.len() { + let removed = self.container.children.remove(index); + + if self.current_index == self.container.children.len() && self.current_index != 0 { self.current_index -= 1; } - let removed = self.container.children.remove(index); + for (idx, child) in self.container.children.iter_mut().enumerate() { + if self.current_index == idx { + child.show(); + } else { + child.hide(); + } + } let window = ApplicationWindow::window(); window._add_removed_widget(removed); @@ -145,6 +153,17 @@ pub(crate) fn generate_stack_impl(name: &Ident) -> syn::Result= self.children().len() { + log::warn!("`index` overrange, skip the `remove_index()`, children len {}, get index {}", self.children().len(), index); + return + } + + let id = self.children_mut().get_mut(index).unwrap().id(); + self.remove_children(id); + } } }) } diff --git a/tmui/src/stack.rs b/tmui/src/stack.rs index b34c3d2..3c77d24 100644 --- a/tmui/src/stack.rs +++ b/tmui/src/stack.rs @@ -156,6 +156,8 @@ pub trait StackImpl { fn switch(&mut self); fn switch_index(&mut self, index: usize); + + fn remove_index(&mut self, index: usize); } stack_impl!(Stack);