Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue#12, toast window stays at app switcher #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Notifications.Wpf/Controls/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void SetCloseOnClick(DependencyObject obj, bool value)
}

public static readonly DependencyProperty CloseOnClickProperty =
DependencyProperty.RegisterAttached("CloseOnClick", typeof(bool), typeof(Notification), new FrameworkPropertyMetadata(false,CloseOnClickChanged));
DependencyProperty.RegisterAttached("CloseOnClick", typeof(bool), typeof(Notification), new FrameworkPropertyMetadata(false, CloseOnClickChanged));

private static void CloseOnClickChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
Expand All @@ -72,25 +72,25 @@ private static void CloseOnClickChanged(DependencyObject dependencyObject, Depen
notification?.Close();
};
}
}
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var closeButton = GetTemplateChild("PART_CloseButton") as Button;
if (closeButton != null)
closeButton.Click += OnCloseButtonOnClick;

var storyboards = Template.Triggers.OfType<EventTrigger>().FirstOrDefault(t => t.RoutedEvent == NotificationCloseInvokedEvent)?.Actions.OfType<BeginStoryboard>().Select(a => a.Storyboard);
_closingAnimationTime = new TimeSpan(storyboards?.Max(s => Math.Min((s.Duration.HasTimeSpan ? s.Duration.TimeSpan + (s.BeginTime ?? TimeSpan.Zero) : TimeSpan.MaxValue).Ticks, s.Children.Select(ch => ch.Duration.TimeSpan + (s.BeginTime ?? TimeSpan.Zero)).Max().Ticks)) ?? 0);

}

private void OnCloseButtonOnClick(object sender, RoutedEventArgs args)
{
var button = sender as Button;
if (button == null) return;

button.Click -= OnCloseButtonOnClick;
Close();
}
Expand All @@ -102,12 +102,22 @@ public async void Close()
{
return;
}

IsClosing = true;

RaiseEvent(new RoutedEventArgs(NotificationCloseInvokedEvent));
await Task.Delay(_closingAnimationTime);
RaiseEvent(new RoutedEventArgs(NotificationClosedEvent));
}
}

var currentWindow = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.Title.Equals("ToastWindow"));

var notificationCount = VisualTreeHelperExtensions.GetActiveNotificationCount(currentWindow);

if (notificationCount == 0)
currentWindow?.Hide();

}


}
}
7 changes: 6 additions & 1 deletion Notifications.Wpf/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ public void Show(object content, string areaName = "", TimeSpan? expirationTime
_window.Show();
}

if (Areas != null && !_window.IsVisible)
_window.Show();

foreach (var area in Areas.Where(a => a.Name == areaName))
{
area.Show(content, (TimeSpan) expirationTime, onClick, onClose);
area.Show(content, (TimeSpan)expirationTime, onClick, onClose);
}


}

internal static void AddArea(NotificationArea area)
Expand Down
42 changes: 40 additions & 2 deletions Notifications.Wpf/Utils/VisualTreeHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,57 @@ namespace Notifications.Wpf.Utils
{
internal class VisualTreeHelperExtensions
{

private static List<Visual> _activeControls = new List<Visual>();

public static T GetParent<T>(DependencyObject child) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(child);

if (parent == null) return null;

var tParent = parent as T;
var tParent = parent as T;
if (tParent != null)
{
return tParent;
}

return GetParent<T>(parent);
}

public static int GetActiveNotificationCount(Visual element)
{
if (element == null)
{
throw new ArgumentNullException(String.Format("Element {0} is null !", element.ToString()));
}

_activeControls.Clear();

GetControlsList(element, 0);

var count = _activeControls.Count(x => x.GetType().Name.Equals("Notification"));

return count;
}

private static void GetControlsList(Visual control, int level)
{
const int indent = 4;
int ChildNumber = VisualTreeHelper.GetChildrenCount(control);

for (int i = 0; i <= ChildNumber - 1; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(control, i);

_activeControls.Add(v);

if (VisualTreeHelper.GetChildrenCount(v) > 0)
{
GetControlsList(v, level + 1);
}
}
}

}
}