Skip to content

Commit

Permalink
fix: 🐛 [bcp: 2370136916] fix width mode for DimensionSelector (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
shengxu7 authored Nov 3, 2023
1 parent 6462bb4 commit 088aef8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import FioriSwiftUICore
import SwiftUI

struct DimensionSelector_Chart: View {
let segmentTitltes = ["intraday: 1min", "one day: 1min", "1year:1day", "3years:1week"]
let segmentTitltes = ["1min", "One day", "1year: 1day", "Three years: one week"]

@ObservedObject var stockModel = Tests.stockModels[0]

@State var widthMode: SegmentWidthMode = .intrinsic
var cancellableSet: Set<AnyCancellable> = []

var dimensionSelector: DimensionSelector!

init() {
self.dimensionSelector = DimensionSelector(segmentTitles: self.segmentTitltes, selectedIndex: stockModel.indexOfStockSeries)
self.dimensionSelector.segmentWidthMode = .equal
let stockModel = self.stockModel
self.dimensionSelector.selectionDidChangePublisher
.sink(receiveValue: { index in
Expand All @@ -25,6 +26,7 @@ struct DimensionSelector_Chart: View {
var body: some View {
VStack(alignment: .center, spacing: 30) {
dimensionSelector

chartView
}
}
Expand Down
54 changes: 18 additions & 36 deletions Sources/FioriSwiftUICore/DimensionSelector/DimensionSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,36 @@ public struct DimensionSelector: View {
private func getHStack() -> some View {
HStack(alignment: .center, spacing: self.model.interItemSpacing) {
ForEach(self.model.titles.indices, id: \.self) { index in
Segment(title: self.model.titles[index], isSelected: self.model.selectedIndex == index, isEnable: self.model.isEnable, cornerRadius: 10.0, backgroundColor: .preferredColor(.primaryFill), segmentAttributes: self.model.segmentAttributes, insets: self.titleInsets)
Text(self.model.titles[index])
.padding(self.titleInsets)
.font(segmentAttributes(for: index)?.font)
.foregroundColor(segmentAttributes(for: index)?.textColor)
.background(SegmentPreferenceSetter())
.modifier(SegmentFrame(segmentWidthMode: self.model.segmentWidthMode, width: self._segmentWidth))
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.inset(by: segmentAttributes(for: index)!.borderWidth! / 2.0)
.stroke(segmentAttributes(for: index)!.borderColor!, lineWidth: segmentAttributes(for: index)!.borderWidth!)
)
.background(RoundedRectangle(cornerRadius: 10, style: .continuous).fill(segmentAttributes(for: index)!.backgroundColor!))
.onTapGesture {
if self.model.isEnable {
self.selectionDidChange(index: index)
}
}
.background(SegmentPreferenceSetter())
.modifier(SegmentFrame(segmentWidthMode: self.model.segmentWidthMode, width: self._segmentWidth))
}
}
.padding(self.contentInset)
.lineLimit(1)
.background(HStackPreferenceSetter())
}

func segmentAttributes(for index: Int) -> SegmentAttributes? {
let isSelected = self.model.selectedIndex == index

return self.model.isEnable ? (isSelected ? self.model.segmentAttributes[.selected] : self.model.segmentAttributes[.normal]) : self.model.segmentAttributes[.disabled]
}

private var _segmentWidth: CGFloat? {
let width: CGFloat?

Expand Down Expand Up @@ -249,39 +264,6 @@ public struct DimensionSelector: View {
}

extension DimensionSelector {
struct Segment: View {
let title: String

let isSelected: Bool

let isEnable: Bool

let cornerRadius: CGFloat

let backgroundColor: Color

let segmentAttributes: [ControlState: SegmentAttributes]

let insets: EdgeInsets

var body: some View {
Text(self.title)
.padding(insets)
.font(getSegmentAttributes()?.font)
.foregroundColor(getSegmentAttributes()?.textColor)
.background(
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
.inset(by: getSegmentAttributes()!.borderWidth! / 2.0)
.stroke(getSegmentAttributes()!.borderColor!, lineWidth: getSegmentAttributes()!.borderWidth!)
)
.background(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous).fill(getSegmentAttributes()!.backgroundColor!))
}

func getSegmentAttributes() -> SegmentAttributes? {
self.isEnable ? (self.isSelected ? self.segmentAttributes[.selected] : self.segmentAttributes[.normal]) : self.segmentAttributes[.disabled]
}
}

class Model: ObservableObject {
@Published var titles: [String] = []
@Published var selectedIndex: Int?
Expand Down

0 comments on commit 088aef8

Please sign in to comment.