浏览代码

Merge branch 'kgdev' into 'master'

MediaQuery

See merge request upm-packages/ui-widgets/com.unity.uiwidgets!9
/main
Fan Zhang 6 年前
当前提交
563bba8d
共有 18 个文件被更改,包括 767 次插入182 次删除
  1. 6
      Runtime/Resources/UIWidgets_canvas.shader
  2. 27
      Runtime/editor/surface.cs
  3. 54
      Runtime/engine/WidgetCanvas.cs
  4. 9
      Runtime/painting/edge_insets.cs
  5. 23
      Runtime/painting/image_resolution.cs
  6. 4
      Runtime/rendering/binding.cs
  7. 177
      Runtime/ui/window.cs
  8. 111
      Runtime/widgets/app.cs
  9. 22
      Runtime/widgets/binding.cs
  10. 38
      Runtime/widgets/framework.cs
  11. 3
      Runtime/widgets/image.cs
  12. 10
      Runtime/widgets/widget_inspector.cs
  13. 2
      Tests/Editor/MouseHover.cs
  14. 19
      Tests/Editor/Widgets.cs
  15. 114
      Runtime/Resources/UIWidgets_UIDefault.shader
  16. 9
      Runtime/Resources/UIWidgets_UIDefault.shader.meta
  17. 310
      Runtime/widgets/media_query.cs
  18. 11
      Runtime/widgets/media_query.cs.meta

6
Runtime/Resources/UIWidgets_canvas.shader


v2f o;
o.ftcoord = v.tcoord;
o.fpos = v.vertex;
#if UNITY_UV_STARTS_AT_TOP
#else
o.vertex = float4(2.0 * v.vertex.x / _viewSize.x - 1.0, 1.0 - 2.0 * v.vertex.y / _viewSize.y, 0, 1);
#endif
return o;
}

27
Runtime/editor/surface.cs


public class EditorWindowSurface : Surface {
static Material _guiTextureMat;
static Material _uiDefaultMat;
public delegate void DrawToTargetFunc(Rect screenRect, Texture texture, Material mat);
internal static Material _getGUITextureMat() {

_guiTextureMat.hideFlags = HideFlags.HideAndDontSave;
return _guiTextureMat;
}
internal static Material _getUIDefaultMat() {
if (_uiDefaultMat) {
return _uiDefaultMat;
}
var uiDefaultShader = Shader.Find("UIWidgets/UIDefault");
if (uiDefaultShader == null) {
throw new Exception("UIWidgets/UIDefault not found");
}
_uiDefaultMat = new Material(uiDefaultShader);
_uiDefaultMat.hideFlags = HideFlags.HideAndDontSave;
return _uiDefaultMat;
}
GrSurface _surface;
private DrawToTargetFunc _drawToTargetFunc;

(float) (this._surface.size.width / this._surface.devicePixelRatio),
(float) (this._surface.size.height / this._surface.devicePixelRatio));
if (_drawToTargetFunc == null)
{
if (this._drawToTargetFunc == null) {
}
else
{
_drawToTargetFunc(screenRect, this._surface.getRenderTexture(), _getGUITextureMat());
} else {
this._drawToTargetFunc(screenRect, this._surface.getRenderTexture(), _getUIDefaultMat());
}
return true;
}

54
Runtime/engine/WidgetCanvas.cs


