forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageDisplay.cs
89 lines (83 loc) · 3.22 KB
/
MessageDisplay.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.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using AltitudeAngelWings.Service.Messaging;
using Message = AltitudeAngelWings.Models.Message;
namespace AltitudeAngelWings.Plugin
{
public class MessageDisplay : IMessageDisplay
{
private readonly Label[] _messagesLabels;
private readonly Control _templateControl;
private readonly IUiThreadInvoke _uiThreadInvoke;
private readonly List<Message> _messages = new List<Message>();
public MessageDisplay(Control[] parentControls, Control templateControl, IUiThreadInvoke uiThreadInvoke)
{
_templateControl = templateControl;
_messagesLabels = new Label[parentControls.Length];
for (var pos = 0; pos < _messagesLabels.Length; pos++)
{
_messagesLabels[pos] = new Label
{
Name = "AA_MessageDisplay",
AutoSize = true,
ForeColor = _templateControl.ForeColor,
BackColor = _templateControl.BackColor,
Visible = false,
Dock = DockStyle.Bottom,
Margin = new Padding(_templateControl.Height),
Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold)
};
}
_uiThreadInvoke = uiThreadInvoke;
_uiThreadInvoke.FireAndForget(() =>
{
for (var pos = 0; pos < _messagesLabels.Length; pos++)
{
parentControls[pos].Controls.Add(_messagesLabels[pos]);
_messagesLabels[pos].BringToFront();
}
});
}
public void AddMessage(Message message)
{
_messages.Add(message);
var messages = FormatMessages();
_uiThreadInvoke.FireAndForget(() =>
{
foreach (var messagesLabel in _messagesLabels)
{
if (!messagesLabel.Parent.Visible) continue;
messagesLabel.Text = messages;
messagesLabel.ForeColor = _templateControl.ForeColor;
messagesLabel.Visible = messages.Length > 0;
}
});
}
public void RemoveMessage(Message message)
{
_messages.Remove(message);
var messages = FormatMessages();
_uiThreadInvoke.FireAndForget(() =>
{
foreach (var messagesLabel in _messagesLabels)
{
if (!messagesLabel.Parent.Visible) continue;
messagesLabel.Text = messages;
messagesLabel.Visible = messages.Length > 0;
}
});
}
private string FormatMessages()
{
var builder = new StringBuilder();
foreach (var message in _messages)
{
builder.AppendLine(message.Content);
}
return builder.ToString(0, Math.Max(0, builder.Length - Environment.NewLine.Length));
}
}
}