Skip to content

Commit

Permalink
feat(ui/webview): expose load_url method (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <[email protected]>
  • Loading branch information
tony-go authored Nov 11, 2024
1 parent b07a538 commit 93c71fa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions example/hello_world/hello_world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class App : public sourcemeta::native::Application {

#ifdef _WIN32
window.add(webview);
webview.loadUrl("https://www.sourcemeta.com");
#endif

this->exit();
Expand Down
25 changes: 23 additions & 2 deletions src/ui/webview/win32/webview_win32.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#include <WebView2.h>

#include <sourcemeta/native/webview.h>
#include <sourcemeta/native/window.h>

#include <windows.h>
#include <wrl.h>

#include <iostream>
#include <optional>
#include <string>

using namespace Microsoft::WRL;

namespace sourcemeta::native {

struct WebViewInternal {
bool ready{false};
std::optional<std::string> url;
HWND parentHwnd;
ComPtr<ICoreWebView2Controller> controller;
ComPtr<ICoreWebView2> webview;
bool ready{false};
};

WebView::WebView() : internal_(new WebViewInternal{}) {}
Expand Down Expand Up @@ -76,13 +81,29 @@ auto WebView::attachToWindow(sourcemeta::native::Window &window) -> void {
RECT bounds;
GetClientRect(internal->parentHwnd, &bounds);
internal->controller->put_Bounds(bounds);
internal->webview->Navigate(L"https://www.google.com");
internal->ready = true;

// Load the URL if it was set before the WebView was ready
if (internal->url.has_value()) {
const auto url = internal->url.value();
internal->webview->Navigate(
std::wstring(url.begin(), url.end()).c_str());
}
return S_OK;
})
.Get());
return S_OK;
})
.Get());
}

auto WebView::loadUrl(const std::string &url) -> void {
auto internal = static_cast<WebViewInternal *>(internal_);
if (internal->webview) {
internal->webview->Navigate(std::wstring(url.begin(), url.end()).c_str());
} else {
// If the WebView is not ready, store the URL to load when it is ready
internal->url = url;
}
}
} // namespace sourcemeta::native
25 changes: 25 additions & 0 deletions src/ui/window/include/sourcemeta/native/window_win32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#ifndef SOURCEMETA_NATIVE_UI_WINDOW_WIN32_H
#define SOURCEMETA_NATIVE_UI_WINDOW_WIN32_H

#include "window.h"

#include <type_traits>

namespace sourcemeta::native {

// Platform-specific implementation of add_impl for Windows
template <typename T> void add_impl(Window &window, T &child) {
if constexpr (requires { child.attachToWindow(window); }) {
child.attachToWindow(window);
}
}

// Define the template add function here in the platform-specific header
template <typename T> void Window::add(T &child) {
add_impl(*this, child); // Forward to platform-specific implementation
}

} // namespace sourcemeta::native

#endif

0 comments on commit 93c71fa

Please sign in to comment.