Skip to content

Commit

Permalink
Optimize view Bounds property
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Jul 11, 2022
1 parent 6cadd69 commit b929ab5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions Runtime/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class View<TState> : UIBehaviour, IView, IViewTreeElement
[NotNull] private readonly List<IViewTreeElement> _children = new List<IViewTreeElement>();
private readonly LifetimeController _viewLifetimeController = new LifetimeController();

private readonly MutableAtom<Vector2Int> _bounds = Atom.Value(Vector2Int.zero, debugName: "View.bounds");
private MutableAtom<Vector2Int> _bounds;

[CanBeNull] private List<Action> _activationCallbacks;
[CanBeNull] private List<Action> _deactivationCallbacks;
Expand All @@ -43,7 +43,18 @@ public abstract class View<TState> : UIBehaviour, IView, IViewTreeElement

bool IView.IsDestroyed => this == null;

protected Vector2Int Bounds => _bounds.Value;
protected Vector2Int Bounds
{
get
{
if (_bounds == null)
{
_bounds = Atom.Value(Vector2Int.zero, debugName: "View.bounds");
}

return _bounds.Value;
}
}

public Lifetime ViewLifetime => _viewLifetimeController.Lifetime;

Expand Down Expand Up @@ -268,6 +279,11 @@ private object DoRender()

private void RefreshBoundsNextFrame()
{
if (_bounds == null)
{
return;
}

if (_refreshBoundsAction == null)
{
_refreshBoundsAction = RefreshBoundsImmediate;
Expand All @@ -278,6 +294,11 @@ private void RefreshBoundsNextFrame()

private void RefreshBoundsImmediate()
{
if (_bounds == null)
{
return;
}

using (Atom.NoWatch)
{
var size = rectTransform.rect.size;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.codewriter.unimob.ui",
"displayName": "UniMob.UI",
"description": "A declarative library for building reactive user interfaces",
"version": "0.4.8",
"version": "0.4.9",
"unity": "2019.3",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit b929ab5

Please sign in to comment.