Skip to content

Commit e676d39

Browse files
committed
Bugfixes (required_size & event propagation).
- Bugfix: Events are now properly propagated down the tree. - Bugfix: The size is now once again properly calculated (not from the fixed layout anymore). - Add example for event propagation.
1 parent 506294f commit e676d39

File tree

3 files changed

+101
-47
lines changed

3 files changed

+101
-47
lines changed

examples/clickable_items.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use cursive::{
2+
theme::{BorderStyle, Palette, Theme},
3+
view::IntoBoxedView,
4+
views::{Button, TextContent, TextView},
5+
Cursive, CursiveExt,
6+
};
7+
8+
use cursive_flexbox::prelude::*;
9+
10+
fn main() {
11+
// Create a cursive object. This is the basic object that handles the ui and event loop.
12+
let mut cursive = Cursive::new();
13+
14+
cursive.add_global_callback('q', |cursive| cursive.quit());
15+
16+
cursive.set_theme(Theme {
17+
shadow: false,
18+
borders: BorderStyle::Simple,
19+
palette: Palette::terminal_default(),
20+
});
21+
22+
let content = TextContent::new("unset");
23+
let textview = TextView::new_with_content(content.clone());
24+
25+
let content_clone = content.clone();
26+
let button1 = Button::new_raw("Set text 'hello'.", move |_| {
27+
content_clone.set_content("hello");
28+
});
29+
30+
let button2 = Button::new_raw("Set text 'world'.", move |_| {
31+
content.set_content("world");
32+
});
33+
34+
// Create the flexbox and put some items in it.
35+
let mut flexbox = Flexbox::from(vec![
36+
button1.into_boxed_view(),
37+
button2.into_boxed_view(),
38+
textview.into_boxed_view(),
39+
]);
40+
41+
// Set a gap between the items on the main axis.
42+
flexbox.set_main_axis_gap(2);
43+
44+
// Set a gap between the main axis (along the container cross axis).
45+
flexbox.set_cross_axis_gap(2);
46+
47+
// Set item grow factors.
48+
// flexbox.set_flex_grow(1, 1);
49+
// flexbox.set_flex_grow(2, 2);
50+
51+
// Set the wrapping behavior of the main axes.
52+
flexbox.set_flex_wrap(FlexWrap::Wrap);
53+
54+
// Set the algorithm to assign free space along the main axis.
55+
flexbox.set_justify_content(JustifyContent::SpaceEvenly);
56+
57+
// Set the algorithm to assign free space along the cross axis for flex items.
58+
flexbox.set_align_items(AlignItems::Center);
59+
60+
// Set the algorithm to assign free space along the cross axis in the container.
61+
flexbox.set_align_content(AlignContent::FlexStart);
62+
63+
// Set the direction of the main axis.
64+
flexbox.set_flex_direction(FlexDirection::Row);
65+
66+
// Add the flexbox to the ui.
67+
cursive.add_fullscreen_layer(flexbox);
68+
69+
// Start running the eventloop.
70+
cursive.run();
71+
}

src/layout.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ impl<T> Layout<T> {
3232
pub fn iter(&self) -> Iter<'_, PlacedElement<T>> {
3333
self.into_iter()
3434
}
35-
36-
/// Return the size of this layout.
37-
pub fn size(&self) -> XY<usize> {
38-
let mut max_size = XY::from((0, 0));
39-
40-
self.iter().for_each(|item| {
41-
if item.position.right() > max_size.x {
42-
max_size.x = item.position.right() + 1;
43-
}
44-
if item.position.bottom() > max_size.y {
45-
max_size.y = item.position.bottom() + 1;
46-
}
47-
});
48-
49-
max_size
50-
}
5135
}
5236

5337
impl<T> IntoIterator for Layout<T> {

src/lib.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -488,27 +488,29 @@ impl MainAxis {
488488
// Axis contains elements that want the free space. Give it to them, don't use
489489
// justify-content.
490490

491-
let mut added_space = 0;
491+
let mut current_item_assigned_space = 0;
492492
let item_main_axis_size =
493493
layout.flexitem_main_axis_size(&mut RefCell::borrow_mut(&item));
494494
if remaining_grow_factor > 0 {
495-
added_space = (RefCell::borrow(&item).flex_grow as usize
496-
/ remaining_grow_factor)
497-
* assignable_free_space;
495+
current_item_assigned_space =
496+
((RefCell::borrow(&item).flex_grow as f64 / remaining_grow_factor as f64)
497+
* assignable_free_space as f64) as usize;
498498
}
499499

500500
match layout.options.direction {
501501
FlexDirection::Row => {
502502
start_x = offset;
503-
width = item_main_axis_size + added_space;
503+
width = item_main_axis_size + current_item_assigned_space;
504504
},
505505
FlexDirection::Column => {
506506
start_y = offset;
507-
height = item_main_axis_size + added_space;
507+
height = item_main_axis_size + current_item_assigned_space;
508508
},
509509
}
510-
offset += item_main_axis_size + layout.options.main_axis_gap as usize + added_space;
511-
assignable_free_space -= added_space;
510+
offset += item_main_axis_size
511+
+ layout.options.main_axis_gap as usize
512+
+ current_item_assigned_space;
513+
assignable_free_space -= current_item_assigned_space;
512514
remaining_grow_factor -= RefCell::borrow(&item).flex_grow as usize;
513515
} else {
514516
// Axis doesn't contain elements that want free space. Use justify-content property
@@ -957,40 +959,37 @@ impl View for Flexbox {
957959
fn required_size(&mut self, constraint: cursive_core::Vec2) -> cursive_core::Vec2 {
958960
// PERF: Cache the values that the previous layout was generated with and regenerate if
959961
// cached version is outdated.
960-
let layout = self.generate_layout(constraint);
961-
layout.size()
962+
constraint
962963
}
963964

964965
fn on_event(
965966
&mut self,
966967
mut event: cursive_core::event::Event,
967968
) -> cursive_core::event::EventResult {
968-
if let Some(active_child) = self.focused {
969-
if let cursive_core::event::Event::Mouse {
970-
ref mut offset,
971-
ref mut position,
972-
..
973-
} = event
974-
{
975-
if let Some(ref layout) = self.layout {
976-
if let Some(placed_element) =
977-
layout.element_at(global_to_view_coordinates(*position, *offset))
978-
{
979-
*offset = *offset + placed_element.position.top_left();
980-
RefCell::borrow_mut(&placed_element.element)
981-
.view
982-
.on_event(event)
983-
} else {
984-
EventResult::Ignored
985-
}
969+
if let cursive_core::event::Event::Mouse {
970+
ref mut offset,
971+
ref mut position,
972+
..
973+
} = event
974+
{
975+
if let Some(ref layout) = self.layout {
976+
if let Some(placed_element) =
977+
layout.element_at(global_to_view_coordinates(*position, *offset))
978+
{
979+
*offset = *offset + placed_element.position.top_left();
980+
RefCell::borrow_mut(&placed_element.element)
981+
.view
982+
.on_event(event)
986983
} else {
987984
EventResult::Ignored
988985
}
989986
} else {
990-
RefCell::borrow_mut(&self.content[active_child])
991-
.view
992-
.on_event(event)
987+
EventResult::Ignored
993988
}
989+
} else if let Some(active_child) = self.focused {
990+
RefCell::borrow_mut(&self.content[active_child])
991+
.view
992+
.on_event(event)
994993
} else {
995994
EventResult::Ignored
996995
}

0 commit comments

Comments
 (0)