How mechanisms of habitat preference evolve and promote divergence with gene flow