bug: fix empty widget in layout

This commit is contained in:
ClementTsang 2020-04-27 16:00:16 -04:00
parent 8e4f6a3a02
commit 15dba2e6cf
2 changed files with 127 additions and 108 deletions

View File

@ -9,11 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features ### Features
- [#58](https://github.com/ClementTsang/bottom/issues/58): I/O stats per process - [#58](https://github.com/ClementTsang/bottom/issues/58): I/O stats per process.
- [#55](https://github.com/ClementTsang/bottom/issues/55): Battery monitoring widget - [#55](https://github.com/ClementTsang/bottom/issues/55): Battery monitoring widget.
- [#114](https://github.com/ClementTsang/bottom/pull/114): Process state per process - [#114](https://github.com/ClementTsang/bottom/pull/114): Process state per process.
### Changes ### Changes
@ -49,6 +49,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed bug where a single empty row as a layout would crash without a proper warning. - Fixed bug where a single empty row as a layout would crash without a proper warning.
The behaviour now errors out with a more helpful message. The behaviour now errors out with a more helpful message.
- Fixed bug where empty widgets in layout would cause widget movement to not work properly when moving vertically.
### Development changes ### Development changes
- Switch to stateful widget style for tables. - Switch to stateful widget style for tables.

View File

@ -322,73 +322,81 @@ impl BottomLayout {
widget.up_neighbour = Some(current_best_widget_id); widget.up_neighbour = Some(current_best_widget_id);
} }
} }
} else if let Some(next_row_up) = layout_mapping } else {
.range( let mut up_range = layout_mapping.range(
..( ..(
row_height_percentage_start, row_height_percentage_start,
row_height_percentage_start, row_height_percentage_start,
), ),
) );
.next_back() while let Some(next_row_up) = up_range.next_back() {
{ let mut current_best_distance = 0;
let mut current_best_distance = 0; let mut current_best_widget_id = widget.widget_id;
let mut current_best_widget_id = widget.widget_id; let (target_start_width, target_end_width) =
let (target_start_width, target_end_width) = if col_row_children_len > 1 {
if col_row_children_len > 1 { (
( col_width_percentage_start
col_width_percentage_start + widget_width_percentage_start
+ widget_width_percentage_start * (col_width_percentage_end
* (col_width_percentage_end - col_width_percentage_start)
- col_width_percentage_start) / 100,
/ 100, col_width_percentage_start
col_width_percentage_start + widget_width_percentage_end
+ widget_width_percentage_end * (col_width_percentage_end
* (col_width_percentage_end - col_width_percentage_start)
- col_width_percentage_start) / 100,
/ 100, )
) } else {
} else { (
(col_width_percentage_start, col_width_percentage_end) col_width_percentage_start,
}; col_width_percentage_end,
)
};
for col_position in &(next_row_up.1).1 { for col_position in &(next_row_up.1).1 {
if let Some(next_col_row) = if let Some(next_col_row) =
(col_position.1).1.iter().next_back() (col_position.1).1.iter().next_back()
{ {
let (candidate_col_start, candidate_col_end) = let (candidate_col_start, candidate_col_end) =
((col_position.0).0, (col_position.0).1); ((col_position.0).0, (col_position.0).1);
let candidate_difference = let candidate_difference =
candidate_col_end - candidate_col_start; candidate_col_end - candidate_col_start;
for candidate_widget in &(next_col_row.1).1 { for candidate_widget in &(next_col_row.1).1 {
let candidate_start = candidate_col_start let candidate_start = candidate_col_start
+ (candidate_widget.0).0 * candidate_difference + (candidate_widget.0).0
/ 100; * candidate_difference
let candidate_end = candidate_col_start / 100;
+ (candidate_widget.0).1 * candidate_difference let candidate_end = candidate_col_start
/ 100; + (candidate_widget.0).1
* candidate_difference
/ 100;
if is_intersecting( if is_intersecting(
(target_start_width, target_end_width),
(candidate_start, candidate_end),
) {
let candidate_distance = get_distance(
(target_start_width, target_end_width), (target_start_width, target_end_width),
(candidate_start, candidate_end), (candidate_start, candidate_end),
); ) {
let candidate_distance = get_distance(
(target_start_width, target_end_width),
(candidate_start, candidate_end),
);
if current_best_distance < candidate_distance { if current_best_distance
current_best_distance = < candidate_distance
candidate_distance + 1; {
current_best_widget_id = current_best_distance =
*(candidate_widget.1); candidate_distance + 1;
current_best_widget_id =
*(candidate_widget.1);
}
} }
} }
} }
} }
}
if current_best_distance > 0 { if current_best_distance > 0 {
widget.up_neighbour = Some(current_best_widget_id); widget.up_neighbour = Some(current_best_widget_id);
break;
}
} }
} }
@ -430,72 +438,81 @@ impl BottomLayout {
widget.down_neighbour = Some(current_best_widget_id); widget.down_neighbour = Some(current_best_widget_id);
} }
} }
} else if let Some(next_row_down) = layout_mapping } else {
.range( let mut down_range = layout_mapping.range(
( (
row_height_percentage_start + 1, row_height_percentage_start + 1,
row_height_percentage_start + 1, row_height_percentage_start + 1,
).., )..,
) );
.next() while let Some(next_row_down) = down_range.next() {
{ let mut current_best_distance = 0;
let mut current_best_distance = 0; let mut current_best_widget_id = widget.widget_id;
let mut current_best_widget_id = widget.widget_id; let (target_start_width, target_end_width) =
let (target_start_width, target_end_width) = if col_row_children_len > 1 {
if col_row_children_len > 1 { (
( col_width_percentage_start
col_width_percentage_start + widget_width_percentage_start
+ widget_width_percentage_start * (col_width_percentage_end
* (col_width_percentage_end - col_width_percentage_start)
- col_width_percentage_start) / 100,
/ 100, col_width_percentage_start
col_width_percentage_start + widget_width_percentage_end
+ widget_width_percentage_end * (col_width_percentage_end
* (col_width_percentage_end - col_width_percentage_start)
- col_width_percentage_start) / 100,
/ 100, )
) } else {
} else { (
(col_width_percentage_start, col_width_percentage_end) col_width_percentage_start,
}; col_width_percentage_end,
)
};
for col_position in &(next_row_down.1).1 { for col_position in &(next_row_down.1).1 {
if let Some(next_col_row) = (col_position.1).1.iter().next() if let Some(next_col_row) =
{ (col_position.1).1.iter().next()
let (candidate_col_start, candidate_col_end) = {
((col_position.0).0, (col_position.0).1); let (candidate_col_start, candidate_col_end) =
let candidate_difference = ((col_position.0).0, (col_position.0).1);
candidate_col_end - candidate_col_start; let candidate_difference =
for candidate_widget in &(next_col_row.1).1 { candidate_col_end - candidate_col_start;
let candidate_start = candidate_col_start for candidate_widget in &(next_col_row.1).1 {
+ (candidate_widget.0).0 * candidate_difference let candidate_start = candidate_col_start
/ 100; + (candidate_widget.0).0
let candidate_end = candidate_col_start * candidate_difference
+ (candidate_widget.0).1 * candidate_difference / 100;
/ 100; let candidate_end = candidate_col_start
+ (candidate_widget.0).1
* candidate_difference
/ 100;
if is_intersecting( if is_intersecting(
(target_start_width, target_end_width),
(candidate_start, candidate_end),
) {
let candidate_distance = get_distance(
(target_start_width, target_end_width), (target_start_width, target_end_width),
(candidate_start, candidate_end), (candidate_start, candidate_end),
); ) {
let candidate_distance = get_distance(
(target_start_width, target_end_width),
(candidate_start, candidate_end),
);
if current_best_distance < candidate_distance { if current_best_distance
current_best_distance = < candidate_distance
candidate_distance + 1; {
current_best_widget_id = current_best_distance =
*(candidate_widget.1); candidate_distance + 1;
current_best_widget_id =
*(candidate_widget.1);
}
} }
} }
} }
} }
}
if current_best_distance > 0 { if current_best_distance > 0 {
widget.down_neighbour = Some(current_best_widget_id); widget.down_neighbour = Some(current_best_widget_id);
break;
}
} }
} }
} }