mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
- charset module renamed to encoding (decode/encode kept)
- time: get_time_ms → y_gettime, sleep_ms → y_sleep
- time: delete get_time_us, get_time_delta, sleep_s
- string: strdup2 → y_strdup, wcsdup2 → y_wcsdup
- string: strtolower → y_strlower, strtrim → y_strtrim
- string: wcstrim → y_wcstrim, strhash → y_strhash
- string: strreplace → y_strreplace, strsplit → y_strsplit
- string: delete wcsreplace, cmdsplit
- math: delete y_iround, y_pow2
- timer module migrated from yutil to ptk as internal timer_scheduler
- WinMain simplified to use __argc/__argv instead of cmdsplit
BREAKING CHANGE: all renamed yutil public APIs require downstream
to update function names. Deleted APIs require code changes:
- get_time_us: use y_gettime() for ms timestamps
- get_time_delta: use y_gettime() - start instead
- sleep_s: use y_sleep(ms) with millisecond argument
- wcsreplace: no replacement, had zero callers
- cmdsplit: WinMain now uses __argc/__argv from CRT
- timer_list_*: migrated to ptk timer_scheduler_*;
ptk_reset_timer still available for external use
120 lines
3.6 KiB
C
120 lines
3.6 KiB
C
/*
|
|
* app/components/code-block-copy.c - Click handler for the Copy
|
|
* button rendered by code-block-copy.tsx.
|
|
*
|
|
* The button lives inside `.code-block-body`, which itself sits
|
|
* under the demo wrapper `.code-block` (demo case) or directly
|
|
* under `.code-block` (mdx case). On click, the handler checks
|
|
* whether the body widget has a `data-source` attribute (demo case:
|
|
* the attribute is on the body); if not, it walks up to the
|
|
* `.code-block` ancestor and uses its `data-source`. The raw UTF-8
|
|
* source is then looked up in the generated code-snippets table
|
|
* and written as wide characters to the system clipboard.
|
|
*
|
|
* After a successful copy the `copied` class is added to the button
|
|
* widget and a 1.5s timeout removes it again. global.css flips the
|
|
* visibility of the inner `.copy-icon` / `.check-icon` based on that
|
|
* class, so callers see the checkmark until the timer fires.
|
|
* Back-to-back clicks (same or different button) cancel any pending
|
|
* revert before starting a new timer, so the feedback always lines
|
|
* up with the most recent action.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <wchar.h>
|
|
#include <LCUI.h>
|
|
#include "code-block-copy.tsx.h"
|
|
#include "code-block-copy.h"
|
|
#include "code-snippets.h"
|
|
|
|
typedef struct {
|
|
code_block_copy_react_t base;
|
|
} code_block_copy_t;
|
|
|
|
typedef struct {
|
|
ui_widget_t *button;
|
|
int timer_id;
|
|
} copy_feedback_t;
|
|
|
|
static copy_feedback_t g_feedback;
|
|
|
|
static ui_widget_t *find_ancestor_with_class(ui_widget_t *w, const char *cls)
|
|
{
|
|
while (w) {
|
|
if (ui_widget_has_class(w, cls)) {
|
|
return w;
|
|
}
|
|
w = w->parent;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void revert_copied_class(void *arg)
|
|
{
|
|
copy_feedback_t *fb = arg;
|
|
if (fb->button) {
|
|
ui_widget_remove_class(fb->button, "copied");
|
|
}
|
|
fb->button = NULL;
|
|
fb->timer_id = 0;
|
|
}
|
|
|
|
static void code_block_copy_on_click(ui_widget_t *w, ui_event_t *e, void *arg)
|
|
{
|
|
ui_widget_t *block;
|
|
const char *id;
|
|
const char *src;
|
|
size_t len;
|
|
size_t wlen;
|
|
wchar_t *wbuf;
|
|
|
|
block = find_ancestor_with_class(w, "code-block-body");
|
|
id = ui_widget_get_attr(block, "data-source");
|
|
if (!id) {
|
|
block = find_ancestor_with_class(w, "code-block");
|
|
id = ui_widget_get_attr(block, "data-source");
|
|
}
|
|
src = code_snippet_for(id);
|
|
|
|
len = strlen(src);
|
|
wbuf = malloc(sizeof(wchar_t) * (len + 1));
|
|
wlen = decode_utf8(wbuf, src, len + 1);
|
|
ptk_clipboard_set_text(wbuf, wlen);
|
|
free(wbuf);
|
|
|
|
if (g_feedback.timer_id) {
|
|
ptk_clear_timeout(g_feedback.timer_id);
|
|
if (g_feedback.button && g_feedback.button != w) {
|
|
ui_widget_remove_class(g_feedback.button, "copied");
|
|
}
|
|
}
|
|
ui_widget_add_class(w, "copied");
|
|
g_feedback.button = w;
|
|
g_feedback.timer_id =
|
|
ptk_set_timeout(1500, revert_copied_class, &g_feedback);
|
|
}
|
|
|
|
static void code_block_copy_init(ui_widget_t *w)
|
|
{
|
|
ui_widget_add_data(w, code_block_copy_proto, sizeof(code_block_copy_t));
|
|
code_block_copy_react_init(w);
|
|
}
|
|
|
|
static void code_block_copy_destroy(ui_widget_t *w)
|
|
{
|
|
code_block_copy_react_destroy(w);
|
|
}
|
|
|
|
ui_widget_t *ui_create_code_block_copy(void)
|
|
{
|
|
return ui_create_widget_with_prototype(code_block_copy_proto);
|
|
}
|
|
|
|
void ui_register_code_block_copy(void)
|
|
{
|
|
code_block_copy_init_prototype();
|
|
code_block_copy_proto->init = code_block_copy_init;
|
|
code_block_copy_proto->destroy = code_block_copy_destroy;
|
|
}
|