Skip to content

Commit

Permalink
Add block.clone()
Browse files Browse the repository at this point in the history
  • Loading branch information
JerwuQu committed Jun 3, 2022
1 parent a643fb8 commit a69244c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# wblocks2

Electric boogaloo

API not stable, but it works somewhat
Scriptable taskbar blocks

## Functions

Expand All @@ -20,6 +18,7 @@ Block functions:
- `block.setColor(r, g, b)`
- `block.setPadding(left, right)`
- `block.setVisible(bool)`
- `block.clone(keepVisibility=false)`
- `block.remove()`

## License
Expand Down
27 changes: 24 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO:
// - give blocks a `<block>.clone([keep-visibility=false])`
// - some way to reload scripts... maybe just restart process until it is figured out
// - click handlers for blocks

Expand Down Expand Up @@ -317,10 +316,10 @@ JSValue jsYieldToC(JSContext *ctx, JSValueConst this, int argc, JSValueConst *ar
return JS_UNDEFINED;
}

JSValue jsCreateBlock(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
JSValue createJSBlockFromSrc(JSContext *ctx, wblock_t *srcBlock)
{
wblock_t *block = xmalloc(sizeof(wblock_t));
memcpy(block, &defaultBlock, sizeof(wblock_t));
memcpy(block, srcBlock, sizeof(wblock_t));
block->font->refCount++;
block->head = NULL;
block->tail = headBlock;
Expand All @@ -335,6 +334,11 @@ JSValue jsCreateBlock(JSContext *ctx, JSValueConst this, int argc, JSValueConst
return obj;
}

JSValue jsCreateBlock(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
{
return createJSBlockFromSrc(ctx, &defaultBlock);
}

static inline wblock_t *getBlockThis(JSValueConst this)
{
return JS_GetOpaque(this, jsBlockClassId);
Expand Down Expand Up @@ -412,6 +416,22 @@ JSValue jsBlockSetVisible(JSContext *ctx, JSValueConst this, int argc, JSValueCo
return JS_UNDEFINED;
}

JSValue jsBlockClone(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
{
bool keepVisibility = false;
if (argc >= 1) {
if (!JS_IsBool(argv[0])) {
return JS_ThrowTypeError(ctx, "Invalid argument");
}
keepVisibility = JS_VALUE_GET_BOOL(argv[0]);
}
JSValue jsBlock = createJSBlockFromSrc(ctx, getBlockThis(this));
if (!keepVisibility) {
getBlockThis(jsBlock)->visible = true;
}
return jsBlock;
}

JSValue jsBlockRemove(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
{
wblock_t *block = getBlockThis(this);
Expand Down Expand Up @@ -614,6 +634,7 @@ int CALLBACK WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdS
JS_CFUNC_DEF("setColor", 3, jsBlockSetColor ),
JS_CFUNC_DEF("setPadding", 2, jsBlockSetPadding ),
JS_CFUNC_DEF("setVisible", 1, jsBlockSetVisible ),
JS_CFUNC_DEF("clone", 1, jsBlockClone ),
JS_CFUNC_DEF("remove", 0, jsBlockRemove ),
};
JS_SetPropertyFunctionList(ctx, proto, protoFuncs, 6);
Expand Down

0 comments on commit a69244c

Please sign in to comment.