Skip to content

Commit

Permalink
File tabs now indicate when unsaved changes are present.
Browse files Browse the repository at this point in the history
  • Loading branch information
austincondiff committed Oct 8, 2023
1 parent 644742c commit e240fed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"identity" : "codeedittextview",
"kind" : "remoteSourceControl",
"location" : "https://github.com/CodeEditApp/CodeEditTextView.git",
"location" : "https://github.com/CodeEditApp/CodeEditTextView",
"state" : {
"revision" : "7f130bd50bb9eb6bacf6a42700cce571ec82bd64",
"version" : "0.6.7"
Expand Down Expand Up @@ -239,8 +239,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Wouter01/SwiftUI-WindowManagement",
"state" : {
"revision" : "03642ad06a3aa51e8284eb22146a208269cdc1ca",
"version" : "2.1.0"
"revision" : "adbebf5d7df325f3d7bf07dc832e5e162a9003f5",
"version" : "2.1.1"
}
},
{
Expand Down
2 changes: 2 additions & 0 deletions CodeEdit/Features/CodeFile/CodeFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ final class CodeFileDocument: NSDocument, ObservableObject, QLPreviewItem {

@Published var cursorPosition = (1, 1)

@Published var isDirty: Bool = false

// MARK: - NSDocument

override class var autosavesInPlace: Bool {
Expand Down
10 changes: 10 additions & 0 deletions CodeEdit/Features/CodeFile/CodeFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct CodeFileView: View {
@Environment(\.colorScheme)
private var colorScheme

@EnvironmentObject private var editorManager: EditorManager

@StateObject private var themeModel: ThemeModel = .shared

private var cancellables = [AnyCancellable]()
Expand All @@ -49,6 +51,14 @@ struct CodeFileView: View {
self.codeFile = codeFile
self.isEditable = isEditable

codeFile
.$content
.dropFirst()
.sink { _ in
codeFile.isDirty = true
}
.store(in: &cancellables)

codeFile
.$content
.dropFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate, Obs
}

@IBAction func saveDocument(_ sender: Any) {
getSelectedCodeFile()?.save(sender)
guard let codeFile = getSelectedCodeFile() else { return }
codeFile.save(sender)
codeFile.isDirty = false
workspace?.editorManager.activeEditor.temporaryTab = nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct EditorTabCloseButton: View {
var isHoveringTab: Bool
var isDragging: Bool
var closeAction: () -> Void

@Binding var closeButtonGestureActive: Bool
var isDirty: Bool = false

@Environment(\.colorScheme)
var colorScheme
Expand All @@ -22,24 +22,22 @@ struct EditorTabCloseButton: View {
var tabBarStyle

@State private var isPressingClose: Bool = false

@State private var isHoveringClose: Bool = false

let buttonSize: CGFloat = 16

var body: some View {
HStack {
HStack(alignment: .center) {
if tabBarStyle == .xcode {
Image(systemName: "xmark")
.font(.system(size: 11.5, weight: .regular, design: .rounded))
Image(systemName: isDirty && !isHoveringTab ? "circlebadge.fill" : "xmark")
.font(.system(size: isDirty && !isHoveringTab ? 9.5 : 11.5, weight: .regular, design: .rounded))
.foregroundColor(
isActive
? colorScheme == .dark ? .primary : Color(.controlAccentColor)
: .secondary
)
.padding(.top, -0.5)
} else {
Image(systemName: "xmark")
Image(systemName: isDirty && !isHoveringTab ? "circlebadge.fill" : "xmark")
.font(.system(size: 9.5, weight: .medium, design: .rounded))
}
}
Expand Down Expand Up @@ -87,7 +85,7 @@ struct EditorTabCloseButton: View {
}
.accessibilityLabel(Text("Close"))
// Only show when the mouse is hovering and there is no tab dragging.
.opacity(isHoveringTab && !isDragging ? 1 : 0)
.opacity((isHoveringTab || isDirty == true) && !isDragging ? 1 : 0)
.animation(.easeInOut(duration: 0.08), value: isHoveringTab)
.padding(.leading, 4)
}
Expand Down
9 changes: 8 additions & 1 deletion CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import Combine

struct EditorTabView: View {

Expand Down Expand Up @@ -46,6 +47,8 @@ struct EditorTabView: View {
/// By default, this value is `false`. When the root view is appeared, it turns `true`.
@State private var isAppeared: Bool = false

@State private var isDirty: Bool = false

/// The expected tab width in native tab bar style.
private var expectedWidth: CGFloat

Expand Down Expand Up @@ -165,10 +168,14 @@ struct EditorTabView: View {
isHoveringTab: isHovering,
isDragging: draggingTabId != nil || onDragTabId != nil,
closeAction: closeAction,
closeButtonGestureActive: $closeButtonGestureActive
closeButtonGestureActive: $closeButtonGestureActive,
isDirty: isDirty
)
}
.frame(maxWidth: .infinity, alignment: .leading)
.onReceive(item.fileDocument?.$isDirty.eraseToAnyPublisher() ?? Empty().eraseToAnyPublisher()) { _ in
isDirty = item.fileDocument?.isDirty ?? false
}
}
.opacity(
// Inactive states for tab bar item content.
Expand Down

0 comments on commit e240fed

Please sign in to comment.