Skip to content

Commit 4999870

Browse files
authored
v1.11 (#8)
* Small refactoring * Fix WebBrowser plugin configuration window size
1 parent 604cf9f commit 4999870

3 files changed

Lines changed: 58 additions & 57 deletions

File tree

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Drawing;
4+
using System.Linq;
35
using System.Windows.Forms;
46
using ContactPoint.BaseDesign.BaseNotifyControls;
57

@@ -9,8 +11,8 @@ public class NotifyManager
911
{
1012
private static NotifyManager _instance;
1113

12-
List<NotifyForm> _items = new List<NotifyForm>();
13-
Form _mainForm;
14+
private readonly List<NotifyForm> _notifyForms = new List<NotifyForm>();
15+
private readonly Form _mainForm;
1416

1517
private NotifyManager(Form mainForm)
1618
{
@@ -41,64 +43,64 @@ public void Notify(NotifyControl control)
4143
Notify(control, 5000);
4244
}
4345

44-
private delegate void NotifyDelegate(NotifyControl control, int timeout);
4546
public void Notify(NotifyControl control, int timeout)
4647
{
4748
if (_mainForm.InvokeRequired)
4849
{
49-
_mainForm.BeginInvoke(new NotifyDelegate(Notify), control, timeout);
50-
50+
_mainForm.BeginInvoke(new Action<NotifyControl, int>(Notify), control, timeout);
5151
return;
5252
}
5353

54-
var frm = new NotifyForm(control) { NotifyControl = control, Timeout = timeout };
55-
control.NotifyForm = frm;
56-
57-
frm.SetPosition(GetNextPosition(frm.Width, frm.Height));
54+
var notifyForm = new NotifyForm(control)
55+
{
56+
NotifyControl = control,
57+
Timeout = timeout
58+
};
5859

59-
this._items.Add(frm);
60+
notifyForm.SetPosition(GetNextPosition(notifyForm.Width, notifyForm.Height));
61+
lock (_notifyForms)
62+
{
63+
_notifyForms.Add(notifyForm);
64+
}
6065

61-
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
66+
notifyForm.FormClosed += NotifyFormClosed;
6267

68+
control.NotifyForm = notifyForm;
6369
control.NotifyOnCreate();
6470

65-
frm.ShowGracefully();
71+
notifyForm.ShowGracefully();
6672
}
6773

6874
private Point GetNextPosition(int frmWidth, int frmHeight)
6975
{
70-
Rectangle rect = Screen.GetWorkingArea(this._mainForm);
76+
var rect = Screen.GetWorkingArea(_mainForm);
7177

72-
for (int i = 1; i <= this._items.Count; i++)
78+
lock (_notifyForms)
7379
{
74-
Point checkPoint = new Point(
75-
rect.Width - frmWidth - 10,
76-
rect.Height - (frmHeight + 10) * i
77-
);
78-
79-
if (CheckPosition(checkPoint))
80-
return checkPoint;
80+
for (int i = 1; i <= _notifyForms.Count; i++)
81+
{
82+
var position = new Point(rect.Width - frmWidth - 10, rect.Height - (frmHeight + 10) * i);
83+
if (_notifyForms.All(form => form.Location.Y != position.Y))
84+
{
85+
return position;
86+
}
87+
}
8188
}
8289

83-
return new Point(
84-
rect.Width - frmWidth - 10,
85-
rect.Height - (frmHeight + 10) * (this._items.Count + 1)
86-
);
90+
return new Point(rect.Width - frmWidth - 10, rect.Height - (frmHeight + 10) * (_notifyForms.Count + 1));
8791
}
8892

89-
private bool CheckPosition(Point point)
93+
private void NotifyFormClosed(object sender, FormClosedEventArgs e)
9094
{
91-
foreach (NotifyForm form in this._items)
92-
if (form.Location.Y == point.Y)
93-
return false;
94-
95-
return true;
96-
}
95+
if (sender is NotifyForm form)
96+
{
97+
form.FormClosed -= NotifyFormClosed;
9798

98-
void frm_FormClosed(object sender, FormClosedEventArgs e)
99-
{
100-
if (sender != null)
101-
this._items.Remove(sender as NotifyForm);
99+
lock (_notifyForms)
100+
{
101+
_notifyForms.Remove(form);
102+
}
103+
}
102104
}
103105
}
104106
}

