forked from SamKr/ChatBubbles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
148 lines (120 loc) · 4.2 KB
/
MainWindow.xaml.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ChatBubbles
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MessageCollection messages = new MessageCollection();
private Storyboard scrollViewerStoryboard;
private DoubleAnimation scrollViewerScrollToEndAnim;
private MessageSide curside; // 0: You 1: Me
#region VerticalOffset DP
/// <summary>
/// VerticalOffset, a private DP used to animate the scrollviewer
/// </summary>
private DependencyProperty VerticalOffsetProperty = DependencyProperty.Register("VerticalOffset",
typeof(double), typeof(MainWindow), new PropertyMetadata(0.0, OnVerticalOffsetChanged));
private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MainWindow app = d as MainWindow;
app.OnVerticalOffsetChanged(e);
}
private void OnVerticalOffsetChanged(DependencyPropertyChangedEventArgs e)
{
ConversationScrollViewer.ScrollToVerticalOffset((double)e.NewValue);
}
#endregion
public MainWindow()
{
InitializeComponent();
messages.Add(new Message()
{
Side = MessageSide.You,
Text = "Hello sir. How may I help you?"
});
curside = MessageSide.You;
this.DataContext = messages;
scrollViewerScrollToEndAnim = new DoubleAnimation()
{
Duration = TimeSpan.FromSeconds(1),
EasingFunction = new SineEase()
};
Storyboard.SetTarget(scrollViewerScrollToEndAnim, this);
Storyboard.SetTargetProperty(scrollViewerScrollToEndAnim, new PropertyPath(VerticalOffsetProperty));
scrollViewerStoryboard = new Storyboard();
scrollViewerStoryboard.Children.Add(scrollViewerScrollToEndAnim);
this.Resources.Add("foo", scrollViewerStoryboard);
TextInput.Focus();
}
private void TextInput_TextChanged(object sender, TextChangedEventArgs e)
{
//
}
private void TextInput_GotFocus(object sender, RoutedEventArgs e)
{
ScrollConversationToEnd();
}
private void ScrollConversationToEnd()
{
scrollViewerScrollToEndAnim.From = ConversationScrollViewer.VerticalOffset;
scrollViewerScrollToEndAnim.To = ConversationContentContainer.ActualHeight;
scrollViewerStoryboard.Begin();
}
private void TextInput_LostFocus(object sender, RoutedEventArgs e)
{
ScrollConversationToEnd();
}
private void TextInput_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
addTextMe(TextInput.Text);
e.Handled = true;
}
}
private void addTextMe(string text)
{
messages.Add(new Message()
{
Side = MessageSide.Me,
Text = text,
PrevSide = curside
});
curside = MessageSide.Me;
ScrollConversationToEnd();
TextInput.Text = "";
TextInput.Focus();
}
private void addTextYou(string text)
{
messages.Add(new Message()
{
Side = MessageSide.You,
Text = TextInput.Text,
PrevSide = curside
});
curside = MessageSide.You;
ScrollConversationToEnd();
TextInput.Text = "";
TextInput.Focus();
}
}
}