optimize<DragDropAdorner>: use realtime visual to create VisualBrush for drag-drop element

This commit is contained in:
leo 2021-08-06 20:04:10 +08:00
parent 2309f9c126
commit d6d4482a4e
2 changed files with 9 additions and 11 deletions

View file

@ -8,8 +8,7 @@ namespace SourceGit.Views.Controls {
/// 展示正在拖拽的视图
/// </summary>
public class DragDropAdorner : Adorner {
private Size renderSize;
private Brush renderBrush;
private FrameworkElement renderElem;
public struct PInPoint {
public int X;
@ -23,14 +22,13 @@ namespace SourceGit.Views.Controls {
static extern void GetCursorPos(ref PInPoint p);
public DragDropAdorner(FrameworkElement elem) : base(elem) {
renderSize = elem.RenderSize;
renderBrush = new VisualBrush(elem);
renderElem = elem;
IsHitTestVisible = false;
Window.AddAdorner(elem, this);
}
public void Remove() {
Window.RemoveAdorner(this);
Window.RemoveAdorner(renderElem, this);
}
protected override void OnRender(DrawingContext dc) {
@ -40,11 +38,11 @@ namespace SourceGit.Views.Controls {
GetCursorPos(ref p);
Point pos = PointFromScreen(new Point(p.X, p.Y));
Rect rect = new Rect(pos.X, pos.Y, renderSize.Width, renderSize.Height);
Rect rect = new Rect(pos.X, pos.Y, renderElem.RenderSize.Width, renderElem.RenderSize.Height);
dc.PushOpacity(1);
dc.DrawRectangle(FindResource("Brush.Window") as Brush, null, rect);
dc.DrawRectangle(renderBrush, null, rect);
dc.DrawRectangle(new VisualBrush(renderElem), null, rect);
dc.DrawRectangle(null, new Pen(Brushes.DeepSkyBlue, 2), rect);
}
}

View file

@ -28,16 +28,16 @@ namespace SourceGit.Views.Controls {
Loaded += (_, __) => adornerLayer = AdornerLayer.GetAdornerLayer(Content as FrameworkElement);
}
public static void AddAdorner(FrameworkElement elem, Adorner adorner) {
var wnd = GetWindow(elem) as Window;
public static void AddAdorner(FrameworkElement windowContext, Adorner adorner) {
var wnd = GetWindow(windowContext) as Window;
if (wnd != null && wnd.adornerLayer != null) {
wnd.adorners.Add(adorner);
wnd.adornerLayer.Add(adorner);
}
}
public static void RemoveAdorner(Adorner adorner) {
var wnd = GetWindow(adorner) as Window;
public static void RemoveAdorner(FrameworkElement windowContext, Adorner adorner) {
var wnd = GetWindow(windowContext) as Window;
if (wnd != null && wnd.adornerLayer != null) {
wnd.adorners.Remove(adorner);
wnd.adornerLayer.Remove(adorner);