diff --git a/binding.gyp b/binding.gyp index abf3038..a973bb3 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,7 +2,12 @@ "targets": [ { "target_name": "milsko", - "sources": ["src/milsko.cpp"], + "sources": [ + "src/milsko.cpp", + "src/widget.cpp", + "src/classes.cpp", + "src/pixmap.cpp", + ], "include_dirs": [ "= 18" - }, "devDependencies": { "@types/node": "^25.6.0", - "fast-xml-parser": "^5.6.0", + "fast-xml-parser": "^5.7.2", "node-gyp": "^12.2.0", "prebuildify": "^6.0.1", "prettier": "^3.8.2", diff --git a/scripts/bindgen.js b/scripts/bindgen.js index b45f444..af0f391 100644 --- a/scripts/bindgen.js +++ b/scripts/bindgen.js @@ -1,6 +1,7 @@ import { XMLParser } from "fast-xml-parser"; -import { createWriteStream } from "node:fs"; +import { createWriteStream, existsSync, mkdirSync, statSync } from "node:fs"; import { createRequire } from "node:module"; +import { join } from "node:path"; const { upstream } = createRequire(import.meta.url)("../package.json"); const source = @@ -10,6 +11,8 @@ const source = const structsOut = createWriteStream("lib/structs.ts"); const enumsOut = createWriteStream("lib/enums.ts"); const constantsOut = createWriteStream("lib/constants.ts"); +const widgetOut = createWriteStream("lib/widget.ts"); +const widgetsIndexOut = createWriteStream("lib/widgets/index.ts"); function toCamelCase(input) { const split = input.split("_"); @@ -33,6 +36,8 @@ function typeToTypescript(type) { return "number"; case "string": return "string"; + case "pixmap": + return "Pixmap"; default: "any"; } @@ -42,7 +47,6 @@ function scanStructs(root) { const list = root["structs"]["struct"]; for (const struct of list) { - console.log(struct); const structName = struct["@_name"]; structsOut.write("export interface " + structName + " {\n"); @@ -51,8 +55,14 @@ function scanStructs(root) { if (!type.startsWith("@_") && Array.isArray(children)) { for (const child of children) { const attributeName = toCamelCase(child["@_name"]); + let attributeType = typeToTypescript(type); + + if (type == "struct") { + attributeType = child["@_defname"]; + } + structsOut.write( - attributeName + ": " + typeToTypescript(type) + ";\n" + attributeName + ": " + attributeType + ";\n" ); } } @@ -115,11 +125,214 @@ function scanConstants(root) { constantsOut.end(); } +function getPropertyInfo(root, query) { + const properties = root["properties"]; + + for (const [type, children] of Object.entries(properties)) { + const match = children.find((k) => k["@_name"] == query); + if (match) { + return { + boolean: match["@_boolean"] == "yes", + defName: match["@_defname"], + type, + }; + } + } +} + +function getPropertySignature(inputName, inputType) { + let prefix = null, + setter = null, + getter = null; + + switch (inputType) { + case "string": + prefix = "S"; + setter = "setString"; + getter = "getString"; + break; + case "integer": + prefix = "I"; + setter = "setInteger"; + getter = "getInteger"; + break; + case "handler": + prefix = "C"; + setter = "addUserHandler"; + break; + default: + prefix = "V"; + setter = "setVoid"; + getter = "getVoid"; + break; + } + + return { prefix, setter, getter }; +} + +function createPropertyMethods(name, type, isBoolean, defName = "unknown") { + const output = []; + + // fluent setter + const { prefix, setter, getter } = getPropertySignature(name, type); + const methodName = toCamelCase( + "set" + name.charAt(0).toUpperCase() + name.slice(1)); + const propertyId = prefix + name; + + let finalType = isBoolean ? "boolean" : typeToTypescript(type); + const finalValue = isBoolean ? "+value" : "value"; + if (type == "struct") finalType = defName; + + output.push( + // signature + "public " + methodName + "(value: " + finalType + "): this {\n" + + + // call internal method + "return this." + setter + "(\"" + propertyId + "\"," + + finalValue + ");\n}\n\n" + ); + + // property setter and getter + const getterName = toCamelCase(name); + let cast = type == "pixmap" ? " as Pixmap" : ""; + + if (isBoolean) cast = " != 0" + else if (type == "struct") cast = "as " + defName + + output.push( + // getter + "get " + getterName + "(): " + finalType + " {\n" + + "return this." + getter + "(\"" + propertyId + + "\")" + cast + ";\n}\n\n" + + + // setter + "set " + getterName + "(value: " + finalType + ") {\n" + + "this." + setter + "(\"" + propertyId + "\", " + finalValue + ");\n}\n\n" + ); + + return output.join(""); +} + +function scanProperties(root) { + const properties = root["properties"]; + + widgetOut.write( + "import { BaseWidget } from \"./milsko\"\n" + + "import { Pixmap } from \"./types\"\n\n" + + "export class Widget extends BaseWidget {\n" + ); + + for (const [type, children] of Object.entries(properties)) { + for (const prop of children) { + if (prop["@_common"] != "yes") continue; + + const name = prop["@_name"]; + const isBoolean = prop["@_boolean"] == "yes"; + + widgetOut.write(createPropertyMethods(name, type, isBoolean)); + } + } + + widgetOut.write("\n}"); + widgetOut.close(); +} + +function scanWidgets(root) { + const widgets = root["widgets"]["widget"]; + const unsupported = ["OpenGL", "Vulkan"]; + + // first loop through widgets to detect for imports and print lines + for (const widget of widgets) { + const widgetName = widget["@_name"]; + if (unsupported.includes(widgetName)) continue; + + const typeImports = ["WidgetOptions"]; + const structImports = []; + const lines = []; + + const widgetFile = createWriteStream( + join("lib/widgets/", widgetName.toLowerCase() + ".ts") + ); + + // scan properties + if (widget["properties"]) { + const widgetProperties = widget["properties"]["property"]; + + for (const prop of widgetProperties) { + const name = prop["@_name"]; + let { boolean, defName, type } = getPropertyInfo(root, name); + + if (type == "struct") { + if (!structImports.includes(defName)) + structImports.push(defName); + } + + if (type == "pixmap") { + if (!typeImports.includes("Pixmap")) + typeImports.push("Pixmap"); + } + + // create methods + lines.push(createPropertyMethods(name, type, boolean, defName)) + } + } + + lines.push("}\n"); + + // after looping write the contents in the correct order + widgetFile.write( + "import { NativeClasses } from \"../milsko\"\n" + + "import { Widget } from \"../widget\"\n" + ); + + if (structImports.length) { + widgetFile.write( + "import { " + structImports.join(", ") + + " } from \"../structs\"\n" + ); + } + + widgetFile.write( + "import { " + typeImports.join(", ") + + " } from \"../types\"\n" + ); + + // init class + widgetFile.write("\nexport class " + widgetName + " extends Widget {\n"); + + // constructor + widgetFile.write( + "constructor(options: WidgetOptions) {\n" + + "super(NativeClasses." + widgetName + ", options);" + + "}\n\n" + ); + + for (const line of lines) { + widgetFile.write(line); + } + + widgetsIndexOut.write( + "export { " + widgetName + " } from \"./" + + widgetName.toLowerCase() + "\"\n" + ); + + widgetFile.close(); + } + + widgetsIndexOut.close(); +} + +function isArray(tagName) { + if (tagName == "property" || tagName == "struct") + return true; +} + async function bindgen() { const parser = new XMLParser({ attributeNamePrefix: "@_", ignoreAttributes: false, - parseAttributeValue: true + parseAttributeValue: true, + isArray }); const response = await fetch(source); @@ -135,14 +348,17 @@ async function bindgen() { const match = tags.find((k) => k.name == upstream); if (!match) { - throw new Error( - "Could not find a tag that matches the upstream version " + upstream + console.warn( + "Could not find a tag that matches the upstream version " + upstream + + ", using as commit hash instead." ); } - const commitHash = match["id"] - const specURL = "https://forgejo.nishi.boats/pyrite-dev/milsko/raw/commit/" + - commitHash + "/milsko.xml"; + const commitHash = match ? match["id"] : upstream; + const specURL = + "https://forgejo.nishi.boats/pyrite-dev/milsko/raw/commit/" + + commitHash + + "/milsko.xml"; const spac = await fetch(specURL); if (!spac.ok) { @@ -158,6 +374,8 @@ async function bindgen() { scanStructs(root); scanEnumerations(root); scanConstants(root); + scanProperties(root); + scanWidgets(root); } bindgen(); diff --git a/src/classes.cpp b/src/classes.cpp index e69de29..9553c4e 100644 --- a/src/classes.cpp +++ b/src/classes.cpp @@ -0,0 +1,32 @@ +#include "classes.hpp" +#include + +#define NAPI_MWCLASS(env, cls) \ + Napi::External<_MwClass>::New(env, cls) + +Napi::Object ClassesInit(Napi::Env env, Napi::Object exports) { + Napi::Object classes = Napi::Object::New(env); + + classes.Set("Box", NAPI_MWCLASS(env, MwBoxClass)); + classes.Set("Button", NAPI_MWCLASS(env, MwButtonClass)); + classes.Set("CheckBox", NAPI_MWCLASS(env, MwCheckBoxClass)); + classes.Set("Entry", NAPI_MWCLASS(env, MwEntryClass)); + classes.Set("Frame", NAPI_MWCLASS(env, MwFrameClass)); + classes.Set("Image", NAPI_MWCLASS(env, MwImageClass)); + classes.Set("Label", NAPI_MWCLASS(env, MwLabelClass)); + classes.Set("ListBox", NAPI_MWCLASS(env, MwListBoxClass)); + classes.Set("Menu", NAPI_MWCLASS(env, MwMenuClass)); + classes.Set("NumberEntry", NAPI_MWCLASS(env, MwNumberEntryClass)); + classes.Set("ScrollBar", NAPI_MWCLASS(env, MwScrollBarClass)); + classes.Set("SubMenu", NAPI_MWCLASS(env, MwSubMenuClass)); + classes.Set("SubWindow", NAPI_MWCLASS(env, MwSubWindowClass)); + classes.Set("Viewport", NAPI_MWCLASS(env, MwViewportClass)); + classes.Set("Window", NAPI_MWCLASS(env, MwWindowClass)); + classes.Set("ProgressBar", NAPI_MWCLASS(env, MwProgressBarClass)); + classes.Set("RadioBox", NAPI_MWCLASS(env, MwRadioBoxClass)); + classes.Set("ComboBox", NAPI_MWCLASS(env, MwComboBoxClass)); + classes.Set("TreeView", NAPI_MWCLASS(env, MwTreeViewClass)); + + exports.Set("NativeClasses", classes); + return exports; +}; diff --git a/src/milsko.cpp b/src/milsko.cpp index 59b2577..3910ea5 100644 --- a/src/milsko.cpp +++ b/src/milsko.cpp @@ -1,13 +1,17 @@ +#include "classes.hpp" +#include "pixmap.hpp" #include "widget.hpp" #include #include Napi::Object Init(Napi::Env env, Napi::Object exports) { - MwLibraryInit(); + MwLibraryInit(); - MwBaseWidget::Init(env, exports); + MwBaseWidget::Init(env, exports); + MwPixmap::Init(env, exports); + ClassesInit(env, exports); - return exports; + return exports; } NODE_API_MODULE(milsko, Init); \ No newline at end of file diff --git a/src/pixmap.cpp b/src/pixmap.cpp new file mode 100644 index 0000000..261b5c4 --- /dev/null +++ b/src/pixmap.cpp @@ -0,0 +1,153 @@ +#include "classes.hpp" +#include "widget.hpp" +#include + +Napi::FunctionReference MwPixmap::constructor; + +Napi::Object MwPixmap::Init(Napi::Env env, Napi::Object exports) { + Napi::Function func = DefineClass(env, "Pixmap", + { + InstanceMethod("getRaw", &MwPixmap::GetRaw), + InstanceMethod("reloadRaw", &MwPixmap::ReloadRaw), + InstanceAccessor("size", &MwPixmap::GetSize, nullptr), + }); + + constructor = Napi::Persistent(func); + constructor.SuppressDestruct(); + + exports.Set("Pixmap", func); + return exports; +} + +MwPixmap::MwPixmap(const Napi::CallbackInfo &info): Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() < 1 || !info[0].IsObject()) { + Napi::TypeError::New(env, "Expected an instance of widget as first argument") + .ThrowAsJavaScriptException(); + return; + } + + Napi::Object obj = info[0].As(); + MwBaseWidget *widget = MwBaseWidget::Unwrap(obj); + unsigned char *data = 0; + int width = 0; + int height = 0; + + if (!widget) { + Napi::TypeError::New(env, "First argument is not an instance of a widget") + .ThrowAsJavaScriptException(); + return; + } + + if (info.Length() > 1 && info[1].IsObject()) { + Napi::Object params = info[1].As(); + + if (params.Has("data")) { + Napi::Value dataParam = params.Get("data"); + + if (dataParam.IsBuffer()) { + data = dataParam.As>().Data(); + } else if (dataParam.IsArrayBuffer()) { + data = reinterpret_cast( + dataParam.As().Data() + ); + } else { + Napi::TypeError::New(env, "The data parameter must be of type Buffer or ArrayBuffer") + .ThrowAsJavaScriptException(); + return; + } + } + + if (params.Has("width")) { + Napi::Value widthParam = params.Get("width"); + + if (widthParam.IsNumber()) { + width = widthParam.As().Int32Value(); + } else { + Napi::TypeError::New(env, "The width parameter must be a number") + .ThrowAsJavaScriptException(); + return; + } + } + + if (params.Has("height")) { + Napi::Value heightParam = params.Get("height"); + + if (heightParam.IsNumber()) { + height = heightParam.As().Int32Value(); + } else { + Napi::TypeError::New(env, "The height parameter must be a number") + .ThrowAsJavaScriptException(); + return; + } + } + } + + handle = MwLoadRaw( + widget->handle, + data, + width, + height + ); +}; + +MwPixmap::~MwPixmap() { + if (handle) + MwLLDestroyPixmap(handle); +} + +Napi::Value MwPixmap::GetRaw(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + unsigned char *data = MwPixmapGetRaw(handle); + Napi::Buffer buffer = Napi::Buffer::New( + env, + data, + sizeof(data), + [](Napi::Env env, unsigned char* finalized) { + free(finalized); + } + ); + + return buffer; +} + +Napi::Value MwPixmap::GetSize(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::Object size = Napi::Object::New(env); + MwRect rect; + + MwPixmapGetSize(handle, &rect); + + size.Set("x", Napi::Number::New(env, rect.x)); + size.Set("y", Napi::Number::New(env, rect.y)); + size.Set("width", Napi::Number::New(env, rect.width)); + size.Set("height", Napi::Number::New(env, rect.height)); + + return size; +} + +Napi::Value MwPixmap::ReloadRaw(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 1 || !info[0].IsBuffer() || !info[0].IsArrayBuffer()) { + Napi::TypeError::New(env, "Expected a buffer or array buffer as argument") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + unsigned char *data = 0; + + if (info[0].IsBuffer()) { + data = info[0].As>().Data(); + } else if (info[0].IsArrayBuffer()) { + data = reinterpret_cast( + info[0].As().Data() + ); + } + + MwPixmapReloadRaw(handle, data); + return env.Undefined(); +} diff --git a/src/pixmap.hpp b/src/pixmap.hpp new file mode 100644 index 0000000..4ba0e78 --- /dev/null +++ b/src/pixmap.hpp @@ -0,0 +1,19 @@ +#pragma once +#include +#include + +class MwPixmap : public Napi::ObjectWrap { +public: + static Napi::FunctionReference constructor; + static Napi::Object Init(Napi::Env env, Napi::Object exports); + static Napi::Object From(Napi::Env env, MwLLPixmap &pixmap); + MwPixmap(const Napi::CallbackInfo &info); + ~MwPixmap(); + + MwLLPixmap handle; + +protected: + Napi::Value GetRaw(const Napi::CallbackInfo &info); + Napi::Value GetSize(const Napi::CallbackInfo &info); + Napi::Value ReloadRaw(const Napi::CallbackInfo &info); +}; \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..ab35dff --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,2 @@ +#include + diff --git a/src/widget.cpp b/src/widget.cpp index e802c30..e11d807 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -4,36 +4,46 @@ Napi::FunctionReference MwBaseWidget::constructor; Napi::Object MwBaseWidget::Init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); - Napi::Function func = DefineClass(env, "MwBaseWidget", + Napi::Function func = DefineClass(env, "BaseWidget", { - InstanceMethod("setArea", &SetArea), - InstanceMethod("move", &Move), - InstanceMethod("resize", &Resize), - InstanceMethod("step", &Step), - InstanceMethod("loop", &Loop), - InstanceMethod("show", &Show), - InstanceMethod("hideCursor", &HideCursor), - InstanceMethod("grabPointer", &GrabPointer), - InstanceMethod("getClipboard", &GetClipboard), - InstanceMethod("addTickList", &AddTickList), - InstanceMethod("forceRender", &ForceRender), - InstanceMethod("reparent", &Reparent), - InstanceMethod("add", &Add), - InstanceMethod("remove", &Remove), - InstanceAccessor("area", &GetArea, nullptr), - InstanceAccessor("isPending", &IsPending, nullptr), - InstanceAccessor("parent", &GetParent, nullptr), - InstanceAccessor("children", &GetChildren, nullptr), - InstanceAccessor("name", &GetName, nullptr), - InstanceAccessor("cursorCoord", &GetCursorCoord, nullptr), - InstanceAccessor("screenSize", &GetScreenSize, nullptr), - InstanceAccessor("coordinateType", &GetCoordinateType, nullptr), + InstanceMethod("setArea", &MwBaseWidget::GetArea), + InstanceMethod("move", &MwBaseWidget::Move), + InstanceMethod("resize", &MwBaseWidget::Resize), + InstanceMethod("step", &MwBaseWidget::Step), + InstanceMethod("loop", &MwBaseWidget::Loop), + InstanceMethod("show", &MwBaseWidget::Show), + InstanceMethod("hideCursor", &MwBaseWidget::HideCursor), + InstanceMethod("grabPointer", &MwBaseWidget::GrabPointer), + InstanceMethod("focus", &MwBaseWidget::Focus), + InstanceMethod("addTickList", &MwBaseWidget::AddTickList), + InstanceMethod("forceRender", &MwBaseWidget::ForceRender), + InstanceMethod("reparent", &MwBaseWidget::Reparent), + InstanceMethod("add", &MwBaseWidget::AddChild), + InstanceMethod("destroy", &MwBaseWidget::Destroy), + InstanceMethod("getClipboard", &MwBaseWidget::GetClipboard), + + InstanceMethod("setInteger", &MwBaseWidget::SetInteger), + InstanceMethod("setString", &MwBaseWidget::SetText), + InstanceMethod("setVoid", &MwBaseWidget::SetVoid), + InstanceMethod("getInteger", &MwBaseWidget::GetInteger), + InstanceMethod("getText", &MwBaseWidget::GetText), + InstanceMethod("getVoid", &MwBaseWidget::GetVoid), + + InstanceAccessor("area", &MwBaseWidget::GetArea, nullptr), + InstanceAccessor("isPending", &MwBaseWidget::IsPending, nullptr), + InstanceAccessor("parent", &MwBaseWidget::GetParent, nullptr), + InstanceAccessor("children", &MwBaseWidget::GetChildren, nullptr), + InstanceAccessor("name", &MwBaseWidget::GetName, nullptr), + InstanceAccessor("cursorCoord", &MwBaseWidget::GetCursorCoord, nullptr), + InstanceAccessor("screenSize", &MwBaseWidget::GetScreenSize, nullptr), + InstanceAccessor("coordinateType", &MwBaseWidget::GetCoordinateType, nullptr), + InstanceAccessor("nativeClass", &MwBaseWidget::GetNativeClass, nullptr), }); constructor = Napi::Persistent(func); constructor.SuppressDestruct(); - exports.Set("MwBaseWidget", func); + exports.Set("BaseWidget", func); return exports; }; @@ -107,7 +117,7 @@ Napi::Value MwBaseWidget::Move(const Napi::CallbackInfo &info) { .ThrowAsJavaScriptException(); } - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::Resize(const Napi::CallbackInfo &info) { @@ -127,7 +137,7 @@ Napi::Value MwBaseWidget::Resize(const Napi::CallbackInfo &info) { .ThrowAsJavaScriptException(); } - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::Step(const Napi::CallbackInfo &info) { @@ -146,11 +156,10 @@ Napi::Value MwBaseWidget::IsPending(const Napi::CallbackInfo &info) { Napi::Value MwBaseWidget::Loop(const Napi::CallbackInfo &info) { MwLoop(handle); - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::Show(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); bool toggle = true; if (info.Length() > 0 && info[0].IsBoolean()) { @@ -158,16 +167,15 @@ Napi::Value MwBaseWidget::Show(const Napi::CallbackInfo &info) { } MwShow(handle, static_cast(toggle)); - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::HideCursor(const Napi::CallbackInfo &info) { MwHideCursor(handle); - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::GrabPointer(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); bool toggle = true; if (info.Length() > 0 && info[0].IsBoolean()) { @@ -175,17 +183,22 @@ Napi::Value MwBaseWidget::GrabPointer(const Napi::CallbackInfo &info) { } MwGrabPointer(handle, static_cast(toggle)); - return Napi::Value(); + return info.This(); +} + +Napi::Value MwBaseWidget::Focus(const Napi::CallbackInfo &info) { + MwFocus(handle); + return info.This(); } Napi::Value MwBaseWidget::AddTickList(const Napi::CallbackInfo &info) { MwAddTickList(handle); - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::ForceRender(const Napi::CallbackInfo &info) { MwForceRender(handle); - return Napi::Value(); + return info.This(); } Napi::Value MwBaseWidget::Reparent(const Napi::CallbackInfo &info) { @@ -230,68 +243,76 @@ Napi::Value MwBaseWidget::Reparent(const Napi::CallbackInfo &info) { parent = newParent; newParent->children.push_back(this); - return Napi::Value(); + return info.This(); } -Napi::Value MwBaseWidget::Add(const Napi::CallbackInfo &info) { +Napi::Value MwBaseWidget::AddChild(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - if (info.Length() < 1 || !info[0].IsObject()) { + if (info.Length() < 1 || !info[0].IsArray()) { Napi::TypeError::New(env, "Expected an instance of a widget class as argument") .ThrowAsJavaScriptException(); return env.Undefined(); } - Napi::Object wrapped = info[0].As(); - const bool isInstance = wrapped.InstanceOf(constructor.Value()); + Napi::Array widgets = info[0].As(); - if (isInstance) { - MwBaseWidget *child = MwBaseWidget::Unwrap(wrapped); + for (uint32_t i = 0; i < widgets.Length(); i++) { + const Napi::Value value = widgets.Get(i); - if (child == this) { - Napi::Error::New(env, "Cannot add widget as child of itself") + if (!value.IsObject()) { + Napi::TypeError::New(env, + "Expected argument " + std::to_string(i) + "to be instance of a widget" + ) .ThrowAsJavaScriptException(); return env.Null(); } - if (child->parent) { - // if the current parent has this widget as a child, disown :( - for (int i = 0; i < child->parent->children.size(); i++) { - if (child->parent->children[i] == this) { - child->parent->children.erase(child->parent->children.begin() + i); - break; + const Napi::Object wrapped = widgets.As(); + const bool isInstance = wrapped.InstanceOf(constructor.Value()); + + if (isInstance) { + MwBaseWidget *child = MwBaseWidget::Unwrap(wrapped); + + if (child == this) { + Napi::Error::New(env, "Cannot add widget as child of itself") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + if (child->parent) { + // if the current parent has this widget as a child, disown :( + for (size_t i = 0; i < child->parent->children.size(); i++) { + if (child->parent->children[i] == this) { + child->parent->children.erase(child->parent->children.begin() + i); + break; + } } } + + MwReparent( + child->handle, + handle + ); + + child->parent = this; + children.push_back(child); + } else { + Napi::TypeError::New(env,"Argument " + std::to_string(i) + "is not an instance of a widget") + .ThrowAsJavaScriptException(); + return env.Null(); } - - MwReparent( - child->handle, - handle - ); - - child->parent = this; - children.push_back(child); - } else { - Napi::TypeError::New(env, "Provided argument is not an instance of a widget") - .ThrowAsJavaScriptException(); - return env.Null(); } - return Napi::Value(); + return info.This(); } -Napi::Value MwBaseWidget::Remove(const Napi::CallbackInfo &info) { - if (parent) { - for (size_t i = 0; i < parent->children.size(); i++) { - if (parent->children[i] == this) { - parent->children.erase(parent->children.begin() + i); - break; - } - } - } +Napi::Value MwBaseWidget::Destroy(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + if (handle) + MwDestroyWidget(handle); - MwReparent(handle, nullptr); - return Napi::Value(); + return env.Null(); } Napi::Value MwBaseWidget::GetParent(const Napi::CallbackInfo &info) { @@ -366,3 +387,136 @@ Napi::Value MwBaseWidget::GetClipboard(const Napi::CallbackInfo &info) { return env.Undefined(); } + +Napi::Value MwBaseWidget::GetNativeClass(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + MwClass cls = MwGetClass(handle); + + return Napi::External<_MwClass>::New(env, cls); +} + +Napi::Value MwBaseWidget::SetInteger(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 2) { + Napi::TypeError::New(env, "Expected two arguments for key and value") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + if (!info[0].IsString() || !info[1].IsNumber()) { + Napi::TypeError::New(env, "Expected key to be a string and value to be a number") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + const std::string key = info[0].As().Utf8Value(); + const int value = info[1].As().Int32Value(); + + MwSetInteger(handle, key.c_str(), value); + return info.This(); +} + +Napi::Value MwBaseWidget::SetText(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 2) { + Napi::TypeError::New(env, "Expected two arguments for key and value") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + if (!info[0].IsString() || !info[1].IsString()) { + Napi::TypeError::New(env, "Expected key and value to be a string") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + const std::string key = info[0].As().Utf8Value(); + const std::string value = info[1].As().Utf8Value(); + + MwSetText(handle, key.c_str(), value.c_str()); + return info.This(); +} + +Napi::Value MwBaseWidget::SetVoid(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 2) { + Napi::TypeError::New(env, "Expected two arguments for key and value") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + if (!info[0].IsString() || !info[1].IsObject()) { + Napi::TypeError::New(env, "Expected key to be a string and value to be a native object") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + const std::string key = info[0].As().Utf8Value(); + const Napi::Object obj = info[1].As(); + + MwPixmap *pixmap = MwPixmap::Unwrap(obj); + + if (pixmap) { + MwSetVoid(handle, key.c_str(), pixmap->handle); + } else { + Napi::TypeError::New(env, "Expected second argument to be a valid native object") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + return info.This(); +} + +Napi::Value MwBaseWidget::GetInteger(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 1 || !info[0].IsString()) { + Napi::TypeError::New(env, "Expected key to be an argument of type string") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + const std::string key = info[0].As().Utf8Value(); + const int value = MwGetInteger(handle, key.c_str()); + + return Napi::Number::New(env, static_cast(value)); +} + +Napi::Value MwBaseWidget::GetText(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 1 || !info[0].IsString()) { + Napi::TypeError::New(env, "Expected key to be an argument of type string") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + const std::string key = info[0].As().Utf8Value(); + const char *value = MwGetText(handle, key.c_str()); + + return Napi::String::New(env, value); +} + +Napi::Value MwBaseWidget::GetVoid(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + if (info.Length() < 1 || !info[0].IsString()) { + Napi::TypeError::New(env, "Expected key to be an argument of type string") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + if (!info[0].IsString() || !info[1].IsObject()) { + Napi::TypeError::New(env, "Expected key to be a string and value to be a native object") + .ThrowAsJavaScriptException(); + return env.Null(); + } + + const std::string key = info[0].As().Utf8Value(); + void *value = MwGetVoid(handle, key.c_str()); + + return Napi::External::New(env, value); +} diff --git a/src/widget.hpp b/src/widget.hpp index cc47ebf..2f83845 100644 --- a/src/widget.hpp +++ b/src/widget.hpp @@ -1,42 +1,53 @@ #pragma once +#include "pixmap.hpp" #include #include #include class MwBaseWidget : public Napi::ObjectWrap { public: - static Napi::Object Init(Napi::Env env, Napi::Object exports); - MwBaseWidget(const Napi::CallbackInfo &info); - ~MwBaseWidget(); + static Napi::Object Init(Napi::Env env, Napi::Object exports); + MwBaseWidget(const Napi::CallbackInfo &info); + ~MwBaseWidget(); protected: - static Napi::FunctionReference constructor; + static Napi::FunctionReference constructor; + friend class MwPixmap; - MwWidget handle; - MwBaseWidget *parent; - std::vector children; + MwWidget handle; + MwBaseWidget *parent; + std::vector children; - Napi::Value SetArea(const Napi::CallbackInfo &info); - Napi::Value Move(const Napi::CallbackInfo &info); - Napi::Value Resize(const Napi::CallbackInfo &info); - Napi::Value Step(const Napi::CallbackInfo &info); - Napi::Value Loop(const Napi::CallbackInfo &info); - Napi::Value Show(const Napi::CallbackInfo &info); - Napi::Value HideCursor(const Napi::CallbackInfo &info); - Napi::Value GrabPointer(const Napi::CallbackInfo &info); - Napi::Value AddTickList(const Napi::CallbackInfo &info); - Napi::Value ForceRender(const Napi::CallbackInfo &info); - Napi::Value Reparent(const Napi::CallbackInfo &info); - Napi::Value Add(const Napi::CallbackInfo &info); - Napi::Value Remove(const Napi::CallbackInfo &info); - Napi::Value GetClipboard(const Napi::CallbackInfo &info); + Napi::Value SetArea(const Napi::CallbackInfo &info); + Napi::Value Move(const Napi::CallbackInfo &info); + Napi::Value Resize(const Napi::CallbackInfo &info); + Napi::Value Step(const Napi::CallbackInfo &info); + Napi::Value Loop(const Napi::CallbackInfo &info); + Napi::Value Show(const Napi::CallbackInfo &info); + Napi::Value HideCursor(const Napi::CallbackInfo &info); + Napi::Value GrabPointer(const Napi::CallbackInfo &info); + Napi::Value Focus(const Napi::CallbackInfo &info); + Napi::Value AddTickList(const Napi::CallbackInfo &info); + Napi::Value ForceRender(const Napi::CallbackInfo &info); + Napi::Value Reparent(const Napi::CallbackInfo &info); + Napi::Value AddChild(const Napi::CallbackInfo &info); + Napi::Value Destroy(const Napi::CallbackInfo &info); + Napi::Value GetClipboard(const Napi::CallbackInfo &info); + + Napi::Value SetInteger(const Napi::CallbackInfo &info); + Napi::Value SetText(const Napi::CallbackInfo &info); + Napi::Value SetVoid(const Napi::CallbackInfo &info); + Napi::Value GetInteger(const Napi::CallbackInfo &info); + Napi::Value GetText(const Napi::CallbackInfo &info); + Napi::Value GetVoid(const Napi::CallbackInfo &info); - Napi::Value GetArea(const Napi::CallbackInfo &info); - Napi::Value IsPending(const Napi::CallbackInfo &info); - Napi::Value GetParent(const Napi::CallbackInfo &info); - Napi::Value GetChildren(const Napi::CallbackInfo &info); - Napi::Value GetName(const Napi::CallbackInfo &info); - Napi::Value GetCursorCoord(const Napi::CallbackInfo &info); - Napi::Value GetScreenSize(const Napi::CallbackInfo &info); - Napi::Value GetCoordinateType(const Napi::CallbackInfo &info); -}; \ No newline at end of file + Napi::Value GetArea(const Napi::CallbackInfo &info); + Napi::Value IsPending(const Napi::CallbackInfo &info); + Napi::Value GetParent(const Napi::CallbackInfo &info); + Napi::Value GetChildren(const Napi::CallbackInfo &info); + Napi::Value GetName(const Napi::CallbackInfo &info); + Napi::Value GetCursorCoord(const Napi::CallbackInfo &info); + Napi::Value GetScreenSize(const Napi::CallbackInfo &info); + Napi::Value GetCoordinateType(const Napi::CallbackInfo &info); + Napi::Value GetNativeClass(const Napi::CallbackInfo &info); +}; diff --git a/tsconfig.json b/tsconfig.json index eb9671c..54669ff 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,5 +13,10 @@ "declaration": true, "isolatedModules": true }, - "exclude": ["scripts"] + "exclude": [ + "scripts", + "examples", + "node_modules", + "dist" + ] }