Skip to content

Commit

Permalink
v1.11 (#8)
Browse files Browse the repository at this point in the history
* Small refactoring

* Fix WebBrowser plugin configuration window size
  • Loading branch information
alexrster authored Jan 14, 2019
1 parent 604cf9f commit 4999870
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 57 deletions.
76 changes: 39 additions & 37 deletions ContactPoint.BaseDesign/NotifyManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ContactPoint.BaseDesign.BaseNotifyControls;

Expand All @@ -9,8 +11,8 @@ public class NotifyManager
{
private static NotifyManager _instance;

List<NotifyForm> _items = new List<NotifyForm>();
Form _mainForm;
private readonly List<NotifyForm> _notifyForms = new List<NotifyForm>();
private readonly Form _mainForm;

private NotifyManager(Form mainForm)
{
Expand Down Expand Up @@ -41,64 +43,64 @@ public void Notify(NotifyControl control)
Notify(control, 5000);
}

private delegate void NotifyDelegate(NotifyControl control, int timeout);
public void Notify(NotifyControl control, int timeout)
{
if (_mainForm.InvokeRequired)
{
_mainForm.BeginInvoke(new NotifyDelegate(Notify), control, timeout);

_mainForm.BeginInvoke(new Action<NotifyControl, int>(Notify), control, timeout);
return;
}

var frm = new NotifyForm(control) { NotifyControl = control, Timeout = timeout };
control.NotifyForm = frm;

frm.SetPosition(GetNextPosition(frm.Width, frm.Height));
var notifyForm = new NotifyForm(control)
{
NotifyControl = control,
Timeout = timeout
};

this._items.Add(frm);
notifyForm.SetPosition(GetNextPosition(notifyForm.Width, notifyForm.Height));
lock (_notifyForms)
{
_notifyForms.Add(notifyForm);
}

frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
notifyForm.FormClosed += NotifyFormClosed;

control.NotifyForm = notifyForm;
control.NotifyOnCreate();

frm.ShowGracefully();
notifyForm.ShowGracefully();
}

private Point GetNextPosition(int frmWidth, int frmHeight)
{
Rectangle rect = Screen.GetWorkingArea(this._mainForm);
var rect = Screen.GetWorkingArea(_mainForm);

for (int i = 1; i <= this._items.Count; i++)
lock (_notifyForms)
{
Point checkPoint = new Point(
rect.Width - frmWidth - 10,
rect.Height - (frmHeight + 10) * i
);

if (CheckPosition(checkPoint))
return checkPoint;
for (int i = 1; i <= _notifyForms.Count; i++)
{
var position = new Point(rect.Width - frmWidth - 10, rect.Height - (frmHeight + 10) * i);
if (_notifyForms.All(form => form.Location.Y != position.Y))
{
return position;
}
}
}

return new Point(
rect.Width - frmWidth - 10,
rect.Height - (frmHeight + 10) * (this._items.Count + 1)
);
return new Point(rect.Width - frmWidth - 10, rect.Height - (frmHeight + 10) * (_notifyForms.Count + 1));
}

private bool CheckPosition(Point point)
private void NotifyFormClosed(object sender, FormClosedEventArgs e)
{
foreach (NotifyForm form in this._items)
if (form.Location.Y == point.Y)
return false;

return true;
}
if (sender is NotifyForm form)
{
form.FormClosed -= NotifyFormClosed;

void frm_FormClosed(object sender, FormClosedEventArgs e)
{
if (sender != null)
this._items.Remove(sender as NotifyForm);
lock (_notifyForms)
{
_notifyForms.Remove(form);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ namespace ContactPoint.Plugins.CallTools.CallNotifyWindow
internal class CallNotifyWindowService : IService
{
private readonly IPlugin _plugin;
private readonly CallDelegate _callStatChanged;

public event ServiceStartedDelegate Started;
public event ServiceStoppedDelegate Stopped;

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

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

public CallNotifyWindowService(IPlugin plugin)
{
_plugin = plugin;

_callStatChanged = CallManager_OnCallStateChanged;
}

public void Start()
{
if (IsStarted) { return; }

_plugin.PluginManager.Core.CallManager.OnCallStateChanged += _callStatChanged;
_plugin.PluginManager.Core.CallManager.OnIncomingCall += CallManager_OnIncomingCall;
NeedShowingNotification = _plugin.PluginManager.Core.SettingsManager.Get<bool>(CallToolsOptions.ShowIncomingCallWindowName);
IsWindowPersistent = _plugin.PluginManager.Core.SettingsManager.Get<bool>(CallToolsOptions.NotHideCallWindowName);
CloseWindowTimeout = IsWindowPersistent ? int.MaxValue : 0;

_plugin.PluginManager.Core.CallManager.OnCallStateChanged += OnCallStateChanged;
_plugin.PluginManager.Core.CallManager.OnIncomingCall += OnIncomingCall;

IsStarted = true;
Started?.Invoke(this);
Expand All @@ -40,38 +42,37 @@ public void Stop()
{
if (!IsStarted) { return; }

_plugin.PluginManager.Core.CallManager.OnCallStateChanged -= _callStatChanged;
_plugin.PluginManager.Core.CallManager.OnIncomingCall -= CallManager_OnIncomingCall;
_plugin.PluginManager.Core.CallManager.OnCallStateChanged -= OnCallStateChanged;
_plugin.PluginManager.Core.CallManager.OnIncomingCall -= OnIncomingCall;

IsStarted = false;
Stopped?.Invoke(this, string.Empty);
}

void CallManager_OnIncomingCall(ICall call)
private void OnIncomingCall(ICall call)
{
if (IsStarted && call.Headers.Contains("x-color") && !call.Tags.ContainsKey("color"))
{
call.Tags.Add("color", call.Headers["x-color"].Value);
}
}

void CallManager_OnCallStateChanged(ICall call)
private void OnCallStateChanged(ICall call)
{
if (call == null || call.IsDisposed)
{
return;
}

if (NeedShowingNotification&& call.State == CallState.INCOMING && call.LastState == CallState.NULL && call.IsIncoming)
if (NeedShowingNotification && call.State == CallState.INCOMING && call.LastState == CallState.NULL && call.IsIncoming)
{
bool isWindowPersistent = IsNotificationWindowPersistent;
int closeWindowTimeout = isWindowPersistent ? int.MaxValue : 0;

if (SyncUi.InvokeRequired)
{
SyncUi.Invoke(new Action(() => NotifyManager.NotifyUser(new CallNotifyControl(call, isWindowPersistent), closeWindowTimeout)));
SyncUi.Invoke(new Action(() => NotifyManager.NotifyUser(new CallNotifyControl(call, IsWindowPersistent), CloseWindowTimeout)));
}
else
{
NotifyManager.NotifyUser(new CallNotifyControl(call, isWindowPersistent), closeWindowTimeout);
NotifyManager.NotifyUser(new CallNotifyControl(call, IsWindowPersistent), CloseWindowTimeout);
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4999870

Please sign in to comment.