summaryrefslogtreecommitdiff
path: root/src/ui/tui.h
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2024-08-28 00:18:53 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2024-08-28 00:18:53 +0330
commite9d373a154f538ef316940a142f652dba1a9bea6 (patch)
tree1330afde6a7e695e6e334f12483c732eca90314e /src/ui/tui.h
initial commit
Diffstat (limited to 'src/ui/tui.h')
-rw-r--r--src/ui/tui.h135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/ui/tui.h b/src/ui/tui.h
new file mode 100644
index 0000000..a3e64f3
--- /dev/null
+++ b/src/ui/tui.h
@@ -0,0 +1,135 @@
+#ifndef A404M_UI_TUI
+#define A404M_UI_TUI 1
+
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+
+typedef enum MOUSE_BUTTON {
+ MOUSE_BUTTON_LEFT_CLICK = 32,
+ MOUSE_BUTTON_MIDDLE_CLICK = 33,
+ MOUSE_BUTTON_RIGHT_CLICK = 34,
+ MOUSE_BUTTON_SCROLL_UP = 96,
+ MOUSE_BUTTON_SCROLL_DOWN = 97
+} MOUSE_BUTTON;
+
+typedef struct MOUSE_ACTION {
+ MOUSE_BUTTON button;
+ unsigned int x;
+ unsigned int y;
+} MOUSE_ACTION;
+
+typedef void (*ON_CLICK_CALLBACK)(MOUSE_ACTION mouse_action);
+
+typedef struct TUI {
+ struct winsize size;
+ struct termios original, raw, helper;
+ ON_CLICK_CALLBACK **on_click_callbacks;
+ int init_cursor_x, init_cursor_y;
+ char *buffer;
+} TUI;
+
+typedef enum WIDGET_TYPE {
+ WIDGET_TYPE_TEXT,
+ WIDGET_TYPE_BUTTON,
+ WIDGET_TYPE_COLUMN,
+ WIDGET_TYPE_ROW,
+ WIDGET_TYPE_BOX,
+} WIDGET_TYPE;
+
+typedef struct WIDGET {
+ WIDGET_TYPE type;
+ void *metadata;
+} WIDGET;
+
+typedef enum COLOR {
+ COLOR_NO_COLOR = -1,
+ COLOR_RESET = 0,
+ COLOR_RED = 1,
+ COLOR_GREEN = 2,
+ COLOR_YELLOW = 3,
+ COLOR_BLUE = 4,
+ COLOR_MAGENTA = 5,
+ COLOR_CYAN = 6,
+ COLOR_WHITE = 7
+} COLOR;
+
+typedef struct WIDGET_ARRAY {
+ int size;
+ WIDGET **widgets;
+} WIDGET_ARRAY;
+
+typedef struct TEXT_METADATA {
+ char *text;
+ COLOR color;
+} TEXT_METADATA;
+
+typedef struct BUTTON_METADATA {
+ WIDGET *child;
+ ON_CLICK_CALLBACK callback;
+} BUTTON_METADATA;
+
+typedef struct COLUMN_METADATA {
+ WIDGET_ARRAY *children;
+} COLUMN_METADATA;
+
+typedef struct ROW_METADATA {
+ WIDGET_ARRAY *children;
+} ROW_METADATA;
+
+typedef struct BOX_METADATA {
+ int width;
+ int height;
+ WIDGET *child;
+ COLOR color;
+} BOX_METADATA;
+
+typedef WIDGET *(*WIDGET_BUILDER)(TUI *tui);
+
+extern TUI *tui_init();
+extern void tui_delete(TUI *tui);
+extern void tui_refresh(TUI *tui);
+
+extern int tui_get_width(TUI *tui);
+extern int tui_get_height(TUI *tui);
+
+extern void tui_get_cursor_pos(TUI *tui, int *x, int *y);
+extern int tui_move_to(int x, int y);
+
+extern int tui_clear_screen();
+
+extern void tui_start_app(TUI *tui, WIDGET_BUILDER widget_builder);
+extern void _tui_draw_widget(TUI *tui, const WIDGET *widget, int widthBegin,
+ int widthEnd, int heightBegin, int heightEnd,
+ COLOR parent_color, int *childWidth,
+ int *childHeight);
+extern void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder);
+
+extern WIDGET *tui_new_widget(WIDGET_TYPE type, void *metadata);
+extern void tui_delete_widget(WIDGET *widget);
+
+extern WIDGET *tui_make_text(char *text, COLOR color);
+extern TEXT_METADATA *_tui_make_text_metadata(char *text, COLOR color);
+extern void _tui_delete_text(WIDGET *text);
+
+extern WIDGET *tui_make_button(WIDGET *child, ON_CLICK_CALLBACK callback);
+extern BUTTON_METADATA *_tui_make_button_metadata(WIDGET *child,
+ ON_CLICK_CALLBACK callback);
+extern void _tui_delete_button(WIDGET *button);
+
+extern WIDGET *tui_make_column(WIDGET_ARRAY *children);
+extern COLUMN_METADATA *_tui_make_column_metadata(WIDGET_ARRAY *children);
+extern void _tui_delete_column(WIDGET *column);
+
+extern WIDGET *tui_make_row(WIDGET_ARRAY *children);
+extern ROW_METADATA *_tui_make_row_metadata(WIDGET_ARRAY *children);
+extern void _tui_delete_row(WIDGET *row);
+
+extern WIDGET *tui_make_box(int width, int height, WIDGET *child, COLOR color);
+extern BOX_METADATA *_tui_make_box_metadata(WIDGET *child, int width,
+ int height, COLOR color);
+extern void _tui_delete_box(WIDGET *box);
+
+extern WIDGET_ARRAY *tui_make_widget_array(int size, ...);
+
+#endif