From 64ab1bc676b24758b6d3cd8b0bbabed5d7667374 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 8 Oct 2022 19:21:11 +0800 Subject: [PATCH] Repo // Reorganize CandidateCellData contents. --- .../CandidateWindow/CandidateCellData.swift} | 90 +++++++++++++++++-- .../CandidateCellDataExtension.swift | 37 -------- .../CandidateCellDataExtensionBackports.swift | 55 ------------ 3 files changed, 84 insertions(+), 98 deletions(-) rename Packages/{vChewing_Shared/Sources/Shared/CandidateBasicUnits.swift => vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData.swift} (65%) delete mode 100644 Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/CandidateCellDataExtension.swift delete mode 100644 Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates_Backports/CandidateCellDataExtensionBackports.swift diff --git a/Packages/vChewing_Shared/Sources/Shared/CandidateBasicUnits.swift b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData.swift similarity index 65% rename from Packages/vChewing_Shared/Sources/Shared/CandidateBasicUnits.swift rename to Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData.swift index b4116cc4..b240afbe 100644 --- a/Packages/vChewing_Shared/Sources/Shared/CandidateBasicUnits.swift +++ b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData.swift @@ -7,6 +7,9 @@ // requirements defined in MIT License. import Cocoa +import Shared +import SwiftUI +import SwiftUIBackports // MARK: - Classes used by Candidate Window @@ -44,6 +47,15 @@ public class CandidateCellData: Hashable { self.isSelected = isSelected } + public static func == (lhs: CandidateCellData, rhs: CandidateCellData) -> Bool { + lhs.key == rhs.key && lhs.displayedText == rhs.displayedText + } + + public func hash(into hasher: inout Hasher) { + hasher.combine(key) + hasher.combine(displayedText) + } + public var cellLength: Int { if displayedText.count <= 2 { return Int(ceil(size * 3)) } let rect = attributedStringForLengthCalculation.boundingRect( @@ -120,13 +132,79 @@ public class CandidateCellData: Hashable { let attrStrCandidate = NSMutableAttributedString(string: displayedText, attributes: attrCandidate) return attrStrCandidate } +} - public static func == (lhs: CandidateCellData, rhs: CandidateCellData) -> Bool { - lhs.key == rhs.key && lhs.displayedText == rhs.displayedText - } +// MARK: - Contents specifically made for macOS 12 and newer. - public func hash(into hasher: inout Hasher) { - hasher.combine(key) - hasher.combine(displayedText) +@available(macOS 12, *) +extension CandidateCellData { + public var attributedStringForSwiftUI: some View { + var result: some View { + ZStack(alignment: .leading) { + if isSelected { + Color(nsColor: CandidateCellData.highlightBackground).ignoresSafeArea().cornerRadius(6) + } + VStack(spacing: 0) { + HStack(spacing: 4) { + if UserDefaults.standard.bool(forKey: UserDef.kHandleDefaultCandidateFontsByLangIdentifier.rawValue) { + Text(AttributedString(attributedStringHeader)) + Text(AttributedString(attributedString)) + } else { + Text(key).font(.system(size: fontSizeKey).monospaced()) + .foregroundColor(.init(nsColor: fontColorKey)).lineLimit(1) + Text(displayedText).font(.system(size: fontSizeCandidate)) + .foregroundColor(.init(nsColor: fontColorCandidate)).lineLimit(1) + } + }.padding(4) + } + }.fixedSize(horizontal: false, vertical: true) + } + return result + } +} + +// MARK: - Contents specifically made for macOS 10.15 and macOS 11. + +@available(macOS 10.15, *) +extension CandidateCellData { + public var themeColorBackports: some View { + // 設定當前高亮候選字的背景顏色。 + let result: Color = { + switch locale { + case "zh-Hans": return Color.red + case "zh-Hant": return Color.blue + case "ja": return Color.pink + default: return Color.accentColor + } + }() + return result.opacity(0.85) + } + + public var attributedStringForSwiftUIBackports: some View { + var result: some View { + ZStack(alignment: .leading) { + if isSelected { + themeColorBackports.cornerRadius(6) + VStack(spacing: 0) { + HStack(spacing: 4) { + Text(key).font(.custom("Menlo", size: fontSizeKey)) + .foregroundColor(Color.white.opacity(0.8)).lineLimit(1) + Text(displayedText).font(.system(size: fontSizeCandidate)) + .foregroundColor(Color(white: 1)).lineLimit(1) + }.padding(4).foregroundColor(Color(white: 0.9)) + } + } else { + VStack(spacing: 0) { + HStack(spacing: 4) { + Text(key).font(.custom("Menlo", size: fontSizeKey)) + .foregroundColor(Color.secondary).lineLimit(1) + Text(displayedText).font(.system(size: fontSizeCandidate)) + .foregroundColor(Color.primary).lineLimit(1) + }.padding(4).foregroundColor(Color(white: 0.9)) + } + } + }.fixedSize(horizontal: false, vertical: true) + } + return result } } diff --git a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/CandidateCellDataExtension.swift b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/CandidateCellDataExtension.swift deleted file mode 100644 index 98364cf1..00000000 --- a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/CandidateCellDataExtension.swift +++ /dev/null @@ -1,37 +0,0 @@ -// (c) 2022 and onwards The vChewing Project (MIT-NTL License). -// ==================== -// This code is released under the MIT license (SPDX-License-Identifier: MIT) -// ... with NTL restriction stating that: -// No trademark license is granted to use the trade names, trademarks, service -// marks, or product names of Contributor, except as required to fulfill notice -// requirements defined in MIT License. - -import Shared -import SwiftUI - -@available(macOS 12, *) -extension CandidateCellData { - public var attributedStringForSwiftUI: some View { - var result: some View { - ZStack(alignment: .leading) { - if isSelected { - Color(nsColor: CandidateCellData.highlightBackground).ignoresSafeArea().cornerRadius(6) - } - VStack(spacing: 0) { - HStack(spacing: 4) { - if UserDefaults.standard.bool(forKey: UserDef.kHandleDefaultCandidateFontsByLangIdentifier.rawValue) { - Text(AttributedString(attributedStringHeader)) - Text(AttributedString(attributedString)) - } else { - Text(key).font(.system(size: fontSizeKey).monospaced()) - .foregroundColor(.init(nsColor: fontColorKey)).lineLimit(1) - Text(displayedText).font(.system(size: fontSizeCandidate)) - .foregroundColor(.init(nsColor: fontColorCandidate)).lineLimit(1) - } - }.padding(4) - } - }.fixedSize(horizontal: false, vertical: true) - } - return result - } -} diff --git a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates_Backports/CandidateCellDataExtensionBackports.swift b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates_Backports/CandidateCellDataExtensionBackports.swift deleted file mode 100644 index f138fbab..00000000 --- a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates_Backports/CandidateCellDataExtensionBackports.swift +++ /dev/null @@ -1,55 +0,0 @@ -// (c) 2022 and onwards The vChewing Project (MIT-NTL License). -// ==================== -// This code is released under the MIT license (SPDX-License-Identifier: MIT) -// ... with NTL restriction stating that: -// No trademark license is granted to use the trade names, trademarks, service -// marks, or product names of Contributor, except as required to fulfill notice -// requirements defined in MIT License. - -import Shared -import SwiftUI -import SwiftUIBackports - -@available(macOS 10.15, *) -extension CandidateCellData { - public var themeColorBackports: some View { - // 設定當前高亮候選字的背景顏色。 - let result: Color = { - switch locale { - case "zh-Hans": return Color.red - case "zh-Hant": return Color.blue - case "ja": return Color.pink - default: return Color.accentColor - } - }() - return result.opacity(0.85) - } - - public var attributedStringForSwiftUIBackports: some View { - var result: some View { - ZStack(alignment: .leading) { - if isSelected { - themeColorBackports.cornerRadius(6) - VStack(spacing: 0) { - HStack(spacing: 4) { - Text(key).font(.custom("Menlo", size: fontSizeKey)) - .foregroundColor(Color.white.opacity(0.8)).lineLimit(1) - Text(displayedText).font(.system(size: fontSizeCandidate)) - .foregroundColor(Color(white: 1)).lineLimit(1) - }.padding(4).foregroundColor(Color(white: 0.9)) - } - } else { - VStack(spacing: 0) { - HStack(spacing: 4) { - Text(key).font(.custom("Menlo", size: fontSizeKey)) - .foregroundColor(Color.secondary).lineLimit(1) - Text(displayedText).font(.system(size: fontSizeCandidate)) - .foregroundColor(Color.primary).lineLimit(1) - }.padding(4).foregroundColor(Color(white: 0.9)) - } - } - }.fixedSize(horizontal: false, vertical: true) - } - return result - } -}