Skip to content

Commit 30fafd6

Browse files
authored
Merge pull request #67 from azooKey/feat/main_and_directional_label
feat: 4方向ラベルの機能を追加
2 parents 688687f + 2b1617e commit 30fafd6

5 files changed

Lines changed: 132 additions & 101 deletions

File tree

json/howToMake.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| ----------------- | ------------------------------------------------------------ |
5959
| text | `"text"`に指定した文字をラベルとして表示します。 |
6060
| main_and_sub | `"main"`に指定した1文字を1行目に大きめに、`"sub"`に指定した文字列を2行目に小さめに表示します。 |
61+
| main_and_directions| `"main"`に指定した文字を中心に、`"directions"``{"left": "...", "top": "..."}`のように指定したラベルを4方向に表示します。 |
6162
| system_image | `"system_image"`に指定した画像をラベルとして表示します。指定できる値の一例は以下の通りです。<br />この画像は[SFSymbols](https://developer.apple.com/sf-symbols/)から取得されます。 |
6263

6364
<img src="../resource/symbols.png" style="zoom:15%;" />

swift/howToMake.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ design: .init(label: .text("@#/&_"), color: .normal)
4949
| -------------------- | ------------------------------------------------------------ |
5050
| .text(String) | 指定した文字をラベルとして表示します。 |
5151
| .mainAndSub(String, String) | 1つ目に指定した1文字を1行目に大きめに、2つ目に指定した文字を2行目に小さめに表示します。 |
52+
| .mainAndDirections(String, Directions)| `main`に指定した文字を中心に、Directionsに指定したラベルを4方向に表示します。 |
5253
| .systemImage(String) | 指定した名前の画像をラベルとして表示します。指定できる値の一例は以下の通りです。<br />この画像は[SFSymbols](https://developer.apple.com/sf-symbols/)から取得されます。 |
5354

5455
<img src="../resource/symbols.png" style="zoom:15%;" />
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/// - キーに指定するラベル
2+
/// - labels on the key
3+
public enum CustardKeyLabelStyle: Codable, Equatable, Hashable, Sendable {
4+
case text(String)
5+
case systemImage(String)
6+
case mainAndSub(String, String)
7+
case mainAndDirections(String, CustardKeyDirectionalLabel)
8+
}
9+
10+
public struct CustardKeyDirectionalLabel: Codable, Equatable, Hashable, Sendable {
11+
var left: String?
12+
var top: String?
13+
var right: String?
14+
var bottom: String?
15+
}
16+
17+
public extension CustardKeyLabelStyle {
18+
private enum CodingKeys: CodingKey {
19+
case text
20+
case system_image
21+
case type
22+
case main
23+
case sub
24+
case directions
25+
}
26+
27+
private enum ValueType: String, Codable {
28+
case text
29+
case system_image
30+
case main_and_sub
31+
case main_and_directions
32+
}
33+
34+
func encode(to encoder: any Encoder) throws {
35+
var container = encoder.container(keyedBy: CodingKeys.self)
36+
switch self {
37+
case let .text(value):
38+
try container.encode(value, forKey: .text)
39+
case let .systemImage(value):
40+
try container.encode(value, forKey: .system_image)
41+
case let .mainAndSub(main, sub):
42+
try container.encode(ValueType.main_and_sub, forKey: .type)
43+
try container.encode(main, forKey: .main)
44+
try container.encode(sub, forKey: .sub)
45+
case let .mainAndDirections(main, directions):
46+
try container.encode(ValueType.main_and_directions, forKey: .type)
47+
try container.encode(main, forKey: .main)
48+
try container.encode(directions, forKey: .directions)
49+
}
50+
}
51+
52+
init(from decoder: any Decoder) throws {
53+
let container = try decoder.container(keyedBy: CodingKeys.self)
54+
// "type"が見つかった場合
55+
if let type = try? container.decode(ValueType.self, forKey: .type) {
56+
switch type {
57+
case .text:
58+
let value = try container.decode(
59+
String.self,
60+
forKey: .text
61+
)
62+
self = .text(value)
63+
case .system_image:
64+
let value = try container.decode(
65+
String.self,
66+
forKey: .system_image
67+
)
68+
self = .systemImage(value)
69+
case .main_and_sub:
70+
let main = try container.decode(
71+
String.self,
72+
forKey: .main
73+
)
74+
let sub = try container.decode(
75+
String.self,
76+
forKey: .sub
77+
)
78+
self = .mainAndSub(main, sub)
79+
case .main_and_directions:
80+
let main = try container.decode(
81+
String.self,
82+
forKey: .main
83+
)
84+
let directions = try container.decode(
85+
CustardKeyDirectionalLabel.self,
86+
forKey: .directions
87+
)
88+
self = .mainAndDirections(main, directions)
89+
}
90+
return
91+
}
92+
93+
// それ以外の場合(old cases)
94+
guard container.allKeys.count == 1, let key = container.allKeys.first else {
95+
throw DecodingError.dataCorrupted(
96+
DecodingError.Context(
97+
codingPath: container.codingPath,
98+
debugDescription: "Unabled to decode CustardKeyLabelStyle."
99+
)
100+
)
101+
}
102+
switch key {
103+
case .text:
104+
let value = try container.decode(
105+
String.self,
106+
forKey: .text
107+
)
108+
self = .text(value)
109+
case .system_image:
110+
let value = try container.decode(
111+
String.self,
112+
forKey: .system_image
113+
)
114+
self = .systemImage(value)
115+
default:
116+
throw DecodingError.dataCorrupted(
117+
DecodingError.Context(
118+
codingPath: container.codingPath,
119+
debugDescription: "Unabled to decode CustardKeyLabelStyle."
120+
)
121+
)
122+
}
123+
}
124+
}

swift/sources/CustardKit.swift

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -440,107 +440,6 @@ public struct CustardVariationKeyDesign: Codable, Equatable, Hashable, Sendable
440440
public var label: CustardKeyLabelStyle
441441
}
442442

443-
/// - キーに指定するラベル
444-
/// - labels on the key
445-
public enum CustardKeyLabelStyle: Codable, Equatable, Hashable, Sendable {
446-
case text(String)
447-
case systemImage(String)
448-
case mainAndSub(String, String)
449-
}
450-
451-
public extension CustardKeyLabelStyle {
452-
private enum CodingKeys: CodingKey {
453-
case text
454-
case system_image
455-
case type
456-
case main
457-
case sub
458-
}
459-
460-
private enum ValueType: String, Codable {
461-
case text
462-
case system_image
463-
case main_and_sub
464-
}
465-
466-
func encode(to encoder: any Encoder) throws {
467-
var container = encoder.container(keyedBy: CodingKeys.self)
468-
switch self {
469-
case let .text(value):
470-
try container.encode(value, forKey: .text)
471-
case let .systemImage(value):
472-
try container.encode(value, forKey: .system_image)
473-
case let .mainAndSub(main, sub):
474-
try container.encode(ValueType.main_and_sub, forKey: .type)
475-
try container.encode(main, forKey: .main)
476-
try container.encode(sub, forKey: .sub)
477-
}
478-
}
479-
480-
init(from decoder: any Decoder) throws {
481-
let container = try decoder.container(keyedBy: CodingKeys.self)
482-
// "type"が見つかった場合
483-
if let type = try? container.decode(ValueType.self, forKey: .type) {
484-
switch type {
485-
case .text:
486-
let value = try container.decode(
487-
String.self,
488-
forKey: .text
489-
)
490-
self = .text(value)
491-
case .system_image:
492-
let value = try container.decode(
493-
String.self,
494-
forKey: .system_image
495-
)
496-
self = .systemImage(value)
497-
case .main_and_sub:
498-
let main = try container.decode(
499-
String.self,
500-
forKey: .main
501-
)
502-
let sub = try container.decode(
503-
String.self,
504-
forKey: .sub
505-
)
506-
self = .mainAndSub(main, sub)
507-
}
508-
return
509-
}
510-
511-
// それ以外の場合(old cases)
512-
guard container.allKeys.count == 1, let key = container.allKeys.first else {
513-
throw DecodingError.dataCorrupted(
514-
DecodingError.Context(
515-
codingPath: container.codingPath,
516-
debugDescription: "Unabled to decode CustardKeyLabelStyle."
517-
)
518-
)
519-
}
520-
switch key {
521-
case .text:
522-
let value = try container.decode(
523-
String.self,
524-
forKey: .text
525-
)
526-
self = .text(value)
527-
case .system_image:
528-
let value = try container.decode(
529-
String.self,
530-
forKey: .system_image
531-
)
532-
self = .systemImage(value)
533-
default:
534-
throw DecodingError.dataCorrupted(
535-
DecodingError.Context(
536-
codingPath: container.codingPath,
537-
debugDescription: "Unabled to decode CustardKeyLabelStyle."
538-
)
539-
)
540-
}
541-
}
542-
}
543-
544443
/// - key's data in interface
545444
public enum CustardInterfaceKey: Equatable, Hashable, Sendable {
546445
case system(CustardInterfaceSystemKey)

swift/tests/CustardKitTests/CustardKeyLabelStyleTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ final class CustardKeyLabelStyleTest: XCTestCase {
4545
"""
4646
XCTAssertEqual(CustardKeyLabelStyle.quickDecode(target: target), .mainAndSub("1", "☆♡◇"))
4747
}
48+
do {
49+
let target = """
50+
{"type": "main_and_directions", "main": "1", "directions": {"left": "2", "top": "3"}}
51+
"""
52+
XCTAssertEqual(CustardKeyLabelStyle.quickDecode(target: target), .mainAndDirections("1", .init(left: "2", top: "3")))
53+
}
4854
}
4955

5056
func testEncode() {

0 commit comments

Comments
 (0)