using CefSharp.Structs; namespace CefSharp.Wpf.Internals { public sealed class MousePositionTransform : IMousePositionTransform { /// /// The x-offset. /// private int xOffset; /// /// The y-offset. /// private int yOffset; /// /// The original rect. /// private Rect originalRect; /// /// The adjusted rect. /// private Rect adjustedRect; /// /// If the popup is open or not. /// private bool isOpen; /// /// Updates the size and the position of the popup. /// /// /// /// The adjusted point. System.Windows.Point IMousePositionTransform.UpdatePopupSizeAndPosition(Rect originalRect, Rect viewRect) { int x = originalRect.X, prevX = originalRect.X, y = originalRect.Y, prevY = originalRect.Y, xOffset = 0, yOffset = 0; // If popup goes outside the view, try to reposition origin if (originalRect.X + originalRect.Width > viewRect.Width) { x = viewRect.Width - originalRect.Width; xOffset = prevX - x; } if (originalRect.Y + originalRect.Height > viewRect.Height) { y = y - originalRect.Height - 20; yOffset = prevY - y; } // If x or y became negative, move them to 0 again if (x < 0) { x = 0; xOffset = prevX; } if (y < 0) { y = 0; yOffset = prevY; } if (x != prevX || y != prevY) { this.isOpen = true; this.xOffset = xOffset; this.yOffset = yOffset; Rect adjustedRect = new Rect(x, y, x + originalRect.Width, y + originalRect.Height); this.originalRect = originalRect; this.adjustedRect = adjustedRect; if (this.originalRect.Y < this.adjustedRect.Y + this.adjustedRect.Height) { var newY = this.adjustedRect.Y + this.adjustedRect.Height; this.originalRect = new Rect(originalRect.X, newY, originalRect.Width, originalRect.Y + originalRect.Height - newY); } } return new System.Windows.Point(x, y); } /// /// Resets the offsets and original-rect. /// If the popup is open or not. /// void IMousePositionTransform.OnPopupShow(bool isOpen) { if (!isOpen) { this.isOpen = false; this.xOffset = 0; this.yOffset = 0; this.originalRect = new Rect(); this.originalRect = new Rect(); } } /// /// Adjusts the mouse-coordinates when the popup is visible. /// /// The original point. /// The transformed point if needed, else the original point. void IMousePositionTransform.TransformMousePoint(ref System.Windows.Point point) { if (!isOpen) return; if (!IsInsideOriginalRect(point) && IsInsideAdjustedRect(point)) point = new System.Windows.Point((int)point.X + this.xOffset, (int)point.Y + this.yOffset); } /// /// Checks if the given point is inside the original-rect. /// /// The point. /// Returns true if the point is inside the original rect, else return false. private bool IsInsideOriginalRect(System.Windows.Point point) { return point.X >= this.originalRect.X && point.X < this.originalRect.X + this.originalRect.Width && point.Y >= this.originalRect.Y && point.Y < this.originalRect.Y + this.originalRect.Height; } /// /// Checks if the given point is inside the adjusted rect. /// /// The point. /// Returns true if the point is inside the adjusted rect, else return false. private bool IsInsideAdjustedRect(System.Windows.Point point) { return point.X >= this.adjustedRect.X && point.X < this.adjustedRect.X + this.adjustedRect.Width && point.Y >= this.adjustedRect.Y && point.Y < this.adjustedRect.Y + this.adjustedRect.Height; } } }