Skip to content

Commit

Permalink
refactor(ui/webview): Appkit webview refactor2 (#41)
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 19, 2024
1 parent 43e6f43 commit a8a5c87
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/ui/webview/appkit/webview_appkit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
~Internal() { [webView_ release]; }

auto load_url(const std::string &url) -> void {
NSString *urlString = [NSString stringWithUTF8String:url.c_str()];
NSURL *nsUrl = [NSURL URLWithString:urlString];
if (nsUrl) {
NSURLRequest *request = [NSURLRequest requestWithURL:nsUrl];
[webView_ loadRequest:request];
} else {
NSLog(@"Invalid URL string: %@", urlString);
NSString *url_ns_str = [NSString stringWithUTF8String:url.c_str()];
NSURL *url_ns = [NSURL URLWithString:url_ns_str];

if (!url_ns) {
NSLog(@"Invalid URL string: %@", url_ns_str);
return;
}

NSURLRequest *request = [NSURLRequest requestWithURL:url_ns];
[webView_ loadRequest:request];
}

auto load_html(const std::string &html_path) -> void {
Expand All @@ -37,11 +39,21 @@ auto load_html(const std::string &html_path) -> void {
NSURL *html_url = [bundle URLForResource:html_filename_without_extension
withExtension:@"html"];

[this->get_webview() loadFileURL:html_url
allowingReadAccessToURL:[html_url URLByDeletingLastPathComponent]];
if (!html_url) {
NSLog(@"Failed to load HTML file: %@", html_filename);
return;
}

[this->webView_ loadFileURL:html_url
allowingReadAccessToURL:[html_url URLByDeletingLastPathComponent]];
}

auto get_webview() -> WKWebView * { return webView_; }
auto attach_to(NSWindow *window) -> void {
NSView *contentView = [window contentView];
[webView_ setFrame:contentView.bounds];
[webView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[contentView addSubview:webView_];
}

private:
WKWebView *webView_;
Expand All @@ -51,25 +63,23 @@ auto load_html(const std::string &html_path) -> void {

WebView::~WebView() { delete internal_; }

auto WebView::resize() -> void {
// Implement resize if needed
}

auto WebView::attach_to(sourcemeta::native::Window &window) -> void {
NSWindow *native_window = static_cast<NSWindow *>(window.handle());
WKWebView *webview = internal_->get_webview();

NSView *contentView = [native_window contentView];
[webview setFrame:contentView.bounds];
[webview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[contentView addSubview:webview];
assert(native_window);
internal_->attach_to(native_window);
}

auto WebView::load_url(const std::string &url) -> void {
if (url.empty()) {
return;
}
internal_->load_url(url);
}

auto WebView::load_html(const std::string &html_path) -> void {
if (html_path.empty() || !html_path.ends_with(".html")) {
return;
}
internal_->load_html(html_path);
}
} // namespace sourcemeta::native

0 comments on commit a8a5c87

Please sign in to comment.