|
6 | 6 | // |
7 | 7 |
|
8 | 8 | import SwiftUI |
| 9 | +import CodeEditSourceEditor |
9 | 10 |
|
10 | 11 | struct StatusBarCursorLocationLabel: View { |
11 | 12 | @Environment(\.controlActiveState) |
12 | 13 | private var controlActive |
13 | 14 |
|
14 | 15 | @EnvironmentObject private var model: UtilityAreaViewModel |
| 16 | + @EnvironmentObject private var editorManager: EditorManager |
| 17 | + |
| 18 | + @State private var file: CEWorkspaceFile? |
| 19 | + @State private var cursorPositions: [CursorPosition]? |
| 20 | + |
| 21 | + func updateSource() { |
| 22 | + file = editorManager.activeEditor.selectedTab |
| 23 | + } |
| 24 | + |
| 25 | + func getLines(_ range: NSRange) -> Int { |
| 26 | + if let fileDocument = file?.fileDocument { |
| 27 | + let selection = fileDocument.content[range] ?? "" |
| 28 | + let lines = selection.components(separatedBy: "\n") |
| 29 | + |
| 30 | + return lines.count |
| 31 | + } |
| 32 | + |
| 33 | + return 0 |
| 34 | + } |
| 35 | + |
| 36 | + func getLabel(_ cursorPositions: [CursorPosition]) -> String { |
| 37 | + if cursorPositions.isEmpty { |
| 38 | + return "" |
| 39 | + } |
| 40 | + |
| 41 | + if cursorPositions.count > 1 { |
| 42 | + return "\(cursorPositions.count) selected ranges" |
| 43 | + } |
| 44 | + |
| 45 | + if cursorPositions[0].range.length > 0 { |
| 46 | + let lineCount = getLines(cursorPositions[0].range) |
| 47 | + |
| 48 | + if lineCount > 1 { |
| 49 | + return "\(lineCount) lines" |
| 50 | + } |
| 51 | + |
| 52 | + return "\(cursorPositions[0].range.length) characters" |
| 53 | + } |
| 54 | + |
| 55 | + return "Line: \(cursorPositions[0].line) Col: \(cursorPositions[0].column)" |
| 56 | + } |
15 | 57 |
|
16 | 58 | var body: some View { |
17 | | - Text("Line: \(model.cursorLocation.line) Col: \(model.cursorLocation.column)") |
18 | | - .font(model.toolbarFont) |
19 | | - .foregroundColor(foregroundColor) |
20 | | - .fixedSize() |
21 | | - .lineLimit(1) |
22 | | - .onHover { isHovering($0) } |
| 59 | + Group { |
| 60 | + if let currentFile = file, let fileDocument = currentFile.fileDocument { |
| 61 | + Group { |
| 62 | + if let cursorPositions = cursorPositions { |
| 63 | + Text(getLabel(cursorPositions)) |
| 64 | + } else { |
| 65 | + EmptyView() |
| 66 | + } |
| 67 | + } |
| 68 | + .onReceive(fileDocument.$cursorPositions) { val in |
| 69 | + cursorPositions = val |
| 70 | + } |
| 71 | + } else { |
| 72 | + EmptyView() |
| 73 | + } |
| 74 | + } |
| 75 | + .font(model.toolbarFont) |
| 76 | + .foregroundColor(foregroundColor) |
| 77 | + .fixedSize() |
| 78 | + .lineLimit(1) |
| 79 | + .onHover { isHovering($0) } |
| 80 | + .onAppear { |
| 81 | + updateSource() |
| 82 | + } |
| 83 | + .onReceive(editorManager.activeEditor.objectWillChange) { _ in |
| 84 | + updateSource() |
| 85 | + } |
| 86 | + .onChange(of: editorManager.activeEditor) { _ in |
| 87 | + updateSource() |
| 88 | + } |
| 89 | + .onChange(of: editorManager.activeEditor.selectedTab) { _ in |
| 90 | + updateSource() |
| 91 | + } |
23 | 92 | } |
24 | 93 |
|
25 | 94 | private var foregroundColor: Color { |
|
0 commit comments