private IContextMenuTarget FindRightTarget(TargetList targets, Rectangle currentRect) { // We compare from the right edge always currentRect.X = currentRect.Right; currentRect.Width = 0; // Best match found so far IContextMenuTarget nextTarget = null; Rectangle nextRect = Rectangle.Empty; // Search all targets, looking for a better match than the current best foreach (IContextMenuTarget target in targets) { Rectangle targetRect = target.ClientRectangle; // Only interested in targets that are after the current one if (targetRect.Left >= currentRect.Right) { // It nothing found so far, it automatically becomes the best match if (nextTarget == null) { nextTarget = target; nextRect = targetRect; } else { double currentDistance = CenterDistance(currentRect, new Rectangle(nextRect.X, nextRect.Y, 0, nextRect.Height)); double nextDistance = CenterDistance(currentRect, new Rectangle(targetRect.X, targetRect.Y, 0, targetRect.Height)); // If next target is nearer than the current best... if (nextDistance < currentDistance) { //...then it becomes the new best match nextTarget = target; nextRect = targetRect; } } } } return(nextTarget); }
private void OnDelayTimerExpire(object sender, EventArgs e) { if (_itemDelayTimer != null) { _itemDelayTimer.Stop(); // If the target and current sub menu target are the same, then there is nothing // to do as we would only tell the sub menu to be removed and shown again. if (_target != _targetSubMenu) { // Do we know a target that was instructed to show any sub menu? if (_targetSubMenu != null) { // Inform the same target to remove any showing sub menu _targetSubMenu.ClearSubMenu(); _targetSubMenu = null; } // If we still have a target and it has a sub menu if (_target is { HasSubMenu : true })
/// <summary> /// Set the provided target as the current target. /// </summary> /// <param name="target">Reference to the new target.</param> /// <param name="startTimer">Should a timer be started for handling sub menu showing.</param> public void SetTarget(IContextMenuTarget target, bool startTimer) { // Only interested in a change of target if (_target != target) { // Tell current target to reset drawing if (_target != null) { _itemDelayTimer.Stop(); _target.ClearTarget(); _target = null; } // Shift the active view to the new target if (target != null) { ActiveView = target.GetActiveView(); } else { ActiveView = null; } _target = target; // Tell new target to draw as highlighted and start delay timer if (_target != null) { _itemDelayTimer.Stop(); if (startTimer) { _itemDelayTimer.Start(); } _target.ShowTarget(); } } }
/// <summary> /// Handle right key being pressed. /// </summary> public void KeyRight() { TargetList targets = ConstructKeyboardTargets(Root); // Find the next appropriate target IContextMenuTarget newTarget = null; if (_target == null) { newTarget = FindTopLeftTarget(targets); } else { newTarget = FindRightTarget(targets, _target); } // If we found a new target, then make it the current target if ((newTarget != null) && (newTarget != _target)) { SetTarget(newTarget, false); } }
/// <summary> /// Set the provided target as the current target and it is already showing a sub menu /// </summary> public void SetTargetSubMenu(IContextMenuTarget target) { // Kill any running timer if (_itemDelayTimer != null) { _itemDelayTimer.Stop(); } // If the currently showing sub menu is not for the new target if (_targetSubMenu != target) { // Do we know a target that was instructed to show any sub menu? if (_targetSubMenu != null) { // Inform the same target to remove any showing sub menu _targetSubMenu.ClearSubMenu(); _targetSubMenu = null; } // Remember the new sub menu target _targetSubMenu = target; } // Tell current target to reset drawing if (_target != target) { _target.ClearTarget(); _target = null; } _target = target; // Tell new target to draw as highlighted and start delay timer if (_target != null) { _target.ShowTarget(); } }
/// <summary> /// Clean up any resources. /// </summary> public override void Dispose() { if (_itemDelayTimer != null) { _itemDelayTimer.Stop(); _itemDelayTimer.Dispose(); _itemDelayTimer = null; } if (_targetSubMenu != null) { _targetSubMenu.ClearSubMenu(); _targetSubMenu = null; } if (_target != null) { _target.ClearTarget(); _target = null; } base.Dispose(); }