Skip to content

Commit

Permalink
refactor(ui/webview): hide load_html in internals (#40)
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 a9e2d8f commit 43e6f43
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/ui/webview/appkit/webview_appkit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

~Internal() { [webView_ release]; }

void load_url(const std::string &url) {
auto load_url(const std::string &url) -> void {
NSString *urlString = [NSString stringWithUTF8String:url.c_str()];
NSURL *nsUrl = [NSURL URLWithString:urlString];
if (nsUrl) {
Expand All @@ -29,6 +29,18 @@ void load_url(const std::string &url) {
}
}

auto load_html(const std::string &html_path) -> void {
NSBundle *bundle = [NSBundle mainBundle];
NSString *html_filename = [NSString stringWithUTF8String:html_path.c_str()];
NSString *html_filename_without_extension =
[html_filename stringByDeletingPathExtension];
NSURL *html_url = [bundle URLForResource:html_filename_without_extension
withExtension:@"html"];

[this->get_webview() loadFileURL:html_url
allowingReadAccessToURL:[html_url URLByDeletingLastPathComponent]];
}

auto get_webview() -> WKWebView * { return webView_; }

private:
Expand Down Expand Up @@ -58,17 +70,6 @@ void load_url(const std::string &url) {
}

auto WebView::load_html(const std::string &html_path) -> void {
NSString *html_filename = [NSString stringWithUTF8String:html_path.c_str()];
NSString *html_filename_without_extension =
html_filename.stringByDeletingPathExtension;
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:html_filename_without_extension
ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil];

WKWebView *webview = internal_->get_webview();
[webview loadHTMLString:html baseURL:[bundle resourceURL]];
internal_->load_html(html_path);
}
} // namespace sourcemeta::native

0 comments on commit 43e6f43

Please sign in to comment.