layout: Place items using simple loops

The C version of looping over buttons and other items was weakly typed, causing runtime errors, and also C doesn't know how to iterate in abstract, so it was full of callbacks with user-defined data. Moving this to Rust, iteration is made of simple loops, and compile-time type-checked, at the cost of some more verbose code.
This commit is contained in:
Dorota Czaplejewicz
2019-08-18 13:20:32 +00:00
parent 531a87825c
commit 4a61ab269b
4 changed files with 116 additions and 109 deletions

View File

@ -46,71 +46,8 @@ eek_layout_init (EekLayout *self)
{
}
const double row_spacing = 7.0;
struct place_data {
double desired_width;
double current_offset;
// Needed for outline (bounds) retrieval
LevelKeyboard *keyboard;
};
static void
row_placer(struct squeek_row *row, gpointer user_data)
{
struct place_data *data = (struct place_data*)user_data;
EekBounds row_bounds = {
.x = 0,
.y = 0,
.width = data->desired_width,
.height = 0,
};
squeek_row_set_bounds(row, row_bounds);
// Gather up all the keys in a row and adjust their bounds.
squeek_row_place_buttons(row, data->keyboard);
row_bounds = squeek_row_get_bounds(row);
row_bounds.y = data->current_offset;
squeek_row_set_bounds(row, row_bounds);
data->current_offset += row_bounds.height + row_spacing;
}
static void
row_counter(struct squeek_row *row, gpointer user_data) {
double *total_height = user_data;
EekBounds row_bounds = squeek_row_get_bounds(row);
*total_height += row_bounds.height + row_spacing;
}
void
eek_layout_place_rows(LevelKeyboard *keyboard, struct squeek_view *level)
{
/* Order rows */
// This needs to be done after outlines, because outlines define key sizes
// TODO: do this only for rows without bounds
// The keyboard width is given by the user via screen size. The height will be given dynamically.
// TODO: calculate max line width beforehand for button centering. Leave keyboard centering to the renderer later
EekBounds view_bounds = squeek_view_get_bounds(level);
struct place_data placer_data = {
.desired_width = view_bounds.width,
.current_offset = 0,
.keyboard = keyboard,
};
squeek_view_foreach(level, row_placer, &placer_data);
double total_height = 0;
squeek_view_foreach(level, row_counter, &total_height);
view_bounds.height = total_height;
squeek_view_set_bounds(level, view_bounds);
}
void
eek_layout_update_layout(LevelKeyboard *keyboard)
{
eek_layout_place_rows(keyboard, level_keyboard_current(keyboard));
squeek_view_place_contents(level_keyboard_current(keyboard), keyboard);
}

View File

@ -929,7 +929,7 @@ eek_xml_layout_real_create_keyboard (EekLayout *self,
for (uint i = 0; i < 4; i++) {
if (views[i]) {
eek_layout_place_rows(keyboard, views[i]);
squeek_view_place_contents(views[i], keyboard);
}
}