pcbnew: Hidden layers on the same board side as the selected layer may be prioritized for selection

Description

Current Behaviour: When selecting an item in the PCB editor, it may be that an item from a different layer is selected. Take the following screenshots as an example. Only components are R1 and R2, the F.Silkscreen layer is selected, and the refdes for R2 is placed on top of the footprint for R1. When I select the silk refdes for R2, pcbnew actually selects the R1 footprint instead.

Expected behaviour: Currently selected layer should always be prioritized for selection.

Other info: Possibly related to #12483 (comment 1109310501)?

Overall layout:

image

Where I am selecting:

image

R1 footprint gets selected instead of R2 silk:

image

This is particularly jarring when hiding all layers except for the selected layer.

All other layers hidden (H hotkey / High Contrast Mode):

image

R1 is still selected:

image

Steps to reproduce

  1. See images / description

KiCad Version

Application: KiCad PCB Editor x64 on x64

Version: 7.0.10, release build

Libraries:
	wxWidgets 3.2.4
	FreeType 2.12.1
	HarfBuzz 8.2.1
	FontConfig 2.14.2
	libcurl/8.4.0-DEV Schannel zlib/1.3

Platform: Windows 10 (build 19045), 64-bit edition, 64 bit, Little endian, wxMSW

Build Info:
	Date: Dec 28 2023 21:18:15
	wxWidgets: 3.2.4 (wchar_t,wx containers)
	Boost: 1.83.0
	OCC: 7.7.1
	Curl: 8.4.0-DEV
	ngspice: 41
	Compiler: Visual C++ 1936 without C++ ABI

Build settings:
	KICAD_SPICE=ON