Repository: terminal
Issue: megathread: Window State Persistence
Labels: Product-Terminal,Issue-Scenario,Area-Remoting

##### [Original thread: #766] [Spec: {none}] [Initial PR: #10972] [Multiple windows PR: #11083]

This thread is being used to track all the component work for restoring the window state. Additionally related threads:

* #766 - **Restore the layout, position of windows** 
  * This is partially implemented in #10972
  * #11083 handles this for multiple windows.
* #961 - Restore the contents of the buffer when resuming a tab.
  * This is not about restoring the process state entirely, since that's impossible. Simply the text content is good enough.
  * Stashing this in JSON doesn't _really_ make sense - imagine `SHRT_MAX` lines of text + attributes in a json string.
* #960 - **Re-open a recently closed tab**
  * This means we shouldn't just discard the terminal control when closing a pane/tab. This is similar to the <kbd>ctrl+shift+t</kbd> shortcut in (Chromium) browsers
  * Likely best to wait for #1256 and #5000 to sort themselves out before starting this one.
* #8841 and  #8812 need to merge, so we can properly reflect all these options in the SUI.
* #11081 
* #6362

<details>
<summary>
Settings UI Mockup (programmer art warning)
</summary>

![mockup](https://user-images.githubusercontent.com/18356694/130969227-73eca7ec-3f9b-4c27-be2d-cb68c6ba8f08.png)

</details>

### 2.0 Bugs

_these are all presuming that #10972 merges basically as is_

* [x] We should wrap this feature up in a Preview-only feature flag (no external community member should have to deal with that)
* [x] Opening an elevated window will automatically re-open all the unelevated window state (as elevated!). This is because there are separate monarchs for elevated and unelevated windows.
  - Elevated windows should probably just participate at all in persist/restore for now. That's okay. We may revisit after https://github.com/microsoft/terminal/projects/5#card-67023745
* [x] How do we persist something like `wt -- cmd.exe`? That won't have a profile, only a `NewTerminalArgs`, that we don't cache at runtime
  - We've mentioned in team syncs before that we may need to keep the `NewTerminalArgs` around when spawning a 
* [ ] Same idea with a defterm connection - We won't be able to restore defterm panes _at all_, because we have no idea what profile they were. Right?
* [x] How exactly does this work with the `_quake` window? Should that participate in the session restore? Probably, yea. 
* [x] Window _names_ need to be persisted as well. 
* [x] Closing the last window with `ClosePane` will clear out any persisted layout. 
  - This is by design
* [x] Similarly with closing the last tab by clicking the `x` on the tab itself. Doesn't seem to save the window layout in that case.
  - This is by design 
* [x] If you have a profile with a `background`, then the colorscheme will blow away the profile's background on restore.
  - fixed in #11083 
* [x] Terminal doesn't remember maximized, focus, fullscreen states #11426
* [ ] #11639
* [x] #11878
* [ ] center on launch overrides persisted location
* [ ] #12902
* [ ] #12633
* [ ] The size of a focus mode window is wrong after a session restore. I believe this is actually the same root cause as:
  - #10730
  - as also noted in https://github.com/microsoft/terminal/pull/17066#discussion_r1567425547