Commit fe955aa1d5
Verified · cmc
Layout: unified · split
Sources/Orgstar/BufferTabs.swift added +80
| @@ -0,0 +1,80 @@ | ||
| 1 | import AppKit | |
| 2 | import OrgApp | |
| 3 | import SwiftUI | |
| 4 | ||
| 5 | /// A divider that resizes the pane before it by dragging. An `HSplitView` inside the window's | |
| 6 | /// split view loops on its size constraints in short windows, so this sets a width instead. | |
| 7 | struct PaneDivider: View { | |
| 8 | @Binding var width: Double | |
| 9 | let range: ClosedRange<Double> | |
| 10 | @State private var start: Double? | |
| 11 | ||
| 12 | var body: some View { | |
| 13 | Rectangle() | |
| 14 | .fill(Color(nsColor: .separatorColor)) | |
| 15 | .frame(width: 1) | |
| 16 | .frame(maxHeight: .infinity) | |
| 17 | .overlay(Color.clear.frame(width: 7).contentShape(Rectangle()).onHover { inside in | |
| 18 | if inside { NSCursor.resizeLeftRight.push() } else { NSCursor.pop() } | |
| 19 | } | |
| 20 | .gesture(DragGesture(minimumDistance: 1).onChanged { drag in | |
| 21 | let base = start ?? width | |
| 22 | start = base | |
| 23 | width = min(max(base + drag.translation.width, range.lowerBound), range.upperBound) | |
| 24 | }.onEnded { _ in start = nil })) | |
| 25 | .accessibilityHidden(true) | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | /// A tab for each open buffer: click to show it, × to close it, a dot for unsaved edits. | |
| 30 | struct BufferTabs: View { | |
| 31 | let session: DocumentSession | |
| 32 | @Environment(\.orgTheme) private var theme | |
| 33 | ||
| 34 | var body: some View { | |
| 35 | ScrollView(.horizontal, showsIndicators: false) { | |
| 36 | HStack(spacing: 0) { | |
| 37 | ForEach(session.openPaths, id: \.self) { path in | |
| 38 | tab(path) | |
| 39 | } | |
| 40 | } | |
| 41 | } | |
| 42 | .frame(height: 28) | |
| 43 | .background { Rectangle().fill(theme.color("modeline-background").map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar)) } | |
| 44 | .overlay(alignment: .bottom) { Divider() } | |
| 45 | } | |
| 46 | ||
| 47 | private func tab(_ path: String) -> some View { | |
| 48 | let current = path == session.path | |
| 49 | return HStack(spacing: 6) { | |
| 50 | Text(session.bufferName(path)) | |
| 51 | .lineLimit(1) | |
| 52 | .foregroundStyle(current ? .primary : .secondary) | |
| 53 | .accessibilityAddTraits(current ? [.isButton, .isSelected] : .isButton) | |
| 54 | .accessibilityAction { session.open(path) } | |
| 55 | Button { | |
| 56 | if path == session.path { | |
| 57 | if UnsavedChanges.resolve(session) { session.kill(path) } | |
| 58 | } else if UnsavedChanges.resolve(session, paths: [path]) { | |
| 59 | session.kill(path) | |
| 60 | } | |
| 61 | } label: { | |
| 62 | Image(systemName: session.dirtyPaths.contains(path) ? "circle.fill" : "xmark") | |
| 63 | .font(.system(size: session.dirtyPaths.contains(path) ? 6 : 9, weight: .semibold)) | |
| 64 | .frame(width: 12, height: 12) | |
| 65 | } | |
| 66 | .buttonStyle(.borderless) | |
| 67 | .help("Close") | |
| 68 | .accessibilityLabel("Close \(session.bufferName(path))" + (session.dirtyPaths.contains(path) ? ", unsaved" : "")) | |
| 69 | } | |
| 70 | .font(.callout) | |
| 71 | .padding(.horizontal, 12) | |
| 72 | .frame(maxHeight: .infinity) | |
| 73 | // A shape, not a color: a color background runs up under the toolbar. | |
| 74 | .background { Rectangle().fill(current ? theme.color("background") ?? Color(nsColor: .textBackgroundColor) : .clear) } | |
| 75 | .overlay(alignment: .trailing) { Divider() } | |
| 76 | .contentShape(Rectangle()) | |
| 77 | .onTapGesture { session.open(path) } | |
| 78 | .help(path) | |
| 79 | } | |
| 80 | } | |
Sources/Orgstar/ContentView.swift −310
| @@ -794,313 +794,3 @@ struct ContentView: View { | ||
| 794 | 794 | Binding(get: { workspace.lastError != nil || session.lastError != nil }, set: { _ in }) |
| 795 | 795 | } |
| 796 | 796 | } |
| 797 | ||
| 798 | struct Sidebar: View { | |
| 799 | let workspace: WorkspaceModel | |
| 800 | let session: DocumentSession | |
| 801 | @Binding var selection: String? | |
| 802 | var addFolder: () -> Void = {} | |
| 803 | /// Folders shown collapsed, by path, one per line. | |
| 804 | @AppStorage("collapsedFolders") private var collapsedText = "" | |
| 805 | @State private var removing: String? | |
| 806 | /// A file operation waiting for a name or a confirmation. | |
| 807 | @Environment(\.orgTheme) private var theme | |
| 808 | @State var pending: FileOperation? | |
| 809 | @State var name = "" | |
| 810 | @State var failure: String? | |
| 811 | ||
| 812 | private var collapsed: Set<String> { Set(collapsedText.split(separator: "\n").map(String.init)) } | |
| 813 | ||
| 814 | private func expanded(_ root: String) -> Binding<Bool> { | |
| 815 | Binding { | |
| 816 | !collapsed.contains(root) | |
| 817 | } set: { isExpanded in | |
| 818 | var set = collapsed | |
| 819 | if isExpanded { set.remove(root) } else { set.insert(root) } | |
| 820 | collapsedText = set.sorted().joined(separator: "\n") | |
| 821 | } | |
| 822 | } | |
| 823 | ||
| 824 | var body: some View { | |
| 825 | List(selection: $selection) { | |
| 826 | ForEach(workspace.rootPaths, id: \.self) { root in | |
| 827 | Section(isExpanded: expanded(root)) { | |
| 828 | OutlineGroup(workspace.tree(forRoot: root).children ?? [], children: \.children) { node in | |
| 829 | Label(node.name, systemImage: icon(node)) | |
| 830 | .foregroundStyle(node.file?.kind == .conflict ? .orange : node.isDownloading ? .secondary : theme.color("sidebar-foreground") ?? .primary) | |
| 831 | .help(node.isDownloading ? "Downloading from iCloud" : "") | |
| 832 | .tag(node.file?.path) | |
| 833 | .contextMenu { fileMenu(node) } | |
| 834 | } | |
| 835 | } header: { | |
| 836 | Text((root as NSString).lastPathComponent) | |
| 837 | .foregroundStyle(theme.color("sidebar-header") ?? .secondary) | |
| 838 | .help(root) | |
| 839 | .contextMenu { folderMenu(root) } | |
| 840 | } | |
| 841 | } | |
| 842 | } | |
| 843 | .listStyle(.sidebar) | |
| 844 | .themedListBackground(theme.color("sidebar-background")) | |
| 845 | .safeAreaInset(edge: .bottom, spacing: 0) { | |
| 846 | HStack(spacing: 4) { | |
| 847 | Button("Add Folder…", systemImage: "plus", action: addFolder) | |
| 848 | .help("Add a folder (⇧⌘O)") | |
| 849 | Menu { | |
| 850 | ForEach(workspace.rootPaths, id: \.self) { root in | |
| 851 | Button((root as NSString).lastPathComponent) { removing = root } | |
| 852 | } | |
| 853 | } label: { | |
| 854 | Label("Remove Folder", systemImage: "minus") | |
| 855 | } | |
| 856 | .menuIndicator(.hidden) | |
| 857 | .fixedSize() | |
| 858 | .disabled(workspace.rootPaths.isEmpty) | |
| 859 | .help("Remove a folder from the sidebar") | |
| 860 | Spacer() | |
| 861 | } | |
| 862 | .labelStyle(.iconOnly) | |
| 863 | .buttonStyle(.borderless) | |
| 864 | .menuStyle(.borderlessButton) | |
| 865 | .padding(.horizontal, 10) | |
| 866 | .padding(.vertical, 6) | |
| 867 | } | |
| 868 | .confirmationDialog( | |
| 869 | "Remove “\(removing.map { ($0 as NSString).lastPathComponent } ?? "")” from the sidebar?", | |
| 870 | isPresented: Binding { removing != nil } set: { if !$0 { removing = nil } } | |
| 871 | ) { | |
| 872 | Button("Remove") { | |
| 873 | if let root = removing { try? workspace.removeRoot(path: root) } | |
| 874 | removing = nil | |
| 875 | } | |
| 876 | } message: { | |
| 877 | Text("The folder and its files stay on disk.") | |
| 878 | } | |
| 879 | .fileOperations(self) | |
| 880 | } | |
| 881 | ||
| 882 | @ViewBuilder | |
| 883 | private func folderMenu(_ root: String) -> some View { | |
| 884 | Button("New File…") { ask(.create(folder: root)) } | |
| 885 | Divider() | |
| 886 | Button(collapsed.contains(root) ? "Expand" : "Collapse") { expanded(root).wrappedValue.toggle() } | |
| 887 | Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: root)]) } | |
| 888 | Divider() | |
| 889 | Button("Remove from Sidebar…") { removing = root } | |
| 890 | } | |
| 891 | ||
| 892 | private func icon(_ node: FileTreeNode) -> String { | |
| 893 | if node.isDownloading { return "icloud.and.arrow.down" } | |
| 894 | guard let file = node.file else { return "folder" } | |
| 895 | switch file.kind { | |
| 896 | case .org: return "doc.text" | |
| 897 | case .archive: return "archivebox" | |
| 898 | case .conflict: return "exclamationmark.triangle" | |
| 899 | case .other: | |
| 900 | guard let type = UTType(filenameExtension: (file.path as NSString).pathExtension) else { return "doc" } | |
| 901 | if type.conforms(to: .image) { return "photo" } | |
| 902 | if type.conforms(to: .pdf) { return "doc.richtext" } | |
| 903 | if type.conforms(to: .audiovisualContent) { return "play.rectangle" } | |
| 904 | if type.conforms(to: .sourceCode) || type.conforms(to: .script) { return "chevron.left.forwardslash.chevron.right" } | |
| 905 | if type.conforms(to: .text) { return "doc.plaintext" } | |
| 906 | return "doc" | |
| 907 | } | |
| 908 | } | |
| 909 | } | |
| 910 | ||
| 911 | struct OutlineColumn: View { | |
| 912 | let session: DocumentSession | |
| 913 | @Environment(\.orgTheme) private var theme | |
| 914 | ||
| 915 | var body: some View { | |
| 916 | List(Array(session.outline.enumerated()), id: \.element.start) { _, entry in | |
| 917 | Button { | |
| 918 | session.reveal(entry.start) | |
| 919 | } label: { | |
| 920 | Text(entry.title.isEmpty ? "(untitled)" : entry.title) | |
| 921 | .foregroundStyle(theme.color("sidebar-foreground") ?? .primary) | |
| 922 | .lineLimit(1) | |
| 923 | .padding(.leading, CGFloat(entry.level - 1) * 12) | |
| 924 | } | |
| 925 | .buttonStyle(.plain) | |
| 926 | } | |
| 927 | .themedListBackground(theme.color("sidebar-background")) | |
| 928 | .overlay { | |
| 929 | if session.path != nil, !session.isOrg { | |
| 930 | ContentUnavailableView("No outline", systemImage: "doc.plaintext", description: Text("Only org files have an outline.")) | |
| 931 | } else if session.path != nil, session.outline.isEmpty { | |
| 932 | ContentUnavailableView("No headings", systemImage: "list.bullet.indent") | |
| 933 | } | |
| 934 | } | |
| 935 | } | |
| 936 | } | |
| 937 | ||
| 938 | struct SearchResults: View { | |
| 939 | let results: [HeadingLocation] | |
| 940 | let open: (HeadingLocation) -> Void | |
| 941 | ||
| 942 | var body: some View { | |
| 943 | List(results, id: \.self) { location in | |
| 944 | Button { | |
| 945 | open(location) | |
| 946 | } label: { | |
| 947 | VStack(alignment: .leading, spacing: 2) { | |
| 948 | Text(location.title.isEmpty ? "(untitled)" : location.title).lineLimit(1) | |
| 949 | Text((location.path as NSString).lastPathComponent).font(.caption).foregroundStyle(.secondary) | |
| 950 | } | |
| 951 | } | |
| 952 | .buttonStyle(.plain) | |
| 953 | } | |
| 954 | .overlay { | |
| 955 | if results.isEmpty { ContentUnavailableView.search } | |
| 956 | } | |
| 957 | } | |
| 958 | } | |
| 959 | ||
| 960 | struct QuickOpenView: View { | |
| 961 | let workspace: WorkspaceModel | |
| 962 | let open: (FileEntry) -> Void | |
| 963 | @State private var query = "" | |
| 964 | /// The highlighted match, moved by ↑/↓ or C-p/C-n and opened by Return. | |
| 965 | @State private var selected = 0 | |
| 966 | @Environment(\.dismiss) private var dismiss | |
| 967 | ||
| 968 | var body: some View { | |
| 969 | let matches = workspace.quickOpen(query) | |
| 970 | let current = min(selected, matches.count - 1) | |
| 971 | VStack(spacing: 0) { | |
| 972 | TextField("Open file", text: $query) | |
| 973 | .textFieldStyle(.plain) | |
| 974 | .font(.title3) | |
| 975 | .padding(12) | |
| 976 | .onSubmit { if current >= 0 { open(matches[current]) } } | |
| 977 | .onKeyPress(phases: .down) { press in | |
| 978 | let control = press.modifiers == .control | |
| 979 | let step = if press.key == .downArrow || control && press.characters == "n" { 1 } | |
| 980 | else if press.key == .upArrow || control && press.characters == "p" { -1 } else { 0 } | |
| 981 | guard step != 0 else { return .ignored } | |
| 982 | selected = max(0, min(current + step, matches.count - 1)) | |
| 983 | return .handled | |
| 984 | } | |
| 985 | .onChange(of: query) { selected = 0 } | |
| 986 | Divider() | |
| 987 | ScrollViewReader { proxy in | |
| 988 | List(matches.indices, id: \.self) { index in | |
| 989 | let entry = matches[index] | |
| 990 | Button { | |
| 991 | open(entry) | |
| 992 | } label: { | |
| 993 | VStack(alignment: .leading, spacing: 2) { | |
| 994 | Text(entry.name) | |
| 995 | Text(entry.relativePath).font(.caption).foregroundStyle(.secondary) | |
| 996 | } | |
| 997 | .frame(maxWidth: .infinity, alignment: .leading) | |
| 998 | .contentShape(Rectangle()) | |
| 999 | } | |
| 1000 | .buttonStyle(.plain) | |
| 1001 | .listRowBackground(index == current ? Color.accentColor.opacity(0.25) : Color.clear) | |
| 1002 | .accessibilityAddTraits(index == current ? .isSelected : []) | |
| 1003 | } | |
| 1004 | .onChange(of: current) { if current >= 0 { proxy.scrollTo(current) } } | |
| 1005 | } | |
| 1006 | } | |
| 1007 | .frame(width: 520, height: 360) | |
| 1008 | .onExitCommand { dismiss() } | |
| 1009 | } | |
| 1010 | } | |
| 1011 | ||
| 1012 | /// A divider that resizes the pane before it by dragging. An `HSplitView` inside the window's | |
| 1013 | /// split view loops on its size constraints in short windows, so this sets a width instead. | |
| 1014 | struct PaneDivider: View { | |
| 1015 | @Binding var width: Double | |
| 1016 | let range: ClosedRange<Double> | |
| 1017 | @State private var start: Double? | |
| 1018 | ||
| 1019 | var body: some View { | |
| 1020 | Rectangle() | |
| 1021 | .fill(Color(nsColor: .separatorColor)) | |
| 1022 | .frame(width: 1) | |
| 1023 | .frame(maxHeight: .infinity) | |
| 1024 | .overlay(Color.clear.frame(width: 7).contentShape(Rectangle()).onHover { inside in | |
| 1025 | if inside { NSCursor.resizeLeftRight.push() } else { NSCursor.pop() } | |
| 1026 | } | |
| 1027 | .gesture(DragGesture(minimumDistance: 1).onChanged { drag in | |
| 1028 | let base = start ?? width | |
| 1029 | start = base | |
| 1030 | width = min(max(base + drag.translation.width, range.lowerBound), range.upperBound) | |
| 1031 | }.onEnded { _ in start = nil })) | |
| 1032 | .accessibilityHidden(true) | |
| 1033 | } | |
| 1034 | } | |
| 1035 | ||
| 1036 | /// A tab for each open buffer: click to show it, × to close it, a dot for unsaved edits. | |
| 1037 | struct BufferTabs: View { | |
| 1038 | let session: DocumentSession | |
| 1039 | @Environment(\.orgTheme) private var theme | |
| 1040 | ||
| 1041 | var body: some View { | |
| 1042 | ScrollView(.horizontal, showsIndicators: false) { | |
| 1043 | HStack(spacing: 0) { | |
| 1044 | ForEach(session.openPaths, id: \.self) { path in | |
| 1045 | tab(path) | |
| 1046 | } | |
| 1047 | } | |
| 1048 | } | |
| 1049 | .frame(height: 28) | |
| 1050 | .background { Rectangle().fill(theme.color("modeline-background").map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar)) } | |
| 1051 | .overlay(alignment: .bottom) { Divider() } | |
| 1052 | } | |
| 1053 | ||
| 1054 | private func tab(_ path: String) -> some View { | |
| 1055 | let current = path == session.path | |
| 1056 | return HStack(spacing: 6) { | |
| 1057 | Text(session.bufferName(path)) | |
| 1058 | .lineLimit(1) | |
| 1059 | .foregroundStyle(current ? .primary : .secondary) | |
| 1060 | .accessibilityAddTraits(current ? [.isButton, .isSelected] : .isButton) | |
| 1061 | .accessibilityAction { session.open(path) } | |
| 1062 | Button { | |
| 1063 | if path == session.path { | |
| 1064 | if UnsavedChanges.resolve(session) { session.kill(path) } | |
| 1065 | } else if UnsavedChanges.resolve(session, paths: [path]) { | |
| 1066 | session.kill(path) | |
| 1067 | } | |
| 1068 | } label: { | |
| 1069 | Image(systemName: session.dirtyPaths.contains(path) ? "circle.fill" : "xmark") | |
| 1070 | .font(.system(size: session.dirtyPaths.contains(path) ? 6 : 9, weight: .semibold)) | |
| 1071 | .frame(width: 12, height: 12) | |
| 1072 | } | |
| 1073 | .buttonStyle(.borderless) | |
| 1074 | .help("Close") | |
| 1075 | .accessibilityLabel("Close \(session.bufferName(path))" + (session.dirtyPaths.contains(path) ? ", unsaved" : "")) | |
| 1076 | } | |
| 1077 | .font(.callout) | |
| 1078 | .padding(.horizontal, 12) | |
| 1079 | .frame(maxHeight: .infinity) | |
| 1080 | // A shape, not a color: a color background runs up under the toolbar. | |
| 1081 | .background { Rectangle().fill(current ? theme.color("background") ?? Color(nsColor: .textBackgroundColor) : .clear) } | |
| 1082 | .overlay(alignment: .trailing) { Divider() } | |
| 1083 | .contentShape(Rectangle()) | |
| 1084 | .onTapGesture { session.open(path) } | |
| 1085 | .help(path) | |
| 1086 | } | |
| 1087 | } | |
| 1088 | ||
| 1089 | enum Panel: Identifiable { | |
| 1090 | case columns(UUID, ColumnView.View) | |
| 1091 | case export(UUID, URL) | |
| 1092 | case conflict(UUID) | |
| 1093 | case syncCopies(UUID) | |
| 1094 | case recovery(UUID) | |
| 1095 | ||
| 1096 | var id: UUID { | |
| 1097 | switch self { | |
| 1098 | case .columns(let id, _), .export(let id, _), .conflict(let id), .syncCopies(let id), .recovery(let id): id | |
| 1099 | } | |
| 1100 | } | |
| 1101 | } | |
| 1102 | ||
| 1103 | struct BlockEditRequest: Identifiable { | |
| 1104 | let id = UUID() | |
| 1105 | let edit: BlockEdit | |
| 1106 | } | |
Sources/Orgstar/Panels.swift added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | ||
| 4 | enum Panel: Identifiable { | |
| 5 | case columns(UUID, ColumnView.View) | |
| 6 | case export(UUID, URL) | |
| 7 | case conflict(UUID) | |
| 8 | case syncCopies(UUID) | |
| 9 | case recovery(UUID) | |
| 10 | ||
| 11 | var id: UUID { | |
| 12 | switch self { | |
| 13 | case .columns(let id, _), .export(let id, _), .conflict(let id), .syncCopies(let id), .recovery(let id): id | |
| 14 | } | |
| 15 | } | |
| 16 | } | |
| 17 | ||
| 18 | struct BlockEditRequest: Identifiable { | |
| 19 | let id = UUID() | |
| 20 | let edit: BlockEdit | |
| 21 | } | |
Sources/Orgstar/SearchViews.swift added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | import AppKit | |
| 2 | import OrgApp | |
| 3 | import OrgIndex | |
| 4 | import OrgWorkspace | |
| 5 | import SwiftUI | |
| 6 | ||
| 7 | struct SearchResults: View { | |
| 8 | let results: [HeadingLocation] | |
| 9 | let open: (HeadingLocation) -> Void | |
| 10 | ||
| 11 | var body: some View { | |
| 12 | List(results, id: \.self) { location in | |
| 13 | Button { | |
| 14 | open(location) | |
| 15 | } label: { | |
| 16 | VStack(alignment: .leading, spacing: 2) { | |
| 17 | Text(location.title.isEmpty ? "(untitled)" : location.title).lineLimit(1) | |
| 18 | Text((location.path as NSString).lastPathComponent).font(.caption).foregroundStyle(.secondary) | |
| 19 | } | |
| 20 | } | |
| 21 | .buttonStyle(.plain) | |
| 22 | } | |
| 23 | .overlay { | |
| 24 | if results.isEmpty { ContentUnavailableView.search } | |
| 25 | } | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | struct QuickOpenView: View { | |
| 30 | let workspace: WorkspaceModel | |
| 31 | let open: (FileEntry) -> Void | |
| 32 | @State private var query = "" | |
| 33 | /// The highlighted match, moved by ↑/↓ or C-p/C-n and opened by Return. | |
| 34 | @State private var selected = 0 | |
| 35 | @Environment(\.dismiss) private var dismiss | |
| 36 | ||
| 37 | var body: some View { | |
| 38 | let matches = workspace.quickOpen(query) | |
| 39 | let current = min(selected, matches.count - 1) | |
| 40 | VStack(spacing: 0) { | |
| 41 | TextField("Open file", text: $query) | |
| 42 | .textFieldStyle(.plain) | |
| 43 | .font(.title3) | |
| 44 | .padding(12) | |
| 45 | .onSubmit { if current >= 0 { open(matches[current]) } } | |
| 46 | .onKeyPress(phases: .down) { press in | |
| 47 | let control = press.modifiers == .control | |
| 48 | let step = if press.key == .downArrow || control && press.characters == "n" { 1 } | |
| 49 | else if press.key == .upArrow || control && press.characters == "p" { -1 } else { 0 } | |
| 50 | guard step != 0 else { return .ignored } | |
| 51 | selected = max(0, min(current + step, matches.count - 1)) | |
| 52 | return .handled | |
| 53 | } | |
| 54 | .onChange(of: query) { selected = 0 } | |
| 55 | Divider() | |
| 56 | ScrollViewReader { proxy in | |
| 57 | List(matches.indices, id: \.self) { index in | |
| 58 | let entry = matches[index] | |
| 59 | Button { | |
| 60 | open(entry) | |
| 61 | } label: { | |
| 62 | VStack(alignment: .leading, spacing: 2) { | |
| 63 | Text(entry.name) | |
| 64 | Text(entry.relativePath).font(.caption).foregroundStyle(.secondary) | |
| 65 | } | |
| 66 | .frame(maxWidth: .infinity, alignment: .leading) | |
| 67 | .contentShape(Rectangle()) | |
| 68 | } | |
| 69 | .buttonStyle(.plain) | |
| 70 | .listRowBackground(index == current ? Color.accentColor.opacity(0.25) : Color.clear) | |
| 71 | .accessibilityAddTraits(index == current ? .isSelected : []) | |
| 72 | } | |
| 73 | .onChange(of: current) { if current >= 0 { proxy.scrollTo(current) } } | |
| 74 | } | |
| 75 | } | |
| 76 | .frame(width: 520, height: 360) | |
| 77 | .onExitCommand { dismiss() } | |
| 78 | } | |
| 79 | } | |
Sources/Orgstar/Sidebar.swift added +145
| @@ -0,0 +1,145 @@ | ||
| 1 | import OrgApp | |
| 2 | import OrgCore | |
| 3 | import OrgWorkspace | |
| 4 | import SwiftUI | |
| 5 | import UniformTypeIdentifiers | |
| 6 | ||
| 7 | struct Sidebar: View { | |
| 8 | let workspace: WorkspaceModel | |
| 9 | let session: DocumentSession | |
| 10 | @Binding var selection: String? | |
| 11 | var addFolder: () -> Void = {} | |
| 12 | /// Folders shown collapsed, by path, one per line. | |
| 13 | @AppStorage("collapsedFolders") private var collapsedText = "" | |
| 14 | @State private var removing: String? | |
| 15 | /// A file operation waiting for a name or a confirmation. | |
| 16 | @Environment(\.orgTheme) private var theme | |
| 17 | @State var pending: FileOperation? | |
| 18 | @State var name = "" | |
| 19 | @State var failure: String? | |
| 20 | ||
| 21 | private var collapsed: Set<String> { Set(collapsedText.split(separator: "\n").map(String.init)) } | |
| 22 | ||
| 23 | private func expanded(_ root: String) -> Binding<Bool> { | |
| 24 | Binding { | |
| 25 | !collapsed.contains(root) | |
| 26 | } set: { isExpanded in | |
| 27 | var set = collapsed | |
| 28 | if isExpanded { set.remove(root) } else { set.insert(root) } | |
| 29 | collapsedText = set.sorted().joined(separator: "\n") | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | var body: some View { | |
| 34 | List(selection: $selection) { | |
| 35 | ForEach(workspace.rootPaths, id: \.self) { root in | |
| 36 | Section(isExpanded: expanded(root)) { | |
| 37 | OutlineGroup(workspace.tree(forRoot: root).children ?? [], children: \.children) { node in | |
| 38 | Label(node.name, systemImage: icon(node)) | |
| 39 | .foregroundStyle(node.file?.kind == .conflict ? .orange : node.isDownloading ? .secondary : theme.color("sidebar-foreground") ?? .primary) | |
| 40 | .help(node.isDownloading ? "Downloading from iCloud" : "") | |
| 41 | .tag(node.file?.path) | |
| 42 | .contextMenu { fileMenu(node) } | |
| 43 | } | |
| 44 | } header: { | |
| 45 | Text((root as NSString).lastPathComponent) | |
| 46 | .foregroundStyle(theme.color("sidebar-header") ?? .secondary) | |
| 47 | .help(root) | |
| 48 | .contextMenu { folderMenu(root) } | |
| 49 | } | |
| 50 | } | |
| 51 | } | |
| 52 | .listStyle(.sidebar) | |
| 53 | .themedListBackground(theme.color("sidebar-background")) | |
| 54 | .safeAreaInset(edge: .bottom, spacing: 0) { | |
| 55 | HStack(spacing: 4) { | |
| 56 | Button("Add Folder…", systemImage: "plus", action: addFolder) | |
| 57 | .help("Add a folder (⇧⌘O)") | |
| 58 | Menu { | |
| 59 | ForEach(workspace.rootPaths, id: \.self) { root in | |
| 60 | Button((root as NSString).lastPathComponent) { removing = root } | |
| 61 | } | |
| 62 | } label: { | |
| 63 | Label("Remove Folder", systemImage: "minus") | |
| 64 | } | |
| 65 | .menuIndicator(.hidden) | |
| 66 | .fixedSize() | |
| 67 | .disabled(workspace.rootPaths.isEmpty) | |
| 68 | .help("Remove a folder from the sidebar") | |
| 69 | Spacer() | |
| 70 | } | |
| 71 | .labelStyle(.iconOnly) | |
| 72 | .buttonStyle(.borderless) | |
| 73 | .menuStyle(.borderlessButton) | |
| 74 | .padding(.horizontal, 10) | |
| 75 | .padding(.vertical, 6) | |
| 76 | } | |
| 77 | .confirmationDialog( | |
| 78 | "Remove “\(removing.map { ($0 as NSString).lastPathComponent } ?? "")” from the sidebar?", | |
| 79 | isPresented: Binding { removing != nil } set: { if !$0 { removing = nil } } | |
| 80 | ) { | |
| 81 | Button("Remove") { | |
| 82 | if let root = removing { try? workspace.removeRoot(path: root) } | |
| 83 | removing = nil | |
| 84 | } | |
| 85 | } message: { | |
| 86 | Text("The folder and its files stay on disk.") | |
| 87 | } | |
| 88 | .fileOperations(self) | |
| 89 | } | |
| 90 | ||
| 91 | @ViewBuilder | |
| 92 | private func folderMenu(_ root: String) -> some View { | |
| 93 | Button("New File…") { ask(.create(folder: root)) } | |
| 94 | Divider() | |
| 95 | Button(collapsed.contains(root) ? "Expand" : "Collapse") { expanded(root).wrappedValue.toggle() } | |
| 96 | Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: root)]) } | |
| 97 | Divider() | |
| 98 | Button("Remove from Sidebar…") { removing = root } | |
| 99 | } | |
| 100 | ||
| 101 | private func icon(_ node: FileTreeNode) -> String { | |
| 102 | if node.isDownloading { return "icloud.and.arrow.down" } | |
| 103 | guard let file = node.file else { return "folder" } | |
| 104 | switch file.kind { | |
| 105 | case .org: return "doc.text" | |
| 106 | case .archive: return "archivebox" | |
| 107 | case .conflict: return "exclamationmark.triangle" | |
| 108 | case .other: | |
| 109 | guard let type = UTType(filenameExtension: (file.path as NSString).pathExtension) else { return "doc" } | |
| 110 | if type.conforms(to: .image) { return "photo" } | |
| 111 | if type.conforms(to: .pdf) { return "doc.richtext" } | |
| 112 | if type.conforms(to: .audiovisualContent) { return "play.rectangle" } | |
| 113 | if type.conforms(to: .sourceCode) || type.conforms(to: .script) { return "chevron.left.forwardslash.chevron.right" } | |
| 114 | if type.conforms(to: .text) { return "doc.plaintext" } | |
| 115 | return "doc" | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | struct OutlineColumn: View { | |
| 121 | let session: DocumentSession | |
| 122 | @Environment(\.orgTheme) private var theme | |
| 123 | ||
| 124 | var body: some View { | |
| 125 | List(Array(session.outline.enumerated()), id: \.element.start) { _, entry in | |
| 126 | Button { | |
| 127 | session.reveal(entry.start) | |
| 128 | } label: { | |
| 129 | Text(entry.title.isEmpty ? "(untitled)" : entry.title) | |
| 130 | .foregroundStyle(theme.color("sidebar-foreground") ?? .primary) | |
| 131 | .lineLimit(1) | |
| 132 | .padding(.leading, CGFloat(entry.level - 1) * 12) | |
| 133 | } | |
| 134 | .buttonStyle(.plain) | |
| 135 | } | |
| 136 | .themedListBackground(theme.color("sidebar-background")) | |
| 137 | .overlay { | |
| 138 | if session.path != nil, !session.isOrg { | |
| 139 | ContentUnavailableView("No outline", systemImage: "doc.plaintext", description: Text("Only org files have an outline.")) | |
| 140 | } else if session.path != nil, session.outline.isEmpty { | |
| 141 | ContentUnavailableView("No headings", systemImage: "list.bullet.indent") | |
| 142 | } | |
| 143 | } | |
| 144 | } | |
| 145 | } | |