-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDismissibleDialog.cs
120 lines (100 loc) · 3.37 KB
/
DismissibleDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using UnityEngine;
namespace UniMob.UI.Widgets
{
public class DismissibleDialog : SingleChildLayoutWidget
{
public override State CreateState() => new DismissibleDialogState();
public float DismissTreshold { get; set; } = 0.1f;
public float? CollapsedHeight { get; set; }
public Action OnExpand { get; set; }
public Action OnCollapse { get; set; }
public Action OnDismiss { get; set; }
}
internal class DismissibleDialogState : SingleChildLayoutState<DismissibleDialog>, IDismissibleDialogState
{
public override WidgetViewReference View { get; }
= WidgetViewReference.Resource("UniMob.DismissibleDialog");
[Atom] private bool ExpandedByUser { get; set; }
[Atom] private float? CollapsedHeight => Widget.CollapsedHeight;
[Atom] private float? ExpandedHeight { get; set; }
[Atom] public float Offset { get; private set; }
[Atom] public bool Expanded => ExpandedByUser || Widget.CollapsedHeight == null;
[Atom] public bool Dismissed { get; private set; }
[Atom] public float DismissThreshold => Widget.DismissTreshold;
[Atom] public WidgetSize ChildSize
{
get
{
if (Expanded || CollapsedHeight == null)
{
return ExpandedHeight.HasValue
? WidgetSize.FixedHeight(ExpandedHeight.Value - Offset)
: WidgetSize.Stretched;
}
return WidgetSize.FixedHeight(CollapsedHeight.Value - Offset);
}
}
public void SetOffset(float offset)
{
if (Dismissed)
{
return;
}
Offset = offset;
if (ExpandedHeight.HasValue && CollapsedHeight.HasValue &&
!Expanded && -offset >= (ExpandedHeight - CollapsedHeight) * 0.99f)
{
OnExpand();
}
else if (ExpandedHeight.HasValue && CollapsedHeight.HasValue &&
Expanded && offset >= (ExpandedHeight - CollapsedHeight) * 0.99f)
{
OnCollapse();
}
else if (Expanded)
{
Offset = Mathf.Max(0, Offset);
}
}
public void SetExpandedHeight(float height)
{
ExpandedHeight = height;
}
public void OnExpand()
{
if (CollapsedHeight == null || ExpandedHeight == null)
{
return;
}
Offset += ExpandedHeight.Value - CollapsedHeight.Value;
ExpandedByUser = true;
using (Atom.NoWatch)
{
Widget.OnExpand?.Invoke();
}
}
public void OnCollapse()
{
if (CollapsedHeight == null || ExpandedHeight == null)
{
OnDismiss();
return;
}
Offset -= ExpandedHeight.Value - CollapsedHeight.Value;
ExpandedByUser = false;
using (Atom.NoWatch)
{
Widget.OnCollapse?.Invoke();
}
}
public void OnDismiss()
{
Dismissed = true;
using (Atom.NoWatch)
{
Widget.OnDismiss?.Invoke();
}
}
}
}