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

TreeView selection APIs #243

Merged
merged 38 commits into from
Jul 30, 2019
Merged

TreeView selection APIs #243

merged 38 commits into from
Jul 30, 2019

Conversation

kaiguo
Copy link
Contributor

@kaiguo kaiguo commented Jan 29, 2019

Fix #197
Fix #124
Fix #386

We already have SelectedNodes to get selected TreeViewNodes in multi-selection mode.

Added

  • SelectedNode: get the selected TreeViewNode in single selection mode
  • SelectedItem: get the selected item in single selection mode
  • SelectedItems: get selected items in multi-selection mode

SelectedNode(s) always returns TreeViewNode.
SelectedItem(s) returns the ItemsSource type object when using databinding, or TreeViewNode when not using databinding.

dev/TreeView/ViewModel.cpp Outdated Show resolved Hide resolved
dev/TreeView/TreeView.cpp Outdated Show resolved Hide resolved
return nullptr;
}

if (IsContentMode())
{
return ContainerFromItem(node.Content());
}
return ContainerFromItem(node);
Copy link
Contributor

@ranjeshj ranjeshj Feb 14, 2019

Choose a reason for hiding this comment

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

nit: avoid multiple points of return if possible. I think that makes it easier to read and debug.

winrt::DependencyObject value = nullptr;

if(node)
{
value = IsContentMode() ? ContainerFromItem(node.Content()): ContainerFromItem(node);
}

return value;

same for methods below as well.

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 multiple / early returns are fine as long as the function is short enough. I think that the presented alternative is slightly harder to read (for me). But we shouldn't have coding style discussions in CR -- @chrisglein I believe this one has been discussed before but I don't see it captured in the coding style guide.

Copy link
Member

Choose a reason for hiding this comment

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

There's not a hard and fast rule on this one. Avoiding early returns altogether leads to the "arrow" pattern where you're overindented and it can be hard to determine what's validation versus real conditional. Doing too many early returns can create problems with object lifetimes/initialization and sometimes make the logic flow hard to read if the early returns have too many different outcomes.

Most of the universal pushback on early returns came from clean up patterns. Embracing RAII, smart pointers, and scope helpers have alleviated that and made error goto macros obsolete. So early returns aren't a bad word as they once were.

We have talked about this but haven't captured it in a style guide because we don't have hard and fast rules. The guideline I would push is to prefer early returns to extra condition hierarchy and indentation because an early return generally reduces the mental load reading the function. You no longer have to think about the condition that returned, as opposed to tracking the condition scopes and thinking about how that could flow.

tl;dr: I need to bring more of our code style guide over to the GitHub docs, and add some commentary on this topic.

Copy link
Contributor

Choose a reason for hiding this comment

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

For smaller functions I agree that this is less of a concern. One thing I like is that I don't have to think about the negative cases intermixed with the happy path. All the if, then's are the happy path which is simpler to parse in my brain for me. Either have a specific else to modify the state or the default is just passed along in which case I don't need to worry about the else blocks. Another is that I can put a breakpoint at the end of the method and see what the returned value is. If there are multiple returns, then you cannot do that. I'm sure there are pro's and con's on this - see this thread - https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement . Again, for small functions, I don't think it matters, for larger ones, I would lean towards single exit.

@jevansaks jevansaks added the release note PR that we want to call out in the next release summary label Apr 16, 2019
@kaiguo kaiguo requested a review from a team June 6, 2019 23:33
@jevansaks
Copy link
Member

@kaiguo can you also kick off the API spec review for this? (or have you already done that?)

@kaiguo
Copy link
Contributor Author

kaiguo commented Jun 7, 2019

@kaiguo Kai Guo FTE can you also kick off the API spec review for this? (or have you already done that?)

Not yet, creating the spec doc now...

@kaiguo
Copy link
Contributor Author

kaiguo commented Jun 7, 2019

@kaiguo Kai Guo FTE can you also kick off the API spec review for this? (or have you already done that?)

@jevansaks spec PR here, microsoft/microsoft-ui-xaml-specs#31

@kaiguo kaiguo requested a review from chrisglein June 7, 2019 23:22
@jevansaks jevansaks added the feature proposal New feature proposal label Jun 10, 2019
@msft-github-bot msft-github-bot added this to Freezer in Feature tracking Jun 10, 2019
@jevansaks jevansaks removed this from Freezer in Feature tracking Jun 27, 2019
@kaiguo kaiguo force-pushed the user/kaiguo/treeview-selecteditem branch from 6d223f6 to a2b27c4 Compare July 22, 2019 04:14
@kaiguo kaiguo force-pushed the user/kaiguo/treeview-selecteditem branch from a2b27c4 to 21683a7 Compare July 22, 2019 17:58
@kaiguo kaiguo merged commit 91f604e into master Jul 30, 2019
@kaiguo kaiguo deleted the user/kaiguo/treeview-selecteditem branch July 30, 2019 23:21
@msft-github-bot
Copy link
Collaborator

🎉Microsoft.UI.Xaml v2.2.190731001-prerelease has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TreeView feature proposal New feature proposal release note PR that we want to call out in the next release summary
Projects
No open projects
Controls Dev
  
Needs review
6 participants