ContactPoint.Plugins.CallTools/CallNotifyWindow/CallNotifyWindowService.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@ namespace ContactPoint.Plugins.CallTools.CallNotifyWindow
88
internal class CallNotifyWindowService : IService
99
{
1010
private readonly IPlugin _plugin;
11-
private readonly CallDelegate _callStatChanged;
1211

1312
public event ServiceStartedDelegate Started;
1413
public event ServiceStoppedDelegate Stopped;
1514

16-
public bool NeedShowingNotification => _plugin.PluginManager.Core.SettingsManager.Get<bool>(CallToolsOptions.ShowIncomingCallWindowName);
17-
public bool IsNotificationWindowPersistent => _plugin.PluginManager.Core.SettingsManager.Get<bool>(CallToolsOptions.NotHideCallWindowName);
15+
public bool NeedShowingNotification { get; private set; }
16+
public bool IsWindowPersistent { get; private set; }
17+
public int CloseWindowTimeout { get; private set; }
1818

1919
public bool IsStarted { get; private set; } = false;
2020

2121
public CallNotifyWindowService(IPlugin plugin)
2222
{
2323
_plugin = plugin;
24-
25-
_callStatChanged = CallManager_OnCallStateChanged;
2624
}
2725

2826
public void Start()
2927
{
3028
if (IsStarted) { return; }
3129

32-
_plugin.PluginManager.Core.CallManager.OnCallStateChanged += _callStatChanged;
33-
_plugin.PluginManager.Core.CallManager.OnIncomingCall += CallManager_OnIncomingCall;
30+
NeedShowingNotification = _plugin.PluginManager.Core.SettingsManager.Get<bool>(CallToolsOptions.ShowIncomingCallWindowName);
31+
IsWindowPersistent = _plugin.PluginManager.Core.SettingsManager.Get<bool>(CallToolsOptions.NotHideCallWindowName);
32+
CloseWindowTimeout = IsWindowPersistent ? int.MaxValue : 0;
33+
34+
_plugin.PluginManager.Core.CallManager.OnCallStateChanged += OnCallStateChanged;
35+
_plugin.PluginManager.Core.CallManager.OnIncomingCall += OnIncomingCall;
3436

3537
IsStarted = true;
3638
Started?.Invoke(this);
@@ -40,38 +42,37 @@ public void Stop()
4042
{
4143
if (!IsStarted) { return; }
4244

43-
_plugin.PluginManager.Core.CallManager.OnCallStateChanged -= _callStatChanged;
44-
_plugin.PluginManager.Core.CallManager.OnIncomingCall -= CallManager_OnIncomingCall;
45+
_plugin.PluginManager.Core.CallManager.OnCallStateChanged -= OnCallStateChanged;
46+
_plugin.PluginManager.Core.CallManager.OnIncomingCall -= OnIncomingCall;
4547

4648
IsStarted = false;
4749
Stopped?.Invoke(this, string.Empty);
4850
}
4951

50-
void CallManager_OnIncomingCall(ICall call)
52+
private void OnIncomingCall(ICall call)
5153
{
5254
if (IsStarted && call.Headers.Contains("x-color") && !call.Tags.ContainsKey("color"))
55+
{
5356
call.Tags.Add("color", call.Headers["x-color"].Value);
57+
}
5458
}
5559

56-
void CallManager_OnCallStateChanged(ICall call)
60+
private void OnCallStateChanged(ICall call)
5761
{
5862
if (call == null || call.IsDisposed)
5963
{
6064
return;
6165
}
6266

63-
if (NeedShowingNotification&& call.State == CallState.INCOMING && call.LastState == CallState.NULL && call.IsIncoming)
67+
if (NeedShowingNotification && call.State == CallState.INCOMING && call.LastState == CallState.NULL && call.IsIncoming)
6468
{
65-
bool isWindowPersistent = IsNotificationWindowPersistent;
66-
int closeWindowTimeout = isWindowPersistent ? int.MaxValue : 0;
67-
6869
if (SyncUi.InvokeRequired)
6970
{
70-
SyncUi.Invoke(new Action(() => NotifyManager.NotifyUser(new CallNotifyControl(call, isWindowPersistent), closeWindowTimeout)));
71+
SyncUi.Invoke(new Action(() => NotifyManager.NotifyUser(new CallNotifyControl(call, IsWindowPersistent), CloseWindowTimeout)));
7172
}
7273
else
7374
{
74-
NotifyManager.NotifyUser(new CallNotifyControl(call, isWindowPersistent), closeWindowTimeout);
75+
NotifyManager.NotifyUser(new CallNotifyControl(call, IsWindowPersistent), CloseWindowTimeout);
7576
}
7677
}
7778
}

ContactPoint.Plugins.WebBrowser/WebBrowserSettings.Designer.cs

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)