-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchResult.xaml.cs
266 lines (224 loc) · 6.73 KB
/
SearchResult.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static SylverInk.Common;
namespace SylverInk
{
/// <summary>
/// Interaction logic for SearchResult.xaml
/// </summary>
public partial class SearchResult : Window
{
private readonly BackgroundWorker? AutosaveThread;
private bool Dragging = false;
private Point DragMouseCoords = new(0, 0);
private bool Edited = false;
private string OriginalText = string.Empty;
public string Query = string.Empty;
public int ResultDatabase = 0;
public NoteRecord? ResultRecord;
public string ResultText = string.Empty;
private readonly double SnapTolerance = 20.0;
private DateTime TimeSinceAutosave = DateTime.Now;
public SearchResult()
{
InitializeComponent();
DataContext = Common.Settings;
AutosaveThread = new();
AutosaveThread.DoWork += (_, _) =>
{
SpinWait.SpinUntil(new(() => DateTime.Now.Subtract(TimeSinceAutosave).Seconds >= 5.0));
Concurrent(SaveRecord);
};
}
public void AddTabToRibbon()
{
NoteTab newTab = new(ResultRecord ?? new(), ResultText);
newTab.Construct();
Close();
}
private void CloseClick(object sender, RoutedEventArgs e)
{
if (Edited)
{
switch(MessageBox.Show("You have unsaved changes. Save before closing this note?", "Sylver Ink: Notification", MessageBoxButton.YesNoCancel, MessageBoxImage.Question))
{
case MessageBoxResult.Cancel:
return;
case MessageBoxResult.No:
Edited = false;
ResultText = OriginalText;
SaveRecord();
break;
}
}
Close();
}
private void Drag(object sender, MouseEventArgs e)
{
if (!Dragging)
return;
var mouse = PointToScreen(e.GetPosition(null));
var newCoords = new Point()
{
X = DragMouseCoords.X + mouse.X,
Y = DragMouseCoords.Y + mouse.Y
};
if (Common.Settings.SnapSearchResults)
Snap(ref newCoords);
Left = newCoords.X;
Top = newCoords.Y;
}
private void Result_Closed(object sender, EventArgs e)
{
if (Edited)
SaveRecord();
CurrentDatabase.Transmit(Network.MessageType.RecordUnlock, IntToBytes(ResultRecord?.Index ?? 0));
foreach (SearchResult result in OpenQueries)
{
if (result.ResultRecord != ResultRecord)
continue;
OpenQueries.Remove(result);
return;
}
}
private void Result_Loaded(object sender, RoutedEventArgs e)
{
if (ResultRecord?.Locked is true)
{
LastChangedLabel.Content = "Note locked by another user";
ResultBlock.IsEnabled = false;
}
else
{
LastChangedLabel.Content = "Last modified: " + ResultRecord?.GetLastChange();
CurrentDatabase.Transmit(Network.MessageType.RecordUnlock, IntToBytes(ResultRecord?.Index ?? 0));
}
if (ResultText.Equals(string.Empty))
ResultText = ResultRecord?.ToString() ?? string.Empty;
Edited = false;
ResultBlock.Text = ResultText;
OriginalText = ResultText;
var tabPanel = GetChildPanel("DatabasesPanel");
for (int i = tabPanel.Items.Count - 1; i > 0; i--)
{
var item = (TabItem)tabPanel.Items[i];
if (((NoteRecord?)item.Tag)?.Equals(ResultRecord) is true)
tabPanel.Items.RemoveAt(i);
}
}
private void ResultBlock_TextChanged(object sender, TextChangedEventArgs e)
{
var senderObject = sender as TextBox;
ResultText = senderObject?.Text ?? string.Empty;
Edited = !ResultText.Equals(ResultRecord?.ToString());
TimeSinceAutosave = DateTime.Now;
if (!AutosaveThread?.IsBusy is true)
AutosaveThread?.RunWorkerAsync();
}
private void SaveRecord()
{
if (ResultRecord is null)
return;
CurrentDatabase.CreateRevision(ResultRecord, ResultText);
LastChangedLabel.Content = "Last modified: " + ResultRecord?.GetLastChange();
DeferUpdateRecentNotes(true);
}
private Point Snap(ref Point Coords)
{
var Snapped = (false, false);
foreach (SearchResult other in OpenQueries)
{
if (Snapped.Item1 && Snapped.Item2)
return Coords;
if (other.ResultRecord == ResultRecord)
continue;
Point LT1 = new(Coords.X, Coords.Y);
Point RB1 = new(Coords.X + Width, Coords.Y + Height);
Point LT2 = new(other.Left, other.Top);
Point RB2 = new(other.Left + other.Width, other.Top + other.Height);
Point LT2A = new(other.Left + 16.0, other.Top + 9.0);
Point RB2A = new(other.Left + other.Width - 16.0, other.Top + other.Height - 9.0);
var dLR = Math.Abs(LT1.X - RB2A.X);
var dRL = Math.Abs(RB1.X - LT2A.X);
var dTB = Math.Abs(LT1.Y - RB2A.Y);
var dBT = Math.Abs(RB1.Y - LT2A.Y);
var dLL = Math.Abs(LT1.X - LT2.X);
var dRR = Math.Abs(RB1.X - RB2.X);
var dTT = Math.Abs(LT1.Y - LT2.Y);
var dBB = Math.Abs(RB1.Y - RB2.Y);
var XTolerance = (LT1.X >= LT2A.X && LT1.X <= RB2A.X)
|| (RB1.X >= LT2A.X && RB1.X <= RB2A.X)
|| (LT2A.X >= LT1.X && LT2A.X <= RB1.X)
|| (RB2A.X >= LT1.X && RB2A.X <= RB1.X);
var YTolerance = (LT1.Y >= LT2A.Y && LT1.Y <= RB2A.Y)
|| (RB1.Y >= LT2A.Y && RB1.Y <= RB2A.Y)
|| (LT2A.Y >= LT1.Y && LT2A.Y <= RB1.Y)
|| (RB2A.Y >= LT1.Y && RB2A.Y <= RB1.Y);
if (dLR < SnapTolerance && YTolerance && !Snapped.Item1)
{
Coords.X = RB2A.X;
Snapped = (true, Snapped.Item2);
}
if (dRL < SnapTolerance && YTolerance && !Snapped.Item1)
{
Coords.X = LT2A.X - Width;
Snapped = (true, Snapped.Item2);
}
if (dTB < SnapTolerance && XTolerance && !Snapped.Item2)
{
Coords.Y = RB2A.Y;
Snapped = (Snapped.Item1, true);
}
if (dBT < SnapTolerance && XTolerance && !Snapped.Item2)
{
Coords.Y = LT2A.Y - Height;
Snapped = (Snapped.Item1, true);
}
if (dLL < SnapTolerance && !Snapped.Item1 && Snapped.Item2)
{
Coords.X = LT2.X;
Snapped = (true, true);
}
if (dRR < SnapTolerance && !Snapped.Item1 && Snapped.Item2)
{
Coords.X = RB2.X - Width;
Snapped = (true, true);
}
if (dTT < SnapTolerance && Snapped.Item1 && !Snapped.Item2)
{
Coords.Y = LT2.Y;
Snapped = (true, true);
}
if (dBB < SnapTolerance && Snapped.Item1 && !Snapped.Item2)
{
Coords.Y = RB2.Y - Height;
Snapped = (true, true);
}
}
return Coords;
}
private void ViewClick(object sender, RoutedEventArgs e)
{
SearchWindow?.Close();
AddTabToRibbon();
}
private void WindowMove(object sender, MouseEventArgs e) => Drag(sender, e);
private void WindowMouseDown(object sender, MouseButtonEventArgs e)
{
var n = PointToScreen(e.GetPosition(null));
CaptureMouse();
DragMouseCoords = new(Left - n.X, Top - n.Y);
Dragging = true;
}
private void WindowMouseUp(object sender, MouseButtonEventArgs e)
{
ReleaseMouseCapture();
DragMouseCoords = new(0, 0);
Dragging = false;
}
}
}