//
// UniWebView.UI.cs
// Created by Wang Wei (@onevcat) on 2025-04-19.
//
// This file is a part of UniWebView Project (https://uniwebview.com)
// By purchasing the asset, you are allowed to use this code in as many as projects
// you want, only if you publish the final products under the name of the same account
// used for the purchase.
//
// This asset and all corresponding files (such as source code) are provided on an
// “as is” basis, without warranty of any kind, express of 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 action of contract, tort or otherwise,
// arising from, out of or in connection with the software or the use of other dealing in the software.
//
using System;
using UnityEngine;
public partial class UniWebView {
///
/// Represents the embedded toolbar in the current web view.
///
public UniWebViewEmbeddedToolbar EmbeddedToolbar { get; private set; }
///
/// Gets or sets the frame of current web view. The value is based on current `Screen.width` and `Screen.height`.
/// The first two values of `Rect` is `x` and `y` position and the followed two `width` and `height`.
///
public Rect Frame {
get => frame;
set {
fullScreen = false;
frame = value;
UpdateFrame();
}
}
///
/// A reference rect transform which the web view should change its position and size to.
/// Set it to a Unity UI element (which contains a `RectTransform`) under a canvas to determine
/// the web view frame by a certain UI element.
///
/// By using this, you could get benefit from [Multiple Resolutions UI](https://docs.unity3d.com/Manual/HOWTO-UIMultiResolution.html).
///
///
public RectTransform ReferenceRectTransform {
get => referenceRectTransform;
set {
fullScreen = false;
referenceRectTransform = value;
UpdateFrame();
}
}
private bool loggedLegacyToolbarSuppressedByEmbeddedToolbar;
#pragma warning disable 618
private bool ShouldDriveLegacyToolbar() {
if (!useToolbar) {
return false;
}
if (!useEmbeddedToolbar) {
return true;
}
if (!loggedLegacyToolbarSuppressedByEmbeddedToolbar) {
loggedLegacyToolbarSuppressedByEmbeddedToolbar = true;
UniWebViewLogger.Instance.Warning(() =>
"Detected legacy toolbar flag (useToolbar=true) together with embedded toolbar (useEmbeddedToolbar=true). " +
"Suppressing legacy toolbar to avoid duplicate toolbars. Please update old prefab/scene serialization.");
}
return false;
}
#pragma warning restore 618
///
/// Updates and sets current frame of web view to match the setting.
///
/// This is useful if the `referenceRectTransform` is changed and you need to sync the frame change
/// to the web view. This method follows the frame determining rules.
///
public void UpdateFrame() {
Rect rect = NextFrameRect();
SyncFramePropertyIfNeeded(rect);
UniWebViewInterface.SetFrame(listener.Name, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
}
void SyncFramePropertyIfNeeded(Rect rect) {
if (fullScreen || referenceRectTransform != null) {
frame = rect;
}
}
Rect UnmappedFrame() {
if (fullScreen) {
return new Rect(0, 0, Screen.width, Screen.height);
}
return referenceRectTransform != null ? referenceRectTransform.rect : frame;
}
Rect NextFrameRect() {
if (fullScreen) {
return FullScreenFrame();
}
if (referenceRectTransform == null) {
UniWebViewLogger.Instance.Info("Using Frame setting to determine web view frame.");
return NativeFrameRect(frame);
}
UniWebViewLogger.Instance.Info("Using reference RectTransform to determine web view frame.");
var worldCorners = new Vector3[4];
referenceRectTransform.GetWorldCorners(worldCorners);
// var bottomLeft = worldCorners[0];
var topLeft = worldCorners[1];
// var topRight = worldCorners[2];
var bottomRight = worldCorners[3];
var canvas = referenceRectTransform.GetComponentInParent