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
1 change: 1 addition & 0 deletions examples/remove_demo/main.rs
Original file line number Diff line number Diff line change
@@ -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::*};
Expand Down
17 changes: 16 additions & 1 deletion examples/remove_demo/remove_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -15,6 +15,8 @@ pub struct RemoveWidget {
top: Box<HBox>,
#[children]
bottom: Box<HBox>,
#[children]
stack: Box<StackWidget>,

to_remove: ObjectId,
widget_id: ObjectId,
Expand All @@ -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(),
Expand All @@ -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"));
Expand Down Expand Up @@ -84,4 +95,8 @@ impl RemoveWidget {
.unwrap()
.remove_child();
}

pub fn remove_stack_widget(&mut self, _: MouseEvent) {
self.stack.remove_index(1);
}
}
38 changes: 38 additions & 0 deletions examples/remove_demo/stack_widget.rs
Original file line number Diff line number Diff line change
@@ -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<Widget> = Object::new(&[]);
let mut w2: Box<Widget> = 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 {}
23 changes: 21 additions & 2 deletions macros/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ pub(crate) fn generate_stack_add_child() -> syn::Result<proc_macro2::TokenStream
pub(crate) fn generate_stack_remove_children() -> syn::Result<proc_macro2::TokenStream> {
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);
Expand Down Expand Up @@ -145,6 +153,17 @@ pub(crate) fn generate_stack_impl(name: &Ident) -> syn::Result<proc_macro2::Toke
self.update();
self.set_render_styles(true);
}

#[inline]
fn remove_index(&mut self, index: usize) {
if index >= 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);
}
}
})
}
2 changes: 2 additions & 0 deletions tmui/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down