File tree: properly handle clicks on nested files and directories

This is a fix for #9316. In order to make this change, I needed to merge in #9288, so that is in this PR as well.