using System;
using Unity.UIWidgets.async;
using Unity.UIWidgets.async;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.foundation;
using RawImage = UnityEngine.UI.RawImage;
using Rect = UnityEngine.Rect;
namespace Unity.UIWidgets.engine

}
[RequireComponent(typeof(RectTransform))]
public abstract class WidgetCanvas : MaskableGraphic, IPointerDownHandler, IPointerUpHandler, IDragHandler,
public abstract class WidgetCanvas : RawImage, IPointerDownHandler, IPointerUpHandler, IDragHandler,
private static Event _repaintEvent;
private static Event _repaintEvent;
_repaintEvent = new Event() { type = EventType.Repaint };
_repaintEvent = new Event {type = EventType.Repaint};
if (_windowAdapter == null)
{
_windowAdapter = new UIWidgetWindowAdapter(this);
}
D.assert(this._windowAdapter == null);
_windowAdapter = new UIWidgetWindowAdapter(this);
var root = new WidgetsApp(null, getWidget(), _windowAdapter);
var root = new WidgetsApp(home: getWidget(), window: _windowAdapter);
private void OnDisable()
protected override void OnDisable()
D.assert(this._windowAdapter != null);
this._windowAdapter = null;
public override Texture mainTexture
{
get { return _texture; }
}
internal void applyRenderTexture(Rect screenRect, Texture texture, Material mat)
{
_texture = texture;
SetMaterialDirty();
}
private void OnDestroy()
{
base.OnDestroy();
internal void applyRenderTexture(Rect screenRect, Texture texture, Material mat) {
this.texture = texture;
this.material = mat;
}
private void Update()

}
_lastMouseMove = Input.mousePosition;
if (this._windowAdapter != null)
{
this._windowAdapter.Update();
}
D.assert(this._windowAdapter != null);
this._windowAdapter.Update();
private void OnGUI()
private void OnGUI()
{
if (Event.current.type == EventType.KeyDown || Event.current.type == EventType.KeyUp)
{

public Vector2 getPointPosition(Vector2 position)
{
// Debug.Log("mouse posse " + position.x + " " + position.y);
Vector2 localPoint;
Camera eventCamera = null;

9
Runtime/painting/edge_insets.cs


return new EdgeInsets(horizontal, vertical, horizontal, vertical);
}
public static EdgeInsets fromWindowPadding(WindowPadding padding, double devicePixelRatio) {
return new EdgeInsets(
left: padding.left / devicePixelRatio,
top: padding.top / devicePixelRatio,
right: padding.right / devicePixelRatio,
bottom: padding.bottom / devicePixelRatio
);
}
public static readonly EdgeInsets zero = EdgeInsets.only();
public Offset topLeft {

23
Runtime/painting/image_resolution.cs


using System;
using System.Collections;
using System.Collections.Generic;
using RSG;
using Unity.UIWidgets.async;
using Unity.UIWidgets.foundation;

public readonly string assetName;
public readonly AssetBundle bundle;
readonly Dictionary<ImageConfiguration, AssetBundleImageKey> _cache =
new Dictionary<ImageConfiguration, AssetBundleImageKey>();
AssetBundleImageKey key;
if (this._cache.TryGetValue(configuration, out key)) {
return Promise<AssetBundleImageKey>.Resolved(key);
}
return (AssetBundleImageKey) result;
key = (AssetBundleImageKey) result;
this._cache[configuration] = key;
return key;
});
}

