Added more TextBox ContextMenu configuration #2758

Closed
wants to merge 13 commits into
from

Conversation

Projects
None yet
3 participants
@Deadpikle
Contributor

Deadpikle commented Dec 6, 2016

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.

Added more TextBox ContextMenu configuration
-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);

This comment has been minimized.

@punker76

punker76 Dec 7, 2016

Member

Why should we add a menu with no function behind?

@punker76

punker76 Dec 7, 2016

Member

Why should we add a menu with no function behind?

This comment has been minimized.

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

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.

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

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.

This comment has been minimized.

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

(let me know if you want me to rebase)

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

(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));

This comment has been minimized.

@punker76

punker76 Dec 7, 2016

Member

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

@punker76

punker76 Dec 7, 2016

Member

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

This comment has been minimized.

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

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?

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

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?

This comment has been minimized.

@thoemmi

thoemmi Dec 8, 2016

Collaborator

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

thoemmi Dec 8, 2016

Collaborator

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.

This comment has been minimized.

@punker76

punker76 Dec 8, 2016

Member

👍

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

This comment has been minimized.

@punker76

punker76 Dec 7, 2016

Member

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.

@punker76

punker76 Dec 7, 2016

Member

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.

This comment has been minimized.

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

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

@Deadpikle

Deadpikle Dec 7, 2016

Contributor

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);

This comment has been minimized.

@thoemmi

thoemmi Dec 8, 2016

Collaborator

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.

@thoemmi

thoemmi Dec 8, 2016

Collaborator

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.

This comment has been minimized.

@punker76

punker76 Dec 8, 2016

Member

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

@punker76

punker76 Dec 8, 2016

Member

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

+ else
+ {
+ tb.SetValue(ExtraContextMenuItemsProperty, e.NewValue);
+ tb.ContextMenu = GetDefaultTextBoxBaseContextMenu(tb);

This comment has been minimized.

@thoemmi

thoemmi Dec 8, 2016

Collaborator

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 ?

@thoemmi

thoemmi Dec 8, 2016

Collaborator

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 ?

This comment has been minimized.

@punker76

punker76 Dec 8, 2016

Member

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

@punker76

punker76 Dec 8, 2016

Member

@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));

This comment has been minimized.

@thoemmi

thoemmi Dec 8, 2016

Collaborator

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

thoemmi Dec 8, 2016

Collaborator

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

This comment has been minimized.

Show comment
Hide comment
@thoemmi

thoemmi Dec 8, 2016

Collaborator
Collaborator

thoemmi commented Dec 8, 2016

Refactor all ContextMenu code to 1 func
Only add ContextMenuOpening event if not already added
@Deadpikle

This comment has been minimized.

Show comment
Hide comment
@Deadpikle

Deadpikle Dec 13, 2016

Contributor

@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.

Contributor

Deadpikle commented Dec 13, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@punker76

punker76 Dec 20, 2016

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

Member

punker76 commented Dec 20, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@Deadpikle

Deadpikle Dec 20, 2016

Contributor

@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. :)

Contributor

Deadpikle commented Dec 20, 2016

@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