Merge pull request #4787 from michael-hawker/llama/fix4784

RadialGauge and WeakEventListener optimizations
This commit is contained in:
Michael Hawker MSFT (XAML Llama) 2022-10-18 09:28:01 -07:00 коммит произвёл GitHub
Родитель b2ea7ba59c 5ba956ee4f
Коммит acf30d6d46
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 93 добавлений и 65 удалений

Просмотреть файл

@ -440,7 +440,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
var incc = (INotifyCollectionChanged)newValue;
_inccWeakEventListener = new WeakEventListener<RotatorTile, object, NotifyCollectionChangedEventArgs>(this)
{
OnEventAction = (instance, source, eventArgs) => instance.Incc_CollectionChanged(source, eventArgs),
OnEventAction = static (instance, source, eventArgs) => instance.Incc_CollectionChanged(source, eventArgs),
OnDetachAction = (listener) => incc.CollectionChanged -= listener.OnEvent
};
incc.CollectionChanged += _inccWeakEventListener.OnEvent;

Просмотреть файл

@ -185,7 +185,80 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
SmallChange = 1;
LargeChange = 10;
Unloaded += RadialGauge_Unloaded;
// Small step
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Left, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.SmallChange));
kaea.Handled = true;
}
});
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Up, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.SmallChange));
kaea.Handled = true;
}
});
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Right, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.SmallChange));
kaea.Handled = true;
}
});
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Down, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.SmallChange));
kaea.Handled = true;
}
});
// Large step
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Left, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.LargeChange));
kaea.Handled = true;
}
});
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Up, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.LargeChange));
kaea.Handled = true;
}
});
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Right, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.LargeChange));
kaea.Handled = true;
}
});
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Down, static (_, kaea) =>
{
if (kaea.Element is RadialGauge gauge)
{
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.LargeChange));
kaea.Handled = true;
}
});
}
private void ThemeListener_ThemeChanged(ThemeListener sender)
@ -195,8 +268,8 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
private void RadialGauge_Unloaded(object sender, RoutedEventArgs e)
{
// Unregister event handlers.
KeyboardAccelerators.Clear();
// TODO: We should just use a WeakEventListener for ThemeChanged here, but ours currently doesn't support it.
// See proposal for general helper here: https://github.com/CommunityToolkit/dotnet/issues/404
ThemeListener.ThemeChanged -= ThemeListener_ThemeChanged;
PointerReleased -= RadialGauge_PointerReleased;
Unloaded -= RadialGauge_Unloaded;
@ -407,6 +480,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// </summary>
protected override void OnApplyTemplate()
{
PointerReleased -= RadialGauge_PointerReleased;
ThemeListener.ThemeChanged -= ThemeListener_ThemeChanged;
Unloaded -= RadialGauge_Unloaded;
// Remember local brushes.
_needleBrush = ReadLocalValue(NeedleBrushProperty) as SolidColorBrush;
_trailBrush = ReadLocalValue(TrailBrushProperty) as SolidColorBrush;
@ -415,58 +492,9 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
_tickBrush = ReadLocalValue(TickBrushProperty) as SolidColorBrush;
_foreground = ReadLocalValue(ForegroundProperty) as SolidColorBrush;
// Small step
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Left, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Up, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Right, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Down, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});
// Large step
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Left, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Up, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Right, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Down, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});
PointerReleased += RadialGauge_PointerReleased;
ThemeListener.ThemeChanged += ThemeListener_ThemeChanged;
Unloaded += RadialGauge_Unloaded;
// Apply color scheme.
OnColorsChanged();

Просмотреть файл