asset = request.asset;
}
if (asset != null) {
if (asset != null) {
if (bundle == null) {
Resources.UnloadAsset(asset);
} else {
bundle.Unload(asset);
}
yield return new AssetBundleImageKey(
bundle,
assetName,

4
Runtime/rendering/binding.cs


);
Window.instance.onMetricsChanged += this.handleMetricsChanged;
Window.instance.onTextScaleFactorChanged += this.handleTextScaleFactorChanged;
this.initRenderView();
D.assert(this.renderView != null);
this.addPersistentFrameCallback(this._handlePersistentFrameCallback);

protected virtual void handleMetricsChanged() {
this.renderView.configuration = this.createViewConfiguration();
this.scheduleForcedFrame();
}
protected virtual void handleTextScaleFactorChanged() {
}
protected virtual ViewConfiguration createViewConfiguration() {

177
Runtime/ui/window.cs


using System;
using System.Collections.Generic;
using Unity.UIWidgets.async;
using Unity.UIWidgets.service;
using Unity.UIWidgets.foundation;

public delegate void PointerDataPacketCallback(PointerDataPacket packet);
public class WindowPadding {
public WindowPadding(double left, double top, double right, double bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public readonly double left;
public readonly double top;
public readonly double right;
public readonly double bottom;
public static WindowPadding zero = new WindowPadding(left: 0.0, top: 0.0, right: 0.0, bottom: 0.0);
public override string ToString() {
return $"{this.GetType()}(left: {this.left}, top: {this.top}, right: {this.right}, bottom: {this.bottom})";
}
}
public class Locale : IEquatable<Locale> {
public Locale(string languageCode, string countryCode = null) {
D.assert(languageCode != null);
this._languageCode = languageCode;
this._countryCode = countryCode;
}
readonly string _languageCode;
public string languageCode => this._languageCode;
readonly string _countryCode;
public string countryCode => this._countryCode;
public bool Equals(Locale other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(this._languageCode, other._languageCode) &&
string.Equals(this._countryCode, other._countryCode);
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((Locale) obj);
}
public override int GetHashCode() {
unchecked {
return ((this._languageCode != null ? this._languageCode.GetHashCode() : 0) * 397) ^
(this._countryCode != null ? this._countryCode.GetHashCode() : 0);
}
}
public static bool operator ==(Locale left, Locale right) {
return Equals(left, right);
}
public static bool operator !=(Locale left, Locale right) {
return !Equals(left, right);
}
public override string ToString() {
if (this.countryCode == null) {
return this.languageCode;
}
return $"{this.languageCode}_{this.countryCode}";
}
}
public abstract class Window {
public static Window instance {
get {

}
public static bool hasInstance {
get {
return _instance != null;
}
get { return _instance != null; }
public double devicePixelRatio {
get { return this._devicePixelRatio; }
}
public double devicePixelRatio => this._devicePixelRatio;
public Size physicalSize {
get { return this._physicalSize; }
}
public Size physicalSize => this._physicalSize;
protected Size _physicalSize = Size.zero;
protected Size _physicalSize = Size.zero;
public WindowPadding viewInsets => this._viewInsets;
protected WindowPadding _viewInsets = WindowPadding.zero;
public WindowPadding padding => this._padding;
protected WindowPadding _padding = WindowPadding.zero;
public VoidCallback onMetricsChanged {
get { return this._onMetricsChanged; }

VoidCallback _onMetricsChanged;
public Locale locale {
get {
if (this._locales != null && this._locales.isNotEmpty()) {
return this._locales[0];
}
return null;
}
}
public List<Locale> locales => this._locales;
protected List<Locale> _locales;
public VoidCallback onLocaleChanged {
get { return this._onLocaleChanged; }
set { this._onLocaleChanged = value; }

public double textScaleFactor => this._textScaleFactor;
protected double _textScaleFactor = 1.0;
public VoidCallback onTextScaleFactorChanged {
get { return this._onTextScaleFactorChanged; }
set { this._onTextScaleFactorChanged = value; }
}
VoidCallback _onTextScaleFactorChanged;
public FrameCallback onBeginFrame {
get { return this._onBeginFrame; }
set { this._onBeginFrame = value; }

}
VoidCallback _onDrawFrame;
public PointerDataPacketCallback onPointerEvent {
get { return this._onPointerEvent; }
set { this._onPointerEvent = value; }

public abstract void flushMicrotasks();
public abstract Timer run(TimeSpan duration, Action callback, bool periodic = false);
public Timer run(Action callback) {
return this.run(TimeSpan.Zero, callback);
}

public abstract IDisposable getScope();
}
public class Locale : IEquatable<Locale> {
public Locale(string languageCode, string countryCode = null) {
D.assert(languageCode != null);
this._languageCode = languageCode;
this._countryCode = countryCode;
}
readonly string _languageCode;
public string languageCode => this._languageCode;
readonly string _countryCode;
public string countryCode => this._countryCode;
public bool Equals(Locale other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(this._languageCode, other._languageCode) &&
string.Equals(this._countryCode, other._countryCode);
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((Locale) obj);
}
public override int GetHashCode() {
unchecked {
return ((this._languageCode != null ? this._languageCode.GetHashCode() : 0) * 397) ^
(this._countryCode != null ? this._countryCode.GetHashCode() : 0);
}
}
public static bool operator ==(Locale left, Locale right) {
return Equals(left, right);
}
public static bool operator !=(Locale left, Locale right) {
return !Equals(left, right);
}
public override string ToString() {
if (this.countryCode == null) {
return this.languageCode;
}
return $"{this.languageCode}_{this.countryCode}";
}
}
}

111
Runtime/widgets/app.cs


using System;
using System.Collections.Generic;
using UnityEngine;
namespace Unity.UIWidgets.widgets
{
public class WidgetsApp : StatefulWidget
{
namespace Unity.UIWidgets.widgets {
public class WidgetsApp : StatefulWidget {
public readonly Window window;
public readonly Widget child;
public readonly Widget home;
public readonly Window window;
public WidgetsApp(Key key, Widget child, Window window) : base(key)
{
public WidgetsApp(
Key key = null,
Window window = null,
Widget home = null
) : base(key) {
this.child = child;
this.home = home;
public override State createState()
{
public override State createState() {
readonly Window _window;
public WindowProvider(Window window, Widget child, Key key = null) : base(key: key, child: child) {
public WindowProvider(Key key = null, Window window = null, Widget child = null) :
base(key: key, child: child) {
_window = window;
this.window = window;
public readonly Window window;
WindowProvider provider = context.inheritFromWidgetOfExactType(typeof(WindowProvider)) as WindowProvider;
WindowProvider provider = (WindowProvider) context.inheritFromWidgetOfExactType(typeof(WindowProvider));
return provider._window;
return provider.window;
D.assert(_window == ((WindowProvider)oldWidget)._window);
D.assert(this.window == ((WindowProvider) oldWidget).window);
class _WidgetsAppState : State<WidgetsApp>
{
class _WidgetsAppState : State<WidgetsApp>, WidgetsBindingObserver {
D.assert(() =>
{
D.assert(() => {
WidgetsBinding.instance.addObserver(this);
WidgetsBinding.instance.removeObserver(this);
D.assert(() =>
{
D.assert(() => {
WidgetInspectorService.instance.inspectorShowCallback -= inspectorShowChanged;
return true;
});

private void inspectorShowChanged()
{
setState(() => {});
private void inspectorShowChanged() {
this.setState();
public override Widget build(BuildContext context)
{
Widget result = widget.child;
result = new WindowProvider(widget.window, result);
D.assert(() =>
{
if (WidgetInspectorService.instance.debugShowInspector)
{
result = new WidgetInspector(null, result, _InspectorSelectButtonBuilder);
public void didChangeMetrics() {
this.setState();
}
public void didChangeTextScaleFactor() {
this.setState();
}
public void didChangeLocales(List<Locale> locale) {
// TODO: support locales.
}
public override Widget build(BuildContext context) {
Widget result = this.widget.home;
D.assert(() => {
if (WidgetInspectorService.instance.debugShowInspector) {
result = new WidgetInspector(null, result, this._InspectorSelectButtonBuilder);
result = new WindowProvider(
window: this.widget.window,
child: result
);
result = new MediaQuery(
data: MediaQueryData.fromWindow(this.widget.window),
child: result
);
private Widget _InspectorSelectButtonBuilder(BuildContext context, VoidCallback onPressed)
{
private Widget _InspectorSelectButtonBuilder(BuildContext context, VoidCallback onPressed) {
) : base(key: key)
{
) : base(key: key) {
this.onPressed = () => onPressed();
}

22
Runtime/widgets/binding.cs


namespace Unity.UIWidgets.widgets {
public interface WidgetsBindingObserver {
void didChangeMetrics();
void didChangeTextScaleFactor();
void didChangeLocales(List<Locale> locale);
}
public class WidgetsBinding : RendererBinding {

observer.didChangeMetrics();
}
}
protected override void handleTextScaleFactorChanged() {
base.handleTextScaleFactorChanged();
foreach (WidgetsBindingObserver observer in this._observers) {
observer.didChangeTextScaleFactor();
}
}
void handleLocaleChanged() {
// todo
// dispatchLocaleChanged(window.locale);
protected virtual void handleLocaleChanged() {
this.dispatchLocalesChanged(Window.instance.locales);
}
protected virtual void dispatchLocalesChanged(List<Locale> locales) {
foreach (WidgetsBindingObserver observer in this._observers) {
observer.didChangeLocales(locales);
}
}
void _handleBuildScheduled() {

38
Runtime/widgets/framework.cs


public virtual void didUpdateWidget(StatefulWidget oldWidget) {
}
protected void setState(VoidCallback fn) {
D.assert(fn != null);
protected void setState(VoidCallback fn = null) {
D.assert(() => {
if (this._debugLifecycleState == _StateLifecycle.defunct) {
throw new UIWidgetsError(

return true;
});
fn();
if (fn != null) {
fn();
}
this._element.markNeedsBuild();
}

D.assert(this.widget != newWidget);
base.update(newWidget);
D.assert(this.widget == newWidget);
this.notifyClients(oldWidget);
this.updated(oldWidget);
protected virtual void updated(ProxyWidget oldWidget) {
this.notifyClients(oldWidget);
}
public abstract void notifyClients(ProxyWidget oldWidget);
protected abstract void notifyClients(ProxyWidget oldWidget);
}
public class ParentDataElement : ProxyElement {

this._applyParentData(newWidget);
}
public override void notifyClients(ProxyWidget oldWidget) {
protected override void notifyClients(ProxyWidget oldWidget) {
this._applyParentData(this.widget);
}
}

}
public void setDependencies(Element dependent, object value) {
object existing;
if (this._dependents.TryGetValue(dependent, out existing)) {
if (object.Equals(existing, value)) {
return;
}
}
this._dependents[dependent] = value;
}

public void notifyDependent(InheritedWidget oldWidget, Element dependent) {
dependent.didChangeDependencies();
}
protected override void updated(ProxyWidget oldWidget) {
if (this.widget.updateShouldNotify((InheritedWidget) oldWidget)) {
base.updated(oldWidget);
}
}
public override void notifyClients(ProxyWidget oldWidgetRaw) {
protected override void notifyClients(ProxyWidget oldWidgetRaw) {
if (!this.widget.updateShouldNotify(oldWidget)) {
return;
}
D.assert(this._debugCheckOwnerBuildTargetExists("notifyClients"));
foreach (Element dependent in this._dependents.Keys) {

3
Runtime/widgets/image.cs


public static ImageConfiguration createLocalImageConfiguration(BuildContext context, Size size = null) {
return new ImageConfiguration(
bundle: DefaultAssetBundle.of(context),
//TODO: add MediaQuery & Localizations.
//devicePixelRatio: MediaQuery.of(context, nullOk: true)?.devicePixelRatio ?? 1.0,
devicePixelRatio: MediaQuery.of(context, nullOk: true)?.devicePixelRatio ?? 1.0,
//locale: Localizations.localeOf(context, nullOk: true),
size: size,
platform: Application.platform

10
Runtime/widgets/widget_inspector.cs


return new Stack(children: children);
}
public void didChangeMetrics()
{
throw new NotImplementedException();
public void didChangeMetrics() {
}
public void didChangeTextScaleFactor() {
}
public void didChangeLocales(List<Locale> locale) {
}
private void _selectionChangedCallback()

2
Tests/Editor/MouseHover.cs


{
public class MouseHoverWidget:StatefulWidget
{
public MouseHoverWidget(Key key) : base(key)
public MouseHoverWidget(Key key = null) : base(key)
{
}

19
Tests/Editor/Widgets.cs


if (this._selected != selected) {
this._selected = selected;
this.windowAdapter.attachRootWidget(null);
this._attachRootWidget(null);
this.windowAdapter.attachRootWidget(rootWidget);
this._attachRootWidget(rootWidget);
this.windowAdapter.attachRootWidget(rootWidget);
this._attachRootWidget(rootWidget);
}
if (GUILayout.Button("UnloadUnusedAssets")) {

var rootWidget = this._options[this._selected]();
this.windowAdapter.attachRootWidget(rootWidget);
this._attachRootWidget(rootWidget);
}
void _attachRootWidget(Widget widget) {
this.windowAdapter.attachRootWidget(new WidgetsApp(window: this.windowAdapter, home: widget));
}
private void Update() {

}
Widget asPage() {
return new WidgetsApp(null, new AsScreen(), windowAdapter);
return new WidgetsApp(home: new AsScreen(), window: this.windowAdapter);
Widget mouseHover()
{
return new WidgetsApp(null, new MouseHoverWidget(null), windowAdapter);
Widget mouseHover() {
return new WidgetsApp(home: new MouseHoverWidget(), window: this.windowAdapter);
}
}

114
Runtime/Resources/UIWidgets_UIDefault.shader


Shader "UIWidgets/UIDefault"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend One OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile __ UNITY_UI_CLIP_RECT
#pragma multi_compile __ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
float4 _MainTex_ST;
v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.worldPosition = v.vertex;
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
OUT.color = v.color * _Color;
return OUT;
}
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
#ifdef UNITY_UI_CLIP_RECT
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
return color;
}
ENDCG
}
}
}

9
Runtime/Resources/UIWidgets_UIDefault.shader.meta


fileFormatVersion: 2
guid: 43b4ae1d316ca415589f96e49cc0fcab
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

310
Runtime/widgets/media_query.cs


using System;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.widgets {
public enum Orientation {
portrait,
landscape
}
public class MediaQueryData : IEquatable<MediaQueryData> {
public MediaQueryData(
Size size = null,
double devicePixelRatio = 1.0,
double textScaleFactor = 1.0,
EdgeInsets viewInsets = null,
EdgeInsets padding = null,
bool alwaysUse24HourFormat = false,
bool accessibleNavigation = false,
bool invertColors = false,
bool disableAnimations = false,
bool boldText = false
) {
this.size = size ?? Size.zero;
this.devicePixelRatio = devicePixelRatio;
this.textScaleFactor = textScaleFactor;
this.viewInsets = viewInsets ?? EdgeInsets.zero;
this.padding = padding ?? EdgeInsets.zero;
this.alwaysUse24HourFormat = alwaysUse24HourFormat;
this.accessibleNavigation = accessibleNavigation;
this.invertColors = invertColors;
this.disableAnimations = disableAnimations;
this.boldText = boldText;
}
public static MediaQueryData fromWindow(Window window) {
return new MediaQueryData(
size: window.physicalSize / window.devicePixelRatio,
devicePixelRatio: window.devicePixelRatio,
textScaleFactor: window.textScaleFactor,
viewInsets: EdgeInsets.fromWindowPadding(window.viewInsets, window.devicePixelRatio),
padding: EdgeInsets.fromWindowPadding(window.padding, window.devicePixelRatio)
// accessibleNavigation: window.accessibilityFeatures.accessibleNavigation,
// invertColors: window.accessibilityFeatures.invertColors,
// disableAnimations: window.accessibilityFeatures.disableAnimations,
// boldText: window.accessibilityFeatures.boldText,
// alwaysUse24HourFormat: window.alwaysUse24HourFormat
);
}
public readonly Size size;
public readonly double devicePixelRatio;
public readonly double textScaleFactor;
public readonly EdgeInsets viewInsets;
public readonly EdgeInsets padding;
public readonly bool alwaysUse24HourFormat;
public readonly bool accessibleNavigation;
public readonly bool invertColors;
public readonly bool disableAnimations;
public readonly bool boldText;
public Orientation orientation =>
this.size.width > this.size.height ? Orientation.landscape : Orientation.portrait;
public MediaQueryData copyWith(
Size size = null,
double? devicePixelRatio = null,
double? textScaleFactor = null,
EdgeInsets viewInsets = null,
EdgeInsets padding = null,
bool? alwaysUse24HourFormat = null,
bool? accessibleNavigation = null,
bool? invertColors = null,
bool? disableAnimations = null,
bool? boldText = null
) {
return new MediaQueryData(
size: size ?? this.size,
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
viewInsets: viewInsets ?? this.viewInsets,
padding: padding ?? this.padding,
alwaysUse24HourFormat: alwaysUse24HourFormat ?? this.alwaysUse24HourFormat,
accessibleNavigation: accessibleNavigation ?? this.accessibleNavigation,
invertColors: invertColors ?? this.invertColors,
disableAnimations: disableAnimations ?? this.disableAnimations,
boldText: boldText ?? this.boldText
);
}
public MediaQueryData removePadding(
bool removeLeft = false,
bool removeTop = false,
bool removeRight = false,
bool removeBottom = false
) {
if (!(removeLeft || removeTop || removeRight || removeBottom)) {
return this;
}
return new MediaQueryData(
size: this.size,
devicePixelRatio: this.devicePixelRatio,
textScaleFactor: this.textScaleFactor,
padding: this.padding.copyWith(
left: removeLeft ? (double?) 0.0 : null,
top: removeTop ? (double?) 0.0 : null,
right: removeRight ? (double?) 0.0 : null,
bottom: removeBottom ? (double?) 0.0 : null
),
viewInsets: this.viewInsets,
alwaysUse24HourFormat: this.alwaysUse24HourFormat,
disableAnimations: this.disableAnimations,
invertColors: this.invertColors,
accessibleNavigation: this.accessibleNavigation,
boldText: this.boldText
);
}
public MediaQueryData removeViewInsets(
bool removeLeft = false,
bool removeTop = false,
bool removeRight = false,
bool removeBottom = false
) {
if (!(removeLeft || removeTop || removeRight || removeBottom)) {
return this;
}
return new MediaQueryData(
size: this.size,
devicePixelRatio: this.devicePixelRatio,
textScaleFactor: this.textScaleFactor,
padding: this.padding,
viewInsets: this.viewInsets.copyWith(
left: removeLeft ? (double?) 0.0 : null,
top: removeTop ? (double?) 0.0 : null,
right: removeRight ? (double?) 0.0 : null,
bottom: removeBottom ? (double?) 0.0 : null
),
alwaysUse24HourFormat: this.alwaysUse24HourFormat,
disableAnimations: this.disableAnimations,
invertColors: this.invertColors,
accessibleNavigation: this.accessibleNavigation,
boldText: this.boldText
);
}
public bool Equals(MediaQueryData other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(this.size, other.size) && this.devicePixelRatio.Equals(other.devicePixelRatio) &&
this.textScaleFactor.Equals(other.textScaleFactor) && Equals(this.viewInsets, other.viewInsets) &&
Equals(this.padding, other.padding) && this.alwaysUse24HourFormat == other.alwaysUse24HourFormat &&
this.accessibleNavigation == other.accessibleNavigation && this.invertColors == other.invertColors &&
this.disableAnimations == other.disableAnimations && this.boldText == other.boldText;
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((MediaQueryData) obj);
}
public override int GetHashCode() {
unchecked {
var hashCode = (this.size != null ? this.size.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.devicePixelRatio.GetHashCode();
hashCode = (hashCode * 397) ^ this.textScaleFactor.GetHashCode();
hashCode = (hashCode * 397) ^ (this.viewInsets != null ? this.viewInsets.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (this.padding != null ? this.padding.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.alwaysUse24HourFormat.GetHashCode();
hashCode = (hashCode * 397) ^ this.accessibleNavigation.GetHashCode();
hashCode = (hashCode * 397) ^ this.invertColors.GetHashCode();
hashCode = (hashCode * 397) ^ this.disableAnimations.GetHashCode();
hashCode = (hashCode * 397) ^ this.boldText.GetHashCode();
return hashCode;
}
}
public static bool operator ==(MediaQueryData left, MediaQueryData right) {
return Equals(left, right);
}
public static bool operator !=(MediaQueryData left, MediaQueryData right) {
return !Equals(left, right);
}
public override string ToString() {
return $"{this.GetType()}(" +
$"size: {this.size}, " +
$"devicePixelRatio: {this.devicePixelRatio:F1}, " +
$"textScaleFactor: {this.textScaleFactor:F1}, " +
$"padding: {this.padding}, " +
$"viewInsets: {this.viewInsets}, " +
$"alwaysUse24HourFormat: {this.alwaysUse24HourFormat}, " +
$"accessibleNavigation: {this.accessibleNavigation}" +
$"disableAnimations: {this.disableAnimations}" +
$"invertColors: {this.invertColors}" +
$"boldText: {this.boldText}" +
")";
}
}
public class MediaQuery : InheritedWidget {
public MediaQuery(
Key key = null,
MediaQueryData data = null,
Widget child = null
) : base(key, child) {
D.assert(child != null);
D.assert(data != null);
this.data = data;
}
public static MediaQuery removePadding(
Key key = null,
BuildContext context = null,
bool removeLeft = false,
bool removeTop = false,
bool removeRight = false,
bool removeBottom = false,
Widget child = null
) {
D.assert(context != null);
return new MediaQuery(
key: key,
data: MediaQuery.of(context).removePadding(
removeLeft: removeLeft,
removeTop: removeTop,
removeRight: removeRight,
removeBottom: removeBottom
),
child: child
);
}
public static MediaQuery removeViewInsets(
Key key = null,
BuildContext context = null,
bool removeLeft = false,
bool removeTop = false,
bool removeRight = false,
bool removeBottom = false,
Widget child = null
) {
D.assert(context != null);
return new MediaQuery(
key: key,
data: MediaQuery.of(context).removeViewInsets(
removeLeft: removeLeft,
removeTop: removeTop,
removeRight: removeRight,
removeBottom: removeBottom
),
child: child
);
}
public readonly MediaQueryData data;
public static MediaQueryData of(BuildContext context, bool nullOk = false) {
D.assert(context != null);
MediaQuery query = (MediaQuery) context.inheritFromWidgetOfExactType(typeof(MediaQuery));
if (query != null) {
return query.data;
}
if (nullOk) {
return null;
}
throw new UIWidgetsError(
"MediaQuery.of() called with a context that does not contain a MediaQuery.\n" +
"No MediaQuery ancestor could be found starting from the context that was passed " +
"to MediaQuery.of(). This can happen because you do not have a WidgetsApp or " +
"MaterialApp widget (those widgets introduce a MediaQuery), or it can happen " +
"if the context you use comes from a widget above those widgets.\n" +
"The context used was:\n" +
$" {context}");
}
public static double textScaleFactorOf(BuildContext context) {
return MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0;
}
static bool boldTextOverride(BuildContext context) {
return MediaQuery.of(context, nullOk: true)?.boldText ?? false;
}
public override bool updateShouldNotify(InheritedWidget oldWidget) =>
this.data != ((MediaQuery) oldWidget).data;
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<MediaQueryData>("data", this.data, showName: false));
}
}
}

11
Runtime/widgets/media_query.cs.meta


fileFormatVersion: 2
guid: e578bc0c4f03f48ef88045315c000575
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存