Prep the potatoes: Place the sliced potatoes in a large bowl of cold water and let them sit for 10 minutes to remove excess starch. Drain thoroughly and pat dry with a clean kitchen towel or paper towels. This is crucial for preventing a gloopy sauce.
Make the roux: Preheat your oven to 375°F (190°C). In a medium saucepan over medium heat, melt the butter. Whisk in the flour and cook for 1-2 minutes until golden and bubbly. Do not let it brown.
Create the base: Slowly whisk in the milk and heavy cream, ensuring no lumps remain. Add the thyme, nutmeg, salt, and pepper. Cook, stirring constantly, until the mixture thickens enough to coat the back of a spoon (about 3-5 minutes). Remove from heat.
Assemble half: In a greased 9x13-inch baking dish, arrange one-third of the potato slices in an overlapping layer. Sprinkle with one-third of the ham and onion. Pour one-third of the sauce over this layer. Repeat two more times, ending with a final layer of potatoes.
Top with cheese: Sprinkle the shredded cheddar evenly over the top layer of potatoes. If using breadcrumbs, sprinkle them over the cheese now for added texture.
Bake covered: Cover tightly with aluminum foil. Bake for 45 minutes. The foil traps steam, ensuring the potatoes become tender all the way through. Check doneness by inserting a knife into the center; it should meet little resistance.
Bake uncovered: Remove the foil and bake for an additional 15-20 minutes until the top is golden brown and bubbling. If you want more browning, switch to broil for the last 3 minutes, watching closely so the cheese doesn't burn.
Rest before serving: Let the dish rest for at least 10 minutes after removing it from the oven. This allows the sauce to set slightly, making it easier to cut clean portions without everything sliding apart.