forked from muffinista/hide-cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HideCursor.cc
101 lines (80 loc) · 2.28 KB
/
HideCursor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <nan.h>
#include <iostream>
#ifdef IS_MAC
#include <Carbon/Carbon.h>
#include <AppKit/AppKit.h>
#define NOT_HIDECURSOR_KEY "Menubar"
#endif
#ifdef IS_WINDOWS
#include <Windows.h>
#endif
NAN_METHOD(hide);
NAN_METHOD(show);
// Example with node ObjectWrap
// Based on https://nodejs.org/api/addons.html#addons_wrapping_c_objects but using NAN
class HideCursor : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
private:
explicit HideCursor();
~HideCursor();
static NAN_METHOD(New);
static Nan::Persistent<v8::Function> constructor;
};
Nan::Persistent<v8::Function> HideCursor::constructor;
NAN_MODULE_INIT(HideCursor::Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("HideCursor").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("HideCursor").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
HideCursor::HideCursor() {
}
HideCursor::~HideCursor() {
}
NAN_METHOD(HideCursor::New) {
if (info.IsConstructCall()) {
HideCursor *obj = new HideCursor();
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
else {
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {info[0]};
v8::Local<v8::Function> cons = Nan::New(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
}
using v8::FunctionTemplate;
NAN_METHOD(hide) {
#ifdef IS_MAC
[NSCursor hide];
#endif
#ifdef IS_WINDOWS
while(ShowCursor(false)>=0);
#endif
}
NAN_METHOD(show) {
#ifdef IS_MAC
[NSCursor unhide];
[NSCursor unhide];
[NSCursor unhide];
#endif
#ifdef IS_WINDOWS
while(ShowCursor(true)>=0);
#endif
}
NAN_MODULE_INIT(InitAll) {
Nan::Set(target, Nan::New("hide").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(hide)).ToLocalChecked());
Nan::Set(target, Nan::New("show").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(show)).ToLocalChecked());
// Passing target down to the next NAN_MODULE_INIT
HideCursor::Init(target);
}
#if NODE_MAJOR_VERSION >= 10
NAN_MODULE_WORKER_ENABLED(HideCursor, InitAll);
#else
NODE_MODULE(HideCursor, InitAll);
#endif