Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Branch for 0.3.0 #103

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
9 changes: 7 additions & 2 deletions examples/event-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ libui.startLoop();
function logAppend(line) {
const lines = log.text.split('\n');
if (lines.length > 25) {
log.text = lines.slice(1).join(os.EOL);
log.text = lines.slice(1).join("\n");
}
log.append(line + os.EOL);
log.append(line + "\n");
}

function setIntervalChanged() {
Expand Down Expand Up @@ -116,7 +116,10 @@ function makeToolbar() {
let timeoutHandle = null;
btnCustom.onClicked(() => {
if (timeoutHandle) {
console.log('before clearTimeout', timeoutHandle);
clearTimeout(timeoutHandle);
console.log('after clearTimeout', timeoutHandle);

timeoutHandle = null;
return;
}
Expand All @@ -125,7 +128,9 @@ function makeToolbar() {
timeoutHandle = setTimeout((a, b, c) => {
const elapsed = Date.now() - now;
logAppend(`Custom setTimeout: ${now} - elapsed ${elapsed} ms. Args: ${a} ${b} ${c}`);
timeoutHandle = null;
}, 1000, 'custom', 'args', 2);
console.log('after timeoutHandle', timeoutHandle);
});
toolbar.append(btnCustom, false);

Expand Down
85 changes: 85 additions & 0 deletions examples/example-from-napi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const libui = require('..');

const {UiWindow, UiHorizontalBox, UiMultilineEntry} = libui;

libui.onShouldQuit(() => {
libui.stopLoop();
global.gc();
});

function createWindow() {
let win = new UiWindow('Test Window', 800, 600, false);
win.margined = true;
const logEntry = new UiMultilineEntry();

const entry = new UiMultilineEntry();
entry.text = 'A test line\n';
entry.append('A second test line\n');
entry.onChanged(() => {
const msg = `Text changed to ${entry.text}`;
console.log(msg);
logEntry.append(msg + '\n');
});

const box = new UiHorizontalBox();
box.setPadded(true);
box.append(entry, true);
box.append(logEntry, true);

win.setChild(box);

win.onContentSizeChanged(() => {
const size = win.getContentSize();
console.log(`size changed to ${size.width}x${size.height}`);
});
let step = 0;
win.onClosing(() => {
if (win.getTitle() == 'Test Window') {
let interval = setInterval(() => {
if (step === 0) {
win.contentSize = {width: 400, height: 300};
}
if (step === 1) {
win.margined = true;
}
if (step === 2) {
win.margined = false;
win.fullscreen = true;
}
if (step === 3) {
win.fullscreen = false;
win.borderless = true;
}
if (step === 4) {
win.borderless = false;
}

if (step > 4) {
clearInterval(interval);
}

step++;

console.log({
Margined: win.margined,
Fullscreen: win.fullscreen,
Borderless: win.borderless,
});
}, 1000);
box.deleteAt(1);
return win.title = 'Wait some seconds please...';
}
console.log('closing', win.title);
win.close();
win = null;
libui.stopLoop();
});

win.show();
}

createWindow();
libui.startLoop();
setInterval(() => {
global.gc();
}, 10);
69 changes: 51 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,59 @@ function startLoop() {
asyncHook.enable();

setTimeoutNode = global.setTimeout;
global.setTimeout = function(cb, t) {
global.setTimeout = function(cb, timeout) {
const timeoutHandle = {running: true};

const args = Array.prototype.slice.call(arguments, 2);
return binding.lib.setTimeout(function() {
cb.apply(null, args);
}, t);
};
binding.lib.Ui.startTimer(timeout, function() {
// console.log('timeoutHandle.running ', timeoutHandle.running);
if (timeoutHandle.running) {
cb.apply(null, args);
}
return false;
});
return timeoutHandle;
}

clearTimeoutNode = global.clearTimeout;
global.clearTimeout = function(obj) {
if (obj && obj.constructor === binding.lib.TimeoutHandle) {

global.clearTimeout = function(timeoutHandle) {
if (timeoutHandle && typeof timeoutHandle.running === 'boolean') {
timeoutHandle.running = false;
// console.log('patched clearTimeout called');
binding.lib.clearTimeout(obj);
} else {
// console.log('node clearTimeout called');
// not created by us, use original
// clearTimeoutNode
clearTimeoutNode(obj);
clearTimeoutNode(timeoutHandle);
}
};

setIntervalNode = global.setInterval;
global.setInterval = function(cb, t) {
global.setInterval = function(cb, timeout) {
const timeoutHandle = {running: true};
const args = Array.prototype.slice.call(arguments, 2);
return binding.lib.setInterval(function() {
cb.apply(null, args);
}, t);
};
binding.lib.Ui.startTimer(timeout, function() {
if (timeoutHandle.running) {
cb.apply(null, args);
return true;
}
return false;
});
return timeoutHandle;
}

clearIntervalNode = global.clearInterval;
global.clearInterval = function(obj) {
if (obj && obj.constructor === binding.lib.TimeoutHandle) {

global.clearInterval = function(timeoutHandle) {
if (timeoutHandle && typeof timeoutHandle.running === 'boolean') {
timeoutHandle.running = false;
// console.log('patched clearInterval called');
binding.lib.clearInterval(obj);
} else {
// console.log('node clearInterval called');
// not created by us, use original
// clearTimeoutNode
clearIntervalNode(obj);
clearIntervalNode(timeoutHandle);
}
}
}
Expand Down Expand Up @@ -344,6 +359,24 @@ binding.lib.FontAttribute.prototype.getOTFeatures = function() {
return this.getOTFeaturesInternal();
};

binding.lib.UiDialogs.openFile = function(parent) {
const v = binding.lib.UiDialogs.openFileInternal(parent);
if (v) {
return v;
} else {
return null;
}
};

binding.lib.UiDialogs.saveFile = function(parent) {
const v = binding.lib.UiDialogs.saveFileInternal(parent);
if (v) {
return v;
} else {
return null;
}
};

const brushType = {
solid: 0,
linearGradient: 1,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libui-node",
"version": "0.2.0-rc1",
"version": "0.3.0-rc1",
"description": "Node.js bindings for libui",
"repository": "parro-it/libui-node",
"license": "MIT",
Expand Down
11 changes: 11 additions & 0 deletions src/Ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ static int onShouldQuit_cb(void *data) {
return 0;
}

static int uiTimer_cb(void *data) {
nbind::cbFunction *cb = (nbind::cbFunction *)data;
return cb->call<int>();
}

struct Ui {
static void main() {
uiMain();
Expand Down Expand Up @@ -40,6 +45,11 @@ struct Ui {
uiFreeInitError(err);
}
}

static void startTimer(int ms, nbind::cbFunction &cb) {
nbind::cbFunction *callbackJs = new nbind::cbFunction(cb);
uiTimer(ms, uiTimer_cb, callbackJs);
}
};

NBIND_CLASS(Ui) {
Expand All @@ -49,4 +59,5 @@ NBIND_CLASS(Ui) {
method(mainStep);
method(mainSteps);
method(onShouldQuit);
method(startTimer);
}
28 changes: 18 additions & 10 deletions src/UiWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,26 @@ NBIND_CLASS(UiWindow) {

struct UiDialogs {

static std::string openFile(UiWindow *parent) {
static std::string openFileInternal(UiWindow *parent) {
char *char_ptr = uiOpenFile(parent->getHandle());
std::string s(char_ptr);
uiFreeText(char_ptr);
return s;
if (char_ptr == NULL) {
return std::string("");
} else {
std::string s(char_ptr);
uiFreeText(char_ptr);
return s;
}
}

static std::string saveFile(UiWindow *parent) {
static std::string saveFileInternal(UiWindow *parent) {
char *char_ptr = uiSaveFile(parent->getHandle());
std::string s(char_ptr);
uiFreeText(char_ptr);
return s;
if (char_ptr == NULL) {
return std::string("");
} else {
std::string s(char_ptr);
uiFreeText(char_ptr);
return s;
}
}

static void msgBox(UiWindow *parent, std::string title,
Expand All @@ -189,8 +197,8 @@ struct UiDialogs {
};

NBIND_CLASS(UiDialogs) {
method(openFile);
method(saveFile);
method(openFileInternal);
method(saveFileInternal);
method(msgBox);
method(msgBoxError);
}
40 changes: 0 additions & 40 deletions src/arch/darwin/timer.mm

This file was deleted.

39 changes: 0 additions & 39 deletions src/arch/unix/timer.cc

This file was deleted.

Loading