mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-27 20:00:51 +00:00
* feat(tabs): move a tab to its own window without reconnecting (#4201) Adds "Move Tab to New Window" to the tab context menu. Instead of opening a new connection like "Copy Tab to New Window", the tab is hidden from the tab bar and an attach popup takes over the same live PTY, so bastion sessions are not re-authenticated and no extra seat is used. Closing the popup returns the tab (with its screen state) to the window it came from. - popup lifecycle notifies the home window via terminal-popup:attach-closed - new runtime poppedOut flag; popped-out tabs still persist for restore and are not labelled as AI silent sessions in the tray * fix(tabs): keep moved tabs alive when their source window closes - A moved-tab popup registers as the lifetime heir of its session. If the source window is destroyed, ownership passes to the popup instead of the session being closed (worker and in-process paths). AI observe popups are unchanged. - If the source window is gone when the popup closes, skip the hand-back so the popup's ownership closes the session. - Only offer/allow Move Tab to New Window for connected sessions; attach mode cannot connect or reconnect a backend. * fix(tabs): expose Move Tab to New Window through TerminalHost The handler was registered but not passed into the terminal domain bag, so AppView received undefined and the menu item never rendered. * fix(tabs): address move-to-window focus, crash ownership, live count, history, and AI context review findings * fix(tabs): activate moved-tab lifetime heir only after output rebind succeeds * fix(tabs): wait for popup attachment before hiding tab and exclude plugin sessions * fix(tabs): confirm renderer reattach before resolving move and await output restore before unhiding tab * fix(tabs): preserve detached output across popup crashes and avoid focusing main window from tray * fix(tabs): deliver attach snapshots and output restore directly to peer session windows * fix(tabs): schedule automatic output restore retries when popup recovery fails * fix(tabs): handle reopen replay preservation, stale restore cancellation, startup workspace move, and shutdown log capture * fix(tabs): reassert popped-out state after tray reopen * test(terminal): provide log finalizer stubs in popup snapshot harness --------- Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
292 lines
12 KiB
TypeScript
292 lines
12 KiB
TypeScript
import { useCallback, useLayoutEffect, useMemo, useRef, useSyncExternalStore } from 'react';
|
|
|
|
import { TERMINAL_THEME_AUTO } from '../../../domain/terminalAppearance';
|
|
import { retainStableSessionsIgnoringPresentation } from '../../../domain/terminalPaneSessionsEqual';
|
|
import { getAppSettingsRuntime } from '../../state/appRuntimeBridge';
|
|
import { useAppearanceChromeStore } from '../../state/appearanceChromeStore';
|
|
import { useCustomThemes } from '../../state/customThemeStore';
|
|
import {
|
|
useSessionSnapshot,
|
|
useSessionSnapshotActions,
|
|
} from '../../state/sessionSnapshotStore';
|
|
import {
|
|
useSettingsChromeActions,
|
|
useSettingsChromeStore,
|
|
} from '../../state/settingsChromeStore';
|
|
import {
|
|
useTerminalSettingsActions,
|
|
useTerminalSettingsStore,
|
|
} from '../../state/terminalSettingsStore';
|
|
import { useThemeRuntime, useTerminalAppearanceInjection } from '../../state/useThemeRuntime';
|
|
import {
|
|
useVaultSnapshot,
|
|
} from '../../state/vaultSnapshotStore';
|
|
import { getAppHandlers, subscribeAppHandlers } from '../appHandlersBridge';
|
|
import { publishAppShellDomainSlice } from '../appShellPropsStore';
|
|
import { useAppLocalUiStore } from '../appLocalUiStore';
|
|
import { registerThemeRuntimeActions } from '../themeRuntimeBridge';
|
|
|
|
/**
|
|
* Terminal island: sessions from `sessionSnapshotStore`, terminal settings
|
|
* from `terminalSettingsStore`, mutators via snapshot actions, theme runtime
|
|
* owned here, glue handlers from the app handlers bridge. Assembles the full
|
|
* terminal domain bag field-by-field — never spreads a prepared bag.
|
|
*/
|
|
export function TerminalHost() {
|
|
const session = useSessionSnapshot();
|
|
const sessionActions = useSessionSnapshotActions();
|
|
const vault = useVaultSnapshot();
|
|
const terminalSettings = useTerminalSettingsStore();
|
|
const terminalSettingsActions = useTerminalSettingsActions();
|
|
const {
|
|
followAppTerminalTheme,
|
|
terminalThemeId,
|
|
terminalThemeDarkId,
|
|
terminalThemeLightId,
|
|
} = terminalSettings;
|
|
const settingsChrome = useSettingsChromeStore();
|
|
const settingsChromeActions = useSettingsChromeActions();
|
|
const appearance = useAppearanceChromeStore();
|
|
const customThemes = useCustomThemes();
|
|
const local = useAppLocalUiStore();
|
|
const handlers = useSyncExternalStore(
|
|
subscribeAppHandlers,
|
|
getAppHandlers,
|
|
getAppHandlers,
|
|
);
|
|
|
|
// Call-time getters: SettingsPublisher registers the runtime in a layout
|
|
// effect after this Host's first render. Capturing noop setters into
|
|
// useThemeRuntime would permanently drop follow-app UI theme persistence.
|
|
const setLightUiThemeId = useCallback((id: string) => {
|
|
getAppSettingsRuntime()?.setLightUiThemeId?.(id);
|
|
}, []);
|
|
const setDarkUiThemeId = useCallback((id: string) => {
|
|
getAppSettingsRuntime()?.setDarkUiThemeId?.(id);
|
|
}, []);
|
|
|
|
const themeRuntime = useThemeRuntime({
|
|
terminalThemeId,
|
|
terminalThemeDarkId,
|
|
terminalThemeLightId,
|
|
followAppTerminalTheme,
|
|
resolvedTheme: settingsChrome.resolvedTheme,
|
|
lightUiThemeId: settingsChrome.lightUiThemeId,
|
|
darkUiThemeId: settingsChrome.darkUiThemeId,
|
|
accentMode: appearance.accentMode,
|
|
customAccent: appearance.customAccent,
|
|
customThemes,
|
|
setTheme: settingsChromeActions.setTheme,
|
|
setLightUiThemeId,
|
|
setDarkUiThemeId,
|
|
});
|
|
|
|
const {
|
|
globalAppearance,
|
|
accentedGlobalAppearance,
|
|
clearIntent: clearThemeIntent,
|
|
settleManualIntent: settleManualThemeIntent,
|
|
pickTheme: pickTerminalTheme,
|
|
resolveFocusedAppearance,
|
|
currentTerminalTheme,
|
|
} = themeRuntime;
|
|
|
|
// Inject live accent into CSS vars without publishing accented theme identity
|
|
// into the terminal domain bag (accent drag must not rebuild AppShell).
|
|
useTerminalAppearanceInjection(accentedGlobalAppearance, {
|
|
includeChromeSurfaces: followAppTerminalTheme,
|
|
});
|
|
|
|
const prevFollowAppTerminalThemeRef = useRef(followAppTerminalTheme);
|
|
useLayoutEffect(() => {
|
|
if (prevFollowAppTerminalThemeRef.current === followAppTerminalTheme) return;
|
|
prevFollowAppTerminalThemeRef.current = followAppTerminalTheme;
|
|
clearThemeIntent();
|
|
}, [followAppTerminalTheme, clearThemeIntent]);
|
|
|
|
// Bridge exposes the stable base theme only — ChromeHost must not republish
|
|
// when accentedGlobalAppearance identity churns during color-picker drag.
|
|
const themeBridgeActions = useMemo(() => ({
|
|
clearThemeIntent,
|
|
settleManualThemeIntent,
|
|
pickTerminalTheme,
|
|
resolveFocusedAppearance: resolveFocusedAppearance as (...args: never[]) => unknown,
|
|
currentTerminalTheme,
|
|
globalAppearance,
|
|
}), [
|
|
clearThemeIntent,
|
|
currentTerminalTheme,
|
|
globalAppearance,
|
|
pickTerminalTheme,
|
|
resolveFocusedAppearance,
|
|
settleManualThemeIntent,
|
|
]);
|
|
|
|
useLayoutEffect(() => {
|
|
registerThemeRuntimeActions(themeBridgeActions);
|
|
return () => {
|
|
registerThemeRuntimeActions(null);
|
|
};
|
|
}, [themeBridgeActions]);
|
|
|
|
const sessionsForShellRef = useRef(session.sessions);
|
|
const sessionsForShell = retainStableSessionsIgnoringPresentation(
|
|
sessionsForShellRef.current,
|
|
session.sessions as never,
|
|
);
|
|
sessionsForShellRef.current = sessionsForShell as typeof session.sessions;
|
|
|
|
const hostById = useMemo(
|
|
() => new Map(vault.hosts.map((host) => [host.id, host])),
|
|
[vault.hosts],
|
|
);
|
|
|
|
const terminalHosts = useMemo(
|
|
() => (
|
|
local.ephemeralHosts.length > 0
|
|
? [...vault.hosts, ...local.ephemeralHosts]
|
|
: vault.hosts
|
|
),
|
|
[local.ephemeralHosts, vault.hosts],
|
|
);
|
|
|
|
const handleDefaultTerminalThemeChange = useCallback((themeId: string) => {
|
|
// Persist the default theme for ephemeral/manual hosts. Mode overrides
|
|
// reset to auto so the chosen theme becomes the new baseline for the
|
|
// current resolved UI mode (same behavior as pre-Host App).
|
|
terminalSettingsActions?.setTerminalThemeId(themeId);
|
|
if (settingsChrome.resolvedTheme === 'dark') {
|
|
terminalSettingsActions?.setTerminalThemeDarkId(TERMINAL_THEME_AUTO);
|
|
} else {
|
|
terminalSettingsActions?.setTerminalThemeLightId(TERMINAL_THEME_AUTO);
|
|
}
|
|
}, [settingsChrome.resolvedTheme, terminalSettingsActions]);
|
|
|
|
const handleFollowAppTerminalThemeChange = useCallback((themeId: string) => {
|
|
pickTerminalTheme(themeId);
|
|
}, [pickTerminalTheme]);
|
|
|
|
const terminalDomain = useMemo(() => {
|
|
if (!handlers) return null;
|
|
return {
|
|
addSessionToWorkspace: sessionActions?.addSessionToWorkspace,
|
|
moveWorkspacePane: sessionActions?.moveWorkspacePane,
|
|
appendHostToWorkspace: sessionActions?.appendHostToWorkspace,
|
|
appendLocalTerminalToWorkspace: sessionActions?.appendLocalTerminalToWorkspace,
|
|
clearSessionFontSizeOverride: sessionActions?.clearSessionFontSizeOverride,
|
|
closeSession: sessionActions?.closeSession,
|
|
closeTabsBatch: handlers.closeTabsBatch,
|
|
copySessionWithCurrentShell: handlers.copySessionWithCurrentShell,
|
|
copyWorkspaceWithCurrentShell: handlers.copyWorkspaceWithCurrentShell,
|
|
copySessionToNewWindowWithCurrentShell: handlers.copySessionToNewWindowWithCurrentShell,
|
|
moveSessionToNewWindow: handlers.moveSessionToNewWindow,
|
|
duplicateSessionWithCurrentShell: handlers.duplicateSessionWithCurrentShell,
|
|
closeWorkspace: sessionActions?.closeWorkspace,
|
|
createWorkspaceFromSessions: sessionActions?.createWorkspaceFromSessions,
|
|
createWorkspaceFromTargets: handlers.createWorkspaceFromTargets,
|
|
createWorkspaceWithHosts: handlers.createWorkspaceWithHosts,
|
|
currentTerminalTheme,
|
|
draggingSessionId: session.draggingSessionId,
|
|
editorWordWrap: terminalSettings.editorWordWrap,
|
|
followAppTerminalTheme: terminalSettings.followAppTerminalTheme,
|
|
clearThemeIntent,
|
|
settleManualThemeIntent,
|
|
pickTerminalTheme,
|
|
resolveSessionAppearance: resolveFocusedAppearance,
|
|
handleConnectSerial: handlers.handleConnectSerial,
|
|
handleConnectToHost: handlers.handleConnectToHost,
|
|
handleCreateLocalTerminal: handlers.handleCreateLocalTerminal,
|
|
handleDefaultTerminalThemeChange,
|
|
handleFollowAppTerminalThemeChange,
|
|
handleHotkeyAction: handlers.handleHotkeyAction,
|
|
handleSessionStatusChange: handlers.handleSessionStatusChange,
|
|
handleTerminalDataCapture: handlers.handleTerminalDataCapture,
|
|
handleUpdateHostFromTerminal: handlers.handleUpdateHostFromTerminal,
|
|
hostById,
|
|
terminalHosts,
|
|
updateTerminalHosts: handlers.updateTerminalHosts,
|
|
hotkeyScheme: terminalSettings.hotkeyScheme,
|
|
isBroadcastEnabled: sessionActions?.isBroadcastEnabled,
|
|
isComposeSendToAll: sessionActions?.isComposeSendToAll,
|
|
onToggleComposeSendTarget: sessionActions?.toggleComposeSendTarget,
|
|
isGlobalBroadcastEnabled: sessionActions?.isGlobalBroadcastEnabled,
|
|
isGlobalComposeSendToAll: sessionActions?.isGlobalComposeSendToAll,
|
|
onToggleGlobalComposeSendTarget: sessionActions?.toggleGlobalComposeSendTarget,
|
|
canUseGlobalBroadcast: sessionActions?.canUseGlobalBroadcast,
|
|
keyBindings: terminalSettings.keyBindings,
|
|
openNoteRequest: local.openNoteRequest,
|
|
portForwardingRules: local.portForwardingRules,
|
|
removeSessionFromWorkspace: sessionActions?.removeSessionFromWorkspace,
|
|
reorderWorkspaceSessions: sessionActions?.reorderWorkspaceSessions,
|
|
runSnippet: handlers.runSnippet,
|
|
sessionLogsDir: terminalSettings.sessionLogsDir,
|
|
sessionLogsEnabled: terminalSettings.sessionLogsEnabled,
|
|
sessionLogsFormat: terminalSettings.sessionLogsFormat,
|
|
sessionLogsTimestampsEnabled: terminalSettings.sessionLogsTimestampsEnabled,
|
|
sessions: sessionsForShell,
|
|
setDraggingSessionId: sessionActions?.setDraggingSessionId,
|
|
setEditorWordWrap: terminalSettingsActions?.setEditorWordWrap,
|
|
setTerminalFontFamilyId: terminalSettingsActions?.setTerminalFontFamilyId,
|
|
setTerminalFontSize: terminalSettingsActions?.setTerminalFontSize,
|
|
setWorkspaceFocusedSession: sessionActions?.setWorkspaceFocusedSession,
|
|
setWorkspaceAnchorChat: sessionActions?.setWorkspaceAnchorChat,
|
|
sftpAutoOpenSidebar: terminalSettings.sftpAutoOpenSidebar,
|
|
sftpFollowTerminalCwd: terminalSettings.sftpFollowTerminalCwd,
|
|
setSftpFollowTerminalCwd: terminalSettingsActions?.setSftpFollowTerminalCwd,
|
|
sftpAutoSync: terminalSettings.sftpAutoSync,
|
|
sftpDefaultViewMode: terminalSettings.sftpDefaultViewMode,
|
|
sftpDoubleClickBehavior: terminalSettings.sftpDoubleClickBehavior,
|
|
sftpShowHiddenFiles: terminalSettings.sftpShowHiddenFiles,
|
|
sftpUseCompressedUpload: terminalSettings.sftpUseCompressedUpload,
|
|
splitSessionWithCurrentShell: handlers.splitSessionWithCurrentShell,
|
|
sshDebugLogsEnabled: terminalSettings.sshDebugLogsEnabled,
|
|
terminalFontFamilyId: terminalSettings.terminalFontFamilyId,
|
|
terminalFontSize: terminalSettings.terminalFontSize,
|
|
terminalSettings: terminalSettings.terminalSettings,
|
|
terminalThemeId: terminalSettings.terminalThemeId,
|
|
toggleBroadcast: sessionActions?.toggleBroadcast,
|
|
toggleGlobalBroadcast: sessionActions?.toggleGlobalBroadcast,
|
|
onToggleGlobalBroadcast: sessionActions?.toggleGlobalBroadcast,
|
|
toggleScriptsSidePanelRef: handlers.toggleScriptsSidePanelRef,
|
|
toggleSidePanelRef: handlers.toggleSidePanelRef,
|
|
terminalPaneMagnificationRef: handlers.terminalPaneMagnificationRef,
|
|
sftpPaneMagnificationRef: handlers.sftpPaneMagnificationRef,
|
|
toggleWorkspaceViewMode: sessionActions?.toggleWorkspaceViewMode,
|
|
updateHostDistro: handlers.updateTerminalHostDistro,
|
|
updateSplitSizes: sessionActions?.updateSplitSizes,
|
|
updateSessionFontSize: sessionActions?.updateSessionFontSize,
|
|
updateSessionRestoreCwd: sessionActions?.updateSessionRestoreCwd,
|
|
updateSessionDynamicTitle: sessionActions?.updateSessionDynamicTitle,
|
|
updateSessionPromptHost: sessionActions?.updateSessionPromptHost,
|
|
updateSessionCodingCliProvider: sessionActions?.updateSessionCodingCliProvider,
|
|
updateTerminalSetting: terminalSettingsActions?.updateTerminalSetting,
|
|
workspaces: session.workspaces,
|
|
};
|
|
}, [
|
|
clearThemeIntent,
|
|
currentTerminalTheme,
|
|
handleDefaultTerminalThemeChange,
|
|
handleFollowAppTerminalThemeChange,
|
|
handlers,
|
|
hostById,
|
|
local.openNoteRequest,
|
|
local.portForwardingRules,
|
|
pickTerminalTheme,
|
|
resolveFocusedAppearance,
|
|
session.draggingSessionId,
|
|
session.workspaces,
|
|
sessionActions,
|
|
sessionsForShell,
|
|
settleManualThemeIntent,
|
|
terminalHosts,
|
|
terminalSettings,
|
|
terminalSettingsActions,
|
|
]);
|
|
|
|
useLayoutEffect(() => {
|
|
if (terminalDomain) publishAppShellDomainSlice('terminal', terminalDomain);
|
|
}, [terminalDomain]);
|
|
|
|
return null;
|
|
}
|