Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SidebarListStyle #210

Merged
merged 7 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions NativeDemo/TokamakDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
3DCDE44424CA6AD400910F17 /* SidebarDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DCDE44324CA6AD400910F17 /* SidebarDemo.swift */; };
3DCDE44524CA6AD400910F17 /* SidebarDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DCDE44324CA6AD400910F17 /* SidebarDemo.swift */; };
854A1A9124B3E3630027BC32 /* ToggleDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85CBD5DE24B3BF090066468A /* ToggleDemo.swift */; };
854A1A9324B3F28F0027BC32 /* ToggleDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85CBD5DE24B3BF090066468A /* ToggleDemo.swift */; };
85ED186A24AD38F20085DFA0 /* UIAppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85ED186924AD38F20085DFA0 /* UIAppDelegate.swift */; };
Expand Down Expand Up @@ -76,6 +78,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
3DCDE44324CA6AD400910F17 /* SidebarDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SidebarDemo.swift; sourceTree = "<group>"; };
85CBD5DE24B3BF090066468A /* ToggleDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToggleDemo.swift; sourceTree = "<group>"; };
85ED184A24AD379A0085DFA0 /* TokamakDemo Native.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "TokamakDemo Native.app"; sourceTree = BUILT_PRODUCTS_DIR; };
85ED185224AD379A0085DFA0 /* TokamakDemo Native.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "TokamakDemo Native.app"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -160,20 +163,21 @@
isa = PBXGroup;
children = (
B5C76E4924C73ED4003EABB2 /* AppStorageDemo.swift */,
D1EE7EA624C0DD2100C0D127 /* PickerDemo.swift */,
B56F22DF24BC89FD001738DF /* ColorDemo.swift */,
85ED189E24AD425E0085DFA0 /* Counter.swift */,
85ED18A024AD425E0085DFA0 /* EnvironmentDemo.swift */,
85ED189C24AD425E0085DFA0 /* ForEachDemo.swift */,
B56F22E224BD1C26001738DF /* GridDemo.swift */,
D1B4228E24B3B9BB00682F74 /* ListDemo.swift */,
D1B4228F24B3B9BB00682F74 /* OutlineGroupDemo.swift */,
B51F214F24B920B400CF2583 /* PathDemo.swift */,
D1EE7EA624C0DD2100C0D127 /* PickerDemo.swift */,
3DCDE44324CA6AD400910F17 /* SidebarDemo.swift */,
85ED189A24AD425E0085DFA0 /* SpacerDemo.swift */,
85ED189B24AD425E0085DFA0 /* TextDemo.swift */,
85ED189C24AD425E0085DFA0 /* ForEachDemo.swift */,
85ED189D24AD425E0085DFA0 /* TokamakDemo.swift */,
85ED189E24AD425E0085DFA0 /* Counter.swift */,
85ED189F24AD425E0085DFA0 /* TextFieldDemo.swift */,
85CBD5DE24B3BF090066468A /* ToggleDemo.swift */,
85ED18A024AD425E0085DFA0 /* EnvironmentDemo.swift */,
B51F214F24B920B400CF2583 /* PathDemo.swift */,
B56F22DF24BC89FD001738DF /* ColorDemo.swift */,
B56F22E224BD1C26001738DF /* GridDemo.swift */,
85ED189D24AD425E0085DFA0 /* TokamakDemo.swift */,
);
name = TokamakDemo;
path = ../Sources/TokamakDemo;
Expand Down Expand Up @@ -328,6 +332,7 @@
D1EE7EA724C0DD2100C0D127 /* PickerDemo.swift in Sources */,
85ED18A924AD425E0085DFA0 /* TokamakDemo.swift in Sources */,
B5C76E4A24C73ED5003EABB2 /* AppStorageDemo.swift in Sources */,
3DCDE44424CA6AD400910F17 /* SidebarDemo.swift in Sources */,
85ED18AD24AD425E0085DFA0 /* TextFieldDemo.swift in Sources */,
85ED18A724AD425E0085DFA0 /* ForEachDemo.swift in Sources */,
854A1A9124B3E3630027BC32 /* ToggleDemo.swift in Sources */,
Expand All @@ -351,6 +356,7 @@
D1EE7EA824C0DD2100C0D127 /* PickerDemo.swift in Sources */,
85ED18B624AD42D70085DFA0 /* NSAppDelegate.swift in Sources */,
B5C76E4B24C73ED5003EABB2 /* AppStorageDemo.swift in Sources */,
3DCDE44524CA6AD400910F17 /* SidebarDemo.swift in Sources */,
85ED18AC24AD425E0085DFA0 /* Counter.swift in Sources */,
85ED18A824AD425E0085DFA0 /* ForEachDemo.swift in Sources */,
854A1A9324B3F28F0027BC32 /* ToggleDemo.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakCore/Styles/ListStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public struct InsetGroupedListStyle: ListStyle {
public init() {}
}

public struct SidebarListStyle: ListStyle {
public init() {}
}

enum ListStyleKey: EnvironmentKey {
static let defaultValue: ListStyle = DefaultListStyle()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakCore/Views/Containers/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public struct _ListRow {
public static func listRow<V: View>(_ view: V, _ style: ListStyle, isLast: Bool) -> some View {
(style as? ListStyleDeferredToRenderer)?.listRow(view) ??
AnyView(view.padding([.trailing, .top, .bottom]))
if !isLast {
if !isLast && style as? SidebarListStyle == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably just add a var addDividers: Bool { get } to the ListStyleDeferredToRenderer protocol, then provide a default value of true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added "hasDividers" to the ListStyle. It wasn't clear how to add it to ListStyleDeferredToRenderer and have the code be clean.

Here is where the property is checked:

@ViewBuilder
  public static func listRow<V: View>(_ view: V, _ style: ListStyle, isLast: Bool) -> some View {
    (style as? ListStyleDeferredToRenderer)?.listRow(view) ??
      AnyView(view.padding([.trailing, .top, .bottom]))
    if !isLast && style.hasDividers {
      Divider()
    }
  }
}

Divider()
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakDOM/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public typealias PlainListStyle = TokamakCore.PlainListStyle
public typealias InsetListStyle = TokamakCore.InsetListStyle
public typealias GroupedListStyle = TokamakCore.GroupedListStyle
public typealias InsetGroupedListStyle = TokamakCore.InsetGroupedListStyle
public typealias SidebarListStyle = TokamakCore.SidebarListStyle

public typealias DefaultPickerStyle = TokamakCore.DefaultPickerStyle
public typealias PopUpButtonPickerStyle = TokamakCore.PopUpButtonPickerStyle
Expand Down
10 changes: 10 additions & 0 deletions Sources/TokamakDOM/Views/Containers/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ extension InsetGroupedListStyle: ListStyleDeferredToRenderer {
.padding(.leading, 20))
}
}

extension SidebarListStyle: ListStyleDeferredToRenderer {
public func listBody<ListBody>(_ content: ListBody) -> AnyView where ListBody: View {
AnyView(content
.padding(.all)
.padding(.leading, 20)
.background(Color(0xF2F2F7))
)
}
}
6 changes: 4 additions & 2 deletions Sources/TokamakDemo/AppStorageDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ struct AppStorageButtons: View {
@SceneStorage("count") var sceneCount: Int = 0

var body: some View {
Button("Increment AppStorage") { count += 1 }
Button("Increment SceneStorage") { sceneCount += 1 }
HStack {
Button("Increment AppStorage") { count += 1 }
Button("Increment SceneStorage") { sceneCount += 1 }
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions Sources/TokamakDemo/SidebarDemo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019-2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Gene Z. Ragan on 7/23/20.
//

import TokamakShim

public struct SidebarListDemo: View {
public var body: some View {
List {
ForEach(0..<10) {
Text("Item \($0 + 1)")
}
}
}
}
15 changes: 11 additions & 4 deletions Sources/TokamakDemo/TokamakDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ var listDemo: AnyView {
var listDemo = ListDemo()
#endif

var sidebarDemo: NavItem {
if #available(iOS 14.0, *) {
return NavItem("Sidebar", destination: SidebarListDemo().listStyle(SidebarListStyle()))
} else {
return NavItem(unavailable: "Sidebar")
}
}

var gridDemo: NavItem {
if #available(OSX 10.16, iOS 14.0, *) {
return NavItem("Grid", destination: GridDemo())
Expand Down Expand Up @@ -101,6 +109,7 @@ var links: [NavItem] {
NavItem("Environment", destination: EnvironmentDemo().font(.system(size: 8))),
NavItem("Picker", destination: PickerDemo()),
NavItem("List", destination: listDemo),
sidebarDemo,
outlineGroupDemo,
NavItem("Color", destination: ColorDemo()),
appStorageDemo,
Expand All @@ -122,6 +131,8 @@ struct TokamakDemoView: View {
} else {
#if os(WASI)
Text(link.id)
#elseif os(macOS)
Text(link.id).opacity(0.5)
#else
HStack {
Text(link.id)
Expand All @@ -134,15 +145,11 @@ struct TokamakDemoView: View {
.frame(minHeight: 300),
title: "Demos"
)
#if os(WASI)
return AnyView(list)
#else
if #available(iOS 14.0, *) {
return AnyView(list.listStyle(SidebarListStyle()))
} else {
return AnyView(list)
}
#endif
}
.environmentObject(TestEnvironment())
}
Expand Down