-
Notifications
You must be signed in to change notification settings - Fork 4
/
ScreenUpdateManager.cs
89 lines (75 loc) · 2.46 KB
/
ScreenUpdateManager.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using WordLight.NativeMethods;
using WordLight.Search;
namespace WordLight
{
public class ScreenUpdateManager
{
private const int MaxScreenWidth = 10000;
private TextView _view;
private object _updateRectSync = new object();
private int _start = int.MaxValue;
private int _end = int.MinValue;
public ScreenUpdateManager(TextView view)
{
if (view == null) throw new ArgumentNullException("view");
_view = view;
}
public void IncludeText(int position, int length)
{
lock (_updateRectSync)
{
if (_view.IsVisibleText(position, length))
{
if (position < _start)
_start = position;
int textEnd = position + length;
if (textEnd > _end)
_end = textEnd;
}
}
}
public void CompleteUpdate()
{
lock (_updateRectSync)
{
if (_start != int.MaxValue)
{
var rect = GetRect(_start, _end);
if (rect != Rectangle.Empty && _view.WindowHandle != IntPtr.Zero)
User32.ValidateRect(_view.WindowHandle, rect);
_start = int.MaxValue;
_end = int.MinValue;
}
}
}
public void RequestUpdate()
{
lock (_updateRectSync)
{
if (_start != int.MaxValue)
{
var rect = GetRect(_start, _end);
if (rect != Rectangle.Empty && _view.WindowHandle != IntPtr.Zero)
User32.InvalidateRect(_view.WindowHandle, rect, false);
}
}
}
private Rectangle GetRect(int textStart, int textEnd)
{
textStart = Math.Max(textStart, _view.VisibleTextStart);
textEnd = Math.Min(textEnd, _view.VisibleTextEnd);
var rect = _view.GetRectangleForMark(textStart, textEnd - textStart);
if (rect != Rectangle.Empty)
{
rect.X = 0;
rect.Width = MaxScreenWidth;
}
return rect;
}
}
}