Extract MainToolbar

This commit is contained in:
Brandon Evans 2020-12-27 23:42:07 -07:00
parent 192a1c6371
commit 4cb60e2929
No known key found for this signature in database
GPG key ID: D58A4B8DB64F8E93
3 changed files with 62 additions and 35 deletions

View file

@ -71,6 +71,7 @@
CAD2E7B82449575100113D76 /* XcodesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD2E7B72449575100113D76 /* XcodesTests.swift */; };
CAFBDB912598FE80003DCC5A /* SelectedXcode.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFBDB902598FE80003DCC5A /* SelectedXcode.swift */; };
CAFBDB952598FE96003DCC5A /* FocusedValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFBDB942598FE96003DCC5A /* FocusedValues.swift */; };
CAFBDC4E2599B33D003DCC5A /* MainToolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFBDC4D2599B33D003DCC5A /* MainToolbar.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -189,6 +190,7 @@
CAFBDB902598FE80003DCC5A /* SelectedXcode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectedXcode.swift; sourceTree = "<group>"; };
CAFBDB942598FE96003DCC5A /* FocusedValues.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FocusedValues.swift; sourceTree = "<group>"; };
CAFBDBA525990C76003DCC5A /* SimpleXPCApp.LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = SimpleXPCApp.LICENSE; sourceTree = "<group>"; };
CAFBDC4D2599B33D003DCC5A /* MainToolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainToolbar.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -292,9 +294,10 @@
CABFAA142592F73000380FEE /* XcodeList */ = {
isa = PBXGroup;
children = (
CAD2E7A32449574E00113D76 /* XcodeListView.swift */,
CA39711824495F0E00AFFB77 /* AppStoreButtonStyle.swift */,
CAFBDC4D2599B33D003DCC5A /* MainToolbar.swift */,
CA44901E2463AD34003D8213 /* Tag.swift */,
CAD2E7A32449574E00113D76 /* XcodeListView.swift */,
);
path = XcodeList;
sourceTree = "<group>";
@ -599,6 +602,7 @@
files = (
CA9FF8CF25959A9700E47BAF /* HelperXPCShared.swift in Sources */,
CA735109257BF96D00EA9CF8 /* AttributedText.swift in Sources */,
CAFBDC4E2599B33D003DCC5A /* MainToolbar.swift in Sources */,
CA11E7BA2598476C00D2EE1C /* XcodeCommands.swift in Sources */,
CABFAA492593162500380FEE /* Bundle+InfoPlistValues.swift in Sources */,
CA9FF8662595130600E47BAF /* View+IsHidden.swift in Sources */,

View file

@ -0,0 +1,56 @@
import SwiftUI
struct MainToolbarModifier: ViewModifier {
@EnvironmentObject var appState: AppState
@Binding var category: XcodeListView.Category
@Binding var searchText: String
func body(content: Content) -> some View {
content
.toolbar { toolbar }
}
private var toolbar: some ToolbarContent {
ToolbarItemGroup(placement: .status) {
Button(action: appState.update) {
Label("Refresh", systemImage: "arrow.clockwise")
}
.keyboardShortcut(KeyEquivalent("r"))
.disabled(appState.isUpdating)
.isHidden(appState.isUpdating)
.overlay(
ProgressView()
.scaleEffect(0.5, anchor: .center)
.isHidden(!appState.isUpdating)
)
Button(action: {
switch category {
case .all: category = .installed
case .installed: category = .all
}
}) {
switch category {
case .all:
Label("Filter", systemImage: "line.horizontal.3.decrease.circle")
case .installed:
Label("Filter", systemImage: "line.horizontal.3.decrease.circle.fill")
.foregroundColor(.accentColor)
}
}
TextField("Search...", text: $searchText)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(width: 200)
}
}
}
extension View {
func mainToolbar(
category: Binding<XcodeListView.Category>,
searchText: Binding<String>
) -> some View {
self.modifier(MainToolbarModifier(category: category, searchText: searchText))
}
}

View file

@ -80,40 +80,7 @@ struct XcodeListView: View {
}
}
}
.toolbar {
ToolbarItemGroup(placement: .status) {
Button(action: appState.update) {
Label("Refresh", systemImage: "arrow.clockwise")
}
.keyboardShortcut(KeyEquivalent("r"))
.disabled(appState.isUpdating)
.isHidden(appState.isUpdating)
.overlay(
ProgressView()
.scaleEffect(0.5, anchor: .center)
.isHidden(!appState.isUpdating)
)
Button(action: {
switch category {
case .all: category = .installed
case .installed: category = .all
}
}) {
switch category {
case .all:
Label("Filter", systemImage: "line.horizontal.3.decrease.circle")
case .installed:
Label("Filter", systemImage: "line.horizontal.3.decrease.circle.fill")
.foregroundColor(.accentColor)
}
}
TextField("Search...", text: $searchText)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(width: 200)
}
}
.mainToolbar(category: $category, searchText: $searchText)
.navigationSubtitle(subtitleText)
.frame(minWidth: 200, maxWidth: .infinity, minHeight: 300, maxHeight: .infinity)
.alert(item: $appState.error) { error in