I found that this works (as of Emacs 26.3). It gives the exact behavior specified, opening a file in the same window (and opening a directory in another), affecting mouse actions only:
(define-key dired-mode-map [mouse-2] (lambda (event)
(interactive "e")
(dired-mouse-find-file event 'find-file 'dired-other-window)))
However, I find it much more useful to open both files and directories in the same window, which is neatly simpler:
(define-key dired-mode-map [mouse-2] 'dired-mouse-find-file)
Since mouse-2
is calling dired-mouse-find-file
just with -other-window
variants passed in, and since dired-mouse-find-file
defaults to the non -other-window
functions, it seems like a clean replacement.