From c24ecc17e8a394a887c196d88989537dd4025ab6 Mon Sep 17 00:00:00 2001 From: Liu Date: Fri, 27 Sep 2024 23:33:04 +0800 Subject: [PATCH] test 1 --- examples/fabric/src/fabric.c | 369 ----------------------------- examples/fabric/src/fabric.h | 44 ---- examples/fabric/src/main.c | 152 ------------ examples/hello/app/hello.css | 12 - examples/hello/app/hello.xml | 11 - examples/hello/src/main.c | 27 --- examples/hello/xmake.lua | 6 - examples/todolist/app/check.png | Bin 1611 -> 0 bytes examples/todolist/app/delete.png | Bin 2870 -> 0 bytes examples/todolist/app/todolist.css | 134 ----------- examples/todolist/app/todolist.xml | 21 -- examples/todolist/src/main.c | 184 -------------- examples/todolist/xmake.lua | 6 - 13 files changed, 966 deletions(-) delete mode 100644 examples/fabric/src/fabric.c delete mode 100644 examples/fabric/src/fabric.h delete mode 100644 examples/hello/app/hello.css delete mode 100644 examples/hello/app/hello.xml delete mode 100644 examples/hello/src/main.c delete mode 100644 examples/hello/xmake.lua delete mode 100644 examples/todolist/app/check.png delete mode 100644 examples/todolist/app/delete.png delete mode 100644 examples/todolist/app/todolist.css delete mode 100644 examples/todolist/app/todolist.xml delete mode 100644 examples/todolist/src/main.c delete mode 100644 examples/todolist/xmake.lua diff --git a/examples/fabric/src/fabric.c b/examples/fabric/src/fabric.c deleted file mode 100644 index fc8d44702..000000000 --- a/examples/fabric/src/fabric.c +++ /dev/null @@ -1,369 +0,0 @@ - - -//////////////////////////////////////////////// -//////////// DELICATE FABRIC /////////// -//////////////////////////////////////////////// - -// Fork from: https://codepen.io/matthewmain/pen/oyyadr - -#include -#include -#include -#include -#include "fabric.h" - -double canvasWidth = 0; -double canvasHeight = 0; - -// fabric components -int pointCount = 0; -int spanCount = 0; -Point *points = NULL; -Span *spans = NULL; - -// settings - -// (iterations of position-accuracy refinement) -int rigidity = 15; - -// (rate of y-velocity increase per frame) -double gravity = 0.5; - -// (proportion of previous velocity after frame refresh) -double friction = 0.999; - -// (proportion of previous velocity after bouncing) -double wallBounceLoss = 0.9; - -// (proportion of previous velocity if touching the ground) -double skidLoss = 0.8; - -// interaction -double grabRadius = 0; - -// (number of times a span's length can stretch before breaking) -double fabricStrength = 25; - -// converts percentage to canvas x value -double xPct(double pct) -{ - return pct * canvasWidth / 100; -} - -// converts percentage to canvas y value -double yPct(double pct) -{ - return pct * canvasHeight / 100; -} - -// point constructor -Point createPoint(double current_x, double current_y) -{ - Point pt = malloc(sizeof(PointRec)); - if (pt == NULL) { - return NULL; - } - pt->cx = current_x; - pt->cy = current_y; - pt->px = pt->cx; // previous x value - pt->py = pt->cy; // previous y value - pt->pinned = false; - pt->grabbed = false; - pt->mxd = 0; // mouse x distance (upon grab) - pt->myd = 0; // mouse y distance (upon grab) - pt->id = pointCount; - pointCount += 1; - return pt; -} - -// creates a point object instance -void addPt(double xp, double yp) -{ - Point pt = createPoint(xPct(xp), yPct(yp)); - Point *newPoints = realloc(points, sizeof(Point) * pointCount); - if (newPoints == NULL || pt == NULL) { - return; - } - newPoints[pointCount - 1] = pt; - points = newPoints; -} - -void Fabric_resize(double width, double height) -{ - canvasWidth = width; - canvasHeight = height; -} - -///---OBJECTS---/// - -// gets distance between two points (pythogorian theorum) -double distanceBetween(Point p1, Point p2) -{ - double x_difference = p2->cx - p1->cx; - double y_difference = p2->cy - p1->cy; - return sqrt(x_difference * x_difference + y_difference * y_difference); -} - -// span constructor -Span createSpan(Point p1, Point p2) -{ - Span span = malloc(sizeof(SpanRec)); - if (span == NULL) { - return NULL; - } - span->p1 = p1; - span->p2 = p2; - span->l = distanceBetween(span->p1, span->p2); // length - spanCount++; - return span; -} - -///---FUNCTIONS---/// - -// gets a point by id number -Point getPt(int id) -{ - int i; - for (i = 0; i < pointCount; i++) { - if (points[i]->id == id) { - return points[i]; - } - } - return NULL; -} - -// generates random number between a minimum and maximum value -int randNumBetween(int min, int max) -{ - return (int)(rand() % (max - min + 1)) + min; -} - -// creates a span object instance -void addSp(int p1, int p2) -{ - Span span = createSpan(getPt(p1), getPt(p2)); - Span *newSpans = realloc(spans, sizeof(Span) * spanCount); - if (newSpans == NULL || span == NULL) { - return; - } - newSpans[spanCount - 1] = span; - spans = newSpans; -} - -// updates points based on verlet velocity (i.e., current coord minus previous -// coord) -void Fabric_updatePoints(void) -{ - int i; - for (i = 0; i < pointCount; i++) { - Point p = points[i]; // point object - if (!p->pinned) { - double xv = (p->cx - p->px) * friction; // x velocity - double yv = (p->cy - p->py) * friction; // y velocity - if (p->py >= canvasHeight - 1 && - p->py <= canvasHeight) { - xv *= skidLoss; - } - p->px = p->cx; // updates previous x as current x - p->py = p->cy; // updates previous y as current y - p->cx += xv; // updates current x with new velocity - p->cy += yv; // updates current y with new velocity - p->cy += gravity; // add gravity to y - } - } -} - -// inverts velocity for bounce if a point reaches a wall -void Fabric_wallBounce(void) -{ - int i; - for (i = 0; i < pointCount; i++) { - Point p = points[i]; - if (p->cx > canvasWidth) { - p->cx = canvasWidth; - p->px = p->cx + (p->cx - p->px) * wallBounceLoss; - } - if (p->cx < 0) { - p->cx = 0; - p->px = p->cx + (p->cx - p->px) * wallBounceLoss; - } - if (p->cy > canvasHeight) { - p->cy = canvasHeight; - p->py = p->cy + (p->cy - p->py) * wallBounceLoss; - } - if (p->cy < 0) { - p->cy = 0; - p->py = p->cy + (p->cy - p->py) * wallBounceLoss; - } - } -} - -// sets spans between points -void Fabric_updateSpans(void) -{ - int i; - for (i = 0; i < spanCount; i++) { - Span s = spans[i]; - if (s == NULL) { - continue; - } - // distance between x values - double dx = s->p2->cx - s->p1->cx; - // distance between y values - double dy = s->p2->cy - s->p1->cy; - // distance between the points - double d = sqrt(dx * dx + dy * dy); - // tear if over-stretched - if (d > s->l * fabricStrength) { - spans[i] = NULL; - } - // ratio (span length over distance between points) midpoint - // between x values - double r = s->l / d; - double mx = s->p1->cx + dx / 2; - // midpoint between y values - double my = s->p1->cy + dy / 2; - // offset of each x value (compared to span length) - double ox = dx / 2 * r; - // offset of each y value (compared to span length) - double oy = dy / 2 * r; - if (!s->p1->pinned) { - // updates span's first point x value - s->p1->cx = mx - ox; - // updates span's first point y value - s->p1->cy = my - oy; - } - if (!s->p2->pinned) { - // updates span's second point x value - s->p2->cx = mx + ox; - // updates span's second point y value - s->p2->cy = my + oy; - } - if (spans[i] == NULL) { - free(s); - } - } -} - -void Fabric_update(void) -{ - int i; - - Fabric_updatePoints(); - //(refines point positions for position accuracy & shape rigidity) - for (i = 0; i < rigidity; i++) { - Fabric_wallBounce(); - Fabric_updateSpans(); - } -} - -// grabs fabric on click -void Fabric_grab(double mouseCanvasX, double mouseCanvasY) -{ - int i; - for (i = 0; i < pointCount; i++) { - double x_diff = points[i]->cx - mouseCanvasX; - double y_diff = points[i]->cy - mouseCanvasY; - double dist = sqrt(x_diff * x_diff + y_diff * y_diff); - if (dist <= grabRadius) { - points[i]->grabbed = true; - points[i]->mxd = x_diff; - points[i]->myd = y_diff; - } - } -} - -// moves fabric -void Fabric_move(double mouseCanvasX, double mouseCanvasY) -{ - int i; - //(drops fabric if mouse leaves canvas) - if (mouseCanvasX < 0 || mouseCanvasX > canvasWidth || - mouseCanvasY < 0 || mouseCanvasY > canvasHeight) { - Fabric_drop(); - } - // updates grabbed points according to mouse position - for (i = 0; i < pointCount; i++) { - if (points[i]->grabbed && !points[i]->pinned) { - points[i]->cx = points[i]->px = - mouseCanvasX + points[i]->mxd; - points[i]->cy = points[i]->py = - mouseCanvasY + points[i]->myd; - } - } -} - -// drops fabric -void Fabric_drop(void) -{ - int i; - for (i = 0; i < pointCount; i++) { - points[i]->grabbed = false; - } -} - -void Fabric_getSpans(Span **outSpans, int *count) -{ - *outSpans = spans; - *count = spanCount; -} - -void Fabric_getPoints(Point **outPoints, int *count) -{ - *outPoints = points; - *count = spanCount; -} - -/** - * @brief init fabirc - * - * @param fw fabric width (as percentage of canvas width) - * @param fh fabric height (as percentage of canvas height) - */ -void Fabric_init(int fw, int fh, int canvaswidth, int canvasHeight) -{ - int i, j, x, y; - int htc = fw; // fabric horizontal thread count - int vtc = fh; // fabric vertical thread count - - srand((unsigned)time(NULL)); - Fabric_resize(canvaswidth, canvasHeight); - grabRadius = canvasWidth / 25; - - // creates points - for (i = 0; i < vtc; i++) { - //(assigns y values so top margin matches l/r margins) - y = (i * fh / (vtc - 1)) + (100 - fw) / 2; - for (j = 0; j < htc; j++) { - //(assigns x values so fabric is centered horizontally) - x = (j * fw / (htc - 1)) + (100 - fw) / 2; - addPt(x, y); - } - } - //(pin top row) - for (i = 0; i < htc; i++) { - points[i]->pinned = true; - } - - // create spans - for (i = 0; i < pointCount; i++) { - if ((i + 1) % htc != 0) { - addSp(i, i + 1); - } // horizontal spans - if (i < pointCount - htc) { - addSp(i, i + htc); - } // vertical spans - } - - // initial unfurl - double rx = randNumBetween(-50, 50); - for (i = 0; i < pointCount / 4; i++) { - int rp = randNumBetween(pointCount / 3, pointCount - 1); - points[rp]->px += rx; - points[rp]->py += randNumBetween(10, 30); - } - for (i = vtc * htc - vtc - 1; i < vtc * htc - 1; i++) { - points[i]->px += rx; - points[i]->py += randNumBetween(100, 300); - } -} diff --git a/examples/fabric/src/fabric.h b/examples/fabric/src/fabric.h deleted file mode 100644 index e493dd96f..000000000 --- a/examples/fabric/src/fabric.h +++ /dev/null @@ -1,44 +0,0 @@ -#include - -typedef struct PointRec_ { - double cx; - double cy; - double px; - double py; - bool pinned; - bool grabbed; - double mxd; - double myd; - int id; -} PointRec, *Point; - -typedef struct SpanRec_ { - Point p1; - Point p2; - double l; -} SpanRec, *Span; - -void Fabric_getSpans(Span **outSpans, int *count); - -void Fabric_getPoints(Point **outPoints, int *count); - -void Fabric_resize(double width, double height); - -void Fabric_update(void); - -// grabs fabric on click -void Fabric_grab(double mouseCanvasX, double mouseCanvasY); - -// moves fabric -void Fabric_move(double mouseCanvasX, double mouseCanvasY); - -// drops fabric -void Fabric_drop(void); - -/** - * @brief init fabirc - * - * @param fw fabric width (as percentage of canvas width) - * @param fh fabric height (as percentage of canvas height) - */ -void Fabric_init(int fw, int fh, int canvaswidth, int canvasHeight); diff --git a/examples/fabric/src/main.c b/examples/fabric/src/main.c index 60fb7ca52..f95de08bf 100644 --- a/examples/fabric/src/main.c +++ b/examples/fabric/src/main.c @@ -1,160 +1,8 @@ #include #include -#ifdef HAS_CAIRO -#include -#endif -#include "fabric.h" - -#define M_PI 3.1415926 -#define FABRIC_WIDTH 800 -#define FABRIC_HEIGHT 600 - -typedef struct ui_fabric_t { - int timer; -} ui_fabric_t; - -bool displayPoints = false; -bool displaySpans = true; - -ui_widget_prototype_t *ui_fabric_proto; - -#ifdef HAS_CAIRO -// displays points -void render_points(cairo_t *cr) -{ - int i, count; - Point p, *points; - - Fabric_getPoints(&points, &count); - cairo_set_source_rgb(cr, 0, 1, 0); - for (i = 0; i < count; i++) { - p = points[i]; - cairo_arc(cr, p->cx, p->cy, 3, 0, 2 * M_PI); - } - cairo_fill(cr); -} - -// displays spans -void render_spans(cairo_t *cr) -{ - int i, count; - Span s, *spans; - - Fabric_getSpans(&spans, &count); - cairo_set_source_rgb(cr, 0.33, 0.15, 0); - cairo_set_line_width(cr, 0.66); - for (i = 0; i < count; i++) { - s = spans[i]; - if (s != NULL) { - cairo_move_to(cr, s->p1->cx, s->p1->cy); - cairo_line_to(cr, s->p2->cx, s->p2->cy); - } - } - cairo_stroke(cr); -} - -#endif - -void ui_fabric_on_frame(ui_widget_t *w) -{ -#ifdef HAS_CAIRO - ui_canvas_context_t *ctx = ui_canvas_get_context(w); - cairo_surface_t *surface = cairo_image_surface_create_for_data( - ctx->buffer.bytes, CAIRO_FORMAT_RGB24, ctx->buffer.width, - ctx->buffer.height, ctx->buffer.bytes_per_row); - cairo_t *cr = cairo_create(surface); - - Fabric_update(); - // clears previous frame - ctx->fill_color = pd_color(255, 255, 255, 255); - ctx->fill_rect(ctx, 0, 0, ctx->width, ctx->height); - if (displayPoints) { - render_points(cr); - } - if (displaySpans) { - render_spans(cr); - } - cairo_destroy(cr); - cairo_surface_destroy(surface); - ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); - ctx->release(ctx); -#endif -} - -static void ui_fabric_on_mousedown(ui_widget_t *w, ui_event_t *e, void *arg) -{ - float x, y; - - ui_widget_get_offset(w, NULL, &x, &y); - Fabric_grab(e->mouse.x - x, e->mouse.y - y); - ui_widget_set_mouse_capture(w); -} - -static void ui_fabric_on_mousemove(ui_widget_t *w, ui_event_t *e, void *arg) -{ - float x, y; - - ui_widget_get_offset(w, NULL, &x, &y); - Fabric_move(e->mouse.x - x, e->mouse.y - y); -} - -static void ui_fabric_on_mouseup(ui_widget_t *w, ui_event_t *e, void *arg) -{ - Fabric_drop(); - ui_widget_release_mouse_capture(w); -} - -void ui_fabric_on_init(ui_widget_t *w) -{ - ui_fabric_t *data; - ui_fabric_proto->proto->init(w); - data = ui_widget_add_data(w, ui_fabric_proto, sizeof(ui_fabric_t)); - ui_widget_on(w, "mousedown", ui_fabric_on_mousedown, NULL); - ui_widget_on(w, "mousemove", ui_fabric_on_mousemove, NULL); - ui_widget_on(w, "mouseup", ui_fabric_on_mouseup, NULL); - data->timer = ptk_set_interval(LCUI_MAX_FRAME_MSEC, - (timer_callback)ui_fabric_on_frame, w); -} - -void ui_fabric_on_destroy(ui_widget_t *w) -{ - ui_fabric_t *data; - data = ui_widget_get_data(w, ui_fabric_proto); - ptk_clear_timer(data->timer); - ui_fabric_proto->proto->destroy(w); -} - -void ui_register_fabric(void) -{ - ui_fabric_proto = ui_create_widget_prototype("fabric", "canvas"); - ui_fabric_proto->init = ui_fabric_on_init; - ui_fabric_proto->destroy = ui_fabric_on_destroy; -} - -const char *app_css = "\ -root {\ - display: flex;\ - align-items: center;\ - justify-content: center;\ - background: #f2f4f5;\ - width: 800px;\ - height: 600px;\ -}\ -fabric {\ - width: 800px;\ - height: 600px;\ - border: 1px solid #697b8c;\ - box-sizing: border-box;\ - box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);\ -}"; int main(int argc, char **argv) { lcui_init(); - Fabric_init(66, 55, 800, 600); - ui_register_fabric(); - ui_load_css_string(app_css, __FILE__); - ui_widget_set_title(ui_root(), L"Fabric"); - ui_root_append(ui_create_widget("fabric")); return lcui_main(); } diff --git a/examples/hello/app/hello.css b/examples/hello/app/hello.css deleted file mode 100644 index 96a37753a..000000000 --- a/examples/hello/app/hello.css +++ /dev/null @@ -1,12 +0,0 @@ -text.text-hello { - font-size: 18px; - font-family: "Segoe UI"; - text-align: center; - padding: 25px; - margin: 25px; - border: 1px solid #eee; - background-color: #f8f9fa; -} -#btn, #edit { - margin: 0 0 0 25px; -} diff --git a/examples/hello/app/hello.xml b/examples/hello/app/hello.xml deleted file mode 100644 index bf1745bd2..000000000 --- a/examples/hello/app/hello.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - [i][color=#f00]Hello[/color][/i], [b][color=#fff][bgcolor=#f00]World![/bgcolor][/color][/b] - - [i][color=#f00]Hello[/color][/i], [b][color=#fff][bgcolor=#f00]World![/bgcolor][/color][/b] - - - diff --git a/examples/hello/src/main.c b/examples/hello/src/main.c deleted file mode 100644 index a5967b6f3..000000000 --- a/examples/hello/src/main.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -static void on_btn_click(ui_widget_t *self, ui_event_t *e, void *arg) -{ - wchar_t str[256]; - ui_widget_t *edit = ui_get_widget("edit"); - ui_widget_t *txt = ui_get_widget("text-hello"); - - ui_textinput_get_text_w(edit, 0, 255, str); - ui_text_set_content_w(txt, str); -} - -int main(int argc, char **argv) -{ - ui_widget_t *pack; - - lcui_init(); - pack = ui_load_xml_file("hello.xml"); - if (!pack) { - return -1; - } - ui_root_append(pack); - ui_widget_unwrap(pack); - ui_widget_on(ui_get_widget("btn"), "click", on_btn_click, NULL); - return lcui_main(); -} diff --git a/examples/hello/xmake.lua b/examples/hello/xmake.lua deleted file mode 100644 index f5bb3ab21..000000000 --- a/examples/hello/xmake.lua +++ /dev/null @@ -1,6 +0,0 @@ -target("hello") - add_installfiles("app/*") - set_kind("binary") - set_rundir("app") - add_packages("lcui") - add_files("src/*.c") diff --git a/examples/todolist/app/check.png b/examples/todolist/app/check.png deleted file mode 100644 index 49c14a00f73133b3524acfe07b9697326897305c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1611 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 - - - - - - Todo list - - - - All - Active - Completed - - - - - - - - diff --git a/examples/todolist/src/main.c b/examples/todolist/src/main.c deleted file mode 100644 index 076dc2cc8..000000000 --- a/examples/todolist/src/main.c +++ /dev/null @@ -1,184 +0,0 @@ -// Reference from: https://codepen.io/knyttneve/details/mddGVjB -#include -#include -#include - -typedef struct task_t { - unsigned id; - wchar_t *name; - const char *status; -} task_t; - -struct todolist_app_t { - unsigned id; - list_t tasks; -} app = { 0 }; - -ui_widget_t *ui_task_item_create(task_t *task) -{ - char id[32] = { 0 }; - ui_widget_t *item = ui_create_widget("text"); - ui_widget_t *status = ui_create_widget(NULL); - ui_widget_t *del = ui_create_widget(NULL); - ui_widget_t *name = ui_create_widget("text"); - snprintf(id, 32, "%u", task->id); - ui_text_set_content_w(name, task->name); - ui_widget_set_attr(item, "data-id", id); - ui_widget_add_class(item, "task-item"); - if (strcmp(task->status, "completed") == 0) { - ui_widget_add_class(item, "is-completed"); - } - ui_widget_add_class(name, "task-name"); - ui_widget_add_class(status, "task-status"); - ui_widget_add_class(del, "task-delete"); - ui_widget_append(item, status); - ui_widget_append(item, name); - ui_widget_append(item, del); - return item; -} - -void ui_todolist_update_count(void) -{ - wchar_t text[32]; - ui_widget_t *count = ui_get_widget("count"); - - swprintf(text, 32, app.tasks.length > 1 ? L"%u tasks" : L"%u task", - app.tasks.length); - ui_text_set_content_w(count, text); -} - -void update_filter_status(ui_widget_t *w, void *activeStatus) -{ - const char *status = ui_widget_get_attr(w, "data-value"); - if (status && strcmp(status, activeStatus) == 0) { - ui_widget_add_class(w, "is-active"); - } else { - ui_widget_remove_class(w, "is-active"); - } -} - -void ui_todolist_filter(const char *status) -{ - task_t *task; - list_node_t *node; - ui_widget_t *list = ui_get_widget("list"); - - ui_widget_empty(list); - for (list_each(node, &app.tasks)) { - task = node->data; - if (strcmp(status, "all") != 0 && - strcmp(task->status, status) != 0) { - continue; - } - ui_widget_append(list, ui_task_item_create(task)); - } - ui_widget_each(ui_get_widget("filters"), update_filter_status, - (void *)status); - ui_todolist_update_count(); -} - -void ui_todolist_add(const wchar_t *name, const char *status) -{ - task_t *task = malloc(sizeof(task_t)); - - task->id = ++app.id; - task->name = wcsdup2(name); - task->status = status ? status : "active"; - list_append(&app.tasks, task); - ui_widget_append(ui_get_widget("list"), ui_task_item_create(task)); - ui_todolist_update_count(); -} - -void on_input_keydown(ui_widget_t *w, ui_event_t *e, void *arg) -{ - wchar_t name[256]; - - if (e->key.code == KEY_ENTER) { - ui_textinput_get_text_w(w, 0, 255, name); - ui_todolist_add(name, "active"); - ui_textinput_clear_text(w); - } -} - -void on_filter_click(ui_widget_t *w, ui_event_t *e, void *arg) -{ - const char *status = ui_widget_get_attr(e->target, "data-value"); - - if (status) { - ui_todolist_filter(status); - } -} - -void on_task_list_click(ui_widget_t *w, ui_event_t *e, void *arg) -{ - const char *id_str; - unsigned id; - task_t *task; - list_node_t *node; - ui_widget_t *item = e->target->parent; - - for (item = e->target; !ui_widget_has_class(item, "task-item"); - item = item->parent) - ; - id_str = ui_widget_get_attr(item, "data-id"); - if (!id_str || sscanf(id_str, "%u", &id) != 1) { - return; - } - if (ui_widget_has_class(e->target, "task-delete")) { - ui_widget_remove(item); - for (list_each(node, &app.tasks)) { - task = node->data; - if (task->id == id) { - list_delete_node(&app.tasks, node); - break; - } - } - ui_todolist_update_count(); - return; - } - if (!ui_widget_has_class(e->target, "task-status")) { - return; - } - for (list_each(node, &app.tasks)) { - task = node->data; - if (task->id != id) { - continue; - } - if (strcmp(task->status, "completed") == 0) { - task->status = "active"; - ui_widget_remove_class(item, "is-completed"); - break; - } - task->status = "completed"; - ui_widget_add_class(item, "is-completed"); - break; - } -} - -void ui_todolist_init(void) -{ - ui_widget_on(ui_get_widget("input"), "keydown", on_input_keydown, NULL); - ui_widget_on(ui_get_widget("filters"), "click", on_filter_click, NULL); - ui_widget_on(ui_get_widget("list"), "click", on_task_list_click, NULL); - ui_todolist_filter("all"); -} - -int main(int argc, char **argv) -{ - ui_widget_t *pack; - - lcui_init(); - pack = ui_load_xml_file("todolist.xml"); - if (!pack) { - return -1; - } - ui_root_append(pack); - ui_widget_unwrap(pack); - ui_widget_set_title(ui_root(), L"Todo list"); - ui_todolist_init(); - ui_todolist_add(L"Download LCUI source code", "completed"); - ui_todolist_add(L"Build LCUI", "completed"); - ui_todolist_add(L"Read LCUI tutorials", "active"); - ui_todolist_add(L"Create my LCUI application", "active"); - return lcui_main(); -} diff --git a/examples/todolist/xmake.lua b/examples/todolist/xmake.lua deleted file mode 100644 index 1cd8585ae..000000000 --- a/examples/todolist/xmake.lua +++ /dev/null @@ -1,6 +0,0 @@ -target("todolist") - add_installfiles("app/*") - set_kind("binary") - set_rundir("app") - add_packages("lcui") - add_files("src/*.c")