diff options
| -rw-r--r-- | src/main.c | 57 | ||||
| -rw-r--r-- | src/ui/tui.c | 37 | ||||
| -rw-r--r-- | src/ui/tui.h | 3 | 
3 files changed, 63 insertions, 34 deletions
@@ -2,28 +2,45 @@  #include <stdio.h>  #include <unistd.h> -void on_button_click(MOUSE_ACTION mouse_action) { -  printf("hello"); -  sleep(1); -} +bool is_clicked = false; + +void on_button_click(MOUSE_ACTION mouse_action) { is_clicked = !is_clicked; }  WIDGET *ui_build(TUI *tui) { -  return tui_make_box( -      -1, -1, -      tui_make_row(tui_make_widget_array( -          3, -          tui_make_box( -              8, 2, -              tui_make_button(tui_make_text("Hello, World!", COLOR_BLUE), -                              on_button_click), -              COLOR_YELLOW), -          tui_make_box( -              8, 2, -              tui_make_button(tui_make_text("Hello, World!", COLOR_RED), -                              on_button_click), -              COLOR_BLUE), -          tui_make_text("Hello, World!", COLOR_RED), 1)), -      COLOR_MAGENTA); +  if (is_clicked) { +    return tui_make_box( +        -1, -1, +        tui_make_column(tui_make_widget_array( +            2, tui_make_box(0, 12, NULL, COLOR_NO_COLOR), +            tui_make_row(tui_make_widget_array( +                2, tui_make_box(50, 0, NULL, COLOR_NO_COLOR), +                tui_make_box( +                    20, 3, +                    tui_make_column(tui_make_widget_array( +                        2, +                        tui_make_text( +                            "This is the second page", +                            COLOR_BLUE), +                        tui_make_button(tui_make_text("       Back", COLOR_RED), +                                        on_button_click))), + +                    COLOR_WHITE))))), +        COLOR_MAGENTA); +  } else { +    return tui_make_box( +        -1, -1, +        tui_make_column(tui_make_widget_array( +            2, tui_make_box(0, 12, NULL, COLOR_NO_COLOR), +            tui_make_row(tui_make_widget_array( +                2, tui_make_box(50, 0, NULL, COLOR_NO_COLOR), +                tui_make_button( +                    tui_make_box( +                        16, 3, +                            tui_make_text("\nClick here", COLOR_BLUE), +                        COLOR_WHITE), +                    on_button_click))))), +        COLOR_MAGENTA); +  }  }  int main() { diff --git a/src/ui/tui.c b/src/ui/tui.c index 664f7a0..96de5c2 100644 --- a/src/ui/tui.c +++ b/src/ui/tui.c @@ -133,11 +133,17 @@ void _tui_set_cell_char(TUI *tui, int x, int y, char c) {  }  void _tui_set_cell_color(TUI *tui, int x, int y, COLOR color) { +  if (color == COLOR_NO_COLOR) { +    return; +  }    tui->cells[_tui_get_cell_index(tui, x, y)].color = color;  }  void _tui_set_cell_background_color(TUI *tui, int x, int y,                                      COLOR background_color) { +  if (background_color == COLOR_NO_COLOR) { +    return; +  }    tui->cells[_tui_get_cell_index(tui, x, y)].background_color =        background_color;  } @@ -240,30 +246,35 @@ void _tui_draw_widget_to_cells(TUI *tui, const WIDGET *widget, int width_begin,    switch (widget->type) {    case WIDGET_TYPE_TEXT: {      const TEXT_METADATA *metadata = widget->metadata; -    const int widthDiff = width_end - width_begin; -    const int textLen = strlen(metadata->text); +    const int width_diff = width_end - width_begin; +    const int text_len = strlen(metadata->text); +    int inserted_index = 0;      int height = height_begin;      for (; height < height_end; ++height) { -      const int begin = (height - height_begin) * widthDiff; -      const int end = begin + widthDiff; +      const int begin = (height - height_begin) * width_diff; -      for (int j = 0; j < end; ++j) { +      for (int j = 0; j < width_diff; ++j) {          const int x = width_begin + j;          const int y = height;          _tui_set_cell_color(tui, x, y, metadata->color); -        const int index = begin + j; -        if (index < textLen) { -          _tui_set_cell_char(tui, x, y, metadata->text[index]); +        if (inserted_index < text_len) { +          const char c = metadata->text[inserted_index]; +          inserted_index += 1; +          if (c == '\n') { +            break; +          } else { +            _tui_set_cell_char(tui, x, y, c); +          } +          if (inserted_index == text_len) { +            goto END_OF_TEXT; +          }          }        } - -      if (end > textLen) { -        break; -      }      } +  END_OF_TEXT:      *child_height = height + 1;      *child_width = -        (textLen > widthDiff ? width_end : textLen + width_begin) + 1; +        (text_len > width_diff ? width_end : text_len + width_begin) + 1;    } break;    case WIDGET_TYPE_BUTTON: {      const BUTTON_METADATA *metadata = widget->metadata; diff --git a/src/ui/tui.h b/src/ui/tui.h index 0ca71b3..b21395a 100644 --- a/src/ui/tui.h +++ b/src/ui/tui.h @@ -4,6 +4,7 @@  #include <stdlib.h>  #include <sys/ioctl.h>  #include <termios.h> +#include <stdint.h>  typedef enum MOUSE_BUTTON {    MOUSE_BUTTON_LEFT_CLICK = 32, @@ -22,7 +23,7 @@ typedef struct MOUSE_ACTION {  typedef void (*ON_CLICK_CALLBACK)(MOUSE_ACTION mouse_action);  #ifndef __cplusplus -typedef enum bool { false = 0, true = 1 } bool; +typedef enum bool : uint8_t { false = 0, true = 1 } bool;  #endif  typedef enum COLOR {  |