axmol/core/ui/UIWebView/UIWebView.h

259 lines
7.2 KiB
C
Raw Normal View History

2014-08-26 12:08:26 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2014-08-26 12:08:26 +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-25 10:04:45 +08:00
2014-08-26 12:08:26 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2014-08-26 12:08:26 +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.
****************************************************************************/
2019-10-23 14:58:31 +08:00
#pragma once
2014-08-25 13:44:38 +08:00
#include "ui/UIWidget.h"
2014-09-18 16:33:46 +08:00
#include "ui/GUIExport.h"
2014-08-25 13:44:38 +08:00
#include "base/CCData.h"
#if defined(_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2015-03-24 14:12:58 +08:00
/**
* @addtogroup ui
* @{
*/
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
namespace ui
{
2014-08-25 13:44:38 +08:00
class WebViewImpl;
2015-03-19 10:38:12 +08:00
/**
2021-12-25 10:04:45 +08:00
* @brief A View that displays web pages.
2015-03-19 10:38:12 +08:00
*
* @note WebView displays web pages base on system widget.
* It's mean WebView displays web pages above all graphical elements of cocos2d-x.
* @js NA
2015-03-19 10:38:12 +08:00
*/
class CC_GUI_DLL WebView : public axis::ui::Widget
2021-12-25 10:04:45 +08:00
{
2014-08-25 13:44:38 +08:00
public:
/**
2015-03-19 10:38:12 +08:00
* Allocates and initializes a WebView.
*/
2021-12-25 10:04:45 +08:00
static WebView* create();
2014-08-25 13:44:38 +08:00
/**
2015-03-19 10:38:12 +08:00
* Set javascript interface scheme.
*
2015-03-27 17:09:54 +08:00
* @see WebView::setOnJSCallback()
2015-03-19 10:38:12 +08:00
*/
2021-12-26 23:26:34 +08:00
void setJavascriptInterfaceScheme(std::string_view scheme);
2014-08-25 13:44:38 +08:00
/**
2015-03-19 10:38:12 +08:00
* Sets the main page contents, MIME type, content encoding, and base URL.
*
* @param data The content for the main page.
* @param MIMEType The MIME type of the data.
* @param encoding The encoding of the data.
* @param baseURL The base URL for the content.
*/
void loadData(const axis::Data& data,
2021-12-26 23:26:34 +08:00
std::string_view MIMEType,
std::string_view encoding,
std::string_view baseURL);
2021-12-25 10:04:45 +08:00
2014-08-25 13:44:38 +08:00
/**
2015-03-19 10:38:12 +08:00
* Sets the main page content and base URL.
*
* @param string The content for the main page.
* @param baseURL The base URL for the content.
*/
2021-12-26 23:26:34 +08:00
void loadHTMLString(std::string_view string, std::string_view baseURL = "");
2014-08-25 13:44:38 +08:00
/**
* Loads the given URL. It doesn't clean cached data.
2015-03-19 10:38:12 +08:00
*
* @param url Content URL.
*/
2021-12-26 23:26:34 +08:00
void loadURL(std::string_view url);
2014-08-25 13:44:38 +08:00
/**
* Loads the given URL with cleaning cached data or not.
* @param url Content URL.
* @cleanCachedData Whether to clean cached data.
*/
2021-12-26 23:26:34 +08:00
void loadURL(std::string_view url, bool cleanCachedData);
2014-08-25 13:44:38 +08:00
/**
2015-03-19 10:38:12 +08:00
* Loads the given fileName.
*
* @param fileName Content fileName.
*/
2021-12-26 23:26:34 +08:00
void loadFile(std::string_view fileName);
2014-08-25 13:44:38 +08:00
/**
2015-03-19 10:38:12 +08:00
* Stops the current load.
*/
2014-08-25 13:44:38 +08:00
void stopLoading();
/**
2015-03-19 10:38:12 +08:00
* Reloads the current URL.
*/
2014-08-25 13:44:38 +08:00
void reload();
/**
2015-03-19 10:38:12 +08:00
* Gets whether this WebView has a back history item.
*
* @return WebView has a back history item.
*/
2014-08-25 13:44:38 +08:00
bool canGoBack();
/**
2015-03-19 10:38:12 +08:00
* Gets whether this WebView has a forward history item.
*
* @return WebView has a forward history item.
*/
2014-08-25 13:44:38 +08:00
bool canGoForward();
/**
2015-03-19 10:38:12 +08:00
* Goes back in the history.
*/
2014-08-25 13:44:38 +08:00
void goBack();
/**
2015-03-19 10:38:12 +08:00
* Goes forward in the history.
*/
2014-08-25 13:44:38 +08:00
void goForward();
/**
2015-03-19 10:38:12 +08:00
* Evaluates JavaScript in the context of the currently displayed page.
*/
2021-12-26 23:26:34 +08:00
void evaluateJS(std::string_view js);
2014-08-25 13:44:38 +08:00
/**
2015-03-19 10:38:12 +08:00
* Set WebView should support zooming. The default value is false.
*/
2014-08-25 13:44:38 +08:00
void setScalesPageToFit(const bool scalesPageToFit);
2021-12-25 10:04:45 +08:00
2014-09-15 18:02:11 +08:00
/**
* Call before a web view begins loading.
2015-03-19 10:38:12 +08:00
*
2015-03-27 17:09:54 +08:00
* @param callback The web view that is about to load new content.
2015-10-21 21:55:45 +08:00
* @return YES if the web view should begin loading content; otherwise, NO.
2014-09-15 18:02:11 +08:00
*/
2021-12-26 23:26:34 +08:00
void setOnShouldStartLoading(const std::function<bool(WebView* sender, std::string_view url)>& callback);
2021-12-25 10:04:45 +08:00
2015-03-27 20:56:12 +08:00
/**
* A callback which will be called when a WebView event happens.
*/
2021-12-26 23:26:34 +08:00
typedef std::function<void(WebView* sender, std::string_view url)> ccWebViewCallback;
2015-03-19 10:38:12 +08:00
2014-09-15 18:02:11 +08:00
/**
* Call after a web view finishes loading.
2015-03-19 10:38:12 +08:00
*
2015-03-27 17:09:54 +08:00
* @param callback The web view that has finished loading.
2014-09-15 18:02:11 +08:00
*/
void setOnDidFinishLoading(const ccWebViewCallback& callback);
2021-12-25 10:04:45 +08:00
2014-09-15 18:02:11 +08:00
/**
* Call if a web view failed to load content.
2015-03-19 10:38:12 +08:00
*
2015-03-27 17:09:54 +08:00
* @param callback The web view that has failed loading.
2014-09-15 18:02:11 +08:00
*/
void setOnDidFailLoading(const ccWebViewCallback& callback);
2021-12-25 10:04:45 +08:00
2014-09-15 18:02:11 +08:00
/**
* This callback called when load URL that start with javascript interface scheme.
*/
void setOnJSCallback(const ccWebViewCallback& callback);
2021-12-25 10:04:45 +08:00
2015-03-27 20:56:12 +08:00
/**
* Get the callback when WebView is about to start.
*/
2021-12-26 23:26:34 +08:00
std::function<bool(WebView* sender, std::string_view url)> getOnShouldStartLoading() const;
2021-12-25 10:04:45 +08:00
2015-03-27 20:56:12 +08:00
/**
* Get the callback when WebView has finished loading.
*/
2021-12-25 10:04:45 +08:00
ccWebViewCallback getOnDidFinishLoading() const;
2015-03-27 20:56:12 +08:00
/**
* Get the callback when WebView has failed loading.
*/
2021-12-25 10:04:45 +08:00
ccWebViewCallback getOnDidFailLoading() const;
2015-03-27 20:56:12 +08:00
/**
*Get the Javascript callback.
*/
2021-12-25 10:04:45 +08:00
ccWebViewCallback getOnJSCallback() const;
2015-03-19 10:38:12 +08:00
/**
* Set whether the webview bounces at end of scroll of WebView.
*/
void setBounces(bool bounce);
virtual void draw(axis::Renderer* renderer, axis::Mat4 const& transform, uint32_t flags) override;
2015-03-19 10:38:12 +08:00
2015-03-27 20:56:12 +08:00
/**
* Toggle visibility of WebView.
*/
2015-03-19 10:38:12 +08:00
virtual void setVisible(bool visible) override;
/**
* SetOpacity of webview.
*/
virtual void setOpacityWebView(float opacity);
2021-12-25 10:04:45 +08:00
/**
* getOpacity of webview.
*/
virtual float getOpacityWebView() const;
2021-12-25 10:04:45 +08:00
/**
* set the background transparent
*/
virtual void setBackgroundTransparent();
2016-05-05 10:20:00 +08:00
virtual void onEnter() override;
virtual void onExit() override;
2021-12-25 10:04:45 +08:00
/**
* Default constructor.
*/
WebView();
virtual ~WebView();
2014-09-15 18:02:11 +08:00
protected:
virtual axis::ui::Widget* createCloneInstance() override;
2014-09-15 18:02:11 +08:00
virtual void copySpecialProperties(Widget* model) override;
2021-12-26 23:26:34 +08:00
std::function<bool(WebView* sender, std::string_view url)> _onShouldStartLoading = nullptr;
2021-12-28 11:00:34 +08:00
ccWebViewCallback _onDidFinishLoading = nullptr;
ccWebViewCallback _onDidFailLoading = nullptr;
ccWebViewCallback _onJSCallback = nullptr;
2021-12-25 10:04:45 +08:00
2014-08-25 13:44:38 +08:00
private:
2021-12-25 10:04:45 +08:00
WebViewImpl* _impl = nullptr;
2014-09-15 18:02:11 +08:00
friend class WebViewImpl;
2014-08-25 13:44:38 +08:00
};
2021-12-25 10:04:45 +08:00
} // namespace ui
NS_AX_END // namespace axis
#endif
2015-03-24 14:12:58 +08:00
// end group
/// @}