diff --git a/src/ui/webview/appkit/webview_appkit.mm b/src/ui/webview/appkit/webview_appkit.mm index ea53406..e61d179 100644 --- a/src/ui/webview/appkit/webview_appkit.mm +++ b/src/ui/webview/appkit/webview_appkit.mm @@ -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 { @@ -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_; @@ -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(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