Skip to content

Commit

Permalink
add webview_get_window_handle() (Edge/MSHTML only)
Browse files Browse the repository at this point in the history
  • Loading branch information
Regentag authored and richardhozak committed Jan 2, 2021
1 parent b28b297 commit 1c20b92
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ impl<'a, T> WebView<'a, T> {
&mut self.user_data_wrapper_mut().inner
}

/// Window handle (Windows only)
pub fn window_handle(&self) -> *mut c_void {
unsafe { webview_get_window_handle(self.inner.unwrap()) as _ }
}

#[deprecated(note = "Please use exit instead")]
pub fn terminate(&mut self) {
self.exit();
Expand Down
1 change: 1 addition & 0 deletions webview-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
pub fn webview_loop(this: *mut CWebView, blocking: c_int) -> c_int;
pub fn webview_exit(this: *mut CWebView);
pub fn webview_get_user_data(this: *mut CWebView) -> *mut c_void;
pub fn webview_get_window_handle(this: *mut CWebView) -> *mut c_void;
pub fn webview_dispatch(this: *mut CWebView, f: Option<ErasedDispatchFn>, arg: *mut c_void);
pub fn webview_eval(this: *mut CWebView, js: *const c_char) -> c_int;
pub fn webview_set_title(this: *mut CWebView, title: *const c_char);
Expand Down
1 change: 1 addition & 0 deletions webview-sys/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ WEBVIEW_API void webview_debug(const char *format, ...);
WEBVIEW_API void webview_print_log(const char *s);

WEBVIEW_API void* webview_get_user_data(webview_t w);
WEBVIEW_API void* webview_get_window_handle(webview_t w);
WEBVIEW_API webview_t webview_new(const char* title, const char* url, int width, int height, int resizable, int debug, int frameless, int visible, int min_width, int min_height, webview_external_invoke_cb_t external_invoke_cb, void* userdata);
WEBVIEW_API void webview_free(webview_t w);
WEBVIEW_API void webview_destroy(webview_t w);
Expand Down
6 changes: 5 additions & 1 deletion webview-sys/webview_edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ class webview : public browser_window {

void navigate(const char* url)
{

std::string html = html_from_uri(url);
if (html != "") {
m_webview.NavigateToString(winrt::to_hstring(html.c_str()));
Expand Down Expand Up @@ -585,6 +584,11 @@ WEBVIEW_API void* webview_get_user_data(webview_t w)
return static_cast<webview::webview*>(w)->get_user_data();
}

WEBVIEW_API void* webview_get_window_handle(webview_t w)
{
return static_cast<webview::webview*>(w)->window();
}

WEBVIEW_API webview_t webview_new(
const char* title, const char* url, int width, int height, int resizable, int debug,
int frameless, int visible, int min_width, int min_height, webview_external_invoke_cb_t external_invoke_cb, void* userdata)
Expand Down
21 changes: 13 additions & 8 deletions webview-sys/webview_mshtml.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ WEBVIEW_API void* webview_get_user_data(webview_t w) {
return wv->userdata;
}

WEBVIEW_API void* webview_get_window_handle(webview_t w) {
struct mshtml_webview* wv = (struct mshtml_webview*)w;
return wv->hwnd;
}

static inline BSTR webview_to_bstr(const char *s) {
DWORD size = MultiByteToWideChar(CP_UTF8, 0, s, -1, 0, 0);
BSTR bs = SysAllocStringLen(0, size);
Expand Down Expand Up @@ -98,7 +103,7 @@ static int webview_fix_ie_compat_mode() {
static const TCHAR *classname = "WebView";

WEBVIEW_API webview_t webview_new(
const char* title, const char* url, int width, int height, int resizable, int debug,
const char* title, const char* url, int width, int height, int resizable, int debug,
int frameless, int visible, int min_width, int min_height, webview_external_invoke_cb_t external_invoke_cb, void* userdata) {

if (webview_fix_ie_compat_mode() < 0) {
Expand All @@ -120,14 +125,14 @@ WEBVIEW_API webview_t webview_new(
EnableDpiAwareness();

HICON winresIcon = (HICON)LoadImage(
hInstance,
(LPWSTR)(1),
IMAGE_ICON,
0,
hInstance,
(LPWSTR)(1),
IMAGE_ICON,
0,
0,
LR_DEFAULTSIZE
);

WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
Expand Down Expand Up @@ -954,7 +959,7 @@ LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
lpMMI->ptMinTrackSize.x = wv->min_width;
lpMMI->ptMinTrackSize.y = wv->min_height;
}

break;
}
}
Expand Down Expand Up @@ -1134,7 +1139,7 @@ WEBVIEW_API void webview_set_maximized(webview_t w, int maximize) {
RECT r;

SystemParametersInfoW(SPI_GETWORKAREA, 0, &r, 0);

ShowWindow(wv->hwnd, SW_MAXIMIZE);
SetWindowPos(wv->hwnd, NULL, r.left, r.top, r.right - r.left,
r.bottom - r.top,
Expand Down

0 comments on commit 1c20b92

Please sign in to comment.