/// <summary> /// Process mouse events from the parent window /// </summary> /// <param name="sender">The object which initiates the event</param> /// <param name="args">The context of the event</param> public bool WindowOnMouseEvent(object sender, MouseEventArgs args) { var isClickEvent = args is MouseButtonEventArgs; var focused = false; var prevFocusedControl = _focusedControl; // process the mouse events from the top-down, focusing on the topmost control if it's clicked on foreach (var control in Controls.OrderByDescending(control => control.ZIndex)) { var stage = control.ProcessMouseEvent(sender, args); if (stage != EventStage.Handled || !control.CanFocus || focused) { continue; } SetFocus(control); focused = true; } // if nothing was clicked on, blur all controls if (!focused && isClickEvent) { SetFocus(null); } // return if we don't need to process click or double click events if (_focusedControl == null || _focusedControl != prevFocusedControl || !(args is MouseButtonEventArgs buttonEvent) || buttonEvent.IsPressed) { return(focused); } var now = DateTime.Now; // check eligibility of click events var doubleClick = _focusedControl == _clickedControl && (now - _clickTime).TotalMilliseconds < KNativeMethods.GetDoubleClickTime(); var result = _focusedControl.ProcessMouseEvent(sender, new KMouseClickEventArgs(args, doubleClick)); // reset the clicked control after double click events _clickTime = now; _clickedControl = doubleClick ? null : _focusedControl; return(result == EventStage.Handled); }
/// <summary> /// Sets the focus to the specified control /// </summary> /// <param name="needle">The control to focus on. Pass null to blur all elements.</param> private void SetFocus(KuatControl needle) { _focusedControl = needle; foreach (var control in Controls) { var wasFocus = control.HasFocus; control.HasFocus = control == needle; if (wasFocus && !control.HasFocus) { control.OnBlur(this, EventArgs.Empty); } else if (!wasFocus && control.HasFocus) { control.OnFocus(this, EventArgs.Empty); } } }