/// <summary> /// Show the contextual menu /// </summary> private void ShowContextMenu() { if (ContextMenu != null) { var mousePosition = new Core.Desktop.Interop.Structs.Point(); NativeMethods.GetCursorPos(ref mousePosition); var activeScreen = Forms.Screen.FromPoint(new Drawing.Point(mousePosition.X, mousePosition.Y)); var screen = SystemInfoHelper.GetAllScreenInfos().Single(s => s.DeviceName == activeScreen.DeviceName); ContextMenu.Placement = PlacementMode.Absolute; ContextMenu.HorizontalOffset = (mousePosition.X / (screen.Scale / 100.0)) - 2; ContextMenu.VerticalOffset = (mousePosition.Y / (screen.Scale / 100.0)) - 2; ContextMenu.Opened += OnContextMenuOpened; ContextMenu.Closed += OnContextMenuClosed; ContextMenu.IsOpen = true; HwndSource source = (HwndSource)PresentationSource.FromVisual(ContextMenu); if (source != null && source.Handle != IntPtr.Zero) { //activate the context menu or the message window to track deactivation - otherwise, the context menu //does not close if the user clicks somewhere else. With the message window //fallback, the context menu can't receive keyboard events - should not happen though NativeMethods.SetForegroundWindow(source.Handle); } } }
private void MouseAndKeyboardHookService_MouseAction(object sender, MouseHookEventArgs e) { var mustClose = false; var activeScreen = Screen.FromPoint(new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y)); var screen = SystemInfoHelper.GetAllScreenInfos().Single(s => s.DeviceName == activeScreen.DeviceName); switch (Settings.Default.PasteBarPosition) { case PasteBarPosition.Top: if (e.Coords.Y - screen.Bounds.Top >= screen.Bounds.Bottom / 2) { if (_canCloseIfMouseMovesAway) { mustClose = true; } } else { _canCloseIfMouseMovesAway = true; } break; case PasteBarPosition.Bottom: if (e.Coords.Y - screen.Bounds.Top <= screen.Bounds.Bottom / 2) { if (_canCloseIfMouseMovesAway) { mustClose = true; } } else { _canCloseIfMouseMovesAway = true; } break; default: throw new ArgumentOutOfRangeException(); } if (mustClose) { if (_mouseAndKeyboardHookService != null) { _mouseAndKeyboardHookService.MouseAction -= MouseAndKeyboardHookService_MouseAction; } Logger.Instance.Information($"Mouse moves away from the paste bar."); var delayer = new Delayer <object>(TimeSpan.FromMilliseconds(10)); delayer.Action += (o, args) => HideBarButtonCommand?.Execute(null); delayer.ResetAndTick(); } }
/// <summary> /// Initialize a new instance of the <see cref="PasteBarWindow"/> class. /// </summary> public PasteBarWindow() { InitializeComponent(); var activeScreen = Screen.FromPoint(new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)); var screen = SystemInfoHelper.GetAllScreenInfos().Single(s => s.DeviceName == activeScreen.DeviceName); InitializePosition(screen); if (Debugger.IsAttached) { Topmost = false; } }
/// <summary> /// Display on the screen the paste bare with an animation. /// </summary> /// <param name="actionOnHidding">Action to perform when the paste bar is hidden.</param> internal void DisplayBar(Action actionOnHidding) { if (_isDisplayed) { throw new Exception("Paste bar already displayed."); } var dataContext = (PasteBarWindowViewModel)DataContext; _isDisplayed = true; _actionOnHidding = actionOnHidding; var activeScreen = Screen.FromPoint(new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)); var screen = SystemInfoHelper.GetAllScreenInfos().Single(s => s.DeviceName == activeScreen.DeviceName); InitializePosition(screen); InitializeStoryboards(screen); Messenger.Default.Register <ComponentModel.Messages.Message>(this, MessageIdentifiers.HidePasteBarWindow, HidePasteBarWindow); Show(); Activate(); Focus(); var firstFocus = VisualHelper.FindVisualChildren <System.Windows.Controls.ListBox>(this).FirstOrDefault(); firstFocus?.Focus(); Keyboard.Focus(firstFocus); Logger.Instance.Information("Paste bar displayed."); var delayer = new Delayer <object>(TimeSpan.FromMilliseconds(50)); delayer.Action += (o, args) => _openingStoryboard.Begin(); delayer.ResetAndTick(); dataContext.DisplayBar(); }
private void MouseAndKeyboardHookService_MouseAction(object sender, MouseHookEventArgs e) { switch (Settings.Default.PasteBarPosition) { case PasteBarPosition.Top: if (e.WheelAction == MouseWheelAction.WheelDown) { var activeScreen = Screen.FromPoint(new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)); var screen = SystemInfoHelper.GetAllScreenInfos().Single(s => s.DeviceName == activeScreen.DeviceName); if (e.Coords.Y <= screen.Bounds.Top + 5 && PasteCommand.CanExecute(null)) { e.Handled = true; Logger.Instance.Information($"Mouse gesture detected at the top of the screen."); _delayedPasteCommand.ResetAndTick(); } } break; case PasteBarPosition.Bottom: if (e.WheelAction == MouseWheelAction.WheelUp) { var activeScreen = Screen.FromPoint(new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)); var screen = SystemInfoHelper.GetAllScreenInfos().Single(s => s.DeviceName == activeScreen.DeviceName); if (e.Coords.Y >= screen.Bounds.Bottom + screen.Bounds.Top - 5 && PasteCommand.CanExecute(null)) { e.Handled = true; Logger.Instance.Information($"Mouse gesture detected at the bottom of the screen."); _delayedPasteCommand.ResetAndTick(); } } break; default: throw new ArgumentOutOfRangeException(); } }