Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activate Zoombox only when a ModifierKey is pressed #66

Open
sa-he opened this issue Sep 2, 2021 · 2 comments
Open

Activate Zoombox only when a ModifierKey is pressed #66

sa-he opened this issue Sep 2, 2021 · 2 comments

Comments

@sa-he
Copy link

sa-he commented Sep 2, 2021

In my current scenario the Zoombox control contains other custom user-controls that support drag&drop and other mouse-related features.

Therefore, I'd appreciate if the Zoombox activates it’s functionality only if e.g. the Control-key is pressed. The following code-snippet demonstrates my idea. OnMouseWheel also would need to respect ActivateOn accordingly..

What do you think of this idea?

  public ModifierKeys ActivateOn
   {
       get { return (ModifierKeys)GetValue(ActivateOnProperty); }
       set { SetValue(ActivateOnProperty, value); }
   }

   public static readonly DependencyProperty ActivateOnProperty = DependencyProperty.Register(nameof(ActivateOn), typeof(ModifierKeys), typeof(Zoombox), new PropertyMetadata(ModifierKeys.None));

   /// <inheritdoc />
   protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
   {
       if (e is null)
       {
           throw new ArgumentNullException(nameof(e));
       }

       this.position = e.GetPosition(this);
       if (ActivateOn == ModifierKeys.None || Keyboard.Modifiers.HasFlag(ActivateOn))
       {
           _ = this.CaptureMouse();
       }

       base.OnMouseLeftButtonDown(e);
   }

@JohanLarsson
Copy link
Member

I get the use case, wonder if there is a simpler way.
https://github.com/GuOrg/Gu.Wpf.Geometry/blob/master/Gu.Wpf.Geometry/Zoombox.cs#L424
Not an ideal implementation for flexibility, should perhaps have been implemented using gestures.
While not awesome there is always the option to subclass ZoomBox and override and add the check.

About the idea I agree it is a solution but it does not feel very idiomatic and discoverable. Feels more like an ad-hoc solution that would be a better fit in application code.

@sa-he
Copy link
Author

sa-he commented Sep 6, 2021

Subclassing is not my prefered solution but it seems to work.

Subclassing looks like this:

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        if (ActivateOn == ModifierKeys.None || Keyboard.Modifiers.HasFlag(ActivateOn))
        {
            base.OnMouseLeftButtonDown(e);
        }
    }

Up to now, I used this Zoombox (https://github.com/xceedsoftware/wpftoolkit/wiki/Zoombox) which has several 'Modifier' properties for dragging and zooming.

Are there any other/better solutions than subclassing or modifier-keys?
Maybe events Zooming and Dragging where you can set e.Enabled = ...my condition... ? It provides more flexibility, but it is better idiomatic/discoverable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants