Skip to content

Commit

Permalink
Merge pull request #338 from nyanko3141592/feature/FlickSuggestViewShape
Browse files Browse the repository at this point in the history
[Design] キーボードフリック時のSuggestViewを変更
  • Loading branch information
ensan-hcl authored Oct 20, 2023
2 parents 03256d8 + 6c4585a commit 7ef67c1
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 41 deletions.
3 changes: 3 additions & 0 deletions AzooKeyCore/Sources/KeyboardViews/Design.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,16 @@ public enum Design {

enum LabelFontSizeStrategy {
case max
case xlarge
case large
case medium
case small
case xsmall

var scale: CGFloat {
switch self {
case .xlarge:
return 1.2
case .large, .max:
return 1
case .medium:
Expand Down
13 changes: 11 additions & 2 deletions AzooKeyCore/Sources/KeyboardViews/View/Components/KeyLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public enum KeyLabelType {
public struct KeyLabel<Extension: ApplicationSpecificKeyboardViewExtension>: View {
private let labelType: KeyLabelType
private let width: CGFloat
private let textColor: Color?
private let textSize: Design.Fonts.LabelFontSizeStrategy
private var textColor: Color?
private var textSize: Design.Fonts.LabelFontSizeStrategy
@Environment(Extension.Theme.self) private var theme
@Environment(\.userActionManager) private var action
@EnvironmentObject private var variableStates: VariableStates
Expand Down Expand Up @@ -99,4 +99,13 @@ public struct KeyLabel<Extension: ApplicationSpecificKeyboardViewExtension>: Vie
}.allowsHitTesting(false)
}
}

consuming func textColor(_ color: Color?) -> Self {
self.textColor = color
return self
}
consuming func textSize(_ textSize: Design.Fonts.LabelFontSizeStrategy) -> Self {
self.textSize = textSize
return self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,19 @@ struct CustomKeyboardView<Extension: ApplicationSpecificKeyboardViewExtension>:
public struct CustardFlickKeysView<Extension: ApplicationSpecificKeyboardViewExtension, Content: View>: View {
@State private var suggestState = FlickSuggestState()

public init(models: [KeyPosition: (model: any FlickKeyModelProtocol, width: Int, height: Int)], tabDesign: TabDependentDesign, layout: CustardInterfaceLayoutGridValue, @ViewBuilder generator: @escaping (FlickKeyView<Extension>, Int, Int) -> (Content)) {
public init(models: [KeyPosition: (model: any FlickKeyModelProtocol, width: Int, height: Int)], tabDesign: TabDependentDesign, layout: CustardInterfaceLayoutGridValue, blur: Bool = false, @ViewBuilder generator: @escaping (FlickKeyView<Extension>, Int, Int) -> (Content)) {
self.models = models
self.tabDesign = tabDesign
self.layout = layout
self.blur = blur
self.contentGenerator = generator
}

private let contentGenerator: (FlickKeyView<Extension>, Int, Int) -> (Content)
private let models: [KeyPosition: (model: any FlickKeyModelProtocol, width: Int, height: Int)]
private let tabDesign: TabDependentDesign
private let layout: CustardInterfaceLayoutGridValue
private let blur: Bool

@MainActor private func flickKeyData(x: Int, y: Int, width: Int, height: Int) -> (position: CGPoint, size: CGSize) {
let width = tabDesign.keyViewWidth(widthCount: width)
Expand All @@ -359,22 +361,30 @@ public struct CustardFlickKeysView<Extension: ApplicationSpecificKeyboardViewExt

public var body: some View {
ZStack {
let hasAllSuggest = self.suggestState.items.contains(where: {$0.value.contains(where: {$0.value == .all})})
let needKeyboardBlur = blur && hasAllSuggest
ForEach(0..<layout.rowCount, id: \.self) {x in
let columnSuggestStates = self.suggestState.items[x, default: [:]]
// 可能ならカラムごとにblurをかけることで描画コストを減らす
let needColumnWideBlur = needKeyboardBlur && columnSuggestStates.allSatisfy {$0.value != .all}
ForEach(0..<layout.columnCount, id: \.self) {y in
if let data = models[.gridFit(x: x, y: y)] {
let info = flickKeyData(x: x, y: y, width: data.width, height: data.height)
contentGenerator(FlickKeyView(model: data.model, size: info.size, position: (x, y), suggestState: $suggestState), x, y) .zIndex(columnSuggestStates[y] != nil ? 1 : 0)
let suggestState = columnSuggestStates[y]
let needBlur = needKeyboardBlur && !needColumnWideBlur && suggestState == nil
contentGenerator(FlickKeyView(model: data.model, size: info.size, position: (x, y), suggestState: $suggestState), x, y) .zIndex(suggestState != nil ? 1 : 0)
.overlay(alignment: .center) {
if let suggestType = columnSuggestStates[y] {
if let suggestType = suggestState {
FlickSuggestView<Extension>(model: data.model, tabDesign: tabDesign, size: info.size, suggestType: suggestType)
.zIndex(2)
}
}
.position(x: info.position.x, y: info.position.y)
.blur(radius: needBlur ? 0.75 : 0)
}
}
.zIndex(columnSuggestStates.isEmpty ? 0 : 1)
.blur(radius: needColumnWideBlur ? 0.75 : 0)
}
.frame(width: tabDesign.keysWidth, height: tabDesign.keysHeight)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct FlickKeyboardView<Extension: ApplicationSpecificKeyboardViewExtension>: V

var body: some View {
let layout = CustardInterfaceLayoutGridValue(rowCount: Int(tabDesign.horizontalKeyCount), columnCount: Int(tabDesign.verticalKeyCount))
CustardFlickKeysView(models: models, tabDesign: tabDesign, layout: layout) {(view: FlickKeyView<Extension>, _, _) in
CustardFlickKeysView(models: models, tabDesign: tabDesign, layout: layout, blur: true) {(view: FlickKeyView<Extension>, _, _) in
view
}
}
Expand Down
Loading

0 comments on commit 7ef67c1

Please sign in to comment.