@ -62,7 +62,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
{
var weakPropertyChangedListener = new WeakEventListener<InterspersedObservableCollection, object, NotifyCollectionChangedEventArgs>(this)
{
OnEventAction = (instance, source, eventArgs) => instance.ItemsSource_CollectionChanged(source, eventArgs),
OnEventAction = static (instance, source, eventArgs) => instance.ItemsSource_CollectionChanged(source, eventArgs),
OnDetachAction = (weakEventListener) => notifier.CollectionChanged -= weakEventListener.OnEvent // Use Local Reference Only
};
notifier.CollectionChanged += weakPropertyChangedListener.OnEvent;

Просмотреть файл

@ -97,7 +97,7 @@ namespace Microsoft.Toolkit.Uwp.UI
new WeakEventListener<AdvancedCollectionView, object, NotifyCollectionChangedEventArgs>(this)
{
// Call the actual collection changed event
OnEventAction = (source, changed, arg3) => SourceNcc_CollectionChanged(source, arg3),
OnEventAction = static (instance, source, arg3) => instance.SourceNcc_CollectionChanged(source, arg3),
// The source doesn't exist anymore
OnDetachAction = (listener) => sourceNcc.CollectionChanged -= _sourceWeakEventListener.OnEvent

Просмотреть файл

@ -23,7 +23,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
var weakEvent =
new WeakEventListener<FullScreenModeStateTrigger, ApplicationView, object>(this)
{
OnEventAction = (instance, source, eventArgs) => instance.FullScreenModeTrigger_VisibleBoundsChanged(source, eventArgs),
OnEventAction = static (instance, source, eventArgs) => instance.FullScreenModeTrigger_VisibleBoundsChanged(source, eventArgs),
OnDetachAction = (weakEventListener) => ApplicationView.GetForCurrentView().VisibleBoundsChanged -= weakEventListener.OnEvent
};
ApplicationView.GetForCurrentView().VisibleBoundsChanged += weakEvent.OnEvent;

Просмотреть файл

@ -47,9 +47,9 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
var valNotifyCollection = val as INotifyCollectionChanged;
if (valNotifyCollection != null)
{
var weakEvent = new WeakEventListener<INotifyCollectionChanged, object, NotifyCollectionChangedEventArgs>(valNotifyCollection)
var weakEvent = new WeakEventListener<IsNullOrEmptyStateTrigger, object, NotifyCollectionChangedEventArgs>(obj)
{
OnEventAction = (instance, source, args) => obj.SetActive(IsNullOrEmpty(instance)),
OnEventAction = static (instance, source, args) => instance.SetActive(IsNullOrEmpty(source)),
OnDetachAction = (weakEventListener) => valNotifyCollection.CollectionChanged -= weakEventListener.OnEvent
};
@ -61,9 +61,9 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
var valObservableVector = val as IObservableVector<object>;
if (valObservableVector != null)
{
var weakEvent = new WeakEventListener<IObservableVector<object>, object, IVectorChangedEventArgs>(valObservableVector)
var weakEvent = new WeakEventListener<IsNullOrEmptyStateTrigger, object, IVectorChangedEventArgs>(obj)
{
OnEventAction = (instance, source, args) => obj.SetActive(IsNullOrEmpty(instance)),
OnEventAction = static (instance, source, args) => instance.SetActive(IsNullOrEmpty(source)),
OnDetachAction = (weakEventListener) => valObservableVector.VectorChanged -= weakEventListener.OnEvent
};
@ -75,9 +75,9 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
var valObservableMap = val as IObservableMap<object, object>;
if (valObservableMap != null)
{
var weakEvent = new WeakEventListener<IObservableMap<object, object>, object, IMapChangedEventArgs<object>>(valObservableMap)
var weakEvent = new WeakEventListener<IsNullOrEmptyStateTrigger, object, IMapChangedEventArgs<object>>(obj)
{
OnEventAction = (instance, source, args) => obj.SetActive(IsNullOrEmpty(instance)),
OnEventAction = static (instance, source, args) => instance.SetActive(IsNullOrEmpty(source)),
OnDetachAction = (weakEventListener) => valObservableMap.MapChanged -= weakEventListener.OnEvent
};

Просмотреть файл

@ -25,7 +25,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
var weakEvent =
new WeakEventListener<NetworkConnectionStateTrigger, object>(this)
{
OnEventAction = (instance, source) => NetworkInformation_NetworkStatusChanged(source),
OnEventAction = static (instance, source) => instance.NetworkInformation_NetworkStatusChanged(source),
OnDetachAction = (weakEventListener) => NetworkInformation.NetworkStatusChanged -= weakEventListener.OnEvent
};
NetworkInformation.NetworkStatusChanged += weakEvent.OnEvent;

Просмотреть файл

@ -24,7 +24,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
var weakEvent =
new WeakEventListener<UserInteractionModeStateTrigger, object, WindowSizeChangedEventArgs>(this)
{
OnEventAction = (instance, source, eventArgs) => UserInteractionModeTrigger_SizeChanged(source, eventArgs),
OnEventAction = static (instance, source, eventArgs) => instance.UserInteractionModeTrigger_SizeChanged(source, eventArgs),
OnDetachAction = (weakEventListener) => Window.Current.SizeChanged -= weakEventListener.OnEvent
};
Window.Current.SizeChanged += weakEvent.OnEvent;