Add project files.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public class EventInvokedArgs : EventArgs
|
||||
{
|
||||
public EventInvokedArgs(object args)
|
||||
{
|
||||
Invoked = args;
|
||||
}
|
||||
|
||||
public object Invoked { get; }
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using TheXamlGuy.Framework.Core;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public class EventSubscriber : DependencyObject, IEventSubscriber
|
||||
{
|
||||
public static DependencyProperty TypeProperty =
|
||||
DependencyProperty.Register(nameof(Type),
|
||||
typeof(Type), typeof(EventSubscriber));
|
||||
|
||||
public event TypedEventHandler<EventSubscriber, EventInvokedArgs>? Invoked;
|
||||
|
||||
public Type Type
|
||||
{
|
||||
get => (Type)GetValue(TypeProperty);
|
||||
set => SetValue(TypeProperty, value);
|
||||
}
|
||||
|
||||
public void Subscribe(IEventAggregator eventAggregator)
|
||||
{
|
||||
if (eventAggregator is not null)
|
||||
{
|
||||
MethodInfo? methodInfo = typeof(EventSubscriber).GetMethod(nameof(EventSubscriber.SubscribeWithType), BindingFlags.NonPublic | BindingFlags.Instance)?.MakeGenericMethod(Type);
|
||||
methodInfo?.Invoke(this, new object[] { eventAggregator });
|
||||
}
|
||||
}
|
||||
|
||||
private void SubscribeWithType<TEvent>(IEventAggregator eventAggregator)
|
||||
{
|
||||
if (eventAggregator is not null)
|
||||
{
|
||||
eventAggregator.SubscribeUI<TEvent>(args =>
|
||||
{
|
||||
if (CanInvoke(args))
|
||||
{
|
||||
Invoked?.Invoke(this, new EventInvokedArgs(args!));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual bool CanInvoke(object? args)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public class EventSubscriberCollection : ObservableCollection<IEventSubscriber>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using TheXamlGuy.Framework.Core;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public interface IEventSubscriber
|
||||
{
|
||||
void Subscribe(IEventAggregator eventAggregator);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public class Interaction
|
||||
{
|
||||
public static readonly DependencyProperty XamlEventAggregatorProperty =
|
||||
DependencyProperty.RegisterAttached("XamlEventAggregator",
|
||||
typeof(XamlEventAggregator), typeof(Interaction));
|
||||
|
||||
public static readonly DependencyProperty InteractiveFrameProperty =
|
||||
DependencyProperty.RegisterAttached("TouchFrame",
|
||||
typeof(InteractiveFrame), typeof(Interaction));
|
||||
|
||||
public static XamlEventAggregator GetXamlEventAggregator(DependencyObject dependencyObject)
|
||||
{
|
||||
return (XamlEventAggregator)dependencyObject.GetValue(XamlEventAggregatorProperty);
|
||||
}
|
||||
|
||||
public static XamlEventAggregator GetInteractiveFrame(DependencyObject dependencyObject)
|
||||
{
|
||||
return (XamlEventAggregator)dependencyObject.GetValue(InteractiveFrameProperty);
|
||||
}
|
||||
|
||||
public static void SetXamlEventAggregator(DependencyObject dependencyObject, XamlEventAggregator value)
|
||||
{
|
||||
dependencyObject.SetValue(XamlEventAggregatorProperty, value);
|
||||
}
|
||||
|
||||
public static void SetInteractiveFrame(DependencyObject dependencyObject, InteractiveFrame value)
|
||||
{
|
||||
dependencyObject.SetValue(InteractiveFrameProperty, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
using TheXamlGuy.Framework.Core;
|
||||
using TheXamlGuy.Framework.WPF.Interactions;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
[ContentProperty("Buttons")]
|
||||
public class InteractiveFrame : DependencyObject
|
||||
{
|
||||
public static DependencyProperty ButtonsProperty =
|
||||
DependencyProperty.Register(nameof(Buttons),
|
||||
typeof(InteractiveFrameButtonCollection), typeof(InteractiveFrame));
|
||||
|
||||
public static DependencyProperty EventAggregatorProperty =
|
||||
DependencyProperty.Register(nameof(EventAggregator),
|
||||
typeof(IEventAggregator), typeof(InteractiveFrame), new PropertyMetadata(null, OnEventAggregatorPropertyChanged));
|
||||
|
||||
public InteractiveFrame()
|
||||
{
|
||||
InteractiveFrameButtonCollection? collection = new();
|
||||
collection.CollectionChanged += OnCollectionChanged;
|
||||
|
||||
SetValue(ButtonsProperty, collection);
|
||||
}
|
||||
|
||||
public InteractiveFrameButtonCollection Buttons
|
||||
{
|
||||
get => (InteractiveFrameButtonCollection)GetValue(ButtonsProperty);
|
||||
set => SetValue(ButtonsProperty, value);
|
||||
}
|
||||
|
||||
public IEventAggregator EventAggregator
|
||||
{
|
||||
get => (IEventAggregator)GetValue(EventAggregatorProperty);
|
||||
set => SetValue(EventAggregatorProperty, value);
|
||||
}
|
||||
|
||||
private static void OnEventAggregatorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
(sender as InteractiveFrame)?.OnEventAggregatorPropertyChanged();
|
||||
}
|
||||
|
||||
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args)
|
||||
{
|
||||
if (EventAggregator is not null)
|
||||
{
|
||||
if (args.NewItems is IList newItems)
|
||||
{
|
||||
foreach (object? item in newItems)
|
||||
{
|
||||
if (item is InteractiveFrameButton button)
|
||||
{
|
||||
button.Register(EventAggregator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void OnEventAggregatorPropertyChanged()
|
||||
{
|
||||
if (EventAggregator is not null)
|
||||
{
|
||||
foreach (InteractiveFrameButton button in Buttons)
|
||||
{
|
||||
button.Register(EventAggregator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using TheXamlGuy.UI;
|
||||
using TheXamlGuy.Framework.Core;
|
||||
using TheXamlGuy.Framework.Microcontroller;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public class InteractiveFrameButton : DependencyObject
|
||||
{
|
||||
public static DependencyProperty PlacementProperty =
|
||||
DependencyProperty.Register(nameof(Type),
|
||||
typeof(InteractiveFrameButtonPlacement), typeof(InteractiveFrameButton));
|
||||
|
||||
public event TypedEventHandler<InteractiveFrameButton, InteractiveFrameButtonInvokedArgs>? Invoked;
|
||||
|
||||
public InteractiveFrameButtonPlacement Placement
|
||||
{
|
||||
get => (InteractiveFrameButtonPlacement)GetValue(PlacementProperty);
|
||||
set => SetValue(PlacementProperty, value);
|
||||
}
|
||||
|
||||
public void Register(IEventAggregator eventAggregator)
|
||||
{
|
||||
eventAggregator.SubscribeUI<CapactiveSensor>(args =>
|
||||
{
|
||||
if ((int)args.Placement == (int)Placement)
|
||||
{
|
||||
if (args.State == SensorState.On)
|
||||
{
|
||||
Invoked?.Invoke(this, new InteractiveFrameButtonInvokedArgs());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF.Interactions;
|
||||
|
||||
public class InteractiveFrameButtonCollection : ObservableCollection<InteractiveFrameButton>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public class InteractiveFrameButtonInvokedArgs : EventArgs
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
public enum InteractiveFrameButtonPlacement
|
||||
{
|
||||
Left = 0,
|
||||
Top = 1,
|
||||
Right = 3,
|
||||
Bottom = 4,
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
using TheXamlGuy.Framework.Core;
|
||||
|
||||
namespace TheXamlGuy.Framework.WPF;
|
||||
|
||||
[ContentProperty("Subscribers")]
|
||||
public class XamlEventAggregator : DependencyObject
|
||||
{
|
||||
public static DependencyProperty EventAggregatorProperty =
|
||||
DependencyProperty.Register(nameof(EventAggregator),
|
||||
typeof(IEventAggregator), typeof(XamlEventAggregator), new PropertyMetadata(null, OnEventAggregatorPropertyChanged));
|
||||
|
||||
public static DependencyProperty SubscribersProperty =
|
||||
DependencyProperty.Register(nameof(Subscribers),
|
||||
typeof(EventSubscriberCollection), typeof(XamlEventAggregator));
|
||||
|
||||
public XamlEventAggregator()
|
||||
{
|
||||
EventSubscriberCollection? collection = new();
|
||||
collection.CollectionChanged += OnCollectionChanged;
|
||||
|
||||
SetValue(SubscribersProperty, collection);
|
||||
}
|
||||
|
||||
public IEventAggregator EventAggregator
|
||||
{
|
||||
get => (IEventAggregator)GetValue(EventAggregatorProperty);
|
||||
set => SetValue(EventAggregatorProperty, value);
|
||||
}
|
||||
|
||||
public EventSubscriberCollection Subscribers
|
||||
{
|
||||
get => (EventSubscriberCollection)GetValue(SubscribersProperty);
|
||||
set => SetValue(SubscribersProperty, value);
|
||||
}
|
||||
|
||||
private static void OnEventAggregatorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
(sender as XamlEventAggregator)?.OnEventAggregatorPropertyChanged();
|
||||
}
|
||||
|
||||
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args)
|
||||
{
|
||||
if (EventAggregator is not null)
|
||||
{
|
||||
if (args.NewItems is IList newItems)
|
||||
{
|
||||
foreach (object? item in newItems)
|
||||
{
|
||||
if (item is IEventSubscriber subscriber)
|
||||
{
|
||||
subscriber.Subscribe(EventAggregator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEventAggregatorPropertyChanged()
|
||||
{
|
||||
if (EventAggregator is not null)
|
||||
{
|
||||
foreach (IEventSubscriber subscriber in Subscribers)
|
||||
{
|
||||
subscriber.Subscribe(EventAggregator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user