Terminal Work In Progress

This commit is contained in:
Hariharan Mudaliar 2026-03-04 01:46:26 +05:30
commit e59d35d89a
No known key found for this signature in database
GPG key ID: 025583EB0BE5640E
12 changed files with 570 additions and 77 deletions

View file

@ -0,0 +1,165 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import SwiftUI
import SwiftTerm
import Defaults
// MARK: - NSViewRepresentable wrapper for SwiftTerm
/// Bridges SwiftTerm's `LocalProcessTerminalView` (NSView) into SwiftUI.
///
/// Returns `TerminalManager.containerView` (a stable NSView) so that
/// SwiftUI's view lifecycle never tears down the actual terminal. The
/// real `LocalProcessTerminalView` lives as a subview of that container
/// and survives notch close/open and tab-switch cycles.
struct TerminalNSViewRepresentable: NSViewRepresentable {
@ObservedObject var terminalManager = TerminalManager.shared
func makeNSView(context: Context) -> NSView {
context.coordinator.terminalManager = terminalManager
terminalManager.ensureTerminalView(delegate: context.coordinator)
if !terminalManager.isProcessRunning {
terminalManager.startShellProcess()
}
return terminalManager.containerView
}
func updateNSView(_ nsView: NSView, context: Context) {
// Re-mount terminal if it was restarted (generation bumped).
terminalManager.ensureTerminalView(delegate: context.coordinator)
if !terminalManager.isProcessRunning {
terminalManager.startShellProcess()
}
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, LocalProcessTerminalViewDelegate {
var terminalManager: TerminalManager?
func sizeChanged(source: LocalProcessTerminalView, newCols: Int, newRows: Int) {
// Terminal resized — no action needed; SwiftTerm handles reflow.
}
func setTerminalTitle(source: LocalProcessTerminalView, title: String) {
Task { @MainActor in
terminalManager?.updateTitle(title)
}
}
func hostCurrentDirectoryUpdate(source: TerminalView, directory: String?) {
// Could be used for breadcrumbs in the future.
}
func processTerminated(source: TerminalView, exitCode: Int32?) {
Task { @MainActor in
terminalManager?.processDidTerminate(exitCode: exitCode)
}
}
}
}
// MARK: - Notch Tab View
/// Guake-style dropdown terminal tab for the notch.
/// Dynamically sizes up to half the screen height; content scrolls when the
/// terminal buffer exceeds the visible area (handled internally by SwiftTerm).
struct NotchTerminalView: View {
@ObservedObject var terminalManager = TerminalManager.shared
@EnvironmentObject var vm: DynamicIslandViewModel
@Default(.enableTerminalFeature) var enableTerminalFeature
@State private var suppressionToken = UUID()
@State private var isSuppressing = false
var body: some View {
VStack(spacing: 0) {
if enableTerminalFeature {
// Terminal header bar
HStack {
Image(systemName: "apple.terminal")
.foregroundStyle(.secondary)
.font(.system(size: 11))
Text(terminalManager.terminalTitle)
.font(.system(size: 11, weight: .medium))
.foregroundStyle(.secondary)
.lineLimit(1)
Spacer()
// Restart button
Button {
terminalManager.restartShell()
} label: {
Image(systemName: "arrow.counterclockwise")
.font(.system(size: 10))
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
.help("Restart shell")
}
.padding(.horizontal, 12)
.padding(.top, 6)
.padding(.bottom, 4)
Divider()
.padding(.horizontal, 8)
// Terminal content — .id(sessionGeneration) forces
// representable recreation only after an explicit restart.
TerminalNSViewRepresentable(terminalManager: terminalManager)
.id(terminalManager.sessionGeneration)
.clipShape(RoundedRectangle(cornerRadius: 6, style: .continuous))
.padding(.horizontal, 8)
.padding(.bottom, 8)
.padding(.top, 4)
.onHover { hovering in
updateSuppression(for: hovering)
}
} else {
// Feature disabled placeholder
VStack(spacing: 8) {
Image(systemName: "apple.terminal")
.font(.system(size: 28))
.foregroundStyle(.secondary)
Text("Terminal is disabled")
.font(.system(size: 12, weight: .medium))
.foregroundStyle(.secondary)
Text("Enable it in Settings → Terminal")
.font(.system(size: 10))
.foregroundStyle(.tertiary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.onDisappear {
updateSuppression(for: false)
}
}
private func updateSuppression(for hovering: Bool) {
guard hovering != isSuppressing else { return }
isSuppressing = hovering
vm.setScrollGestureSuppression(hovering, token: suppressionToken)
}
}

View file

@ -37,6 +37,7 @@ private enum SettingsTab: String, CaseIterable, Identifiable {
case shelf
case shortcuts
case notes
case terminal
case about
var id: String { rawValue }
@ -62,6 +63,7 @@ private enum SettingsTab: String, CaseIterable, Identifiable {
case .shelf: return "Shelf"
case .shortcuts: return "Shortcuts"
case .notes: return "Notes"
case .terminal: return "Terminal"
case .about: return "About"
}
}
@ -87,6 +89,7 @@ private enum SettingsTab: String, CaseIterable, Identifiable {
case .shelf: return "books.vertical"
case .shortcuts: return "keyboard"
case .notes: return "note.text"
case .terminal: return "apple.terminal"
case .about: return "info.circle"
}
}
@ -112,6 +115,7 @@ private enum SettingsTab: String, CaseIterable, Identifiable {
case .shelf: return .brown
case .shortcuts: return .orange
case .notes: return Color(red: 0.979, green: 0.716, blue: 0.153, opacity: 1.000)
case .terminal: return Color(red: 0.2, green: 0.8, blue: 0.4)
case .about: return .secondary
}
}
@ -421,6 +425,7 @@ struct SettingsView: View {
.downloads,
.shelf,
.shortcuts,
.terminal,
.extensions,
.about
]
@ -781,13 +786,20 @@ struct SettingsView: View {
SettingsSearchEntry(tab: .colorPicker, title: "Show Color Picker Icon", keywords: ["color icon", "toolbar"], highlightID: SettingsTab.colorPicker.highlightID(for: "Show Color Picker Icon")),
SettingsSearchEntry(tab: .colorPicker, title: "Display Mode", keywords: ["color", "list"], highlightID: SettingsTab.colorPicker.highlightID(for: "Display Mode")),
SettingsSearchEntry(tab: .colorPicker, title: "History Size", keywords: ["color history"], highlightID: SettingsTab.colorPicker.highlightID(for: "History Size")),
SettingsSearchEntry(tab: .colorPicker, title: "Show All Color Formats", keywords: ["hex", "hsl", "color formats"], highlightID: SettingsTab.colorPicker.highlightID(for: "Show All Color Formats"))
SettingsSearchEntry(tab: .colorPicker, title: "Show All Color Formats", keywords: ["hex", "hsl", "color formats"], highlightID: SettingsTab.colorPicker.highlightID(for: "Show All Color Formats")),
// Terminal
SettingsSearchEntry(tab: .terminal, title: "Enable terminal", keywords: ["terminal", "guake", "shell"], highlightID: SettingsTab.terminal.highlightID(for: "Enable terminal")),
SettingsSearchEntry(tab: .terminal, title: "Shell path", keywords: ["shell", "zsh", "bash", "terminal"], highlightID: SettingsTab.terminal.highlightID(for: "Shell path")),
SettingsSearchEntry(tab: .terminal, title: "Font size", keywords: ["terminal", "font", "text size"], highlightID: SettingsTab.terminal.highlightID(for: "Font size")),
SettingsSearchEntry(tab: .terminal, title: "Terminal opacity", keywords: ["terminal", "opacity", "transparency"], highlightID: SettingsTab.terminal.highlightID(for: "Terminal opacity")),
SettingsSearchEntry(tab: .terminal, title: "Maximum height", keywords: ["terminal", "height", "size"], highlightID: SettingsTab.terminal.highlightID(for: "Maximum height")),
]
}
private func isTabVisible(_ tab: SettingsTab) -> Bool {
switch tab {
case .timer, .stats, .clipboard, .screenAssistant, .colorPicker, .shelf, .notes:
case .timer, .stats, .clipboard, .screenAssistant, .colorPicker, .shelf, .notes, .terminal:
return !enableMinimalisticUI
default:
return true
@ -873,6 +885,10 @@ struct SettingsView: View {
SettingsForm(tab: .notes) {
NotesSettingsView()
}
case .terminal:
SettingsForm(tab: .terminal) {
TerminalSettings()
}
case .about:
if let controller = updaterController {
SettingsForm(tab: .about) {
@ -6990,3 +7006,109 @@ struct NotesSettingsView: View {
.navigationTitle("Notes")
}
}
// MARK: - Terminal Settings
struct TerminalSettings: View {
@ObservedObject var terminalManager = TerminalManager.shared
@Default(.enableTerminalFeature) var enableTerminalFeature
@Default(.terminalShellPath) var terminalShellPath
@Default(.terminalFontSize) var terminalFontSize
@Default(.terminalOpacity) var terminalOpacity
@Default(.terminalMaxHeightFraction) var terminalMaxHeightFraction
private func highlightID(_ title: String) -> String {
SettingsTab.terminal.highlightID(for: title)
}
private var formattedMaxHeight: String {
"\(Int(terminalMaxHeightFraction * 100))% of screen"
}
var body: some View {
Form {
Section {
Defaults.Toggle("Enable terminal", key: .enableTerminalFeature)
.settingsHighlight(id: highlightID("Enable terminal"))
} header: {
Text("General")
} footer: {
Text("Adds a Guake-style dropdown terminal tab. The terminal runs a shell session that persists while the notch is open.")
.font(.caption)
.foregroundStyle(.secondary)
}
if enableTerminalFeature {
Section {
HStack {
Text("Shell path")
Spacer()
TextField("", text: $terminalShellPath)
.textFieldStyle(.roundedBorder)
.frame(width: 200)
.multilineTextAlignment(.trailing)
}
.settingsHighlight(id: highlightID("Shell path"))
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("Font size")
Spacer()
Text("\(Int(terminalFontSize)) pt")
.foregroundStyle(.secondary)
.monospacedDigit()
}
Slider(value: $terminalFontSize, in: 8...24, step: 1)
.onChange(of: terminalFontSize) { _, newValue in
terminalManager.applyFontSize(newValue)
}
}
.settingsHighlight(id: highlightID("Font size"))
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("Terminal opacity")
Spacer()
Text("\(Int(terminalOpacity * 100))%")
.foregroundStyle(.secondary)
.monospacedDigit()
}
Slider(value: $terminalOpacity, in: 0.3...1.0, step: 0.05)
.onChange(of: terminalOpacity) { _, newValue in
terminalManager.applyOpacity(newValue)
}
}
.settingsHighlight(id: highlightID("Terminal opacity"))
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("Maximum height")
Spacer()
Text(formattedMaxHeight)
.foregroundStyle(.secondary)
.monospacedDigit()
}
Slider(value: $terminalMaxHeightFraction, in: 0.2...0.5, step: 0.05)
}
.settingsHighlight(id: highlightID("Maximum height"))
} header: {
Text("Appearance")
}
Section {
Button("Restart Shell") {
terminalManager.restartShell()
}
.disabled(!terminalManager.isProcessRunning)
} header: {
Text("Actions")
} footer: {
Text("Restarts the shell process. Any unsaved work in the terminal will be lost.")
.font(.caption)
.foregroundStyle(.secondary)
}
}
}
.navigationTitle("Terminal")
}
}

View file

@ -83,6 +83,9 @@ struct TabSelectionView: View {
let icon = Defaults[.enableNotes] ? "note.text" : "doc.on.clipboard"
tabsArray.append(TabModel(label: label, icon: icon, view: .notes))
}
if Defaults[.enableTerminalFeature] {
tabsArray.append(TabModel(label: "Terminal", icon: "apple.terminal", view: .terminal))
}
if extensionTabsEnabled {
for payload in extensionTabPayloads {
guard let tab = payload.descriptor.tab else { continue }