From cb4d43f6d0a45292275d6e77282b34f615f21973 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 27 Mar 2023 17:02:52 +0200 Subject: [PATCH] [quikcjs] libdom unibar.c --- src/ecmascript/libdom/quickjs/Makefile | 2 +- src/ecmascript/libdom/quickjs/meson.build | 2 +- src/ecmascript/libdom/quickjs/unibar.c | 169 ++++++++++++++++++++++ src/ecmascript/quickjs/unibar.cpp | 3 + src/ecmascript/quickjs/unibar.h | 9 +- 5 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 src/ecmascript/libdom/quickjs/unibar.c diff --git a/src/ecmascript/libdom/quickjs/Makefile b/src/ecmascript/libdom/quickjs/Makefile index 162fa5215..e4815d2f0 100644 --- a/src/ecmascript/libdom/quickjs/Makefile +++ b/src/ecmascript/libdom/quickjs/Makefile @@ -1,6 +1,6 @@ top_builddir=../../../.. include $(top_builddir)/Makefile.config -OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o localstorage.o mapa.obj navigator.o screen.o +OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o localstorage.o mapa.obj navigator.o screen.o unibar.o include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/libdom/quickjs/meson.build b/src/ecmascript/libdom/quickjs/meson.build index 8b34510f5..7cf31322e 100644 --- a/src/ecmascript/libdom/quickjs/meson.build +++ b/src/ecmascript/libdom/quickjs/meson.build @@ -1 +1 @@ -srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp', 'navigator.c', 'screen.c') +srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp', 'navigator.c', 'screen.c', 'unibar.c') diff --git a/src/ecmascript/libdom/quickjs/unibar.c b/src/ecmascript/libdom/quickjs/unibar.c new file mode 100644 index 000000000..2274dc7fc --- /dev/null +++ b/src/ecmascript/libdom/quickjs/unibar.c @@ -0,0 +1,169 @@ +/* The quickjs unibar objects implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "dialogs/status.h" +#include "document/view.h" +#include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" +#include "ecmascript/quickjs/unibar.h" +#include "main/select.h" +#include "session/session.h" + +#define countof(x) (sizeof(x) / sizeof((x)[0])) + +static JSClassID js_menubar_class_id; +static JSClassID js_statusbar_class_id; + +static JSValue +js_unibar_get_property_visible(JSContext *ctx, JSValueConst this_val, int magic) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return JS_UNDEFINED; + } + + struct session_status *status = &doc_view->session->status; + +#define unibar_fetch(bar) \ + status->force_show_##bar##_bar >= 0 \ + ? status->force_show_##bar##_bar \ + : status->show_##bar##_bar + switch (magic) { + case 0: + return JS_NewBool(ctx, unibar_fetch(status)); + case 1: + return JS_NewBool(ctx, unibar_fetch(title)); + default: + return JS_NewBool(ctx, 0); + } +#undef unibar_fetch +} + +static JSValue +js_unibar_set_property_visible(JSContext *ctx, JSValueConst this_val, JSValue val, int magic) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + REF_JS(val); + + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return JS_UNDEFINED; + } + + struct session_status *status = &doc_view->session->status; + + switch (magic) { + case 0: + status->force_show_status_bar = JS_ToBool(ctx, val); + break; + case 1: + status->force_show_title_bar = JS_ToBool(ctx, val); + break; + default: + break; + } + register_bottom_half(update_status, NULL); + + return JS_UNDEFINED; +} + +static JSValue +js_menubar_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + + return JS_NewString(ctx, "[menubar object]"); +} + +static JSValue +js_statusbar_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + + return JS_NewString(ctx, "[statusbar object]"); +} + +static const JSCFunctionListEntry js_menubar_proto_funcs[] = { + JS_CGETSET_MAGIC_DEF("visible", js_unibar_get_property_visible, js_unibar_set_property_visible, 1), + JS_CFUNC_DEF("toString", 0, js_menubar_toString) +}; + +static const JSCFunctionListEntry js_statusbar_proto_funcs[] = { + JS_CGETSET_MAGIC_DEF("visible", js_unibar_get_property_visible, js_unibar_set_property_visible, 0), + JS_CFUNC_DEF("toString", 0, js_statusbar_toString) +}; + +static JSClassDef js_menubar_class = { + "menubar", +}; + +static JSClassDef js_statusbar_class = { + "statusbar", +}; + +int +js_unibar_init(JSContext *ctx) +{ + JSValue menubar_proto; + JSValue statusbar_proto; + + /* create the menubar class */ + JS_NewClassID(&js_menubar_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_menubar_class_id, &js_menubar_class); + + JSValue global_obj = JS_GetGlobalObject(ctx); + REF_JS(global_obj); + + menubar_proto = JS_NewObject(ctx); + REF_JS(menubar_proto); + + JS_SetPropertyFunctionList(ctx, menubar_proto, js_menubar_proto_funcs, countof(js_menubar_proto_funcs)); + + JS_SetClassProto(ctx, js_menubar_class_id, menubar_proto); + JS_SetPropertyStr(ctx, global_obj, "menubar", JS_DupValue(ctx, menubar_proto)); + + /* create the statusbar class */ + JS_NewClassID(&js_statusbar_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_statusbar_class_id, &js_statusbar_class); + + statusbar_proto = JS_NewObject(ctx); + REF_JS(statusbar_proto); + + JS_SetPropertyFunctionList(ctx, statusbar_proto, js_statusbar_proto_funcs, countof(js_statusbar_proto_funcs)); + + JS_SetClassProto(ctx, js_statusbar_class_id, statusbar_proto); + JS_SetPropertyStr(ctx, global_obj, "statusbar", JS_DupValue(ctx, statusbar_proto)); + + JS_FreeValue(ctx, global_obj); + + return 0; +} diff --git a/src/ecmascript/quickjs/unibar.cpp b/src/ecmascript/quickjs/unibar.cpp index 8b86ee8d3..259453d0f 100644 --- a/src/ecmascript/quickjs/unibar.cpp +++ b/src/ecmascript/quickjs/unibar.cpp @@ -43,6 +43,8 @@ #include "viewer/text/link.h" #include "viewer/text/vs.h" +#ifndef CONFIG_LIBDOM + #define countof(x) (sizeof(x) / sizeof((x)[0])) static JSClassID js_menubar_class_id; @@ -192,3 +194,4 @@ js_unibar_init(JSContext *ctx) return 0; } +#endif diff --git a/src/ecmascript/quickjs/unibar.h b/src/ecmascript/quickjs/unibar.h index db5dedbf8..a4c3453d8 100644 --- a/src/ecmascript/quickjs/unibar.h +++ b/src/ecmascript/quickjs/unibar.h @@ -1,9 +1,16 @@ - #ifndef EL__ECMASCRIPT_QUICKJS_UNIBAR_H #define EL__ECMASCRIPT_QUICKJS_UNIBAR_H #include +#ifdef __cplusplus +extern "C" { +#endif + int js_unibar_init(JSContext *ctx); +#ifdef __cplusplus +} +#endif + #endif