2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2024-07-11 23:28:31 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2024-08-15 12:14:02 +08:00
|
|
|
|
2024-06-10 02:25:43 +08:00
|
|
|
https://axmol.dev/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#import <WebKit/WKWebView.h>
|
|
|
|
#import <WebKit/WKUIDelegate.h>
|
|
|
|
#import <WebKit/WKNavigationDelegate.h>
|
|
|
|
|
|
|
|
#include "ui/UIWebView/UIWebViewImpl-ios.h"
|
|
|
|
#include "ui/UIWebView/UIWebView.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "platform/GLView.h"
|
|
|
|
#include "platform/ios/EAGLView-ios.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
@interface UIWebViewWrapper : NSObject
|
2021-12-31 12:12:40 +08:00
|
|
|
@property(nonatomic) std::function<bool(std::string url)> shouldStartLoading;
|
|
|
|
@property(nonatomic) std::function<void(std::string url)> didFinishLoading;
|
|
|
|
@property(nonatomic) std::function<void(std::string url)> didFailLoading;
|
|
|
|
@property(nonatomic) std::function<void(std::string url)> onJsCallback;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
|
|
|
|
@property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward;
|
|
|
|
|
|
|
|
+ (instancetype)newWebViewWrapper;
|
|
|
|
|
|
|
|
- (void)setVisible:(bool)visible;
|
|
|
|
|
|
|
|
- (void)setBounces:(bool)bounces;
|
|
|
|
|
|
|
|
- (void)setOpacityWebView:(float)opacity;
|
|
|
|
|
|
|
|
- (float)getOpacityWebView;
|
|
|
|
|
|
|
|
- (void)setBackgroundTransparent;
|
|
|
|
|
|
|
|
- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setJavascriptInterfaceScheme:(std::string_view)scheme;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadData:(std::string_view)data
|
|
|
|
MIMEType:(std::string_view)MIMEType
|
|
|
|
textEncodingName:(std::string_view)encodingName
|
|
|
|
baseURL:(std::string_view)baseURL;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadHTMLString:(std::string_view)string baseURL:(std::string_view)baseURL;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadUrl:(std::string_view)urlString cleanCachedData:(BOOL)needCleanCachedData;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadFile:(std::string_view)filePath;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
- (void)stopLoading;
|
|
|
|
|
|
|
|
- (void)reload;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)evaluateJS:(std::string_view)js;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
- (void)goBack;
|
|
|
|
|
|
|
|
- (void)goForward;
|
|
|
|
|
|
|
|
- (void)setScalesPageToFit:(const bool)scalesPageToFit;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface UIWebViewWrapper () <WKUIDelegate, WKNavigationDelegate>
|
2021-12-31 12:12:40 +08:00
|
|
|
@property(nonatomic) WKWebView* wkWebView;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
@property(nonatomic, copy) NSString* jsScheme;
|
2019-11-23 20:27:39 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation UIWebViewWrapper {
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
+ (instancetype)newWebViewWrapper
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return [[self alloc] init];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
self = [super init];
|
2021-12-31 12:12:40 +08:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
self.wkWebView = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
self.shouldStartLoading = nullptr;
|
2021-12-31 12:12:40 +08:00
|
|
|
self.didFinishLoading = nullptr;
|
|
|
|
self.didFailLoading = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
self.wkWebView.UIDelegate = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
self.wkWebView.navigationDelegate = nil;
|
|
|
|
[self.wkWebView removeFromSuperview];
|
|
|
|
[self.wkWebView release];
|
|
|
|
self.wkWebView = nil;
|
2021-12-31 12:12:40 +08:00
|
|
|
self.jsScheme = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setupWebView
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
self.wkWebView = [[WKWebView alloc] init];
|
|
|
|
self.wkWebView.UIDelegate = self;
|
2019-11-23 20:27:39 +08:00
|
|
|
self.wkWebView.navigationDelegate = self;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!self.wkWebView.superview)
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
auto view = ax::Director::getInstance()->getGLView();
|
2023-09-02 19:56:50 +08:00
|
|
|
auto eaglView = (EAGLView*)view->getEAGLView();
|
2022-09-05 14:13:52 +08:00
|
|
|
[eaglView addSubview:self.wkWebView];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setVisible:(bool)visible
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
self.wkWebView.hidden = !visible;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setBounces:(bool)bounces
|
|
|
|
{
|
|
|
|
self.wkWebView.scrollView.bounces = bounces;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setOpacityWebView:(float)opacity
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
self.wkWebView.alpha = opacity;
|
|
|
|
[self.wkWebView setOpaque:YES];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (float)getOpacityWebView
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return self.wkWebView.alpha;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setBackgroundTransparent
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
[self.wkWebView setOpaque:NO];
|
|
|
|
[self.wkWebView setBackgroundColor:[UIColor clearColor]];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
CGRect newFrame = CGRectMake(x, y, width, height);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!CGRectEqualToRect(self.wkWebView.frame, newFrame))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
self.wkWebView.frame = CGRectMake(x, y, width, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setJavascriptInterfaceScheme:(std::string_view)scheme
|
|
|
|
{
|
|
|
|
self.jsScheme = @(scheme.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadData:(std::string_view)data
|
|
|
|
MIMEType:(std::string_view)MIMEType
|
|
|
|
textEncodingName:(std::string_view)encodingName
|
|
|
|
baseURL:(std::string_view)baseURL
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto path = [[NSBundle mainBundle] resourcePath];
|
2021-12-31 12:12:40 +08:00
|
|
|
path = [path stringByAppendingPathComponent:@(baseURL.data())];
|
|
|
|
auto url = [NSURL fileURLWithPath:path];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
[self.wkWebView loadData:[NSData dataWithBytes:data.data() length:data.length()]
|
|
|
|
MIMEType:@(MIMEType.data())
|
|
|
|
characterEncodingName:@(encodingName.data())
|
|
|
|
baseURL:url];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadHTMLString:(std::string_view)string baseURL:(std::string_view)baseURL
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
auto path = [[NSBundle mainBundle] resourcePath];
|
2021-12-31 12:12:40 +08:00
|
|
|
path = [path stringByAppendingPathComponent:@(baseURL.data())];
|
|
|
|
auto url = [NSURL fileURLWithPath:path];
|
|
|
|
[self.wkWebView loadHTMLString:@(string.data()) baseURL:url];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadUrl:(std::string_view)urlString cleanCachedData:(BOOL)needCleanCachedData
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
|
|
|
NSURL* url = [NSURL URLWithString:@(urlString.data())];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NSURLRequest* request = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (needCleanCachedData)
|
2021-12-31 12:12:40 +08:00
|
|
|
request = [NSURLRequest requestWithURL:url
|
|
|
|
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
|
|
|
|
timeoutInterval:60];
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
request = [NSURLRequest requestWithURL:url];
|
|
|
|
|
|
|
|
[self.wkWebView loadRequest:request];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)loadFile:(std::string_view)filePath
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
|
|
|
NSURL* url = [NSURL fileURLWithPath:@(filePath.data())];
|
|
|
|
NSURLRequest* request = [NSURLRequest requestWithURL:url];
|
2019-11-23 20:27:39 +08:00
|
|
|
[self.wkWebView loadRequest:request];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)stopLoading
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self.wkWebView stopLoading];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)reload
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self.wkWebView reload];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)canGoForward
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return self.wkWebView.canGoForward;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)canGoBack
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return self.wkWebView.canGoBack;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)goBack
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self.wkWebView goBack];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)goForward
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self.wkWebView goForward];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)evaluateJS:(std::string_view)js
|
|
|
|
{
|
|
|
|
if (!self.wkWebView)
|
|
|
|
{
|
|
|
|
[self setupWebView];
|
|
|
|
}
|
|
|
|
[self.wkWebView evaluateJavaScript:@(js.data()) completionHandler:nil];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setScalesPageToFit:(const bool)scalesPageToFit
|
|
|
|
{
|
|
|
|
// TODO: there is not corresponding API in WK.
|
|
|
|
// https://stackoverflow.com/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit/43048514 seems
|
|
|
|
// has a solution, but it doesn't support setting it dynamically. If we want to set this feature dynamically, then
|
|
|
|
// it will be too complex.
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - WKNavigationDelegate
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)webView:(WKWebView*)webView
|
|
|
|
decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction
|
|
|
|
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
|
|
|
|
{
|
|
|
|
NSString* url = [[[navigationAction request] URL] absoluteString];
|
|
|
|
if ([[webView.URL scheme] isEqualToString:self.jsScheme])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
self.onJsCallback([url UTF8String]);
|
|
|
|
decisionHandler(WKNavigationActionPolicyCancel);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (self.shouldStartLoading && url)
|
|
|
|
{
|
|
|
|
if (self.shouldStartLoading([url UTF8String]))
|
2019-11-23 20:27:39 +08:00
|
|
|
decisionHandler(WKNavigationActionPolicyAllow);
|
|
|
|
else
|
|
|
|
decisionHandler(WKNavigationActionPolicyCancel);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decisionHandler(WKNavigationActionPolicyAllow);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation
|
|
|
|
{
|
|
|
|
if (self.didFinishLoading)
|
|
|
|
{
|
|
|
|
NSString* url = [webView.URL absoluteString];
|
2019-11-23 20:27:39 +08:00
|
|
|
self.didFinishLoading([url UTF8String]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)webView:(WKWebView*)webView didFailProvisionalNavigation:(WKNavigation*)navigation withError:(NSError*)error
|
|
|
|
{
|
|
|
|
if (self.didFailLoading)
|
|
|
|
{
|
|
|
|
NSString* errorInfo = error.userInfo[NSURLErrorFailingURLStringErrorKey];
|
|
|
|
if (errorInfo)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
self.didFailLoading([errorInfo UTF8String]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma WKUIDelegate
|
|
|
|
|
|
|
|
// Implement js alert function.
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)webView:(WKWebView*)webView
|
|
|
|
runJavaScriptAlertPanelWithMessage:(NSString*)message
|
|
|
|
initiatedByFrame:(WKFrameInfo*)frame
|
|
|
|
completionHandler:(void (^)())completionHandler
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:message
|
2019-11-23 20:27:39 +08:00
|
|
|
message:nil
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok"
|
|
|
|
style:UIAlertActionStyleCancel
|
2021-12-31 12:12:40 +08:00
|
|
|
handler:^(UIAlertAction* action) {
|
|
|
|
completionHandler();
|
2019-11-23 20:27:39 +08:00
|
|
|
}]];
|
|
|
|
|
|
|
|
auto rootViewController = [UIApplication sharedApplication].windows[0].rootViewController;
|
2021-12-31 12:12:40 +08:00
|
|
|
[rootViewController presentViewController:alertController
|
|
|
|
animated:YES
|
|
|
|
completion:^{
|
|
|
|
}];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
WebViewImpl::WebViewImpl(WebView* webView) : _uiWebViewWrapper([UIWebViewWrapper newWebViewWrapper]), _webView(webView)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_uiWebViewWrapper.shouldStartLoading = [this](std::string url) {
|
2021-12-31 12:12:40 +08:00
|
|
|
if (this->_webView->_onShouldStartLoading)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return this->_webView->_onShouldStartLoading(this->_webView, url);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
_uiWebViewWrapper.didFinishLoading = [this](std::string url) {
|
2021-12-31 12:12:40 +08:00
|
|
|
if (this->_webView->_onDidFinishLoading)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
this->_webView->_onDidFinishLoading(this->_webView, url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_uiWebViewWrapper.didFailLoading = [this](std::string url) {
|
2021-12-31 12:12:40 +08:00
|
|
|
if (this->_webView->_onDidFailLoading)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
this->_webView->_onDidFailLoading(this->_webView, url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_uiWebViewWrapper.onJsCallback = [this](std::string url) {
|
2021-12-31 12:12:40 +08:00
|
|
|
if (this->_webView->_onJSCallback)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
this->_webView->_onJSCallback(this->_webView, url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
WebViewImpl::~WebViewImpl()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper release];
|
|
|
|
_uiWebViewWrapper = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::setJavascriptInterfaceScheme(std::string_view scheme)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper setJavascriptInterfaceScheme:scheme];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadData(const Data& data,
|
|
|
|
std::string_view MIMEType,
|
|
|
|
std::string_view encoding,
|
|
|
|
std::string_view baseURL)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string dataString(reinterpret_cast<char*>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper loadData:dataString MIMEType:MIMEType textEncodingName:encoding baseURL:baseURL];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadHTMLString(std::string_view string, std::string_view baseURL)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper loadHTMLString:string baseURL:baseURL];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadURL(std::string_view url)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
this->loadURL(url, false);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadURL(std::string_view url, bool cleanCachedData)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper loadUrl:url cleanCachedData:cleanCachedData];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadFile(std::string_view fileName)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
auto fullPath = ax::FileUtils::getInstance()->fullPathForFilename(fileName);
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper loadFile:fullPath];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::stopLoading()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper stopLoading];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::reload()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper reload];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool WebViewImpl::canGoBack()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return _uiWebViewWrapper.canGoBack;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool WebViewImpl::canGoForward()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return _uiWebViewWrapper.canGoForward;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::goBack()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper goBack];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::goForward()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper goForward];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::evaluateJS(std::string_view js)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper evaluateJS:js];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::setBounces(bool bounces)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper setBounces:bounces];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper setScalesPageToFit:scalesPageToFit];
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void WebViewImpl::draw(ax::Renderer* renderer, ax::Mat4 const& transform, uint32_t flags)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
if (flags & ax::Node::FLAGS_TRANSFORM_DIRTY)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
auto director = ax::Director::getInstance();
|
2023-12-01 09:04:51 +08:00
|
|
|
auto glView = director->getGLView();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto frameSize = glView->getFrameSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2023-09-02 19:56:50 +08:00
|
|
|
auto scaleFactor = [static_cast<EAGLView*>(glView->getEAGLView()) contentScaleFactor];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto winSize = director->getWinSize();
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
auto leftBottom = this->_webView->convertToWorldSpace(ax::Vec2::ZERO);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto rightTop = this->_webView->convertToWorldSpace(
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::Vec2(this->_webView->getContentSize().width, this->_webView->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto x = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX()) / scaleFactor;
|
|
|
|
auto y = (frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor;
|
|
|
|
auto width = (rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto height = (rightTop.y - leftBottom.y) * glView->getScaleY() / scaleFactor;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
[_uiWebViewWrapper setFrameWithX:x y:y width:width height:height];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::setVisible(bool visible)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper setVisible:visible];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
void WebViewImpl::setOpacityWebView(float opacity)
|
|
|
|
{
|
|
|
|
[_uiWebViewWrapper setOpacityWebView:opacity];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
float WebViewImpl::getOpacityWebView() const
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return [_uiWebViewWrapper getOpacityWebView];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::setBackgroundTransparent()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[_uiWebViewWrapper setBackgroundTransparent];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
} // namespace ui
|
2023-06-12 14:15:37 +08:00
|
|
|
} // namespace ax
|