@osmn-byhn/widget-core-windows 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +154 -162
- package/binding.gyp +44 -69
- package/dist/autostart.d.ts +1 -7
- package/dist/autostart.d.ts.map +1 -1
- package/dist/autostart.js +8 -106
- package/dist/autostart.js.map +1 -1
- package/dist/autostart.test.js +4 -20
- package/dist/autostart.test.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -16
- package/dist/index.js.map +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/test.js +53 -53
- package/native/WidgetManager.h +29 -26
- package/native/host.cpp +70 -70
- package/native/main.cpp +65 -57
- package/native/win32/WidgetManager.cpp +246 -246
- package/package.json +49 -49
- package/native/linux/WidgetManager.cpp +0 -278
- package/native/macos/WidgetManager.mm +0 -87
package/native/main.cpp
CHANGED
|
@@ -1,57 +1,65 @@
|
|
|
1
|
-
#include <napi.h>
|
|
2
|
-
#include "WidgetManager.h"
|
|
3
|
-
|
|
4
|
-
Napi::Value CreateWidget(const Napi::CallbackInfo& info) {
|
|
5
|
-
Napi::Env env = info.Env();
|
|
6
|
-
|
|
7
|
-
if (info.Length() < 2 || !info[0].IsString() || !info[1].IsObject()) {
|
|
8
|
-
Napi::TypeError::New(env, "String and Object expected").ThrowAsJavaScriptException();
|
|
9
|
-
return env.Null();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
std::string url = info[0].As<Napi::String>().Utf8Value();
|
|
13
|
-
Napi::Object opts = info[1].As<Napi::Object>();
|
|
14
|
-
|
|
15
|
-
WidgetOptions options;
|
|
16
|
-
options.width = opts.Get("width").ToNumber().Int32Value();
|
|
17
|
-
options.height = opts.Get("height").ToNumber().Int32Value();
|
|
18
|
-
options.x = opts.Get("x").ToNumber().Int32Value();
|
|
19
|
-
options.y = opts.Get("y").ToNumber().Int32Value();
|
|
20
|
-
options.opacity = opts.Has("opacity") ? opts.Get("opacity").ToNumber().DoubleValue() : 1.0;
|
|
21
|
-
options.blur = opts.Has("blur") ? opts.Get("blur").ToBoolean().Value() : false;
|
|
22
|
-
options.sticky = opts.Has("sticky") ? opts.Get("sticky").ToBoolean().Value() : true;
|
|
23
|
-
options.interactive = opts.Has("interactive") ? opts.Get("interactive").ToBoolean().Value() : true;
|
|
24
|
-
options.html = opts.Has("html") ? opts.Get("html").ToString().Utf8Value() : "";
|
|
25
|
-
options.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
Napi::
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Napi::
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include "WidgetManager.h"
|
|
3
|
+
|
|
4
|
+
Napi::Value CreateWidget(const Napi::CallbackInfo& info) {
|
|
5
|
+
Napi::Env env = info.Env();
|
|
6
|
+
|
|
7
|
+
if (info.Length() < 2 || !info[0].IsString() || !info[1].IsObject()) {
|
|
8
|
+
Napi::TypeError::New(env, "String and Object expected").ThrowAsJavaScriptException();
|
|
9
|
+
return env.Null();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
std::string url = info[0].As<Napi::String>().Utf8Value();
|
|
13
|
+
Napi::Object opts = info[1].As<Napi::Object>();
|
|
14
|
+
|
|
15
|
+
WidgetOptions options;
|
|
16
|
+
options.width = opts.Get("width").ToNumber().Int32Value();
|
|
17
|
+
options.height = opts.Get("height").ToNumber().Int32Value();
|
|
18
|
+
options.x = opts.Get("x").ToNumber().Int32Value();
|
|
19
|
+
options.y = opts.Get("y").ToNumber().Int32Value();
|
|
20
|
+
options.opacity = opts.Has("opacity") ? opts.Get("opacity").ToNumber().DoubleValue() : 1.0;
|
|
21
|
+
options.blur = opts.Has("blur") ? opts.Get("blur").ToBoolean().Value() : false;
|
|
22
|
+
options.sticky = opts.Has("sticky") ? opts.Get("sticky").ToBoolean().Value() : true;
|
|
23
|
+
options.interactive = opts.Has("interactive") ? opts.Get("interactive").ToBoolean().Value() : true;
|
|
24
|
+
options.html = opts.Has("html") ? opts.Get("html").ToString().Utf8Value() : "";
|
|
25
|
+
options.css = opts.Has("css") ? opts.Get("css").ToString().Utf8Value() : "";
|
|
26
|
+
options.js = opts.Has("js") ? opts.Get("js").ToString().Utf8Value() : "";
|
|
27
|
+
Napi::Object config = Napi::Object::New(env);
|
|
28
|
+
if (opts.Has("keys")) config.Set("keys", opts.Get("keys"));
|
|
29
|
+
if (opts.Has("data")) config.Set("data", opts.Get("data"));
|
|
30
|
+
Napi::Value serializedConfig = env.Global().Get("JSON").As<Napi::Object>()
|
|
31
|
+
.Get("stringify").As<Napi::Function>().Call(env.Global().Get("JSON"), { config });
|
|
32
|
+
options.config = serializedConfig.As<Napi::String>().Utf8Value();
|
|
33
|
+
options.scroll = opts.Has("scroll") ? opts.Get("scroll").ToBoolean().Value() : true;
|
|
34
|
+
|
|
35
|
+
void* handle = WidgetManager::CreateWidget(url, options);
|
|
36
|
+
return Napi::External<void>::New(env, handle);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
Napi::Value UpdateOpacity(const Napi::CallbackInfo& info) {
|
|
40
|
+
Napi::Env env = info.Env();
|
|
41
|
+
void* handle = info[0].As<Napi::External<void>>().Data();
|
|
42
|
+
double opacity = info[1].As<Napi::Number>().DoubleValue();
|
|
43
|
+
|
|
44
|
+
WidgetManager::UpdateOpacity(handle, opacity);
|
|
45
|
+
return env.Undefined();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Napi::Value UpdatePosition(const Napi::CallbackInfo& info) {
|
|
49
|
+
Napi::Env env = info.Env();
|
|
50
|
+
void* handle = info[0].As<Napi::External<void>>().Data();
|
|
51
|
+
int x = info[1].As<Napi::Number>().Int32Value();
|
|
52
|
+
int y = info[2].As<Napi::Number>().Int32Value();
|
|
53
|
+
|
|
54
|
+
WidgetManager::UpdatePosition(handle, x, y);
|
|
55
|
+
return env.Undefined();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
59
|
+
exports.Set("createWidget", Napi::Function::New(env, CreateWidget));
|
|
60
|
+
exports.Set("updateOpacity", Napi::Function::New(env, UpdateOpacity));
|
|
61
|
+
exports.Set("updatePosition", Napi::Function::New(env, UpdatePosition));
|
|
62
|
+
return exports;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
NODE_API_MODULE(widget_shield_native, Init)
|
|
@@ -1,246 +1,246 @@
|
|
|
1
|
-
#include "WidgetManager.h"
|
|
2
|
-
#include <windows.h>
|
|
3
|
-
#include <dwmapi.h>
|
|
4
|
-
#include <exdisp.h>
|
|
5
|
-
#include <mshtml.h>
|
|
6
|
-
#include <string>
|
|
7
|
-
#include <thread>
|
|
8
|
-
#include <mutex>
|
|
9
|
-
#include <condition_variable>
|
|
10
|
-
#include <queue>
|
|
11
|
-
#include <atomic>
|
|
12
|
-
#include <iostream>
|
|
13
|
-
|
|
14
|
-
// Minimal implementation for embedding IWebBrowser2
|
|
15
|
-
class SimpleClientSite : public IOleClientSite, public IOleInPlaceSite, public IOleInPlaceFrame {
|
|
16
|
-
public:
|
|
17
|
-
STDMETHODIMP QueryInterface(REFIID riid, void** ppv) {
|
|
18
|
-
if (riid == IID_IUnknown || riid == IID_IOleClientSite) *ppv = (IOleClientSite*)this;
|
|
19
|
-
else if (riid == IID_IOleInPlaceSite) *ppv = (IOleInPlaceSite*)this;
|
|
20
|
-
else if (riid == IID_IOleInPlaceFrame || riid == IID_IOleInPlaceUIWindow) *ppv = (IOleInPlaceFrame*)this;
|
|
21
|
-
else return (*ppv = NULL, E_NOINTERFACE);
|
|
22
|
-
return S_OK;
|
|
23
|
-
}
|
|
24
|
-
STDMETHODIMP_(ULONG) AddRef() { return 1; }
|
|
25
|
-
STDMETHODIMP_(ULONG) Release() { return 1; }
|
|
26
|
-
STDMETHODIMP SaveObject() { return E_NOTIMPL; }
|
|
27
|
-
STDMETHODIMP GetMoniker(DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
|
|
28
|
-
STDMETHODIMP GetContainer(IOleContainer**) { return E_NOTIMPL; }
|
|
29
|
-
STDMETHODIMP ShowObject() { return S_OK; }
|
|
30
|
-
STDMETHODIMP OnShowWindow(BOOL) { return S_OK; }
|
|
31
|
-
STDMETHODIMP RequestNewObjectLayout() { return E_NOTIMPL; }
|
|
32
|
-
STDMETHODIMP GetWindow(HWND* phwnd) { *phwnd = m_hwnd; return S_OK; }
|
|
33
|
-
STDMETHODIMP ContextSensitiveHelp(BOOL) { return E_NOTIMPL; }
|
|
34
|
-
STDMETHODIMP CanInPlaceActivate() { return S_OK; }
|
|
35
|
-
STDMETHODIMP OnInPlaceActivate() { return S_OK; }
|
|
36
|
-
STDMETHODIMP OnUIActivate() { return S_OK; }
|
|
37
|
-
STDMETHODIMP GetWindowContext(IOleInPlaceFrame** ppFrame, IOleInPlaceUIWindow** ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo) {
|
|
38
|
-
*ppFrame = (IOleInPlaceFrame*)this;
|
|
39
|
-
*ppDoc = NULL;
|
|
40
|
-
GetClientRect(m_hwnd, lprcPosRect);
|
|
41
|
-
GetClientRect(m_hwnd, lprcClipRect);
|
|
42
|
-
lpFrameInfo->cb = sizeof(OLEINPLACEFRAMEINFO);
|
|
43
|
-
lpFrameInfo->fMDIApp = FALSE;
|
|
44
|
-
lpFrameInfo->hwndFrame = m_hwnd;
|
|
45
|
-
lpFrameInfo->haccel = NULL;
|
|
46
|
-
lpFrameInfo->cAccelEntries = 0;
|
|
47
|
-
return S_OK;
|
|
48
|
-
}
|
|
49
|
-
STDMETHODIMP Scroll(SIZE) { return E_NOTIMPL; }
|
|
50
|
-
STDMETHODIMP OnUIDeactivate(BOOL) { return S_OK; }
|
|
51
|
-
STDMETHODIMP OnInPlaceDeactivate() { return S_OK; }
|
|
52
|
-
STDMETHODIMP DiscardUndoState() { return E_NOTIMPL; }
|
|
53
|
-
STDMETHODIMP DeactivateAndUndo() { return E_NOTIMPL; }
|
|
54
|
-
STDMETHODIMP OnPosRectChange(LPCRECT) { return S_OK; }
|
|
55
|
-
STDMETHODIMP InsertMenus(HMENU, LPOLEMENUGROUPWIDTHS) { return E_NOTIMPL; }
|
|
56
|
-
STDMETHODIMP SetMenu(HMENU, HOLEMENU, HWND) { return E_NOTIMPL; }
|
|
57
|
-
STDMETHODIMP RemoveMenus(HMENU) { return E_NOTIMPL; }
|
|
58
|
-
STDMETHODIMP SetStatusText(LPCOLESTR) { return E_NOTIMPL; }
|
|
59
|
-
STDMETHODIMP EnableModeless(BOOL) { return E_NOTIMPL; }
|
|
60
|
-
STDMETHODIMP TranslateAccelerator(LPMSG, WORD) { return E_NOTIMPL; }
|
|
61
|
-
STDMETHODIMP GetBorder(LPRECT) { return E_NOTIMPL; }
|
|
62
|
-
STDMETHODIMP RequestBorderSpace(LPCBORDERWIDTHS) { return E_NOTIMPL; }
|
|
63
|
-
STDMETHODIMP SetBorderSpace(LPCBORDERWIDTHS) { return E_NOTIMPL; }
|
|
64
|
-
STDMETHODIMP SetActiveObject(IOleInPlaceActiveObject*, LPCOLESTR) { return E_NOTIMPL; }
|
|
65
|
-
HWND m_hwnd;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
struct Win32WidgetContext {
|
|
69
|
-
HWND hwnd;
|
|
70
|
-
IWebBrowser2* webBrowser;
|
|
71
|
-
SimpleClientSite clientSite;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
struct Win32Task {
|
|
75
|
-
enum Type { CREATE, OPACITY, POSITION } type;
|
|
76
|
-
const std::string* url;
|
|
77
|
-
const WidgetOptions* options;
|
|
78
|
-
void* handle;
|
|
79
|
-
double opacity;
|
|
80
|
-
int x, y;
|
|
81
|
-
void* result;
|
|
82
|
-
std::mutex mutex;
|
|
83
|
-
std::condition_variable cv;
|
|
84
|
-
bool done;
|
|
85
|
-
Win32Task() : done(false), result(nullptr) {}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
static std::queue<Win32Task*> task_queue;
|
|
89
|
-
static std::mutex queue_mutex;
|
|
90
|
-
static std::atomic<bool> loop_running{false};
|
|
91
|
-
|
|
92
|
-
static LRESULT CALLBACK WidgetWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
93
|
-
if (msg == WM_SIZE) {
|
|
94
|
-
Win32WidgetContext* ctx = (Win32WidgetContext*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
|
95
|
-
if (ctx && ctx->webBrowser) {
|
|
96
|
-
RECT rect;
|
|
97
|
-
GetClientRect(hwnd, &rect);
|
|
98
|
-
ctx->webBrowser->put_Width(rect.right);
|
|
99
|
-
ctx->webBrowser->put_Height(rect.bottom);
|
|
100
|
-
}
|
|
101
|
-
return 0;
|
|
102
|
-
}
|
|
103
|
-
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
void run_win32_loop() {
|
|
107
|
-
std::cout << "[Native] Starting Win32 loop thread..." << std::endl;
|
|
108
|
-
OleInitialize(NULL);
|
|
109
|
-
|
|
110
|
-
HINSTANCE hInstance = GetModuleHandle(NULL);
|
|
111
|
-
WNDCLASS wc = {0};
|
|
112
|
-
wc.lpfnWndProc = WidgetWndProc;
|
|
113
|
-
wc.hInstance = hInstance;
|
|
114
|
-
wc.lpszClassName = "WidgetWindowClass";
|
|
115
|
-
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
116
|
-
wc.hbrBackground = NULL; // No background brush for true transparency
|
|
117
|
-
RegisterClass(&wc);
|
|
118
|
-
|
|
119
|
-
loop_running = true;
|
|
120
|
-
while (true) {
|
|
121
|
-
MSG msg;
|
|
122
|
-
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
123
|
-
if (msg.message == WM_QUIT) return;
|
|
124
|
-
TranslateMessage(&msg);
|
|
125
|
-
DispatchMessage(&msg);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
std::vector<Win32Task*> local_tasks;
|
|
129
|
-
{
|
|
130
|
-
std::lock_guard<std::mutex> lock(queue_mutex);
|
|
131
|
-
while (!task_queue.empty()) {
|
|
132
|
-
local_tasks.push_back(task_queue.front());
|
|
133
|
-
task_queue.pop();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
for (auto* task : local_tasks) {
|
|
138
|
-
if (task->type == Win32Task::CREATE) {
|
|
139
|
-
std::cout << "[Native] Creating chromeless widget window (Cyber-Glow Mode)..." << std::endl;
|
|
140
|
-
HWND hwnd = CreateWindowEx(
|
|
141
|
-
WS_EX_LAYERED | WS_EX_TOOLWINDOW | ((task->options->sticky && !task->options->interactive) ? WS_EX_TRANSPARENT : 0),
|
|
142
|
-
"WidgetWindowClass", "Widget",
|
|
143
|
-
WS_POPUP | WS_VISIBLE | WS_CLIPCHILDREN,
|
|
144
|
-
task->options->x, task->options->y, task->options->width, task->options->height,
|
|
145
|
-
NULL, NULL, hInstance, NULL
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
if (!hwnd) {
|
|
149
|
-
std::cerr << "[Native] CreateWindowEx failed: " << GetLastError() << std::endl;
|
|
150
|
-
} else {
|
|
151
|
-
// Set Color Key to RGB(1, 1, 1) for transparency
|
|
152
|
-
SetLayeredWindowAttributes(hwnd, RGB(1, 1, 1), 0, LWA_COLORKEY);
|
|
153
|
-
|
|
154
|
-
if (task->options->blur) {
|
|
155
|
-
DWM_BLURBEHIND bb = {DWM_BB_ENABLE, TRUE, NULL, FALSE};
|
|
156
|
-
DwmEnableBlurBehindWindow(hwnd, &bb);
|
|
157
|
-
}
|
|
158
|
-
if (task->options->sticky) SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
|
159
|
-
|
|
160
|
-
Win32WidgetContext* ctx = new Win32WidgetContext();
|
|
161
|
-
ctx->hwnd = hwnd;
|
|
162
|
-
ctx->clientSite.m_hwnd = hwnd;
|
|
163
|
-
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)ctx);
|
|
164
|
-
|
|
165
|
-
IOleObject* oleObject = NULL;
|
|
166
|
-
if (SUCCEEDED(CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_INPROC_SERVER, IID_IOleObject, (void**)&oleObject))) {
|
|
167
|
-
oleObject->SetClientSite(&ctx->clientSite);
|
|
168
|
-
RECT rect;
|
|
169
|
-
GetClientRect(hwnd, &rect);
|
|
170
|
-
oleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, &ctx->clientSite, 0, hwnd, &rect);
|
|
171
|
-
oleObject->QueryInterface(IID_IWebBrowser2, (void**)&ctx->webBrowser);
|
|
172
|
-
if (ctx->webBrowser) {
|
|
173
|
-
ctx->webBrowser->put_Visible(VARIANT_TRUE);
|
|
174
|
-
std::wstring wurl(task->url->begin(), task->url->end());
|
|
175
|
-
BSTR bstrURL = SysAllocString(wurl.c_str());
|
|
176
|
-
ctx->webBrowser->Navigate(bstrURL, NULL, NULL, NULL, NULL);
|
|
177
|
-
SysFreeString(bstrURL);
|
|
178
|
-
std::cout << "[Native] Browser navigated to: " << task->url->c_str() << std::endl;
|
|
179
|
-
}
|
|
180
|
-
oleObject->Release();
|
|
181
|
-
}
|
|
182
|
-
UpdateWindow(hwnd);
|
|
183
|
-
task->result = ctx;
|
|
184
|
-
}
|
|
185
|
-
} else if (task->type == Win32Task::OPACITY) {
|
|
186
|
-
// Opacity handled via Color Key / Global Alpha mixture if needed,
|
|
187
|
-
// but for Color Key mode we focus on the key color.
|
|
188
|
-
if (task->handle) {
|
|
189
|
-
Win32WidgetContext* ctx = (Win32WidgetContext*)task->handle;
|
|
190
|
-
SetLayeredWindowAttributes(ctx->hwnd, 0, (BYTE)(task->opacity * 255), LWA_ALPHA);
|
|
191
|
-
}
|
|
192
|
-
} else if (task->type == Win32Task::POSITION) {
|
|
193
|
-
if (task->handle) {
|
|
194
|
-
Win32WidgetContext* ctx = (Win32WidgetContext*)task->handle;
|
|
195
|
-
SetWindowPos(ctx->hwnd, NULL, task->x, task->y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
{
|
|
199
|
-
std::lock_guard<std::mutex> lock(task->mutex);
|
|
200
|
-
task->done = true;
|
|
201
|
-
task->cv.notify_one();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
Sleep(10);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
void* WidgetManager::CreateWidget(const std::string& url, const WidgetOptions& options) {
|
|
209
|
-
static std::once_flag init_flag;
|
|
210
|
-
std::call_once(init_flag, []() { std::thread(run_win32_loop).detach(); });
|
|
211
|
-
while (!loop_running) Sleep(10);
|
|
212
|
-
|
|
213
|
-
Win32Task task;
|
|
214
|
-
task.type = Win32Task::CREATE;
|
|
215
|
-
task.url = &url;
|
|
216
|
-
task.options = &options;
|
|
217
|
-
{
|
|
218
|
-
std::lock_guard<std::mutex> lock(queue_mutex);
|
|
219
|
-
task_queue.push(&task);
|
|
220
|
-
}
|
|
221
|
-
std::unique_lock<std::mutex> lock(task.mutex);
|
|
222
|
-
task.cv.wait(lock, [&]{ return task.done; });
|
|
223
|
-
return task.result;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
void WidgetManager::UpdateOpacity(void* handle, double opacity) {
|
|
227
|
-
if (!handle) return;
|
|
228
|
-
Win32Task task;
|
|
229
|
-
task.type = Win32Task::OPACITY;
|
|
230
|
-
task.handle = handle;
|
|
231
|
-
task.opacity = opacity;
|
|
232
|
-
{ std::lock_guard<std::mutex> lock(queue_mutex); task_queue.push(&task); }
|
|
233
|
-
std::unique_lock<std::mutex> lock(task.mutex);
|
|
234
|
-
task.cv.wait(lock, [&]{ return task.done; });
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
void WidgetManager::UpdatePosition(void* handle, int x, int y) {
|
|
238
|
-
if (!handle) return;
|
|
239
|
-
Win32Task task;
|
|
240
|
-
task.type = Win32Task::POSITION;
|
|
241
|
-
task.handle = handle;
|
|
242
|
-
task.x = x; task.y = y;
|
|
243
|
-
{ std::lock_guard<std::mutex> lock(queue_mutex); task_queue.push(&task); }
|
|
244
|
-
std::unique_lock<std::mutex> lock(task.mutex);
|
|
245
|
-
task.cv.wait(lock, [&]{ return task.done; });
|
|
246
|
-
}
|
|
1
|
+
#include "WidgetManager.h"
|
|
2
|
+
#include <windows.h>
|
|
3
|
+
#include <dwmapi.h>
|
|
4
|
+
#include <exdisp.h>
|
|
5
|
+
#include <mshtml.h>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <thread>
|
|
8
|
+
#include <mutex>
|
|
9
|
+
#include <condition_variable>
|
|
10
|
+
#include <queue>
|
|
11
|
+
#include <atomic>
|
|
12
|
+
#include <iostream>
|
|
13
|
+
|
|
14
|
+
// Minimal implementation for embedding IWebBrowser2
|
|
15
|
+
class SimpleClientSite : public IOleClientSite, public IOleInPlaceSite, public IOleInPlaceFrame {
|
|
16
|
+
public:
|
|
17
|
+
STDMETHODIMP QueryInterface(REFIID riid, void** ppv) {
|
|
18
|
+
if (riid == IID_IUnknown || riid == IID_IOleClientSite) *ppv = (IOleClientSite*)this;
|
|
19
|
+
else if (riid == IID_IOleInPlaceSite) *ppv = (IOleInPlaceSite*)this;
|
|
20
|
+
else if (riid == IID_IOleInPlaceFrame || riid == IID_IOleInPlaceUIWindow) *ppv = (IOleInPlaceFrame*)this;
|
|
21
|
+
else return (*ppv = NULL, E_NOINTERFACE);
|
|
22
|
+
return S_OK;
|
|
23
|
+
}
|
|
24
|
+
STDMETHODIMP_(ULONG) AddRef() { return 1; }
|
|
25
|
+
STDMETHODIMP_(ULONG) Release() { return 1; }
|
|
26
|
+
STDMETHODIMP SaveObject() { return E_NOTIMPL; }
|
|
27
|
+
STDMETHODIMP GetMoniker(DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
|
|
28
|
+
STDMETHODIMP GetContainer(IOleContainer**) { return E_NOTIMPL; }
|
|
29
|
+
STDMETHODIMP ShowObject() { return S_OK; }
|
|
30
|
+
STDMETHODIMP OnShowWindow(BOOL) { return S_OK; }
|
|
31
|
+
STDMETHODIMP RequestNewObjectLayout() { return E_NOTIMPL; }
|
|
32
|
+
STDMETHODIMP GetWindow(HWND* phwnd) { *phwnd = m_hwnd; return S_OK; }
|
|
33
|
+
STDMETHODIMP ContextSensitiveHelp(BOOL) { return E_NOTIMPL; }
|
|
34
|
+
STDMETHODIMP CanInPlaceActivate() { return S_OK; }
|
|
35
|
+
STDMETHODIMP OnInPlaceActivate() { return S_OK; }
|
|
36
|
+
STDMETHODIMP OnUIActivate() { return S_OK; }
|
|
37
|
+
STDMETHODIMP GetWindowContext(IOleInPlaceFrame** ppFrame, IOleInPlaceUIWindow** ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo) {
|
|
38
|
+
*ppFrame = (IOleInPlaceFrame*)this;
|
|
39
|
+
*ppDoc = NULL;
|
|
40
|
+
GetClientRect(m_hwnd, lprcPosRect);
|
|
41
|
+
GetClientRect(m_hwnd, lprcClipRect);
|
|
42
|
+
lpFrameInfo->cb = sizeof(OLEINPLACEFRAMEINFO);
|
|
43
|
+
lpFrameInfo->fMDIApp = FALSE;
|
|
44
|
+
lpFrameInfo->hwndFrame = m_hwnd;
|
|
45
|
+
lpFrameInfo->haccel = NULL;
|
|
46
|
+
lpFrameInfo->cAccelEntries = 0;
|
|
47
|
+
return S_OK;
|
|
48
|
+
}
|
|
49
|
+
STDMETHODIMP Scroll(SIZE) { return E_NOTIMPL; }
|
|
50
|
+
STDMETHODIMP OnUIDeactivate(BOOL) { return S_OK; }
|
|
51
|
+
STDMETHODIMP OnInPlaceDeactivate() { return S_OK; }
|
|
52
|
+
STDMETHODIMP DiscardUndoState() { return E_NOTIMPL; }
|
|
53
|
+
STDMETHODIMP DeactivateAndUndo() { return E_NOTIMPL; }
|
|
54
|
+
STDMETHODIMP OnPosRectChange(LPCRECT) { return S_OK; }
|
|
55
|
+
STDMETHODIMP InsertMenus(HMENU, LPOLEMENUGROUPWIDTHS) { return E_NOTIMPL; }
|
|
56
|
+
STDMETHODIMP SetMenu(HMENU, HOLEMENU, HWND) { return E_NOTIMPL; }
|
|
57
|
+
STDMETHODIMP RemoveMenus(HMENU) { return E_NOTIMPL; }
|
|
58
|
+
STDMETHODIMP SetStatusText(LPCOLESTR) { return E_NOTIMPL; }
|
|
59
|
+
STDMETHODIMP EnableModeless(BOOL) { return E_NOTIMPL; }
|
|
60
|
+
STDMETHODIMP TranslateAccelerator(LPMSG, WORD) { return E_NOTIMPL; }
|
|
61
|
+
STDMETHODIMP GetBorder(LPRECT) { return E_NOTIMPL; }
|
|
62
|
+
STDMETHODIMP RequestBorderSpace(LPCBORDERWIDTHS) { return E_NOTIMPL; }
|
|
63
|
+
STDMETHODIMP SetBorderSpace(LPCBORDERWIDTHS) { return E_NOTIMPL; }
|
|
64
|
+
STDMETHODIMP SetActiveObject(IOleInPlaceActiveObject*, LPCOLESTR) { return E_NOTIMPL; }
|
|
65
|
+
HWND m_hwnd;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
struct Win32WidgetContext {
|
|
69
|
+
HWND hwnd;
|
|
70
|
+
IWebBrowser2* webBrowser;
|
|
71
|
+
SimpleClientSite clientSite;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
struct Win32Task {
|
|
75
|
+
enum Type { CREATE, OPACITY, POSITION } type;
|
|
76
|
+
const std::string* url;
|
|
77
|
+
const WidgetOptions* options;
|
|
78
|
+
void* handle;
|
|
79
|
+
double opacity;
|
|
80
|
+
int x, y;
|
|
81
|
+
void* result;
|
|
82
|
+
std::mutex mutex;
|
|
83
|
+
std::condition_variable cv;
|
|
84
|
+
bool done;
|
|
85
|
+
Win32Task() : done(false), result(nullptr) {}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
static std::queue<Win32Task*> task_queue;
|
|
89
|
+
static std::mutex queue_mutex;
|
|
90
|
+
static std::atomic<bool> loop_running{false};
|
|
91
|
+
|
|
92
|
+
static LRESULT CALLBACK WidgetWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
93
|
+
if (msg == WM_SIZE) {
|
|
94
|
+
Win32WidgetContext* ctx = (Win32WidgetContext*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
|
95
|
+
if (ctx && ctx->webBrowser) {
|
|
96
|
+
RECT rect;
|
|
97
|
+
GetClientRect(hwnd, &rect);
|
|
98
|
+
ctx->webBrowser->put_Width(rect.right);
|
|
99
|
+
ctx->webBrowser->put_Height(rect.bottom);
|
|
100
|
+
}
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void run_win32_loop() {
|
|
107
|
+
std::cout << "[Native] Starting Win32 loop thread..." << std::endl;
|
|
108
|
+
OleInitialize(NULL);
|
|
109
|
+
|
|
110
|
+
HINSTANCE hInstance = GetModuleHandle(NULL);
|
|
111
|
+
WNDCLASS wc = {0};
|
|
112
|
+
wc.lpfnWndProc = WidgetWndProc;
|
|
113
|
+
wc.hInstance = hInstance;
|
|
114
|
+
wc.lpszClassName = "WidgetWindowClass";
|
|
115
|
+
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
116
|
+
wc.hbrBackground = NULL; // No background brush for true transparency
|
|
117
|
+
RegisterClass(&wc);
|
|
118
|
+
|
|
119
|
+
loop_running = true;
|
|
120
|
+
while (true) {
|
|
121
|
+
MSG msg;
|
|
122
|
+
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
123
|
+
if (msg.message == WM_QUIT) return;
|
|
124
|
+
TranslateMessage(&msg);
|
|
125
|
+
DispatchMessage(&msg);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
std::vector<Win32Task*> local_tasks;
|
|
129
|
+
{
|
|
130
|
+
std::lock_guard<std::mutex> lock(queue_mutex);
|
|
131
|
+
while (!task_queue.empty()) {
|
|
132
|
+
local_tasks.push_back(task_queue.front());
|
|
133
|
+
task_queue.pop();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (auto* task : local_tasks) {
|
|
138
|
+
if (task->type == Win32Task::CREATE) {
|
|
139
|
+
std::cout << "[Native] Creating chromeless widget window (Cyber-Glow Mode)..." << std::endl;
|
|
140
|
+
HWND hwnd = CreateWindowEx(
|
|
141
|
+
WS_EX_LAYERED | WS_EX_TOOLWINDOW | ((task->options->sticky && !task->options->interactive) ? WS_EX_TRANSPARENT : 0),
|
|
142
|
+
"WidgetWindowClass", "Widget",
|
|
143
|
+
WS_POPUP | WS_VISIBLE | WS_CLIPCHILDREN,
|
|
144
|
+
task->options->x, task->options->y, task->options->width, task->options->height,
|
|
145
|
+
NULL, NULL, hInstance, NULL
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
if (!hwnd) {
|
|
149
|
+
std::cerr << "[Native] CreateWindowEx failed: " << GetLastError() << std::endl;
|
|
150
|
+
} else {
|
|
151
|
+
// Set Color Key to RGB(1, 1, 1) for transparency
|
|
152
|
+
SetLayeredWindowAttributes(hwnd, RGB(1, 1, 1), 0, LWA_COLORKEY);
|
|
153
|
+
|
|
154
|
+
if (task->options->blur) {
|
|
155
|
+
DWM_BLURBEHIND bb = {DWM_BB_ENABLE, TRUE, NULL, FALSE};
|
|
156
|
+
DwmEnableBlurBehindWindow(hwnd, &bb);
|
|
157
|
+
}
|
|
158
|
+
if (task->options->sticky) SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
|
159
|
+
|
|
160
|
+
Win32WidgetContext* ctx = new Win32WidgetContext();
|
|
161
|
+
ctx->hwnd = hwnd;
|
|
162
|
+
ctx->clientSite.m_hwnd = hwnd;
|
|
163
|
+
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)ctx);
|
|
164
|
+
|
|
165
|
+
IOleObject* oleObject = NULL;
|
|
166
|
+
if (SUCCEEDED(CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_INPROC_SERVER, IID_IOleObject, (void**)&oleObject))) {
|
|
167
|
+
oleObject->SetClientSite(&ctx->clientSite);
|
|
168
|
+
RECT rect;
|
|
169
|
+
GetClientRect(hwnd, &rect);
|
|
170
|
+
oleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, &ctx->clientSite, 0, hwnd, &rect);
|
|
171
|
+
oleObject->QueryInterface(IID_IWebBrowser2, (void**)&ctx->webBrowser);
|
|
172
|
+
if (ctx->webBrowser) {
|
|
173
|
+
ctx->webBrowser->put_Visible(VARIANT_TRUE);
|
|
174
|
+
std::wstring wurl(task->url->begin(), task->url->end());
|
|
175
|
+
BSTR bstrURL = SysAllocString(wurl.c_str());
|
|
176
|
+
ctx->webBrowser->Navigate(bstrURL, NULL, NULL, NULL, NULL);
|
|
177
|
+
SysFreeString(bstrURL);
|
|
178
|
+
std::cout << "[Native] Browser navigated to: " << task->url->c_str() << std::endl;
|
|
179
|
+
}
|
|
180
|
+
oleObject->Release();
|
|
181
|
+
}
|
|
182
|
+
UpdateWindow(hwnd);
|
|
183
|
+
task->result = ctx;
|
|
184
|
+
}
|
|
185
|
+
} else if (task->type == Win32Task::OPACITY) {
|
|
186
|
+
// Opacity handled via Color Key / Global Alpha mixture if needed,
|
|
187
|
+
// but for Color Key mode we focus on the key color.
|
|
188
|
+
if (task->handle) {
|
|
189
|
+
Win32WidgetContext* ctx = (Win32WidgetContext*)task->handle;
|
|
190
|
+
SetLayeredWindowAttributes(ctx->hwnd, 0, (BYTE)(task->opacity * 255), LWA_ALPHA);
|
|
191
|
+
}
|
|
192
|
+
} else if (task->type == Win32Task::POSITION) {
|
|
193
|
+
if (task->handle) {
|
|
194
|
+
Win32WidgetContext* ctx = (Win32WidgetContext*)task->handle;
|
|
195
|
+
SetWindowPos(ctx->hwnd, NULL, task->x, task->y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
{
|
|
199
|
+
std::lock_guard<std::mutex> lock(task->mutex);
|
|
200
|
+
task->done = true;
|
|
201
|
+
task->cv.notify_one();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
Sleep(10);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
void* WidgetManager::CreateWidget(const std::string& url, const WidgetOptions& options) {
|
|
209
|
+
static std::once_flag init_flag;
|
|
210
|
+
std::call_once(init_flag, []() { std::thread(run_win32_loop).detach(); });
|
|
211
|
+
while (!loop_running) Sleep(10);
|
|
212
|
+
|
|
213
|
+
Win32Task task;
|
|
214
|
+
task.type = Win32Task::CREATE;
|
|
215
|
+
task.url = &url;
|
|
216
|
+
task.options = &options;
|
|
217
|
+
{
|
|
218
|
+
std::lock_guard<std::mutex> lock(queue_mutex);
|
|
219
|
+
task_queue.push(&task);
|
|
220
|
+
}
|
|
221
|
+
std::unique_lock<std::mutex> lock(task.mutex);
|
|
222
|
+
task.cv.wait(lock, [&]{ return task.done; });
|
|
223
|
+
return task.result;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
void WidgetManager::UpdateOpacity(void* handle, double opacity) {
|
|
227
|
+
if (!handle) return;
|
|
228
|
+
Win32Task task;
|
|
229
|
+
task.type = Win32Task::OPACITY;
|
|
230
|
+
task.handle = handle;
|
|
231
|
+
task.opacity = opacity;
|
|
232
|
+
{ std::lock_guard<std::mutex> lock(queue_mutex); task_queue.push(&task); }
|
|
233
|
+
std::unique_lock<std::mutex> lock(task.mutex);
|
|
234
|
+
task.cv.wait(lock, [&]{ return task.done; });
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
void WidgetManager::UpdatePosition(void* handle, int x, int y) {
|
|
238
|
+
if (!handle) return;
|
|
239
|
+
Win32Task task;
|
|
240
|
+
task.type = Win32Task::POSITION;
|
|
241
|
+
task.handle = handle;
|
|
242
|
+
task.x = x; task.y = y;
|
|
243
|
+
{ std::lock_guard<std::mutex> lock(queue_mutex); task_queue.push(&task); }
|
|
244
|
+
std::unique_lock<std::mutex> lock(task.mutex);
|
|
245
|
+
task.cv.wait(lock, [&]{ return task.done; });
|
|
246
|
+
}
|