feat: several terminal tabs per project, with a dropdown

A directory now has a TerminalWorkspace holding a list of tabs instead of a
single session. The status bar's left side became a dropdown: it shows the
active tab and its state ("终端 2 · 运行中") and lists the other tabs (the
active one checked) plus "新建终端". New tabs take the smallest free number,
so closing one frees its name.

Only the active tab keeps a socket: switching detaches the old tab and
attaches the new one, whose screen is replayed from the server snapshot; the
shell itself keeps running. The tab's model stays in memory, so switching
back is immediate.

The tab list (ids, labels, active one) is saved per connection and directory.
On the first open the server's sessions for the directory are reconciled with
it: saved labels and order are kept for sessions that still exist, sessions
opened elsewhere (web UI) are appended in creation order, and tabs whose
session is gone are dropped. Project-action command runners are not shown as
tabs. The 0.4.2 single-session value is read as one tab.

0.4.3 (403).
This commit is contained in:
blumia committed 2026-11-22 15:23:21 +08:00
1 parent 675d19d563
commit a7bc380ac0
9 files changed
+717 -298

No files matched your search

+60 -23
View File
@@ -64,6 +64,21 @@ class Connection {
);
}
/// One remembered terminal tab: the server's session id and the local label.
class SavedTerminalTab {
final String id;
final String label;
const SavedTerminalTab(this.id, this.label);
}
/// The terminal tabs remembered for one directory, with the tab that was
/// showing.
class SavedTerminalTabs {
final List<SavedTerminalTab> tabs;
final String? activeId;
const SavedTerminalTabs(this.tabs, this.activeId);
}
class Storage {
static const _connsKey = 'openchamber.connections';
static const _installKey = 'openchamber.installId';
@@ -95,49 +110,71 @@ class Storage {
// ------------------------------------------------------------- terminals
// The last terminal session per directory, so reopening the terminal after
// an app restart can reattach to the shell the server still holds instead of
// starting another one. Ids are meaningless across servers, hence one map
// per connection.
// The terminal tabs per directory, so reopening the terminal after an app
// restart can reattach to the shells the server still holds instead of
// starting new ones. Ids are meaningless across servers, hence one map per
// connection.
static String _terminalKey(String connectionId) =>
'$_terminalPrefix$connectionId';
Future<Map<String, String>> _terminalSessions(String connectionId) async {
Future<Map<String, dynamic>> _terminalMap(String connectionId) async {
if (connectionId.isEmpty) return {};
final prefs = await SharedPreferences.getInstance();
final raw = prefs.getString(_terminalKey(connectionId));
if (raw == null || raw.isEmpty) return {};
try {
final decoded = jsonDecode(raw);
if (decoded is! Map) return {};
return {
for (final entry in decoded.entries)
if (entry.key is String && entry.value is String)
entry.key as String: entry.value as String,
};
} catch (_) {
return {};
}
if (decoded is Map) return Map<String, dynamic>.from(decoded);
} catch (_) {}
return {};
}
Future<String?> terminalSession(String connectionId, String directory) async =>
(await _terminalSessions(connectionId))[directory];
/// The tabs saved for [directory], or null when there are none. A value
/// written by 0.4.2 (a bare session id) is read as a single tab.
Future<SavedTerminalTabs?> terminalTabs(
String connectionId, String directory) async {
final entry = (await _terminalMap(connectionId))[directory];
if (entry is String && entry.isNotEmpty) {
return SavedTerminalTabs(
[SavedTerminalTab(entry, '终端 1')], entry);
}
if (entry is! Map) return null;
final rawTabs = entry['tabs'];
if (rawTabs is! List) return null;
final tabs = <SavedTerminalTab>[];
for (final raw in rawTabs) {
if (raw is! Map) continue;
final id = raw['id'];
final label = raw['label'];
if (id is! String || id.isEmpty) continue;
tabs.add(SavedTerminalTab(
id, label is String && label.trim().isNotEmpty ? label : '终端'));
}
if (tabs.isEmpty) return null;
final active = entry['active'];
return SavedTerminalTabs(tabs, active is String ? active : null);
}
Future<void> saveTerminalSession(
String connectionId, String directory, String sessionId) async {
if (connectionId.isEmpty || directory.isEmpty || sessionId.isEmpty) return;
Future<void> saveTerminalTabs(
String connectionId, String directory, SavedTerminalTabs value) async {
if (connectionId.isEmpty || directory.isEmpty) return;
final prefs = await SharedPreferences.getInstance();
final map = await _terminalSessions(connectionId);
map[directory] = sessionId;
final map = await _terminalMap(connectionId);
map[directory] = {
'tabs': [
for (final tab in value.tabs) {'id': tab.id, 'label': tab.label},
],
if (value.activeId != null) 'active': value.activeId,
};
await prefs.setString(_terminalKey(connectionId), jsonEncode(map));
}
Future<void> clearTerminalSession(
Future<void> clearTerminalTabs(
String connectionId, String directory) async {
if (connectionId.isEmpty) return;
final prefs = await SharedPreferences.getInstance();
final map = await _terminalSessions(connectionId);
final map = await _terminalMap(connectionId);
if (map.remove(directory) == null) return;
if (map.isEmpty) {
await prefs.remove(_terminalKey(connectionId));