Skip to content

Commit

Permalink
增加 gui.open 方便简单 gui 开发
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Nov 13, 2016
1 parent 2b15b64 commit 6c05f87
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 28 deletions.
32 changes: 26 additions & 6 deletions fibjs/include/ifs/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class gui_base : public object_base

public:
// gui_base
static result_t open(exlib::string url, exlib::string title, obj_ptr<WebView_base>& retVal, AsyncEvent* ac);
static result_t load(exlib::string url, exlib::string title, obj_ptr<WebView_base>& retVal, AsyncEvent* ac);
static result_t open(exlib::string url, exlib::string title, AsyncEvent* ac);

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

public:
static void s_load(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_open(const v8::FunctionCallbackInfo<v8::Value>& args);

public:
ASYNC_STATICVALUE3(gui_base, open, exlib::string, exlib::string, obj_ptr<WebView_base>);
ASYNC_STATICVALUE3(gui_base, load, exlib::string, exlib::string, obj_ptr<WebView_base>);
ASYNC_STATIC2(gui_base, open, exlib::string, exlib::string);
};

}
Expand All @@ -55,13 +58,14 @@ namespace fibjs
{
static ClassData::ClassMethod s_method[] =
{
{"load", s_load, true},
{"open", s_open, true}
};

static ClassData s_cd =
{
"gui", s__new, NULL,
1, s_method, 0, NULL, 0, NULL, NULL, NULL,
2, s_method, 0, NULL, 0, NULL, NULL, NULL,
NULL
};

Expand All @@ -70,7 +74,7 @@ namespace fibjs
}


inline void gui_base::s_open(const v8::FunctionCallbackInfo<v8::Value>& args)
inline void gui_base::s_load(const v8::FunctionCallbackInfo<v8::Value>& args)
{
obj_ptr<WebView_base> vr;

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

if(!cb.IsEmpty()) {
acb_open(v0, v1, vr, cb);
acb_load(v0, v1, vr, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_open(v0, v1, vr);
hr = ac_load(v0, v1, vr);

METHOD_RETURN();
}

inline void gui_base::s_open(const v8::FunctionCallbackInfo<v8::Value>& args)
{
ASYNC_METHOD_ENTER(2, 1);

ARG(exlib::string, 0);
OPT_ARG(exlib::string, 1, "");

if(!cb.IsEmpty()) {
acb_open(v0, v1, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_open(v0, v1);

METHOD_VOID();
}

}

#endif
Expand Down
7 changes: 5 additions & 2 deletions fibjs/include/ifs/gui.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
module gui
{
/*! @brief 打开一个窗口并访问指定网址 */
static WebView open(String url, String title = "") async;
/*! @brief 打开一个窗口并访问指定网址 */
static WebView load(String url, String title = "") async;

/*! @brief 打开一个窗口并访问指定网址并等待窗口关闭 */
static open(String url, String title = "") async;
};
47 changes: 31 additions & 16 deletions fibjs/src/gui/windows/WebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,25 @@ void init_gui()
(new gui_thread())->start();
}

result_t gui_base::open(exlib::string url, exlib::string title,
result_t gui_base::load(exlib::string url, exlib::string title,
obj_ptr<WebView_base>& retVal, AsyncEvent* ac)
{
if (!ac)
return CHECK_ERROR(CALL_E_GUICALL);

retVal = new WebView(url, title);

return 0;
}

result_t gui_base::open(exlib::string url, exlib::string title, AsyncEvent* ac)
{
if (!ac)
return CHECK_ERROR(CALL_E_GUICALL);

new WebView(url, title, ac);
return CALL_E_PENDDING;
}

const wchar_t* szWndClassMain = L"fibjs_window";

static void RegMainClass()
Expand Down Expand Up @@ -97,8 +105,10 @@ static void RegMainClass()
}
}

WebView::WebView(exlib::string url, exlib::string title)
WebView::WebView(exlib::string url, exlib::string title, AsyncEvent* ac)
{
m_ac = ac;

oleObject = NULL;
oleInPlaceObject = NULL;
webBrowser2 = NULL;
Expand Down Expand Up @@ -176,38 +186,42 @@ void WebView::clear()
oleObject->Release();
oleObject = NULL;
}

if (m_ac)
{
m_ac->post(0);
m_ac = NULL;
}
}

LRESULT CALLBACK WebView::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WebView* webBrowser1;
WebView* webView1;

switch (uMsg)
{
case WM_SIZE:
webBrowser1 = (WebView*)GetWindowLongPtr(hWnd, 0);
if (webBrowser1 != 0)
webView1 = (WebView*)GetWindowLongPtr(hWnd, 0);
if (webView1 != 0)
{
RECT rcClient;
GetClientRect(hWnd, &rcClient);
webBrowser1->SetRect(rcClient);
webView1->SetRect(rcClient);
}
break;
case WM_DESTROY:
webBrowser1 = (WebView*)GetWindowLongPtr(hWnd, 0);
if (webBrowser1 != 0)
case WM_CLOSE:
webView1 = (WebView*)GetWindowLongPtr(hWnd, 0);
if (webView1 != 0)
{
SetWindowLongPtr(hWnd, 0, 0);
webBrowser1->_trigger("close", (Variant*)NULL, 0);
webBrowser1->clear();
webBrowser1->Release();
webView1->_trigger("close", (Variant*)NULL, 0);
webView1->clear();
webView1->Release();
}
break;
default:
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}

return 0;
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}

result_t WebView::close(AsyncEvent* ac)
Expand Down Expand Up @@ -547,6 +561,7 @@ HRESULT WebView::QueryService(
void **ppvObject) {
if (siid == IID_IInternetSecurityManager && riid == IID_IInternetSecurityManager) {
*ppvObject = static_cast<IInternetSecurityManager*>(this);
AddRef();
} else {
*ppvObject = 0;
return E_NOINTERFACE;
Expand Down
4 changes: 3 additions & 1 deletion fibjs/src/gui/windows/WebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class WebView : public WebView_base,
public IInternetSecurityManager
{
public:
WebView(exlib::string url, exlib::string title);
WebView(exlib::string url, exlib::string title, AsyncEvent* ac = NULL);
~WebView();

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

HWND hWndParent;
HWND hWndControl;

AsyncEvent* m_ac;
};

} /* namespace fibjs */
Expand Down
6 changes: 3 additions & 3 deletions test/gui_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ if (win) {
});
svr.asyncRun();

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

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

assert.ok(check);
Expand All @@ -37,7 +37,7 @@ if (win) {
win.close();
win = undefined;

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

coroutine.sleep(100);
Expand Down

0 comments on commit 6c05f87

Please sign in to comment.