TDKCandidates // UI accommodation with long reverse-lookup results.

- Use array to handle reverse-lookup UI representation.
- Remove SessionCtl.annotate().
This commit is contained in:
ShikiSuen 2022-10-24 10:06:51 +08:00
parent c9c81803b8
commit 5fbc2fb728
9 changed files with 106 additions and 44 deletions

View File

@ -14,7 +14,7 @@ open class CtlCandidate: NSWindowController, CtlCandidateProtocol {
open var currentLayout: NSUserInterfaceLayoutOrientation = .horizontal open var currentLayout: NSUserInterfaceLayoutOrientation = .horizontal
open var locale: String = "" open var locale: String = ""
open var useLangIdentifier: Bool = false open var useLangIdentifier: Bool = false
open var reverseLookupResult: String = "" open var reverseLookupResult: [String] = []
open func highlightedColor() -> NSColor { open func highlightedColor() -> NSColor {
var result = NSColor.alternateSelectedControlColor var result = NSColor.alternateSelectedControlColor

View File

@ -117,7 +117,7 @@ public class CtlCandidateTDK: CtlCandidate {
override open func updateDisplay() { override open func updateDisplay() {
guard let window = window else { return } guard let window = window else { return }
reverseLookupResult = delegate?.annotate(for: currentSelectedCandidateText) ?? "" reverseLookupResult = delegate?.reverseLookup(for: currentSelectedCandidateText) ?? []
switch currentLayout { switch currentLayout {
case .horizontal: case .horizontal:
DispatchQueue.main.async { [self] in DispatchQueue.main.async { [self] in

View File

@ -36,9 +36,10 @@ struct CandidatePoolViewUIHorizontal_Previews: PreviewProvider {
@available(macOS 12, *) @available(macOS 12, *)
public struct VwrCandidateHorizontal: View { public struct VwrCandidateHorizontal: View {
public var controller: CtlCandidateTDK public var controller: CtlCandidateTDK
@Environment(\.colorScheme) var colorScheme
@State public var thePool: CandidatePool @State public var thePool: CandidatePool
@State public var tooltip: String = "" @State public var tooltip: String = ""
@State public var reverseLookupResult: String = "" @State public var reverseLookupResult: [String] = ["MLGB"]
private var positionLabel: String { private var positionLabel: String {
(thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description (thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description
@ -90,20 +91,33 @@ public struct VwrCandidateHorizontal: View {
} }
} }
} }
.fixedSize(horizontal: false, vertical: true).padding(5) .fixedSize(horizontal: false, vertical: true)
.background(Color(nsColor: NSColor.controlBackgroundColor).ignoresSafeArea()) .padding([.horizontal], 5).padding([.top], 5).padding([.bottom], -1)
if controller.delegate?.showReverseLookupResult ?? true {
ZStack(alignment: .leading) {
Color(white: colorScheme == .dark ? 0.15 : 0.97)
HStack(alignment: .center, spacing: 4) {
Text("")
ForEach(reverseLookupResult, id: \.self) { currentResult in
ZStack(alignment: .center) {
Color(white: colorScheme == .dark ? 0.3 : 0.9).cornerRadius(3)
Text(" \(currentResult) ").lineLimit(1)
}.fixedSize()
}
}
.font(.system(size: max(CandidateCellData.unifiedSize * 0.6, 9)))
.padding([.horizontal], 4).padding([.vertical], 4)
.foregroundColor(colorScheme == .light ? Color(white: 0.1) : Color(white: 0.9))
}
}
ZStack(alignment: .trailing) { ZStack(alignment: .trailing) {
Color(nsColor: tooltip.isEmpty ? .windowBackgroundColor : CandidateCellData.highlightBackground) Color(nsColor: tooltip.isEmpty ? .windowBackgroundColor : CandidateCellData.highlightBackground)
.ignoresSafeArea() .ignoresSafeArea()
HStack(alignment: .bottom) { HStack(alignment: .center) {
if !tooltip.isEmpty { if !tooltip.isEmpty {
Text(tooltip).lineLimit(1) Text(tooltip).lineLimit(1)
Spacer() Spacer()
} }
if !reverseLookupResult.isEmpty, !(controller.delegate?.isVerticalTyping ?? true) {
Text(reverseLookupResult).lineLimit(1)
Spacer()
}
Text(positionLabel).lineLimit(1) Text(positionLabel).lineLimit(1)
} }
.font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold)) .font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold))
@ -114,6 +128,7 @@ public struct VwrCandidateHorizontal: View {
.fixedSize(horizontal: false, vertical: true) .fixedSize(horizontal: false, vertical: true)
} }
.frame(minWidth: thePool.maxWindowWidth, maxWidth: thePool.maxWindowWidth) .frame(minWidth: thePool.maxWindowWidth, maxWidth: thePool.maxWindowWidth)
.background(Color(nsColor: NSColor.controlBackgroundColor).ignoresSafeArea())
.overlay( .overlay(
RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1) RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1)
) )

View File

@ -35,9 +35,10 @@ struct CandidatePoolViewUIVertical_Previews: PreviewProvider {
@available(macOS 12, *) @available(macOS 12, *)
public struct VwrCandidateVertical: View { public struct VwrCandidateVertical: View {
public var controller: CtlCandidateTDK public var controller: CtlCandidateTDK
@Environment(\.colorScheme) var colorScheme
@State public var thePool: CandidatePool @State public var thePool: CandidatePool
@State public var tooltip: String = "" @State public var tooltip: String = ""
@State public var reverseLookupResult: String = "" @State public var reverseLookupResult: [String] = ["MLGB"]
private var positionLabel: String { private var positionLabel: String {
(thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description (thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description
@ -99,19 +100,31 @@ public struct VwrCandidateVertical: View {
} }
} }
.fixedSize(horizontal: true, vertical: false).padding(5) .fixedSize(horizontal: true, vertical: false).padding(5)
.background(Color(nsColor: NSColor.controlBackgroundColor).ignoresSafeArea()) if controller.delegate?.showReverseLookupResult ?? true {
ZStack(alignment: .leading) {
Color(white: colorScheme == .dark ? 0.15 : 0.97)
HStack(alignment: .center, spacing: 4) {
Text("")
ForEach(reverseLookupResult, id: \.self) { currentResult in
ZStack(alignment: .center) {
Color(white: colorScheme == .dark ? 0.3 : 0.9).cornerRadius(3)
Text(" \(currentResult) ").lineLimit(1)
}.fixedSize()
}
}
.font(.system(size: max(CandidateCellData.unifiedSize * 0.6, 9)))
.padding([.horizontal], 4).padding([.vertical], 4)
.foregroundColor(colorScheme == .light ? Color(white: 0.1) : Color(white: 0.9))
}
}
ZStack(alignment: .trailing) { ZStack(alignment: .trailing) {
Color(nsColor: tooltip.isEmpty ? .windowBackgroundColor : CandidateCellData.highlightBackground) Color(nsColor: tooltip.isEmpty ? .windowBackgroundColor : CandidateCellData.highlightBackground)
.ignoresSafeArea() .ignoresSafeArea()
HStack(alignment: .bottom) { HStack(alignment: .center) {
if !tooltip.isEmpty { if !tooltip.isEmpty {
Text(tooltip).lineLimit(1) Text(tooltip).lineLimit(1)
Spacer() Spacer()
} }
if !reverseLookupResult.isEmpty, !(controller.delegate?.isVerticalTyping ?? true) {
Text(reverseLookupResult).lineLimit(1)
Spacer()
}
Text(positionLabel).lineLimit(1) Text(positionLabel).lineLimit(1)
} }
.font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold)) .font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold))
@ -121,6 +134,7 @@ public struct VwrCandidateVertical: View {
} }
.fixedSize(horizontal: false, vertical: true) .fixedSize(horizontal: false, vertical: true)
} }
.background(Color(nsColor: NSColor.controlBackgroundColor).ignoresSafeArea())
.overlay( .overlay(
RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1) RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1)
) )

View File

@ -36,11 +36,11 @@ struct CandidatePoolViewUIHorizontalBackports_Previews: PreviewProvider {
@available(macOS 10.15, *) @available(macOS 10.15, *)
public struct VwrCandidateHorizontalBackports: View { public struct VwrCandidateHorizontalBackports: View {
@Environment(\.colorScheme) var colorScheme
public var controller: CtlCandidateTDK public var controller: CtlCandidateTDK
@Environment(\.colorScheme) var colorScheme
@State public var thePool: CandidatePool @State public var thePool: CandidatePool
@State public var tooltip: String = "" @State public var tooltip: String = ""
@State public var reverseLookupResult: String = "" @State public var reverseLookupResult: [String] = ["MLGB"]
private var positionLabel: String { private var positionLabel: String {
(thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description (thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description
@ -92,8 +92,25 @@ public struct VwrCandidateHorizontalBackports: View {
} }
} }
} }
.fixedSize(horizontal: false, vertical: true).padding(5) .fixedSize(horizontal: false, vertical: true)
.background(Color(white: colorScheme == .dark ? 0.1 : 1)) .padding([.horizontal], 5).padding([.top], 5).padding([.bottom], -1)
if controller.delegate?.showReverseLookupResult ?? true {
ZStack(alignment: .leading) {
Color(white: colorScheme == .dark ? 0.15 : 0.97)
HStack(alignment: .center, spacing: 4) {
Text("")
ForEach(reverseLookupResult, id: \.self) { currentResult in
ZStack(alignment: .center) {
Color(white: colorScheme == .dark ? 0.3 : 0.9).cornerRadius(3)
Text(" \(currentResult) ").lineLimit(1)
}.fixedSize()
}
}
.font(.system(size: max(CandidateCellData.unifiedSize * 0.6, 9)))
.padding([.horizontal], 4).padding([.vertical], 4)
.foregroundColor(colorScheme == .light ? Color(white: 0.1) : Color(white: 0.9))
}
}
ZStack(alignment: .trailing) { ZStack(alignment: .trailing) {
if tooltip.isEmpty { if tooltip.isEmpty {
Color(white: colorScheme == .dark ? 0.2 : 0.9) Color(white: colorScheme == .dark ? 0.2 : 0.9)
@ -101,15 +118,11 @@ public struct VwrCandidateHorizontalBackports: View {
Color(white: colorScheme == .dark ? 0.0 : 1) Color(white: colorScheme == .dark ? 0.0 : 1)
controller.highlightedColorUIBackports controller.highlightedColorUIBackports
} }
HStack(alignment: .bottom) { HStack(alignment: .center) {
if !tooltip.isEmpty { if !tooltip.isEmpty {
Text(tooltip).lineLimit(1) Text(tooltip).lineLimit(1)
Spacer() Spacer()
} }
if !reverseLookupResult.isEmpty, !(controller.delegate?.isVerticalTyping ?? true) {
Text(reverseLookupResult).lineLimit(1)
Spacer()
}
Text(positionLabel).lineLimit(1) Text(positionLabel).lineLimit(1)
} }
.font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold)) .font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold))
@ -120,6 +133,7 @@ public struct VwrCandidateHorizontalBackports: View {
.fixedSize(horizontal: false, vertical: true) .fixedSize(horizontal: false, vertical: true)
} }
.frame(minWidth: thePool.maxWindowWidth, maxWidth: thePool.maxWindowWidth) .frame(minWidth: thePool.maxWindowWidth, maxWidth: thePool.maxWindowWidth)
.background(Color(white: colorScheme == .dark ? 0.1 : 1))
.overlay( .overlay(
RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1) RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1)
) )

View File

@ -35,11 +35,11 @@ struct CandidatePoolViewUIVerticalBackports_Previews: PreviewProvider {
@available(macOS 10.15, *) @available(macOS 10.15, *)
public struct VwrCandidateVerticalBackports: View { public struct VwrCandidateVerticalBackports: View {
@Environment(\.colorScheme) var colorScheme
public var controller: CtlCandidateTDK public var controller: CtlCandidateTDK
@Environment(\.colorScheme) var colorScheme
@State public var thePool: CandidatePool @State public var thePool: CandidatePool
@State public var tooltip: String = "" @State public var tooltip: String = ""
@State public var reverseLookupResult: String = "" @State public var reverseLookupResult: [String] = ["MLGB"]
private var positionLabel: String { private var positionLabel: String {
(thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description (thePool.highlightedIndex + 1).description + "/" + thePool.candidateDataAll.count.description
@ -101,7 +101,23 @@ public struct VwrCandidateVerticalBackports: View {
} }
} }
.fixedSize(horizontal: true, vertical: false).padding(5) .fixedSize(horizontal: true, vertical: false).padding(5)
.background(Color(white: colorScheme == .dark ? 0.1 : 1)) if controller.delegate?.showReverseLookupResult ?? true {
ZStack(alignment: .leading) {
Color(white: colorScheme == .dark ? 0.15 : 0.97)
HStack(alignment: .center, spacing: 4) {
Text("")
ForEach(reverseLookupResult, id: \.self) { currentResult in
ZStack(alignment: .center) {
Color(white: colorScheme == .dark ? 0.3 : 0.9).cornerRadius(3)
Text(" \(currentResult) ").lineLimit(1)
}.fixedSize()
}
}
.font(.system(size: max(CandidateCellData.unifiedSize * 0.6, 9)))
.padding([.horizontal], 4).padding([.vertical], 4)
.foregroundColor(colorScheme == .light ? Color(white: 0.1) : Color(white: 0.9))
}
}
ZStack(alignment: .trailing) { ZStack(alignment: .trailing) {
if tooltip.isEmpty { if tooltip.isEmpty {
Color(white: colorScheme == .dark ? 0.2 : 0.9) Color(white: colorScheme == .dark ? 0.2 : 0.9)
@ -109,15 +125,11 @@ public struct VwrCandidateVerticalBackports: View {
Color(white: colorScheme == .dark ? 0.0 : 1) Color(white: colorScheme == .dark ? 0.0 : 1)
controller.highlightedColorUIBackports controller.highlightedColorUIBackports
} }
HStack(alignment: .bottom) { HStack(alignment: .center) {
if !tooltip.isEmpty { if !tooltip.isEmpty {
Text(tooltip).lineLimit(1) Text(tooltip).lineLimit(1)
Spacer() Spacer()
} }
if !reverseLookupResult.isEmpty, !(controller.delegate?.isVerticalTyping ?? true) {
Text(reverseLookupResult).lineLimit(1)
Spacer()
}
Text(positionLabel).lineLimit(1) Text(positionLabel).lineLimit(1)
} }
.font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold)) .font(.system(size: max(CandidateCellData.unifiedSize * 0.7, 11), weight: .bold))
@ -127,6 +139,7 @@ public struct VwrCandidateVerticalBackports: View {
} }
.fixedSize(horizontal: false, vertical: true) .fixedSize(horizontal: false, vertical: true)
} }
.background(Color(white: colorScheme == .dark ? 0.1 : 1))
.overlay( .overlay(
RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1) RoundedRectangle(cornerRadius: 10).stroke(.white.opacity(0.2), lineWidth: 1)
) )

View File

@ -12,14 +12,15 @@ public protocol CtlCandidateDelegate {
func candidatePairs(conv: Bool) -> [(String, String)] func candidatePairs(conv: Bool) -> [(String, String)]
func candidatePairSelected(at index: Int) func candidatePairSelected(at index: Int)
func candidates(_ sender: Any!) -> [Any]! func candidates(_ sender: Any!) -> [Any]!
@discardableResult func annotate(for value: String) -> String @discardableResult func reverseLookup(for value: String) -> [String]
var selectionKeys: String { get } var selectionKeys: String { get }
var isVerticalTyping: Bool { get } var isVerticalTyping: Bool { get }
var showReverseLookupResult: Bool { get }
} }
public protocol CtlCandidateProtocol { public protocol CtlCandidateProtocol {
var tooltip: String { get set } var tooltip: String { get set }
var reverseLookupResult: String { get set } var reverseLookupResult: [String] { get set }
var locale: String { get set } var locale: String { get set }
var currentLayout: NSUserInterfaceLayoutOrientation { get set } var currentLayout: NSUserInterfaceLayoutOrientation { get set }
var delegate: CtlCandidateDelegate? { get set } var delegate: CtlCandidateDelegate? { get set }

View File

@ -56,14 +56,19 @@ extension SessionCtl: InputHandlerDelegate {
// MARK: - Candidate Controller Delegate // MARK: - Candidate Controller Delegate
extension SessionCtl: CtlCandidateDelegate { extension SessionCtl: CtlCandidateDelegate {
@discardableResult public func annotate(for value: String) -> String { public var showReverseLookupResult: Bool {
!isVerticalTyping && PrefMgr.shared.showReverseLookupInCandidateUI
}
@discardableResult public func reverseLookup(for value: String) -> [String] {
let blankResult: [String] = []
// //
if !PrefMgr.shared.showReverseLookupInCandidateUI { return "" } if !PrefMgr.shared.showReverseLookupInCandidateUI { return blankResult }
if isVerticalTyping { return "" } // if isVerticalTyping { return blankResult } //
if value.isEmpty { return "" } // 西 if value.isEmpty { return blankResult } // 西
if value.contains("_") { return "" } if value.contains("_") { return blankResult }
guard let lookupResult = LMMgr.currentLM.currentCassette.reverseLookupMap[value] else { return "" } guard let lookupResult = LMMgr.currentLM.currentCassette.reverseLookupMap[value] else { return blankResult }
return lookupResult.stableSort(by: { $0.count < $1.count }).joined(separator: " ") return lookupResult.stableSort(by: { $0.count < $1.count })
} }
public var selectionKeys: String { public var selectionKeys: String {

View File

@ -12,7 +12,7 @@ import Shared
/// IMKCandidates bridging header Swift Package /// IMKCandidates bridging header Swift Package
public class CtlCandidateIMK: IMKCandidates, CtlCandidateProtocol { public class CtlCandidateIMK: IMKCandidates, CtlCandidateProtocol {
public var tooltip: String = "" public var tooltip: String = ""
public var reverseLookupResult: String = "" public var reverseLookupResult: [String] = []
public var locale: String = "" public var locale: String = ""
public var useLangIdentifier: Bool = false public var useLangIdentifier: Bool = false
public var currentLayout: NSUserInterfaceLayoutOrientation = .horizontal public var currentLayout: NSUserInterfaceLayoutOrientation = .horizontal