Skip to content

Commit 6c05f87

Browse files
committed
增加 gui.open 方便简单 gui 开发
1 parent 2b15b64 commit 6c05f87

File tree

5 files changed

+68
-28
lines changed

5 files changed

+68
-28
lines changed

fibjs/include/ifs/gui.h

+26-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class gui_base : public object_base
2525

2626
public:
2727
// gui_base
28-
static result_t open(exlib::string url, exlib::string title, obj_ptr<WebView_base>& retVal, AsyncEvent* ac);
28+
static result_t load(exlib::string url, exlib::string title, obj_ptr<WebView_base>& retVal, AsyncEvent* ac);
29+
static result_t open(exlib::string url, exlib::string title, AsyncEvent* ac);
2930

3031
public:
3132
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args)
@@ -39,10 +40,12 @@ class gui_base : public object_base
3940
}
4041

4142
public:
43+
static void s_load(const v8::FunctionCallbackInfo<v8::Value>& args);
4244
static void s_open(const v8::FunctionCallbackInfo<v8::Value>& args);
4345

4446
public:
45-
ASYNC_STATICVALUE3(gui_base, open, exlib::string, exlib::string, obj_ptr<WebView_base>);
47+
ASYNC_STATICVALUE3(gui_base, load, exlib::string, exlib::string, obj_ptr<WebView_base>);
48+
ASYNC_STATIC2(gui_base, open, exlib::string, exlib::string);
4649
};
4750

4851
}
@@ -55,13 +58,14 @@ namespace fibjs
5558
{
5659
static ClassData::ClassMethod s_method[] =
5760
{
61+
{"load", s_load, true},
5862
{"open", s_open, true}
5963
};
6064

6165
static ClassData s_cd =
6266
{
6367
"gui", s__new, NULL,
64-
1, s_method, 0, NULL, 0, NULL, NULL, NULL,
68+
2, s_method, 0, NULL, 0, NULL, NULL, NULL,
6569
NULL
6670
};
6771

@@ -70,7 +74,7 @@ namespace fibjs
7074
}
7175

7276

73-
inline void gui_base::s_open(const v8::FunctionCallbackInfo<v8::Value>& args)
77+
inline void gui_base::s_load(const v8::FunctionCallbackInfo<v8::Value>& args)
7478
{
7579
obj_ptr<WebView_base> vr;
7680

@@ -80,14 +84,30 @@ namespace fibjs
8084
OPT_ARG(exlib::string, 1, "");
8185

8286
if(!cb.IsEmpty()) {
83-
acb_open(v0, v1, vr, cb);
87+
acb_load(v0, v1, vr, cb);
8488
hr = CALL_RETURN_NULL;
8589
} else
86-
hr = ac_open(v0, v1, vr);
90+
hr = ac_load(v0, v1, vr);
8791

8892
METHOD_RETURN();
8993
}
9094

95+
inline void gui_base::s_open(const v8::FunctionCallbackInfo<v8::Value>& args)
96+
{
97+
ASYNC_METHOD_ENTER(2, 1);
98+
99+
ARG(exlib::string, 0);
100+
OPT_ARG(exlib::string, 1, "");
101+
102+
if(!cb.IsEmpty()) {
103+
acb_open(v0, v1, cb);
104+
hr = CALL_RETURN_NULL;
105+
} else
106+
hr = ac_open(v0, v1);
107+
108+
METHOD_VOID();
109+
}
110+
91111
}
92112

93113
#endif

fibjs/include/ifs/gui.idl

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88
module gui
99
{
10-
/*! @brief 打开一个窗口并访问指定网址 */
11-
static WebView open(String url, String title = "") async;
10+
/*! @brief 打开一个窗口并访问指定网址 */
11+
static WebView load(String url, String title = "") async;
12+
13+
/*! @brief 打开一个窗口并访问指定网址并等待窗口关闭 */
14+
static open(String url, String title = "") async;
1215
};

fibjs/src/gui/windows/WebView.cpp

+31-16
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,25 @@ void init_gui()
5959
(new gui_thread())->start();
6060
}
6161

