// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
// Copyright (c) (2018-2022) Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2
// Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein.
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
namespace UnityEngine.XR.MagicLeap
{
using System;
///
/// API for MLWebView that allows an application to instantiate a hardware
/// accelerated WebView and interact with it(via "mouse" and "keyboard" events).
///
public partial class MLWebView : MLAPIBase
{
///
/// The type of the webview pause.
///
public enum PauseType
{
///
/// Pause all multimedia activities of the webview.
///
MultiMedia,
///
/// Pause javascript timers of the webview.
///
Timers,
///
/// Pause and discard the webview rendering process. But keep alive the MLWebView handle.
///
Discard
}
///
/// Create a MLWebView.
/// The MLWebView will be ready to use once this function returns with MLResult_OK.
///
/// Width of the WebView in pixels.
/// Height of the WebView in pixels.
/// MLWebView instance if creation was successful, null otherwise.
public static MLWebView Create(uint width, uint height, bool isPopup = false, ulong popupId = 0)
{
MLWebView webView = new MLWebView();
return webView.CreateInternal(width, height, isPopup, popupId) == MLResult.Code.Ok ? webView : null;
}
///
/// Destroy a MLWebView. The MLWebView will be terminated by this function call and the shall no longer be valid.
///
/// MLResult.Code.Ok if was destroyed successfully.
/// MLResult.Code.UnspecifiedFailure if an error occurred destroying the MLWebView.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult Destroy() => MLResult.Create(DestroyInternal());
///
/// Retrieves the assigned web view handle.
///
/// The assigned web view handle, MagicLeapNativeBindings.InvalidHandle if it has not been created.
public ulong WebViewHandle
{
get => Handle;
}
///
/// Flag to indicate if urls issuing certificate errors should be loaded or not
///
public bool IgnoreCertificateError
{
get; set;
} = false;
///
/// Flag to indicate if the application accepts the popup
///
///
public bool AcceptPopup
{
get; set;
} = true;
///
/// Go to a URL with the specified MLWebView. Note that success with this call only indicates that a load will be
/// attempted. Caller can be notified about issues loading the URL via the event r on_load_error.
///
/// URL that will be loaded.
/// MLResult.Code.Ok if WebView is attempting to load the specified URL.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult GoTo(string url) => MLResult.Create(GoToInternal(url));
///
/// Trigger a "Reload" action in the MLWebView.
///
/// MLResult.Code.Ok if WebView Reload action was initiated.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult Reload() => MLResult.Create(ReloadInternal());
///
/// Trigger a "Back" action in the MLWebView. Query #MLWebViewCanGoBack before calling this method. If there is no valid
/// page to go back to, this method will be no-op.
///
/// MLResult.Code.Ok if WebView Back action was initiated or cannot go back any further.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult GoBack() => MLResult.Create(GoBackInternal());
///
/// Trigger a "Forward" action in the MLWebView. Query MLWebViewCanGoForward before calling this method. If there is no
/// valid page to go forward to, this method will be no-op.
///
/// MLResult.Code.Ok if WebView Forward action was initiated or cannot go forward any further.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult GoForward() => MLResult.Create(GoForwardInternal());
///
/// Checks if the "Back" action is currently valid.
///
/// True if can use the "Back" action.
public bool CanGoBack() => CanGoBackInternal();
///
/// Checks if the "Forward" action is currently valid.
///
/// True if can use the "Forward" action.
public bool CanGoForward() => CanGoForwardInternal();
///
/// Get the current URL.
///
/// Current URL.
public string GetURL() => GetURLInternal();
///
/// Moves the WebView mouse.
///
/// Horizontal position of the cursor.
/// Vertical position of the cursor.
/// Should be one or combination of EventFlags.
/// MLResult.Code.Ok if internal mouse was moved.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult InjectMouseMove(uint xPosition, uint yPosition, EventFlags modifiers) => MLResult.Create(InjectMouseMoveInternal(xPosition, yPosition, modifiers));
///
/// Sends a mouse button down/pressed event on a specific location on screen.
///
/// Horizontal position of the cursor.
/// Vertical position of the cursor.
/// Should be one or combination of EventFlags.
/// MLResult.Code.Ok if successful.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult InjectMouseButtonDown(uint xPosition, uint yPosition, EventFlags modifiers) => MLResult.Create(InjectMouseButtonDownInternal(xPosition, yPosition, modifiers));
///
/// Sends a mouse button up/released event on a specific location on screen.
///
/// Horizontal position of the cursor.
/// Vertical position of the cursor.
/// Should be one or combination of EventFlags.
/// MLResult.Code.Ok if successful.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult InjectMouseButtonUp(uint xPosition, uint yPosition, EventFlags modifiers) => MLResult.Create(InjectMouseButtonUpInternal(xPosition, yPosition, modifiers));
///
/// Sends a printable char keyboard event to MLWebView.
///
/// printable char utf code
/// MLResult.Code.Ok if key event was injected.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult InjectChar(char charUtf) => MLResult.Create(InjectCharInternal(charUtf));
///
/// Sends a key down/pressed event to MLWebView.
///
/// MLWebView.KeyCode.
/// Should be one or combination of MLWebView.EventFlags.
/// MLResult.Code.Ok if key event was injected.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult InjectKeyDown(MLWebView.KeyCode keyCode, uint modifierMask) => MLResult.Create(InjectKeyDownInternal(keyCode, modifierMask));
///
/// Sends a key up/release event to MLWebView.
///
/// MLWebView.KeyCode.
/// Should be one or combination of MLWebView.EventFlags.
/// MLResult.Code.Ok if key event was injected.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult InjectKeyUp(MLWebView.KeyCode keyCode, uint modifierMask) => MLResult.Create(InjectKeyUpInternal(keyCode, modifierMask));
///
/// Triggers a mouse "Scroll" event.
///
/// The number of pixels to scroll on the x axis.
/// The number of pixels to scroll on the y axis.
/// MLResult.Code.Ok if MLWebView was scrolled.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
public MLResult ScrollBy(uint xPixels, uint yPixels) => MLResult.Create(ScrollByInternal(xPixels, yPixels));
///
/// Get the entire scrollable size of the webview.
/// This should be typically called afer OnLoadEnd to determine the scollable size
/// of the main frame of the loaded page.Some pages might dynamically resize and this should be called
/// before each frame draw to correctly determine the scrollable size of the webview.
///
/// Vector2Int representing the entire width and height of the webview, in pixels.
[Obsolete("GetScrollSize has been deprecated and will be removed in a future release.")]
public Vector2Int GetScrollSize() => GetScrollSizeInternal();
///
/// Get the scroll offset of the webview.
///
/// Vector2Int representing the horizontal and vertical offset of the webview, in pixels.
[Obsolete("GetScrollOffset has been deprecated and will be removed in a future release.")]
public Vector2Int GetScrollOffset() => GetScrollOffsetInternal();
///
/// Reset zoom level to 1.0.
///
/// MLResult.Code.Ok if MLWebView zoom was reset.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
/// MLResult.Code.UnspecifiedFailure if it failed to reset zoom due to an internal error.
public MLResult ResetZoom() => MLResult.Create(ResetZoomInternal());
///
/// Zoom in one level.
///
/// MLResult.Code.Ok if MLWebView zoomed in.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
/// MLResult.Code.WebViewResultZoomLimitReached if cannot zoom in any further.
/// MLResult.Code.UnspecifiedFailure if it failed to reset zoom due to an internal error.
public MLResult ZoomIn() => MLResult.Create(ZoomInInternal());
///
/// Zoom out one level.
///
/// MLResult.Code.Ok if MLWebView zoomed out.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
/// MLResult.Code.WebViewResultZoomLimitReached if cannot zoom out any further.
/// MLResult.Code.UnspecifiedFailure if it failed to reset zoom due to an internal error.
public MLResult ZoomOut() => MLResult.Create(ZoomOutInternal());
///
/// Get the current zoom factor. The default zoom factor is 1.0.
///
/// Current numeric value for zoom factor.
public double GetZoomFactor() => GetZoomFactorInternal();
///
/// Clear the webview cache.
///
/// MLResult.Code.Ok if cache cleared successfully
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
/// MLResult.Code.UnspecifiedFailure if clearing cache failed due to an internal error.
public MLResult ClearCache() => MLResult.Create(ClearCacheInternal());
///
/// Remove all webview cookies.
///
/// MLResult.Code.Ok if all cookies removed successfully.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView .
/// MLResult.Code.UnspecifiedFailure if removing all cookies failed due to an internal error.
public MLResult RemoveAllCookies() => MLResult.Create(RemoveAllCookiesInternal());
///
/// Pause the webview. Call MLWebViewResume to resume.
/// This method provides a multiple pause types to the webview.
///
/// The type of pause to be used.
/// MLResult.Code.Ok if paused successfully.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView handle or PauseType value.
/// MLResult.Code.UnspecifiedFailure if failed due to an internal error.
/// MLResult.Code.Pending if the MLWebView handle is not ready to use. See an asynchronous mode of MLWebViewCreate.
public MLResult Pause(PauseType pauseType) => MLResult.Create(PauseInternal(pauseType));
///
/// Resumes a webview after a previous call of Pause.
/// Resume webview to the normal operation for all webview pause types.
///
/// The webview being accessed.
/// MLResult.Code.Ok if resumed successfully.
/// MLResult.Code.IllegalState if WebView was paused. See MLWebViewPause.
/// MLResult.Code.InvalidParam if its unable to find the specified MLWebView handle.
/// MLResult.Code.UnspecifiedFailure if failed due to an internal error.
/// MLResult.Code.Pending if the MLWebView handle is not ready to use. See an asynchronous mode of MLWebViewCreate.
public MLResult Resume() => MLResult.Create(ResumeInternal());
///
/// Struct containing data about clicked input field in WebView.
///
public struct InputFieldData
{
///
/// Horizontal position of the input field.
///
public int X;
///
/// Vertical position of the input field.
///
public int Y;
///
/// Width of the input field.
///
public int Width;
///
/// Height of the input field.
///
public int Height;
///
/// One or combination of TextInputFlags.
///
public TextInputFlags TextInputFlags;
///
/// One of TextInputType.
///
public TextInputType TextInputType;
}
}
}