Skip to content

Commit

Permalink
Fix invoke block.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft authored and SteveSandersonMS committed Dec 23, 2019
1 parent a76ccd1 commit acdb2a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
30 changes: 6 additions & 24 deletions src/WebWindow.Native/WebWindow.Windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ HINSTANCE WebWindow::_hInstance;
HWND messageLoopRootWindowHandle;
std::map<HWND, WebWindow*> hwndToWebWindow;

struct InvokeWaitInfo
{
std::condition_variable completionNotifier;
bool isCompleted;
};

struct ShowMessageParams
{
std::wstring title;
Expand All @@ -41,7 +35,7 @@ void WebWindow::Register(HINSTANCE hInstance)
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClassW(&wc);
RegisterClass(&wc);

SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
}
Expand All @@ -51,7 +45,7 @@ WebWindow::WebWindow(AutoString title, WebWindow* parent, WebMessageReceivedCall
// Create the window
_webMessageReceivedCallback = webMessageReceivedCallback;
_parent = parent;
_hWnd = CreateWindowExW(
_hWnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
title, // Window text
Expand Down Expand Up @@ -94,7 +88,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
case WM_USER_SHOWMESSAGE:
{
ShowMessageParams* params = (ShowMessageParams*)wParam;
MessageBoxW(hwnd, params->body.c_str(), params->title.c_str(), params->type);
MessageBox(hwnd, params->body.c_str(), params->title.c_str(), params->type);
delete params;
return 0;
}
Expand All @@ -103,12 +97,6 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ACTION callback = (ACTION)wParam;
callback();
InvokeWaitInfo* waitInfo = (InvokeWaitInfo*)lParam;
{
std::lock_guard<std::mutex> guard(invokeLockMutex);
waitInfo->isCompleted = true;
}
waitInfo->completionNotifier.notify_one();
return 0;
}
case WM_SIZE:
Expand Down Expand Up @@ -152,7 +140,7 @@ void WebWindow::RefitContent()

void WebWindow::SetTitle(AutoString title)
{
SetWindowTextW(_hWnd, title);
SetWindowText(_hWnd, title);
}

void WebWindow::Show()
Expand Down Expand Up @@ -187,18 +175,12 @@ void WebWindow::ShowMessage(AutoString title, AutoString body, UINT type)
params->title = title;
params->body = body;
params->type = type;
PostMessageW(_hWnd, WM_USER_SHOWMESSAGE, (WPARAM)params, 0);
::SendMessage(_hWnd, WM_USER_SHOWMESSAGE, (WPARAM)params, 0);
}

void WebWindow::Invoke(ACTION callback)
{
InvokeWaitInfo waitInfo = {};
PostMessageW(_hWnd, WM_USER_INVOKE, (WPARAM)callback, (LPARAM)&waitInfo);

// Block until the callback is actually executed and completed
// TODO: Add return values, exception handling, etc.
std::unique_lock<std::mutex> uLock(invokeLockMutex);
waitInfo.completionNotifier.wait(uLock, [&] { return waitInfo.isCompleted; });
::SendMessage(_hWnd, WM_USER_INVOKE, (WPARAM)callback, 0);
}

void WebWindow::AttachWebView()
Expand Down
2 changes: 1 addition & 1 deletion testassets/MyBlazorApp/MyBlazorApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion testassets/MyBlazorApp/Pages/WindowProp.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@inject WebWindow Window

<h1>Window properties</h1>
<button class="btn btn-primary" @onclick="StateHasChanged">Refresh</button>
<h2>Screen size</h2>
<p>DPI: @Window.ScreenDpi</p>
@foreach (var (m, i) in Window.Monitors.Select((monitor, index) => (monitor, index)))
Expand Down Expand Up @@ -55,6 +54,12 @@
</div>

@code {
protected override void OnInitialized()
{
Window.SizeChanged += (sender, e) => StateHasChanged();
Window.LocationChanged += (sender, e) => StateHasChanged();
}

string iconFilename;

void ChangeIconFile()
Expand Down

0 comments on commit acdb2a1

Please sign in to comment.