Esempio n. 1
0
        public static void ActivateWorkaround(this global::Avalonia.Controls.Window window)
        {
            if (ReferenceEquals(window, null))
            {
                throw new System.ArgumentNullException(nameof(window));
            }

            // Call default Activate() anyway.
            window.Activate();

            // Skip workaround for non-windows platforms.
            if (!IsWin32NT)
            {
                return;
            }

            var platformImpl = window.PlatformImpl;

            if (ReferenceEquals(platformImpl, null))
            {
                return;
            }

            var platformHandle = platformImpl.Handle;

            if (ReferenceEquals(platformHandle, null))
            {
                return;
            }

            var handle = platformHandle.Handle;

            if (System.IntPtr.Zero == handle)
            {
                return;
            }

            try
            {
                SetForegroundWindow(handle);
            }
            catch
            {
                // ignored
            }
        }
 public AvaloniaWindow(global::Avalonia.Controls.Window window)
     : base(window)
     => _window = window;
Esempio n. 3
0
        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;
                }
            }
            ;
        }