Merge pull request #26 from RobotsAndPencils/toolbar

Extract toolbar modifier and tweak contents
This commit is contained in:
Brandon Evans 2020-12-28 12:10:04 -07:00 committed by GitHub
commit 2316a19bd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 36 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

@ -21,13 +21,17 @@ struct ProgressButton<Label: View>: View {
var body: some View {
Button(action: action) {
if isInProgress {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.scaleEffect(x: 0.5, y: 0.5, anchor: .center)
} else {
label()
}
// This might look like a strange way to switch between the label and the progress view.
// Doing it this way, so that the label is hidden but still has the same frame and is in the view hierarchy
// makes sure that the button's frame doesn't change when isInProgress changes.
label()
.isHidden(isInProgress)
.overlay(
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.scaleEffect(x: 0.5, y: 0.5, anchor: .center)
.isHidden(!isInProgress)
)
}
.disabled(isInProgress)
}

View file

@ -0,0 +1,52 @@
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) {
ProgressButton(
isInProgress: appState.isUpdating,
action: appState.update
) {
Label("Refresh", systemImage: "arrow.clockwise")
}
.keyboardShortcut(KeyEquivalent("r"))
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,34 +80,7 @@ struct XcodeListView: View {
}
}
}
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
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)
)
}
ToolbarItem(placement: .principal) {
Picker("", selection: $category) {
ForEach(Category.allCases, id: \.self) {
Text($0.description).tag($0)
}
}
.pickerStyle(SegmentedPickerStyle())
}
ToolbarItem {
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