Skip to content

Commit a0635cc

Browse files
authored
Feature/support mobile touch for popup in sample project (#113)
* Fix input height * Add touch support for showing context menu popup - show on touch and hide when touching outside)
1 parent 39ee1ae commit a0635cc

5 files changed

Lines changed: 118 additions & 39 deletions

File tree

Assets/Plugins/StreamChat/SampleProject/Prefabs/UI/RightColumn.prefab

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ MonoBehaviour:
296296
m_MinWidth: -1
297297
m_MinHeight: -1
298298
m_PreferredWidth: -1
299-
m_PreferredHeight: 120
299+
m_PreferredHeight: 150
300300
m_FlexibleWidth: -1
301301
m_FlexibleHeight: -1
302302
m_LayoutPriority: 1
@@ -443,6 +443,10 @@ PrefabInstance:
443443
propertyPath: m_Value
444444
value: 0
445445
objectReference: {fileID: 0}
446+
- target: {fileID: 8090220119038640879, guid: 0491f73f9656de1479b08d87bf70ec74, type: 3}
447+
propertyPath: m_Size
448+
value: 0.9999999
449+
objectReference: {fileID: 0}
446450
- target: {fileID: 8726209695421210470, guid: 0491f73f9656de1479b08d87bf70ec74, type: 3}
447451
propertyPath: m_AnchorMax.x
448452
value: 0
@@ -487,7 +491,7 @@ PrefabInstance:
487491
objectReference: {fileID: 0}
488492
- target: {fileID: 1972999991939388118, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
489493
propertyPath: m_Size
490-
value: 0.99514675
494+
value: 1
491495
objectReference: {fileID: 0}
492496
- target: {fileID: 1972999991939388118, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
493497
propertyPath: m_Value
@@ -511,7 +515,7 @@ PrefabInstance:
511515
objectReference: {fileID: 0}
512516
- target: {fileID: 3010964881072045271, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
513517
propertyPath: m_AnchoredPosition.y
514-
value: 0.00030779475
518+
value: 0.000055454144
515519
objectReference: {fileID: 0}
516520
- target: {fileID: 3773565267568751462, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
517521
propertyPath: m_AnchorMax.x
@@ -521,6 +525,10 @@ PrefabInstance:
521525
propertyPath: m_AnchorMax.y
522526
value: 0
523527
objectReference: {fileID: 0}
528+
- target: {fileID: 3773565267568751462, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
529+
propertyPath: m_AnchorMin.x
530+
value: 0
531+
objectReference: {fileID: 0}
524532
- target: {fileID: 6818924929287490322, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
525533
propertyPath: m_Pivot.x
526534
value: 0.5
@@ -606,9 +614,13 @@ PrefabInstance:
606614
value: 0
607615
objectReference: {fileID: 0}
608616
- target: {fileID: 7167094196472387625, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
609-
propertyPath: m_Value
617+
propertyPath: m_Size
610618
value: 1
611619
objectReference: {fileID: 0}
620+
- target: {fileID: 7167094196472387625, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
621+
propertyPath: m_Value
622+
value: 0
623+
objectReference: {fileID: 0}
612624
- target: {fileID: 7702233621548626119, guid: 4bdb886d8d2854741be2bcc591e470b0, type: 3}
613625
propertyPath: m_AnchorMax.x
614626
value: 0

Assets/Plugins/StreamChat/SampleProject/Scripts/Popups/MessageOptionsPopup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public Args(bool hideOnPointerExit, bool hideOnButtonClicked, IEnumerable<MenuOp
3131
}
3232

3333
public bool IsPointerOver { get; private set; }
34+
public RectTransform RectTransform { get; private set; }
35+
36+
protected override void OnStart()
37+
{
38+
base.OnStart();
39+
RectTransform = GetComponent<RectTransform>();
40+
}
3441

3542
protected override void OnShow(Args args)
3643
{

Assets/Plugins/StreamChat/SampleProject/Scripts/Views/BaseView.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ protected void OnDestroy()
3838
_destroyed = true;
3939
}
4040

41+
protected void Start() => OnStart();
42+
4143
protected void Update()
4244
{
4345
if (!_isInited || _destroyed)
@@ -47,6 +49,10 @@ protected void Update()
4749

4850
OnUpdate();
4951
}
52+
53+
protected virtual void OnStart()
54+
{
55+
}
5056

5157
protected virtual void OnInited()
5258
{

Assets/Plugins/StreamChat/SampleProject/Scripts/Views/MessageListView.cs

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using StreamChat.Core.Models;
66
using StreamChat.Core.StatefulModels;
77
using StreamChat.Libs.Utils;
8+
using StreamChat.SampleProject.Popups;
89
using StreamChat.SampleProject.Utils;
910
using UnityEngine;
11+
using UnityEngine.EventSystems;
1012
using UnityEngine.UI;
1113

1214
namespace StreamChat.SampleProject.Views
@@ -29,6 +31,11 @@ protected override void OnUpdate()
2931
{
3032
base.OnUpdate();
3133

34+
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
35+
HideContextMenuIfTouchedOutside();
36+
37+
#endif
38+
3239
if (_scrollRect.content.rect.height < _scrollRect.viewport.rect.height)
3340
{
3441
// if scroll view doesn't fill whole screen the verticalNormalizedPosition is 1 so it calls for previous messages on empty message list
@@ -68,6 +75,9 @@ protected override void OnDisposing()
6875
private Task _loadPreviousMessagesTask;
6976
private IStreamChannel _activeChannel;
7077

78+
private MessageOptionsPopup _activePopup;
79+
private int _frameShownPopup;
80+
7181
//we wait 2 frames before depending on scroll list position in order for the list to render and update its internal state
7282
private bool IsScrollListRebuilding => _scrollListLastUpdateFrame + 2 > Time.frameCount;
7383

@@ -76,27 +86,27 @@ private void OnActiveChannelChanged(IStreamChannel channel)
7686
if (_activeChannel != null)
7787
{
7888
_activeChannel.MessageReceived -= OnMessageReceived;
79-
_activeChannel.MessageDeleted -= OnMessageDeleted;
89+
_activeChannel.MessageDeleted -= OnMessageDeleted;
8090
_activeChannel.MessageUpdated -= OnMessageUpdated;
8191
_activeChannel.ReactionAdded -= OnReactionAdded;
8292
_activeChannel.ReactionUpdated -= OnReactionUpdated;
8393
_activeChannel.ReactionRemoved -= OnReactionRemoved;
8494
}
85-
95+
8696
if (channel == null)
8797
{
8898
ClearAll();
8999
return;
90100
}
91-
101+
92102
_activeChannel = channel;
93103
_activeChannel.MessageReceived += OnMessageReceived;
94104
_activeChannel.MessageDeleted += OnMessageDeleted;
95105
_activeChannel.MessageUpdated += OnMessageUpdated;
96106
_activeChannel.ReactionAdded += OnReactionAdded;
97107
_activeChannel.ReactionUpdated += OnReactionUpdated;
98108
_activeChannel.ReactionRemoved += OnReactionRemoved;
99-
109+
100110
RebuildMessages(channel, scrollToBottom: true);
101111
}
102112

@@ -122,6 +132,7 @@ private void ClearAll()
122132
{
123133
foreach (var m in _messages)
124134
{
135+
m.PointedDown -= OnMessagePointedDown;
125136
Destroy(m.gameObject);
126137
}
127138

@@ -214,6 +225,7 @@ private MessageView CreateMessageView(IStreamMessage message)
214225
var prefab = isLocal ? _localUserMessageViewPrefab : _messageViewPrefab;
215226
var view = Instantiate(prefab, _messagesContainer);
216227
view.Init(ViewContext);
228+
view.PointedDown += OnMessagePointedDown;
217229
return view;
218230
}
219231

@@ -224,5 +236,75 @@ private IEnumerator ScrollToBottomAfterResized()
224236
yield return new WaitForEndOfFrame();
225237
GetComponent<ScrollRect>().verticalNormalizedPosition = 0;
226238
}
239+
240+
private void OnMessagePointedDown(MessageView messageView, PointerEventData pointerEventData)
241+
{
242+
#if UNITY_STANDALONE
243+
if (!InputSystem.GetMouseButton(1))
244+
{
245+
return;
246+
}
247+
#endif
248+
249+
ShowContextMenu(messageView, pointerEventData);
250+
}
251+
252+
private void ShowContextMenu(MessageView parent, PointerEventData pointerEventData)
253+
{
254+
HideContextMenu();
255+
256+
Debug.Log("ShowContextMenu on fame " + Time.frameCount);
257+
258+
var pointerPosition = pointerEventData.position;
259+
260+
_activePopup = Factory.CreateMessageOptionsPopup(parent, State);
261+
262+
var rectTransform = ((RectTransform)_activePopup.transform);
263+
264+
rectTransform.position = pointerPosition + new Vector2(-10, 10);
265+
266+
_frameShownPopup = Time.frameCount;
267+
}
268+
269+
private void HideContextMenu()
270+
{
271+
if (_activePopup != null)
272+
{
273+
Debug.Log("HideContextMenu on fame " + Time.frameCount);
274+
Destroy(_activePopup.gameObject);
275+
_activePopup = null;
276+
}
277+
}
278+
279+
private void HideContextMenuIfTouchedOutside()
280+
{
281+
if (_frameShownPopup == Time.frameCount)
282+
{
283+
return;
284+
}
285+
286+
if (Input.touchCount == 0)
287+
{
288+
289+
return;
290+
}
291+
292+
var anyTouchOnPopup = false;
293+
for (int i = 0; i < Input.touchCount; i++)
294+
{
295+
var touch = Input.GetTouch(i);
296+
297+
if (RectTransformUtility.RectangleContainsScreenPoint(_activePopup.RectTransform,
298+
touch.position))
299+
{
300+
anyTouchOnPopup = true;
301+
}
302+
}
303+
304+
if (!anyTouchOnPopup)
305+
{
306+
HideContextMenu();
307+
}
308+
}
227309
}
228310
}

Assets/Plugins/StreamChat/SampleProject/Scripts/Views/MessageView.cs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using StreamChat.Core.Models;
55
using StreamChat.Core.StatefulModels;
66
using StreamChat.Libs.Utils;
7-
using StreamChat.SampleProject.Popups;
87
using StreamChat.SampleProject.Utils;
98
using TMPro;
109
using UnityEngine;
@@ -19,6 +18,8 @@ namespace StreamChat.SampleProject.Views
1918
/// </summary>
2019
public class MessageView : BaseView, IPointerDownHandler
2120
{
21+
public event Action<MessageView, PointerEventData> PointedDown;
22+
2223
public IStreamMessage Message { get; private set; }
2324

2425
public void UpdateData(IStreamMessage message, IImageLoader imageLoader)
@@ -65,15 +66,7 @@ public void TryPlay()
6566
}
6667
}
6768

68-
public void OnPointerDown(PointerEventData eventData)
69-
{
70-
if (!InputSystem.GetMouseButton(1))
71-
{
72-
return;
73-
}
74-
75-
SetOptionsMenuActive(true);
76-
}
69+
public void OnPointerDown(PointerEventData eventData) => PointedDown?.Invoke(this, eventData);
7770

7871
protected void Awake()
7972
{
@@ -94,7 +87,6 @@ protected override void OnDisposing()
9487
}
9588

9689
private bool _isDestroyed;
97-
private MessageOptionsPopup _activePopup;
9890
private RenderTexture _renderTexture;
9991

10092
[SerializeField]
@@ -152,26 +144,6 @@ private void ShowReactions(IStreamMessage message)
152144
_emojisContainer.gameObject.SetActive(anyShown);
153145
}
154146

155-
private void SetOptionsMenuActive(bool active)
156-
{
157-
if (_activePopup != null)
158-
{
159-
Destroy(_activePopup.gameObject);
160-
_activePopup = null;
161-
}
162-
163-
if (active)
164-
{
165-
var mousePosition = InputSystem.MousePosition;
166-
167-
_activePopup = Factory.CreateMessageOptionsPopup(this, State);
168-
169-
var rectTransform = ((RectTransform)_activePopup.transform);
170-
171-
rectTransform.position = mousePosition + new Vector2(-10, 10);
172-
}
173-
}
174-
175147
private static string GetMessageText(IStreamMessage message)
176148
=> message.IsDeleted ? ChatState.MessageDeletedInfo : message.Text;
177149

0 commit comments

Comments
 (0)