protected void CommonPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e) { OnPropertyChanged(e.PropertyName); if (e.PropertyName == "Focused") { FocusedChanged?.Invoke(); } if (e.PropertyName == "Checkouted") { CheckoutedChanged?.Invoke(); } }
/// <inheritdoc /> protected override bool BubbleUp(ConsoleKeyInfo key) { if (base.BubbleUp(key)) { return(true); } switch (key.Key) { case ConsoleKey.Tab when key.Modifiers.HasFlag(ConsoleModifiers.Shift): case ConsoleKey.UpArrow when Orientation == Orientation.Vertical: case ConsoleKey.LeftArrow when Orientation == Orientation.Horizontal: return(MoveSelection(-1)); case ConsoleKey.Enter: case ConsoleKey.Tab: case ConsoleKey.DownArrow when Orientation == Orientation.Vertical: case ConsoleKey.RightArrow when Orientation == Orientation.Horizontal: return(MoveSelection(+1)); case ConsoleKey.End: case ConsoleKey.PageDown: { var focusable = FindFocusable(); if (focusable.Count == 0) { return(false); } (Focused, Position) = focusable.Last(); FocusedChanged?.Invoke(); return(true); } case ConsoleKey.Home: case ConsoleKey.PageUp: { var focusable = FindFocusable(); if (focusable.Count == 0) { return(false); } (Focused, Position) = focusable.First(); FocusedChanged?.Invoke(); return(true); } default: return(false); } }
/// <summary> /// Call when the focused object has been updated so the properties in the UI are refreshed. /// </summary> public void UpdateFocused() { if (_focused != null) { try { PropertyGrid.Refresh(); if (FocusedChanged != null) { FocusedChanged.Raise(this, EventArgsHelper.Create(_focused)); } } catch (NullReferenceException) { } } }
/// <summary> /// Передивагет фокус на указанное количество элементов. /// </summary> /// <returns>Удалось ли передвинуть.</returns> private bool MoveSelection(int direction) { var focusable = FindFocusable(); if (focusable.Count == 0) { return(false); } var currentlySelected = focusable.FindIndex(x => ReferenceEquals(x.Item1, Focused)); if (currentlySelected == -1) { // Ранее сфокусированный эл-т больше не может быть сфокусированным. // Поэтому _вместо_ движения фокуса, просто установим его на ближайший возможный к желаемому. var wanted = Position + direction; var nearest = focusable.MinBy(i => Math.Abs(i.index - wanted)); if (nearest == null) { (Focused, Position) = (null, 0); FocusedChanged?.Invoke(); return(false); } (Focused, Position) = nearest.Value; } else { var newSelected = currentlySelected + direction; if (newSelected < 0 || newSelected >= focusable.Count) { return(false); } (Focused, Position) = focusable[newSelected]; } FocusedChanged?.Invoke(); return(true); }
protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); FocusedChanged?.Invoke(new FocusedChangedEventArgs(false)); }
protected override void OnGotFocus(GotFocusEventArgs e) { base.OnGotFocus(e); FocusedChanged?.Invoke(new FocusedChangedEventArgs(true)); }
private void ComboBoxIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e) => FocusedChanged?.Invoke(sender, EventArgs.Empty);
private void TextBoxIsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e) { FocusedChanged?.Invoke(sender, EventArgs.Empty); }
private void PasswordBoxIsKeyboardFocusedChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e) { FocusedChanged?.Invoke(this, EventArgs.Empty); }
private void RegisterWindowCallbacks() { if (_callbackRegistered) { return; } _callbackRegistered = true; // [NOTE] // Do not register callback to GLFW as lambda or method. (That cannot work) // Put delegate on a field and register it to GLFW. _posCallback = (wnd, x, y) => { if (wnd != _window) { return; } _location = new Vector2i(x, y); Move?.Invoke(this, new WindowPositionEventArgs(x, y)); }; GLFW.SetWindowPosCallback(_window, _posCallback); _sizeCallback = (wnd, width, height) => { if (wnd != _window) { return; } GLFW.GetWindowFrameSize(_window, out var left, out var top, out var right, out var bottom); _clientSize = new Vector2i(width, height); _size = new Vector2i(_clientSize.X + left + right, _clientSize.Y + top + bottom); Resize?.Invoke(this, new ResizeEventArgs(width, height)); }; GLFW.SetWindowSizeCallback(_window, _sizeCallback); _frameBufferSizeCallback = (wnd, width, height) => { if (wnd != _window) { return; } _frameBufferSize = new Vector2i(width, height); FrameBufferSizeChanged?.Invoke(this, _frameBufferSize); }; GLFW.SetFramebufferSizeCallback(_window, _frameBufferSizeCallback); _closeCallback = wnd => { if (wnd != _window) { return; } var cancel = false; var e = new CancelEventArgs(&cancel); Closing?.Invoke(this, e); if (e.Cancel) { GLFW.SetWindowShouldClose(_window, false); } }; GLFW.SetWindowCloseCallback(_window, _closeCallback); _iconifyCallback = (wnd, minimized) => { if (wnd != _window) { return; } Minimized?.Invoke(this, new MinimizedEventArgs(minimized)); }; GLFW.SetWindowIconifyCallback(_window, _iconifyCallback); _focusCallback = (wnd, focused) => { if (wnd != _window) { return; } FocusedChanged?.Invoke(this, new FocusedChangedEventArgs(focused)); }; GLFW.SetWindowFocusCallback(_window, _focusCallback); _charCallback = (wnd, unicode) => { if (wnd != _window) { return; } CharInput?.Invoke(this, new CharInputEventArgs(unicode)); }; GLFW.SetCharCallback(_window, _charCallback); _keyCallback = (wnd, glfwKey, scanCode, action, glfwMods) => { if (wnd != _window) { return; } var e = new KeyboardKeyEventArgs(glfwKey, scanCode, glfwMods, action == GlfwInputAction.Repeat); if (action == GlfwInputAction.Release) { KeyUp?.Invoke(this, e); } else { KeyDown?.Invoke(this, e); } }; GLFW.SetKeyCallback(_window, _keyCallback); _cursorEnterCallback = (wnd, entered) => { if (wnd != _window) { return; } if (entered) { MouseEnter?.Invoke(this); } else { MouseLeave?.Invoke(this); } }; GLFW.SetCursorEnterCallback(_window, _cursorEnterCallback); _mouseButtonCallback = (wnd, button, action, mods) => { if (wnd != _window) { return; } var e = new MouseButtonEventArgs(button, action, mods); if (action == GlfwInputAction.Release) { MouseUp?.Invoke(this, e); } else { MouseDown?.Invoke(this, e); } }; GLFW.SetMouseButtonCallback(_window, _mouseButtonCallback); _cursorPosCallback = (wnd, posX, posY) => { if (wnd != _window) { return; } var e = new MouseMoveEventArgs(new Vector2((int)posX, (int)posY)); MouseMove?.Invoke(this, e); }; GLFW.SetCursorPosCallback(_window, _cursorPosCallback); _scrollCallback = (wnd, offsetX, offsetY) => { if (wnd != _window) { return; } var e = new MouseWheelEventArgs((float)offsetX, (float)offsetY); MouseWheel?.Invoke(this, e); }; GLFW.SetScrollCallback(_window, _scrollCallback); _dropCallback = (wnd, count, paths) => { if (wnd != _window) { return; } var e = new FileDropEventArgs(count, paths); FileDrop?.Invoke(this, e); }; GLFW.SetDropCallback(_window, _dropCallback); _joystickCallback = (joystick, state) => { var e = new JoystickConnectionEventArgs(joystick, state == GlfwConnectedState.Connected); JoystickConnectionChanged?.Invoke(this, e); }; GLFW.SetJoystickCallback(_joystickCallback); _monitorCallback = (monitor, state) => { var e = new MonitorConnectionEventArgs(monitor, state == GlfwConnectedState.Connected); MonitorConnectionChanged?.Invoke(this, e); }; GLFW.SetMonitorCallback(_monitorCallback); _refreshCallback = wnd => { if (wnd != _window) { return; } Refresh?.Invoke(this); }; GLFW.SetWindowRefreshCallback(_window, _refreshCallback); }
public AvaloniaControlWrapper(Control control) { _control = control; var topLevel = GetTopLevelControl(control.Parent); _window = topLevel as global::Avalonia.Controls.Window ?? throw new ArgumentException($"Control {nameof(control)} has no toplevel parent"); control.PointerWheelChanged += (sender, args) => MouseWheel?.Invoke(new MouseWheelEventArgs((float)args.Delta.X, (float)args.Delta.Y)); control.KeyDown += (sender, args) => { var mappedKey = AvaloniaKeyMap.Map[(int)args.Key]; LastKeyboardState = KeyboardState; KeyboardState.SetKeyState(mappedKey, true); }; control.KeyUp += (sender, args) => { var mappedKey = AvaloniaKeyMap.Map[(int)args.Key]; LastKeyboardState = KeyboardState; KeyboardState.SetKeyState(mappedKey, false); }; control.TextInput += (sender, args) => { if (args.Text == null) { return; } foreach (var c in args.Text) { KeyPress?.Invoke(new TextInputEventArgs(c)); } }; // // control.PointerEnter += (sender, args) => MouseEnter?.Invoke(); // control.PointerLeave += (sender, args) => MouseLeave?.Invoke(); control.PointerMoved += (sender, args) => { var cursorPos = args.GetCurrentPoint(_control); LastMouseState = MouseState; var tmpMouseState = MouseState; tmpMouseState.X = (int)cursorPos.Position.X; tmpMouseState.Y = (int)cursorPos.Position.Y; MouseState = tmpMouseState; // // MouseMove?.Invoke(new MouseMoveEventArgs(tmpMouseState.Position.X, tmpMouseState.Position.Y, 0, // 0)); }; control.PointerPressed += (sender, args) => { var cursorPos = args.GetCurrentPoint(_control); var mouseButton = AvaloniaKeyMap.MapMouseButton(cursorPos.Properties.PointerUpdateKind); LastMouseState = MouseState; var tmpMouseState = MouseState; tmpMouseState[mouseButton] = true; // MouseState = tmpMouseState; // MouseDown?.Invoke(new MouseButtonEventArgs(mouseButton, InputAction.Press, 0)); }; control.PointerReleased += (sender, args) => { var cursorPos = args.GetCurrentPoint(_control); var mouseButton = AvaloniaKeyMap.MapMouseButton(cursorPos.Properties.PointerUpdateKind); LastMouseState = MouseState; var tmpMouseState = MouseState; tmpMouseState[mouseButton] = false; // MouseState = tmpMouseState; // MouseUp?.Invoke(new MouseButtonEventArgs(mouseButton, InputAction.Release, 0)); }; //control.PointerWheelChanged += (sender, args) => MouseWheel?.Invoke(new MouseWheelEventArgs(args.)) control.GotFocus += (sender, args) => FocusedChanged?.Invoke(new FocusedChangedEventArgs(true)); control.LostFocus += (sender, args) => FocusedChanged?.Invoke(new FocusedChangedEventArgs(false)); _boundsSubscription = control.GetObservable(Visual.BoundsProperty).Subscribe(args => { var oldBounds = control.Bounds; var newBounds = args; // if (newBounds.Position != oldBounds.Position) // { // Move?.Invoke(new WindowPositionEventArgs((int) newBounds.Position.X, (int) newBounds.Position.Y)); // } if (newBounds.Size != oldBounds.Size) { Resize?.Invoke(new ResizeEventArgs((int)newBounds.Size.Width, (int)newBounds.Size.Height)); } }); _window.Closing += (sender, args) => { var c = new CancelEventArgs(false); Closing?.Invoke(c); args.Cancel = c.Cancel; }; // _windowStateSubscription = _window.GetObservable(global::Avalonia.Controls.Window.WindowStateProperty).Subscribe(args => // { // switch (args) // { // case global::Avalonia.Controls.WindowState.Minimized: // Minimized?.Invoke(new MinimizedEventArgs(true)); // break; // default: // Minimized?.Invoke(new MinimizedEventArgs(false)); // break; // } // }); if (topLevel is global::Avalonia.Controls.Window window) { window.Closing += (sender, args) => { var c = new CancelEventArgs(args.Cancel); Closing?.Invoke(c); args.Cancel = c.Cancel; } } ; }