using System.Collections.Generic; using System.Windows.Input; using System.Windows.Media; using WPFDark.Internals; namespace WPFDark { public static class Caches { public static Pen GetPen(ByteColor color, double thickness) { var hashCode = HashCodeMaker.Make(color, thickness); if (_borderPens.TryGetValue(hashCode, out var p)) return p; p = new Pen(GetSolidColorBrush(color), thickness); p.Freeze(); _borderPens.Add(hashCode, p); return p; } public static Pen GetCapPen(ByteColor color, double thickness) { var hashCode = HashCodeMaker.Make(color, thickness); if (_capPens.TryGetValue(hashCode, out var p)) return p; p = new Pen(GetSolidColorBrush(color), thickness) { StartLineCap = PenLineCap.Round, EndLineCap = PenLineCap.Round }; p.Freeze(); _capPens.Add(hashCode, p); return p; } public static Pen GetDashedPen(ByteColor color, double thickness) { var hashCode = HashCodeMaker.Make(color, thickness); if (_dashedPens.TryGetValue(hashCode, out var p)) return p; p = new Pen(GetSolidColorBrush(color), thickness) { DashStyle = DashStyles.Dash }; p.Freeze(); _dashedPens.Add(hashCode, p); return p; } public static SolidColorBrush GetSolidColorBrush(ByteColor color) { var hashCode = HashCodeMaker.Make(color); if (_solidColorBrushes.TryGetValue(hashCode, out var b)) return b; b = new SolidColorBrush(color.ToColor()); b.Freeze(); _solidColorBrushes.Add(hashCode, b); return b; } public static Geometry GetClipGeom(in LayoutRounder rounder, double w, double h, double cornerRadius, bool isWidthBorder) { var hashCode = HashCodeMaker.Make(w, h, cornerRadius, isWidthBorder); if (_clipGeoms.TryGetValue(hashCode, out var c)) return c; if (isWidthBorder) { c = new RectangleGeometry { RadiusX = cornerRadius, RadiusY = cornerRadius, Rect = rounder.RoundLayoutRect( FrameworkElementExtensions.BorderWidth * 0.5, FrameworkElementExtensions.BorderWidth * 0.5, w - FrameworkElementExtensions.BorderWidth, h - FrameworkElementExtensions.BorderWidth) }; } else { c = new RectangleGeometry { RadiusX = cornerRadius, RadiusY = cornerRadius, Rect = rounder.RoundLayoutRect(0, 0, w, h) }; } c.Freeze(); _clipGeoms.Add(hashCode, c); return c; } private static readonly Dictionary _clipGeoms = new Dictionary(); private static readonly Dictionary _borderPens = new Dictionary(); private static readonly Dictionary _capPens = new Dictionary(); private static readonly Dictionary _dashedPens = new Dictionary(); private static readonly Dictionary _solidColorBrushes = new Dictionary(); public static TraversalRequest PreviousTraversalRequest => _PreviousTraversalRequest ??= new TraversalRequest(FocusNavigationDirection.Previous); public static TraversalRequest NextTraversalRequest => _NextTraversalRequest ??= new TraversalRequest(FocusNavigationDirection.Next); private static TraversalRequest? _PreviousTraversalRequest; private static TraversalRequest? _NextTraversalRequest; } }