62-
result_t gui_base::open(exlib::string url, exlib::string title,
62+
result_t gui_base::load(exlib::string url, exlib::string title,
6363
obj_ptr<WebView_base>& retVal, AsyncEvent* ac)
6464
{
6565
if (!ac)
6666
return CHECK_ERROR(CALL_E_GUICALL);
6767

6868
retVal = new WebView(url, title);
69-
7069
return 0;
7170
}
7271

72+
result_t gui_base::open(exlib::string url, exlib::string title, AsyncEvent* ac)
73+
{
74+
if (!ac)
75+
return CHECK_ERROR(CALL_E_GUICALL);
76+
77+
new WebView(url, title, ac);
78+
return CALL_E_PENDDING;
79+
}
80+
7381
const wchar_t* szWndClassMain = L"fibjs_window";
7482

7583
static void RegMainClass()
@@ -97,8 +105,10 @@ static void RegMainClass()
97105
}
98106
}
99107

100-
WebView::WebView(exlib::string url, exlib::string title)
108+
WebView::WebView(exlib::string url, exlib::string title, AsyncEvent* ac)
101109
{
110+
m_ac = ac;
111+
102112
oleObject = NULL;
103113
oleInPlaceObject = NULL;
104114
webBrowser2 = NULL;
@@ -176,38 +186,42 @@ void WebView::clear()
176186
oleObject->Release();
177187
oleObject = NULL;
178188
}
189+
190+
if (m_ac)
191+
{
192+
m_ac->post(0);
193+
m_ac = NULL;
194+
}
179195
}
180196

181197
LRESULT CALLBACK WebView::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
182198
{
183-
WebView* webBrowser1;
199+
WebView* webView1;
184200

185201
switch (uMsg)
186202
{
187203
case WM_SIZE:
188-
webBrowser1 = (WebView*)GetWindowLongPtr(hWnd, 0);
189-
if (webBrowser1 != 0)
204+
webView1 = (WebView*)GetWindowLongPtr(hWnd, 0);
205+
if (webView1 != 0)
190206
{
191207
RECT rcClient;
192208
GetClientRect(hWnd, &rcClient);
193-
webBrowser1->SetRect(rcClient);
209+
webView1->SetRect(rcClient);
194210
}
195211
break;
196-
case WM_DESTROY:
197-
webBrowser1 = (WebView*)GetWindowLongPtr(hWnd, 0);
198-
if (webBrowser1 != 0)
212+
case WM_CLOSE:
213+
webView1 = (WebView*)GetWindowLongPtr(hWnd, 0);
214+
if (webView1 != 0)
199215
{
200216
SetWindowLongPtr(hWnd, 0, 0);
201-
webBrowser1->_trigger("close", (Variant*)NULL, 0);
202-
webBrowser1->clear();
203-
webBrowser1->Release();
217+
webView1->_trigger("close", (Variant*)NULL, 0);
218+
webView1->clear();
219+
webView1->Release();
204220
}
205221
break;
206-
default:
207-
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
208222
}
209223

210-
return 0;
224+
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
211225
}
212226

213227
result_t WebView::close(AsyncEvent* ac)
@@ -547,6 +561,7 @@ HRESULT WebView::QueryService(
547561
void **ppvObject) {
548562
if (siid == IID_IInternetSecurityManager && riid == IID_IInternetSecurityManager) {
549563
*ppvObject = static_cast<IInternetSecurityManager*>(this);
564+
AddRef();
550565
} else {
551566
*ppvObject = 0;
552567
return E_NOINTERFACE;

fibjs/src/gui/windows/WebView.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class WebView : public WebView_base,
2323
public IInternetSecurityManager
2424
{
2525
public:
26-
WebView(exlib::string url, exlib::string title);
26+
WebView(exlib::string url, exlib::string title, AsyncEvent* ac = NULL);
2727
~WebView();
2828

2929
static RECT PixelToHiMetric(const RECT& _rc);
@@ -147,6 +147,8 @@ class WebView : public WebView_base,
147147

148148
HWND hWndParent;
149149
HWND hWndControl;
150+
151+
AsyncEvent* m_ac;
150152
};
151153

152154
} /* namespace fibjs */

test/gui_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ if (win) {
2121
});
2222
svr.asyncRun();
2323

24-
var win = gui.open("http://127.0.0.1:" + (8899 + base_port) + "/");
24+
var win = gui.load("http://127.0.0.1:" + (8899 + base_port) + "/");
2525

26-
for (var i = 0; i < 100 && !check; i++)
26+
for (var i = 0; i < 1000 && !check; i++)
2727
coroutine.sleep(10);
2828

2929
assert.ok(check);
@@ -37,7 +37,7 @@ if (win) {
3737
win.close();
3838
win = undefined;
3939

40-
for (var i = 0; i < 100 && !closed; i++)
40+
for (var i = 0; i < 1000 && !closed; i++)
4141
coroutine.sleep(10);
4242

4343
coroutine.sleep(100);

0 commit comments

Comments
 (0)