InputState // Simplify validToWrite() and allowedMarkRange().
This commit is contained in:
parent
5a40f24ff8
commit
49ab499b13
|
@ -151,11 +151,9 @@ class InputState {
|
||||||
|
|
||||||
// MARK: -
|
// MARK: -
|
||||||
|
|
||||||
private let kMinMarkRangeLength = 2
|
|
||||||
private let kMaxMarkRangeLength = mgrPrefs.maxCandidateLength
|
|
||||||
|
|
||||||
/// Represents that the user is marking a range in the composing buffer.
|
/// Represents that the user is marking a range in the composing buffer.
|
||||||
class Marking: NotEmpty {
|
class Marking: NotEmpty {
|
||||||
|
private var allowedMarkRange = 2...mgrPrefs.maxCandidateLength
|
||||||
private(set) var markerIndex: Int = 0 { didSet { markerIndex = max(markerIndex, 0) } }
|
private(set) var markerIndex: Int = 0 { didSet { markerIndex = max(markerIndex, 0) } }
|
||||||
private(set) var markedRange: Range<Int>
|
private(set) var markedRange: Range<Int>
|
||||||
private var deleteTargetExists = false
|
private var deleteTargetExists = false
|
||||||
|
@ -177,20 +175,20 @@ class InputState {
|
||||||
}
|
}
|
||||||
|
|
||||||
let text = composingBuffer.utf16SubString(with: markedRange)
|
let text = composingBuffer.utf16SubString(with: markedRange)
|
||||||
if markedRange.count < kMinMarkRangeLength {
|
if markedRange.count < allowedMarkRange.lowerBound {
|
||||||
ctlInputMethod.tooltipController.setColor(state: .denialInsufficiency)
|
ctlInputMethod.tooltipController.setColor(state: .denialInsufficiency)
|
||||||
return String(
|
return String(
|
||||||
format: NSLocalizedString(
|
format: NSLocalizedString(
|
||||||
"\"%@\" length must ≥ 2 for a user phrase.", comment: ""
|
"\"%@\" length must ≥ 2 for a user phrase.", comment: ""
|
||||||
), text
|
), text
|
||||||
)
|
)
|
||||||
} else if markedRange.count > kMaxMarkRangeLength {
|
} else if markedRange.count > allowedMarkRange.upperBound {
|
||||||
ctlInputMethod.tooltipController.setColor(state: .denialOverflow)
|
ctlInputMethod.tooltipController.setColor(state: .denialOverflow)
|
||||||
return String(
|
return String(
|
||||||
format: NSLocalizedString(
|
format: NSLocalizedString(
|
||||||
"\"%@\" length should ≤ %d for a user phrase.", comment: ""
|
"\"%@\" length should ≤ %d for a user phrase.", comment: ""
|
||||||
),
|
),
|
||||||
text, kMaxMarkRangeLength
|
text, allowedMarkRange.upperBound
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,20 +275,12 @@ class InputState {
|
||||||
/// from the amount of Bopomofo readings. In this case, the range
|
/// from the amount of Bopomofo readings. In this case, the range
|
||||||
/// in the composing buffer and the readings could not match, so
|
/// in the composing buffer and the readings could not match, so
|
||||||
/// we disable the function to write user phrases in this case.
|
/// we disable the function to write user phrases in this case.
|
||||||
if composingBuffer.count != readings.count {
|
/// 這裡的 deleteTargetExists 是防止使用者排除「詞庫內尚未存在的詞」,
|
||||||
return false
|
/// 免得使用者誤操作之後靠北「我怎麼敲不了這個詞?」之類的。
|
||||||
}
|
((composingBuffer.count != readings.count)
|
||||||
if markedRange.count < kMinMarkRangeLength {
|
|| (ctlInputMethod.areWeDeleting && !deleteTargetExists))
|
||||||
return false
|
? false
|
||||||
}
|
: allowedMarkRange.contains(markedRange.count)
|
||||||
if markedRange.count > kMaxMarkRangeLength {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if ctlInputMethod.areWeDeleting, !deleteTargetExists {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return markedRange.count >= kMinMarkRangeLength
|
|
||||||
&& markedRange.count <= kMaxMarkRangeLength
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var chkIfUserPhraseExists: Bool {
|
var chkIfUserPhraseExists: Bool {
|
||||||
|
|
Loading…
Reference in New Issue