Function to serialize tab state as session

This commit is contained in:
Kovid Goyal 2025-08-12 11:37:58 +05:30
parent e3888b6262
commit 53dbdeeca5
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 26 additions and 2 deletions

View file

@ -288,6 +288,24 @@ def serialize_state(self) -> dict[str, Any]:
'name': self.name,
}
def serialize_state_as_session(self) -> list[str]:
import shlex
self._current_layout_name
ans = [
f'new_tab {self.name}'.rstrip(),
f'layout {self._current_layout_name}',
f'enabled_layouts {",".join(self.enabled_layouts)}'
]
launch_cmds = []
for g in self.windows.iter_groups_in_activation_order():
gw: list[str] = []
for window in g:
lc = window.as_launch_command(is_overlay=bool(gw))
if lc:
gw.append(shlex.join(lc))
launch_cmds.extend(gw)
return (ans + launch_cmds) if launch_cmds else []
def active_window_changed(self) -> None:
w = self.active_window
set_active_window(self.os_window_id, self.id, 0 if w is None else w.id)

View file

@ -1943,7 +1943,7 @@ def current_mouse_position(self) -> Optional['MousePosition']:
' Return the last position at which a mouse event was received by this window '
return get_mouse_data_for_window(self.os_window_id, self.tab_id, self.id)
def as_launch_command(self) -> list[str]:
def as_launch_command(self, is_overlay: bool = False) -> list[str]:
' Return a launch command that can be used to serialize this window. Empty list indicates not serializable. '
if self.actions_on_close or self.actions_on_focus_change or self.actions_on_removal:
# such windows are typically UI kittens. The actions are not
@ -1996,7 +1996,7 @@ def as_launch_command(self) -> list[str]:
lpos = (f'{ypos}-{xpos}' if ypos else xpos) if xpos else ypos
ans.append(f'--logo-position={lpos}')
if self.overlay_parent is not None:
if is_overlay:
t = 'overlay-main' if self.overlay_type is OverlayType.main else 'overlay'
ans.append(f'--type={t}')

View file

@ -333,6 +333,12 @@ def make_previous_group_active(self, which: int = 1, notify: bool = True) -> Non
return
self.set_active_group_idx(len(self.groups) - 1, notify=notify)
def iter_groups_in_activation_order(self) -> Iterator[WindowGroup]:
imap = {gid: i for i, gid in enumerate(self.active_group_history)}
def skey(g: WindowGroup) -> int:
return imap.get(g.id, -1)
yield from sorted(self.groups, key=skey)
@property
def num_groups(self) -> int:
return len(self.groups)