Merge branch 'master' of https://github.com/HandyOrg/HandyControl into upstream-master

This commit is contained in:
noctwolf committed 2019-08-15 23:06:29 +08:00
commit 2f34bb72d6
53 files changed
+775 -69

No files matched your search

@@ -8,6 +8,44 @@ namespace HandyControl.Controls
{
public class DataGridAttach
{
public static readonly DependencyProperty ApplyDefaultStyleProperty = DependencyProperty.RegisterAttached(
"ApplyDefaultStyle", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnApplyDefaultStyleChanged));
private static void OnApplyDefaultStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = (DataGrid)d;
if ((bool)e.NewValue)
{
if (grid.AutoGenerateColumns)
{
grid.AutoGeneratedColumns += Grid_AutoGeneratedColumns;
return;
}
UpdateTextColumnStyles(grid);
UpdateComboBoxColumnStyles(grid);
UpdateCheckBoxColumnStyles(grid);
}
else
{
grid.AutoGeneratedColumns -= Grid_AutoGeneratedColumns;
}
}
private static void Grid_AutoGeneratedColumns(object sender, System.EventArgs e)
{
var grid = (DataGrid)sender;
UpdateTextColumnStyles(grid);
UpdateComboBoxColumnStyles(grid);
UpdateCheckBoxColumnStyles(grid);
}
public static void SetApplyDefaultStyle(DependencyObject element, bool value)
=> element.SetValue(ApplyDefaultStyleProperty, value);
public static bool GetApplyDefaultStyle(DependencyObject element)
=> (bool) element.GetValue(ApplyDefaultStyleProperty);
public static readonly DependencyProperty TextColumnStyleProperty = DependencyProperty.RegisterAttached(
"TextColumnStyle", typeof(Style), typeof(DataGridAttach), new PropertyMetadata(default(Style), OnTextColumnStyleChanged));
@@ -1,6 +1,9 @@
using System.Windows;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using HandyControl.Data;
using HandyControl.Tools.Interop;
namespace HandyControl.Controls
{
@@ -21,7 +24,7 @@ namespace HandyControl.Controls
{
if ((bool)e.NewValue)
{
ctl.MouseLeftButtonDown += DragElement_MouseLeftButtonDown; ;
ctl.MouseLeftButtonDown += DragElement_MouseLeftButtonDown;
}
else
{
@@ -37,5 +40,72 @@ namespace HandyControl.Controls
System.Windows.Window.GetWindow(obj)?.DragMove();
}
}
public static readonly DependencyProperty IgnoreAltF4Property = DependencyProperty.RegisterAttached(
"IgnoreAltF4", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIgnoreAltF4Changed));
private static void OnIgnoreAltF4Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
if ((bool)e.NewValue)
{
window.PreviewKeyDown += Window_PreviewKeyDown;
}
else
{
window.PreviewKeyDown -= Window_PreviewKeyDown;
}
}
}
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.System && e.SystemKey == Key.F4)
{
e.Handled = true;
}
}
public static void SetIgnoreAltF4(DependencyObject element, bool value)
=> element.SetValue(IgnoreAltF4Property, value);
public static bool GetIgnoreAltF4(DependencyObject element)
=> (bool) element.GetValue(IgnoreAltF4Property);
public static readonly DependencyProperty ShowInTaskManagerProperty = DependencyProperty.RegisterAttached(
"ShowInTaskManager", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.TrueBox, OnShowInTaskManagerChanged));
private static void OnShowInTaskManagerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Window window)
{
if ((bool)e.NewValue)
{
window.SourceInitialized -= Window_SourceInitialized;
}
else
{
window.SourceInitialized += Window_SourceInitialized;
}
}
}
private static void Window_SourceInitialized(object sender, EventArgs e)
{
if (sender is System.Windows.Window window)
{
var _ = new WindowInteropHelper(window)
{
Owner = NativeMethods.GetDesktopWindow()
};
}
}
public static void SetShowInTaskManager(DependencyObject element, bool value)
=> element.SetValue(ShowInTaskManagerProperty, value);
public static bool GetShowInTaskManager(DependencyObject element)
=> (bool) element.GetValue(ShowInTaskManagerProperty);
}
}
@@ -1,8 +1,5 @@
using System;
using System.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using HandyControl.Tools.Interop;
namespace HandyControl.Controls
{
@@ -26,16 +23,6 @@ namespace HandyControl.Controls
GrowlPanel = GetTemplateChild(ElementGrowPanel) as Panel;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var _ = new WindowInteropHelper(this)
{
Owner = NativeMethods.GetDesktopWindow()
};
}
public void Init()
{
var desktopWorkingArea = SystemParameters.WorkArea;
@@ -65,7 +65,8 @@ namespace HandyControl.Controls
{
if (_textBox != null)
{
_textBox.TextChanged -= TextBox_TextChanged;
TextCompositionManager.RemovePreviewTextInputHandler(_textBox, PreviewTextInputHandler);
_textBox.TextChanged -= _textBox_TextChanged;
_textBox.PreviewKeyDown -= TextBox_PreviewKeyDown;
_textBox.LostFocus -= TextBox_LostFocus;
}
@@ -76,13 +77,27 @@ namespace HandyControl.Controls
if (_textBox != null)
{
_textBox.TextChanged += TextBox_TextChanged;
TextCompositionManager.AddPreviewTextInputHandler(_textBox, PreviewTextInputHandler);
_textBox.TextChanged += _textBox_TextChanged;
_textBox.PreviewKeyDown += TextBox_PreviewKeyDown;
_textBox.LostFocus += TextBox_LostFocus;
_textBox.Text = CurrentText;
}
}
private void _textBox_TextChanged(object sender, TextChangedEventArgs e) => UpdateData();
private void UpdateData()
{
if (!VerifyData()) return;
if (double.TryParse(_textBox.Text, out var value))
{
Value = value;
}
}
private void PreviewTextInputHandler(object sender, TextCompositionEventArgs e) => UpdateData();
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (IsError) return;
@@ -103,15 +118,6 @@ namespace HandyControl.Controls
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!VerifyData()) return;
if (double.TryParse(_textBox.Text, out var value))
{
Value = value;
}
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
@@ -0,0 +1,64 @@
using System.Windows;
using System.Windows.Input;
namespace HandyControl.Controls
{
public sealed class Sprite : System.Windows.Window
{
private bool _isLeftButtonPressed;
private double _leftMax;
private double _topMax;
private Sprite()
{
WindowStyle = WindowStyle.None;
AllowsTransparency = true;
InputManager.Current.PostProcessInput += Current_PostProcessInput;
}
private void Current_PostProcessInput(object sender, ProcessInputEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
_isLeftButtonPressed = true;
}
else if(_isLeftButtonPressed)
{
if (Left < 0)
{
Left = 0;
}
if (Left > _leftMax)
{
Left = _leftMax;
}
if (Top > _topMax)
{
Top = _topMax;
}
}
}
public static Sprite Show(object content)
{
var sprite = new Sprite
{
Content = content
};
sprite.Show();
var desktopWorkingArea = SystemParameters.WorkArea;
sprite._leftMax = desktopWorkingArea.Width - sprite.ActualWidth;
sprite._topMax = desktopWorkingArea.Height - sprite.ActualHeight;
sprite.Left = sprite._leftMax - 50;
sprite.Top = 50 - sprite.Padding.Top;
return sprite;
}
}
}
@@ -35,18 +35,28 @@ namespace HandyControl.Controls
set => SetValue(OffsetAngleProperty, value);
}
// ReSharper disable once RedundantAssignment
protected override Size MeasureOverride(Size availableSize)
{
if (Children.Count == 0) return new Size(Diameter, Diameter);
availableSize = new Size(Diameter, Diameter);
var newSize = new Size(Diameter, Diameter);
foreach (UIElement element in Children)
{
element.Measure(newSize);
}
return newSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
var i = 0;
var perDeg = 360.0 / Children.Count;
var radius = Diameter / 2;
foreach (UIElement element in Children)
{
element.Measure(availableSize);
var centerX = element.DesiredSize.Width / 2.0;
var centerY = element.DesiredSize.Height / 2.0;
var angle = perDeg * i++ + OffsetAngle;
@@ -60,11 +70,12 @@ namespace HandyControl.Controls
var r = Math.PI * angle / 180.0;
var x = radius * Math.Cos(r);
var y = radius * Math.Sin(r);
var rectX = x + availableSize.Width / 2 - centerX;
var rectY = y + availableSize.Height / 2 - centerY;
var rectX = x + finalSize.Width / 2 - centerX;
var rectY = y + finalSize.Height / 2 - centerY;
element.Arrange(new Rect(rectX, rectY, element.DesiredSize.Width, element.DesiredSize.Height));
}
return availableSize;
return finalSize;
}
}
}
@@ -0,0 +1,166 @@
using System;
using System.Windows;
using System.Windows.Controls;
namespace HandyControl.Controls
{
public class HoneycombPanel : Panel
{
private double _unitLength;
private HoneycombStuffer _stuffer;
private static int GetXCount(int count)
{
if (count == 0) return 0;
count -= 1;
var index = (int) Math.Floor(Math.Pow((12.0 * count + 25) / 36, 0.5) - 5.0 / 6);
var valeIndex = 3 * index * index + 5 * index;
var centerValue = valeIndex + 2;
return count >= centerValue
? 4 * index + 6
: count > valeIndex
? 4 * index + 4
: 4 * index + 2;
}
private static int GetYCount(int count)
{
if (count == 0) return 0;
count -= 1;
var index = (int)Math.Floor(Math.Pow(count / 3.0 + 0.25, 0.5) - 0.5);
var valeIndex = 3 * index * index + 3 * index;
return count > valeIndex
? 2 * index + 2
: 2 * index;
}
/*
* layout order
*
* ● ●
* (7) (8)
*
* ● ● ●
* (6) (1) (9)
*
* ● ● ● ...
* (5) (0) (2)
*
* ● ● ...
* (4) (3)
*
*/
protected override Size MeasureOverride(Size availableSize)
{
var maxSize = new Size();
foreach (UIElement child in InternalChildren)
{
if (child != null)
{
child.Measure(availableSize);
maxSize.Width = Math.Max(maxSize.Width, child.DesiredSize.Width);
maxSize.Height = Math.Max(maxSize.Height, child.DesiredSize.Height);
}
}
_unitLength = Math.Max(maxSize.Width, maxSize.Height) / 2;
var xCount = GetXCount(InternalChildren.Count);
var yCount = GetYCount(InternalChildren.Count);
var availableWidth = xCount * _unitLength;
var availableHeight = yCount * Math.Pow(3, 0.5) * _unitLength + _unitLength * 2;
return new Size(availableWidth, availableHeight);
}
protected override Size ArrangeOverride(Size finalSize)
{
var childLength = _unitLength * 2;
_stuffer = new HoneycombStuffer(new Rect(finalSize.Width / 2 - _unitLength,
finalSize.Height / 2 - _unitLength, childLength, childLength));
foreach (UIElement child in InternalChildren)
{
child.Arrange(_stuffer.Move());
}
return finalSize;
}
private class HoneycombStuffer
{
private int _turns;
private int _maxIndex;
private int _currentIndex = -1;
private readonly double _offsetX;
private readonly double _offsetY;
private Rect _childBounds;
private readonly double[] _offsetXArr;
private readonly double[] _offsetYArr;
public HoneycombStuffer(Rect childBounds)
{
_childBounds = childBounds;
_offsetX = childBounds.Width / 2;
_offsetY = Math.Pow(3, 0.5) * _offsetX;
_offsetXArr = new[]
{
2 * _offsetX,
_offsetX,
-_offsetX,
-2 * _offsetX,
-_offsetX,
_offsetX
};
_offsetYArr = new[]
{
0,
_offsetY,
_offsetY,
0,
-_offsetY,
-_offsetY
};
}
public Rect Move()
{
_currentIndex++;
if (_currentIndex > _maxIndex)
{
_turns++;
_maxIndex = _turns * 6 - 1;
_currentIndex = 0;
_childBounds.Offset(_offsetX, -_offsetY);
return _childBounds;
}
if (_turns > 0)
{
var index = _currentIndex / _turns;
_childBounds.Offset(_offsetXArr[index], _offsetYArr[index]);
}
return _childBounds;
}
}
}
}