-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathwebview_host.cc
More file actions
102 lines (82 loc) · 2.7 KB
/
Copy pathwebview_host.cc
File metadata and controls
102 lines (82 loc) · 2.7 KB
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
102
#include "webview_host.h"
#include <tuple>
#include "edgeview_data.h"
static bool class_registered = false;
static const wchar_t kEdgeViewWidgetClassName[] = L"EdgeView_WidgetWin_0";
static void SetUserDataPtr(HWND hWnd, void* ptr) {
SetLastError(ERROR_SUCCESS);
std::ignore =
::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(ptr));
}
template <typename T>
static T GetUserDataPtr(HWND hWnd) {
return reinterpret_cast<T>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
}
namespace edgeview {
EdgeWidgetHost::EdgeWidgetHost(base::WeakPtr<BrowserData> parent, HWND hParent,
RECT rtRect)
: webview(parent) {
RegisterWidgetClass();
const DWORD dwStyle = hParent ? WS_CHILD | WS_VISIBLE : WS_POPUP | WS_VISIBLE;
base_window = CreateWindowExW(WS_EX_TRANSPARENT, kEdgeViewWidgetClassName,
kEdgeViewWidgetClassName, dwStyle, rtRect.left,
rtRect.top, rtRect.right, rtRect.bottom,
hParent, nullptr, GetModuleHandle(0), this);
}
EdgeWidgetHost::~EdgeWidgetHost() {
SetUserDataPtr(base_window, nullptr);
DestroyWindow(base_window);
}
void EdgeWidgetHost::OnSize() {
if (webview->core_controller) {
RECT rt;
::GetClientRect(base_window, &rt);
webview->core_controller->put_Bounds(rt);
}
}
void EdgeWidgetHost::OnFocus() {
if (webview->core_controller) {
HWND chrome_widget =
::GetWindow(::GetWindow(base_window, GW_CHILD), GW_CHILD);
::SetFocus(chrome_widget);
}
}
void EdgeWidgetHost::RegisterWidgetClass() {
// Only register the class one time.
if (class_registered) return;
class_registered = true;
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpszClassName = kEdgeViewWidgetClassName;
wcex.lpfnWndProc = MainWndProc;
wcex.hInstance = GetModuleHandle(0);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClassEx(&wcex);
}
LRESULT EdgeWidgetHost::MainWndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
EdgeWidgetHost* self = nullptr;
self = GetUserDataPtr<EdgeWidgetHost*>(hWnd);
// Callback for the main window
switch (message) {
case WM_NCCREATE: {
SetLayeredWindowAttributes(hWnd, 0, 0, LWA_ALPHA);
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
self = reinterpret_cast<EdgeWidgetHost*>(cs->lpCreateParams);
SetUserDataPtr(hWnd, self);
break;
}
case WM_SIZE: {
if (self) self->OnSize();
break;
}
case WM_SETFOCUS: {
if (self) self->OnFocus();
break;
}
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // namespace edgeview