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

Added more TextBox ContextMenu configuration #2758

Closed
wants to merge 13 commits into from
Closed

Added more TextBox ContextMenu configuration #2758

wants to merge 13 commits into from

Conversation

Deadpikle
Copy link
Contributor

What changed?

  • Allow for disabling cut/copy/paste in a TextBox
  • Allow for custom context menu items in a TextBox
  • Added IsCutCopyPasteInContextMenu property (defaults to true)
  • Added ExtraContextMenuItems property (defaults to null)

I'm not sure that properties were the best way to go about doing this, but I thought I'd do a pull request all the same. I'm open to better ways.

-Allow for disabling cut/copy/paste in a TextBox
-Allow for custom context menu items in a TextBox
-Added IsCutCopyPasteInContextMenu property (defaults to true)
-Added ExtraContextMenuItems property (defaults to null)
AddExtraItemsToContextMenu(tb, tb.ContextMenu.Items.Count > 0);
if (tb.ContextMenu.Items.Count == 0)
{
AddNoItemsAvailToContextMenu(tb);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we add a menu with no function behind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 😅 That was my gut reaction when an empty menu showed a small black square. I set it to hidden/visible to show/not show the menu as necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(let me know if you want me to rebase)

@@ -53,6 +56,8 @@ public class TextBoxHelper
public static readonly DependencyProperty HasTextProperty = DependencyProperty.RegisterAttached("HasText", typeof (bool), typeof (TextBoxHelper), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsRender));

public static readonly DependencyProperty IsSpellCheckContextMenuEnabledProperty = DependencyProperty.RegisterAttached("IsSpellCheckContextMenuEnabled", typeof(bool), typeof(TextBoxHelper), new FrameworkPropertyMetadata(false, UseSpellCheckContextMenuChanged));
public static readonly DependencyProperty IsCutCopyPasteInContextMenuProperty = DependencyProperty.RegisterAttached("IsCutCopyPasteInContextMenu", typeof(bool), typeof(TextBoxHelper), new FrameworkPropertyMetadata(true, IsCutCopyPasteInContextMenuChanged));
Copy link
Member

@punker76 punker76 Dec 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to rename it to UseDefaultContextMenu or AllowDefaultContextMenu...
/cc @thoemmi

Copy link
Contributor Author

@Deadpikle Deadpikle Dec 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like UseDefaultContextMenu much better than what I chose! I'll wait a day or two before changing it to wait to hear from @thoemmi .
Edit: Although UseDefaultContextMenu = False does imply that spelling corrections will be removed...should we differentiate between the spelling corrections and cut/copy/paste?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @punker76 that the naming can be improved. However, I'd would negate it (generally I think boolean properties should be false by default and you have to switch 'em on explicitly) and name it ExcludeDefaultContextMenuItems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if (extraMenuItems != null && extraMenuItems.Count > 0)
{
// Add separator if necessary, then extra items to menu
if (shouldAddSeparator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not add a default separator, cause users can't avoid this. If the user want it then he should do this itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The List<MenuItem> is now a List<FrameworkElement>.

throw new InvalidOperationException("The property 'IsCutCopyPasteInContextMenuChanged' may only be set on TextBoxBase elements.");
}

tb.SetValue(IsCutCopyPasteInContextMenuProperty, e.NewValue);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? I'd think this method is called because the IsCutCopyPasteInContextMenu was changed, so setting it again is not needed. But I may be wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thoemmi You're not wrong, this is not necessary. /cc @Deadpikle

else
{
tb.SetValue(ExtraContextMenuItemsProperty, e.NewValue);
tb.ContextMenu = GetDefaultTextBoxBaseContextMenu(tb);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating the context menu in each and every XxxChanged method seems wrong to me. I'd think that creating the actual context menu inside TextBoxBaseContextMenuOpening should be sufficient. @punker76 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thoemmi Yes the opening event should be sufficient /cc @Deadpikle So the ExtraContextMenuItemsChanged is not necessary.

@@ -53,6 +56,8 @@ public class TextBoxHelper
public static readonly DependencyProperty HasTextProperty = DependencyProperty.RegisterAttached("HasText", typeof (bool), typeof (TextBoxHelper), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsRender));

public static readonly DependencyProperty IsSpellCheckContextMenuEnabledProperty = DependencyProperty.RegisterAttached("IsSpellCheckContextMenuEnabled", typeof(bool), typeof(TextBoxHelper), new FrameworkPropertyMetadata(false, UseSpellCheckContextMenuChanged));
public static readonly DependencyProperty IsCutCopyPasteInContextMenuProperty = DependencyProperty.RegisterAttached("IsCutCopyPasteInContextMenu", typeof(bool), typeof(TextBoxHelper), new FrameworkPropertyMetadata(true, IsCutCopyPasteInContextMenuChanged));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @punker76 that the naming can be improved. However, I'd would negate it (generally I think boolean properties should be false by default and you have to switch 'em on explicitly) and name it ExcludeDefaultContextMenuItems.

@thoemmi
Copy link
Collaborator

thoemmi commented Dec 8, 2016 via email

Only add ContextMenuOpening event if not already added
@Deadpikle
Copy link
Contributor Author

@punker76 @thoemmi Sorry for the wait. Refactored the context menu creation code to a single method. I added a private property to help keep track of the ContextMenuOpening event so it isn't added more than one time.
Let me know if there are any further changes you'd like to see.

@punker76
Copy link
Member

@Deadpikle Hey, thx for this changes and your time to do this. Sorry for my lateness. I review this and had some thoughts about this and the new attached properties. And I come to the conclusion that we don't need this new properties with some other changes...
Here are my changes to solve this without adding new attached properties #2772.
I hope this is ok for you 🙈
/cc @thoemmi

@punker76 punker76 closed this Dec 20, 2016
@Deadpikle
Copy link
Contributor Author

@punker76 It's cool; I'm glad there is a solution that will work for everyone and is a lot cleaner than what I did. :)

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

Successfully merging this pull request may close these issues.

3 participants