From c10c726816422269fb4c90a98b68cab0b04cf1e5 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Sun, 23 Jun 2024 15:15:49 -0400 Subject: [PATCH] simplify point operation --- sources/BridgingFunctions.swift | 34 +++++++++++++ sources/SquirrelInputController.swift | 3 +- sources/SquirrelView.swift | 71 +++++++-------------------- 3 files changed, 53 insertions(+), 55 deletions(-) diff --git a/sources/BridgingFunctions.swift b/sources/BridgingFunctions.swift index 39fdcdffd..b45c4e210 100644 --- a/sources/BridgingFunctions.swift +++ b/sources/BridgingFunctions.swift @@ -62,3 +62,37 @@ func ?=(left: inout T?, right: T?) { extension NSRange { static let empty = NSRange(location: NSNotFound, length: 0) } + +extension NSPoint { + static func + (lhs: Self, rhs: Self) -> Self { + Self.init(x: lhs.x + rhs.x, y: lhs.y + rhs.y) + } + static func += (lhs: inout Self, rhs: Self) { + lhs.x += rhs.x + lhs.y += rhs.y + } + static func - (lhs: Self, rhs: Self) -> Self { + Self.init(x: lhs.x - rhs.x, y: lhs.y - rhs.y) + } + static func -= (lhs: inout Self, rhs: Self) { + lhs.x -= rhs.x + lhs.y -= rhs.y + } + static func * (lhs: Self, rhs: CGFloat) -> Self { + Self.init(x: lhs.x * rhs, y: lhs.y * rhs) + } + static func *= (lhs: inout Self, rhs: CGFloat) { + lhs.x *= rhs + lhs.y *= rhs + } + static func / (lhs: Self, rhs: CGFloat) -> Self { + Self.init(x: lhs.x / rhs, y: lhs.y / rhs) + } + static func /= (lhs: inout Self, rhs: CGFloat) { + lhs.x /= rhs + lhs.y /= rhs + } + var length: CGFloat { + sqrt(pow(self.x, 2) + pow(self.y, 2)) + } +} diff --git a/sources/SquirrelInputController.swift b/sources/SquirrelInputController.swift index 5e2956e00..91a8b1bd4 100644 --- a/sources/SquirrelInputController.swift +++ b/sources/SquirrelInputController.swift @@ -555,7 +555,8 @@ private extension SquirrelInputController { if let panel = NSApp.squirrelAppDelegate.panel { panel.position = inputPos panel.inputController = self - panel.update(preedit: preedit, selRange: selRange, caretPos: caretPos, candidates: candidates, comments: comments, labels: labels, highlighted: highlighted, page: page, lastPage: lastPage, update: true) + panel.update(preedit: preedit, selRange: selRange, caretPos: caretPos, candidates: candidates, comments: comments, labels: labels, + highlighted: highlighted, page: page, lastPage: lastPage, update: true) } } } diff --git a/sources/SquirrelView.swift b/sources/SquirrelView.swift index 23b0ab43e..ec577ca54 100644 --- a/sources/SquirrelView.swift +++ b/sources/SquirrelView.swift @@ -116,6 +116,7 @@ final class SquirrelView: NSView { } // Will triger - (void)drawRect:(NSRect)dirtyRect + // swiftlint:disable:next function_parameter_count func drawView(candidateRanges: [NSRange], hilightedIndex: Int, preeditRange: NSRange, highlightedPreeditRange: NSRange, canPageUp: Bool, canPageDown: Bool) { self.candidateRanges = candidateRanges self.hilightedIndex = hilightedIndex @@ -350,23 +351,14 @@ final class SquirrelView: NSView { private extension SquirrelView { // A tweaked sign function, to winddown corner radius when the size is small - func sign(_ number: CGFloat) -> CGFloat { - if number >= 2 { - return 1 - } else if number <= -2 { - return -1 + func sign(_ number: NSPoint) -> NSPoint { + if number.length >= 2 { + return number / number.length } else { return number / 2 } } - func scale(_ value: CGFloat, multiplier: CGFloat, divider: CGFloat) -> CGFloat { - if abs(value / divider) < 1e-3 { - return multiplier / divider - } - return sign(value / divider) * multiplier / value - } - // Bezier cubic curve, which has continuous roundness func drawSmoothLines(_ vertex: [NSPoint], straightCorner: Set, alpha: CGFloat, beta rawBeta: CGFloat) -> CGPath? { guard vertex.count >= 3 else { @@ -380,17 +372,9 @@ private extension SquirrelView { var control1: NSPoint var control2: NSPoint var target = previousPoint - var diff = NSPoint(x: point.x - previousPoint.x, y: point.y - previousPoint.y) - var scaleFactor: CGFloat = 1 + var diff = point - previousPoint if straightCorner.isEmpty || !straightCorner.contains(vertex.count-1) { - if vertex.count > 3 { - target.x += sign(diff.x / beta) * beta - target.y += sign(diff.y / beta) * beta - } else { // triangle - scaleFactor = min(scale(diff.x, multiplier: beta, divider: beta), scale(diff.y, multiplier: beta, divider: beta)) - target.x += scaleFactor * diff.x - target.y += scaleFactor * diff.y - } + target += sign(diff / beta) * beta } path.move(to: target) for i in 0.. 3 { - target.x -= sign(diff.x / beta) * beta - target.y -= sign(diff.y / beta) * beta - control1.x -= sign(diff.x / beta) * alpha - control1.y -= sign(diff.y / beta) * alpha - } else { // triangle - scaleFactor = min(scale(diff.x, multiplier: beta, divider: beta), scale(diff.y, multiplier: beta, divider: beta)) - target.x -= scaleFactor * diff.x - target.y -= scaleFactor * diff.y - scaleFactor = min(scale(diff.x, multiplier: alpha, divider: beta), scale(diff.y, multiplier: alpha, divider: beta)) - control1.x -= scaleFactor * diff.x - control1.y -= scaleFactor * diff.y - } + diff = point - previousPoint + + target -= sign(diff / beta) * beta + control1 -= sign(diff / beta) * alpha path.addLine(to: target) target = point control2 = point - diff = NSPoint(x: nextPoint.x - point.x, y: nextPoint.y - point.y) - - if vertex.count > 3 { - target.x += sign(diff.x / beta) * beta - target.y += sign(diff.y / beta) * beta - control2.x += sign(diff.x / beta) * alpha - control2.y += sign(diff.y / beta) * alpha - } else { - scaleFactor = min(scale(diff.x, multiplier: beta, divider: beta), scale(diff.y, multiplier: beta, divider: beta)) - target.x += scaleFactor * diff.x - target.y += scaleFactor * diff.y - scaleFactor = min(scale(diff.x, multiplier: alpha, divider: beta), scale(diff.y, multiplier: beta, divider: beta)) - control2.x += scaleFactor * diff.x - control2.y += scaleFactor * diff.y - } + diff = nextPoint - point + + target += sign(diff / beta) * beta + control2 += sign(diff / beta) * alpha path.addCurve(to: target, control1: control1, control2: control2) } @@ -611,10 +573,10 @@ private extension SquirrelView { point = vertex[i] nextPoint = vertex[(i+1) % vertex.count] newPoint = point - displacement = direction(diff: NSPoint(x: point.x - previousPoint.x, y: point.y - previousPoint.y)) + displacement = direction(diff: point - previousPoint) newPoint.x += by * displacement.x newPoint.y += by * displacement.y - displacement = direction(diff: NSPoint(x: nextPoint.x - point.x, y: nextPoint.y - point.y)) + displacement = direction(diff: nextPoint - point) newPoint.x += by * displacement.x newPoint.y += by * displacement.y results[i] = newPoint @@ -651,6 +613,7 @@ private extension SquirrelView { } } + // swiftlint:disable:next large_tuple func linearMultilineFor(body: NSRect, leading: NSRect, trailing: NSRect) -> (Array, Array, Set, Set) { let highlightedPoints, highlightedPoints2: [NSPoint] let rightCorners, rightCorners2: Set