Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ui/webview): hide load_html in internals #40

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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