From 71fad2ebc24f25361078bf767063b03eaf0ed0e1 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Wed, 29 Aug 2018 17:17:47 +0200 Subject: [PATCH] GH-666 Refactor slightly --- NvimView/NvimView.xcodeproj/project.pbxproj | 4 ++ NvimView/NvimView/NvimView+Debug.swift | 65 +++++++++++++++++ NvimView/NvimView/NvimView+Draw.swift | 80 +++------------------ NvimView/NvimView/NvimView+Key.swift | 8 +++ NvimView/NvimView/NvimView.swift | 4 -- NvimView/NvimView/RunDrawer.swift | 2 +- 6 files changed, 86 insertions(+), 77 deletions(-) create mode 100644 NvimView/NvimView/NvimView+Debug.swift diff --git a/NvimView/NvimView.xcodeproj/project.pbxproj b/NvimView/NvimView.xcodeproj/project.pbxproj index 547dd2de..5c3b7588 100644 --- a/NvimView/NvimView.xcodeproj/project.pbxproj +++ b/NvimView/NvimView.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 1929B2DB631E6EB5C3452B68 /* MyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BAF033A398BFBC2A7890 /* MyView.swift */; }; 1929B30D6C4175835D1F5B21 /* MessagePackCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B44323D6611E2927EC3B /* MessagePackCommons.swift */; }; 1929B36C51BCDFCCEE974EA2 /* SwiftCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B9C55A79D97272894F5D /* SwiftCommons.swift */; }; + 1929B3B70C96A78FD63DE737 /* NvimView+Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BDC8F32F4A0D2299B5C5 /* NvimView+Debug.swift */; }; 1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */; }; 1929B40C9C30B46C0E0B9DE2 /* CellAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BE45756C88F8B43804D2 /* CellAttributes.swift */; }; 1929B434DB094D61B3977390 /* TypesetterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BFCCDE5C7145BE5A7387 /* TypesetterTest.swift */; }; @@ -187,6 +188,7 @@ 1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NvimApiExtension.swift; sourceTree = ""; }; 1929BD167BE7C6BB788DAE2A /* ProcessUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProcessUtils.swift; sourceTree = ""; }; 1929BD4C55D5EA933BFFB16B /* OldCellAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OldCellAttributes.swift; sourceTree = ""; }; + 1929BDC8F32F4A0D2299B5C5 /* NvimView+Debug.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NvimView+Debug.swift"; sourceTree = ""; }; 1929BDE2C6003A6EDC02129C /* ColorUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorUtils.swift; sourceTree = ""; }; 1929BE45756C88F8B43804D2 /* CellAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellAttributes.swift; sourceTree = ""; }; 1929BFCCDE5C7145BE5A7387 /* TypesetterTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypesetterTest.swift; sourceTree = ""; }; @@ -387,6 +389,7 @@ 1929BD4C55D5EA933BFFB16B /* OldCellAttributes.swift */, 1929B4355610D4E5607EAA40 /* Drawing */, 1929B2BA05AC3E1A11D6EBFC /* Commons */, + 1929BDC8F32F4A0D2299B5C5 /* NvimView+Debug.swift */, ); path = NvimView; sourceTree = ""; @@ -667,6 +670,7 @@ 1929BC8B14CA31C283455CF5 /* RxSwiftCommons.swift in Sources */, 1929B83EAD32DC419FEC68DB /* CocoaCommons.swift in Sources */, 1929BAB9A0399206FB7EBC76 /* CellAttributesCollection.swift in Sources */, + 1929B3B70C96A78FD63DE737 /* NvimView+Debug.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/NvimView/NvimView/NvimView+Debug.swift b/NvimView/NvimView/NvimView+Debug.swift new file mode 100644 index 00000000..504949da --- /dev/null +++ b/NvimView/NvimView/NvimView+Debug.swift @@ -0,0 +1,65 @@ +/** + * Tae Won Ha - http://taewon.de - @hataewon + * See LICENSE + */ + +import Cocoa + +#if DEBUG +extension NvimView { + + private func draw(cellGridIn context: CGContext) { + context.saveGState() + defer { context.restoreGState() } + + let color = NSColor.magenta.cgColor + context.setFillColor(color) + + let discreteSize = self.discreteSize(size: self.bounds.size) + var lines = [ + CGRect(x: 0 + self.xOffset, y: 0, width: 1, height: self.bounds.height), + CGRect( + x: self.bounds.width - 1 + self.xOffset, + y: 0, + width: 1, + height: self.bounds.height + ), + CGRect( + x: 0, + y: self.bounds.height - 1 - self.yOffset, + width: self.bounds.width, + height: 1 + ), + CGRect( + x: 0, + y: self.bounds.height - 1 - self.yOffset + - CGFloat(discreteSize.height) * self.self.cellSize.height, + width: self.bounds.width, + height: 1 + ), + ] + + for row in 0...discreteSize.height { + for col in 0...discreteSize.width { + lines.append(contentsOf: [ + CGRect( + x: CGFloat(col) * self.cellSize.width + self.xOffset - 1, + y: 0, + width: 1, + height: self.bounds.height + ), + CGRect( + x: 0, + y: self.bounds.height - 1 + - self.yOffset - CGFloat(row) * self.self.cellSize.height, + width: self.bounds.width, + height: 1 + ), + ]) + } + } + + lines.forEach { $0.fill() } + } +} +#endif diff --git a/NvimView/NvimView/NvimView+Draw.swift b/NvimView/NvimView/NvimView+Draw.swift index cc8348b3..640124f0 100644 --- a/NvimView/NvimView/NvimView+Draw.swift +++ b/NvimView/NvimView/NvimView+Draw.swift @@ -12,11 +12,9 @@ extension NvimView { } override public func draw(_ dirtyUnionRect: NSRect) { - guard self.ugrid.hasData else { - return - } + guard self.ugrid.hasData else { return } - let context = NSGraphicsContext.current!.cgContext + guard let context = NSGraphicsContext.current?.cgContext else { return } context.saveGState() defer { context.restoreGState() } @@ -30,9 +28,9 @@ extension NvimView { return } - // When both anti-aliasing and font smoothing is turned on, then the "Use LCD font smoothing - // when available" setting is used to render texts, - // cf. chapter 11 from "Programming with Quartz". + // When both anti-aliasing and font smoothing is turned on, + // then the "Use LCD font smoothing when available" setting is used + // to render texts, cf. chapter 11 from "Programming with Quartz". context.setShouldSmoothFonts(true); context.setTextDrawingMode(.fill); @@ -47,61 +45,9 @@ extension NvimView { } // self.drawCursor(context: context) -// self.draw(cellGridIn: context) - } - - private func draw(cellGridIn context: CGContext) { - context.saveGState() - defer { context.restoreGState() } - - let color = NSColor.magenta.cgColor - context.setFillColor(color) - - let discreteSize = self.discreteSize(size: self.bounds.size) - var lines = [ - CGRect(x: 0 + self.xOffset, y: 0, width: 1, height: self.bounds.height), - CGRect( - x: self.bounds.width - 1 + self.xOffset, - y: 0, - width: 1, - height: self.bounds.height - ), - CGRect( - x: 0, - y: self.bounds.height - 1 - self.yOffset, - width: self.bounds.width, - height: 1 - ), - CGRect( - x: 0, - y: self.bounds.height - 1 - self.yOffset - - CGFloat(discreteSize.height) * self.self.cellSize.height, - width: self.bounds.width, - height: 1 - ), - ] - - for row in 0...discreteSize.height { - for col in 0...discreteSize.width { - lines.append(contentsOf: [ - CGRect( - x: CGFloat(col) * self.cellSize.width + self.xOffset - 1, - y: 0, - width: 1, - height: self.bounds.height - ), - CGRect( - x: 0, - y: self.bounds.height - 1 - - self.yOffset - CGFloat(row) * self.self.cellSize.height, - width: self.bounds.width, - height: 1 - ), - ]) - } - } - - lines.forEach { $0.fill() } +#if DEBUG + // self.draw(cellGridIn: context) +#endif } private func draw(_ run: AttributesRun, in context: CGContext) { @@ -325,14 +271,6 @@ extension NvimView { ) } - func wrapNamedKeys(_ string: String) -> String { - return "<\(string)>" - } - - func vimPlainString(_ string: String) -> String { - return string.replacingOccurrences(of: "<", with: self.wrapNamedKeys("lt")) - } - func updateFontMetaData(_ newFont: NSFont) { self.drawer.font = newFont self.runDrawer.baseFont = newFont @@ -340,8 +278,6 @@ extension NvimView { self.cellSize = FontUtils.cellSize( of: newFont, linespacing: self.linespacing ) - self.descent = self.drawer.descent - self.leading = self.drawer.leading self.resizeNeoVimUi(to: self.bounds.size) } diff --git a/NvimView/NvimView/NvimView+Key.swift b/NvimView/NvimView/NvimView+Key.swift index db7a4a05..cab7c1a2 100644 --- a/NvimView/NvimView/NvimView+Key.swift +++ b/NvimView/NvimView/NvimView+Key.swift @@ -309,4 +309,12 @@ extension NvimView { return nil } + + func wrapNamedKeys(_ string: String) -> String { + return "<\(string)>" + } + + func vimPlainString(_ string: String) -> String { + return string.replacingOccurrences(of: "<", with: self.wrapNamedKeys("lt")) + } } diff --git a/NvimView/NvimView/NvimView.swift b/NvimView/NvimView/NvimView.swift index 4abd9ca4..76bae7ab 100644 --- a/NvimView/NvimView/NvimView.swift +++ b/NvimView/NvimView/NvimView.swift @@ -215,8 +215,6 @@ public class NvimView: NSView, self.cellSize = FontUtils.cellSize( of: self.font, linespacing: self.linespacing ) - self.descent = self.drawer.descent - self.leading = self.drawer.leading self.api.queue = self.queue self.bridge.stream @@ -367,8 +365,6 @@ public class NvimView: NSView, var xOffset = CGFloat(0) var yOffset = CGFloat(0) var cellSize = CGSize.zero - var descent = CGFloat(0) - var leading = CGFloat(0) var scrollGuardCounterX = 5 var scrollGuardCounterY = 5 diff --git a/NvimView/NvimView/RunDrawer.swift b/NvimView/NvimView/RunDrawer.swift index 914e85ef..b0c252d4 100644 --- a/NvimView/NvimView/RunDrawer.swift +++ b/NvimView/NvimView/RunDrawer.swift @@ -80,7 +80,7 @@ class AttributesRunDrawer { private let typesetter = Typesetter() - var cellSize: CGSize = .zero + private var cellSize: CGSize = .zero private var baselineOffset: CGFloat = 0 private var underlinePosition: CGFloat = 0 private var underlineThickness: CGFloat = 0