Skip to content

Commit

Permalink
cleanup code & prepare for releasing
Browse files Browse the repository at this point in the history
  • Loading branch information
unixzii authored and ktiays committed Feb 11, 2022
1 parent b10bc0d commit 4b3819e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
7 changes: 2 additions & 5 deletions Sources/CyanUI/HexView/DrawingHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HexViewDrawingHelper {
}
}

struct Position: Comparable {
struct Position: Comparable, Equatable {
let line: Int
let column: Int

Expand Down Expand Up @@ -196,7 +196,7 @@ class HexViewDrawingHelper {

// Octets area:
var startX = gutterWidth
var endX = origin(of: .ascii, at: .init(line: 0, column: 0)).x
let endX = origin(of: .ascii, at: .init(line: 0, column: 0)).x
if pointX < endX {
let column = min(max(Int(floor((pointX - startX) / (charWidth * 3))), 0), octetsPerLine - 1)
return (.hex, .init(line: line, column: column))
Expand Down Expand Up @@ -239,9 +239,6 @@ class _HexViewComponentLineLayer: CALayer {
private func commomInit() {
let charWidth = drawingHelper.charWidth
let lineHeight = drawingHelper.lineHeight
let font = drawingHelper.font!
let fontSize = font.pointSize
let screenScale = NSScreen.main?.backingScaleFactor ?? 2

// Populate components.
var currentComponentWidth = charWidth * 2
Expand Down
47 changes: 44 additions & 3 deletions Sources/CyanUI/HexView/HexView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

import Cocoa

/// A set of optional methods that hex view delegates can use to
/// manage selection, and more.
@objc public protocol HexViewDelegate: NSObjectProtocol {

/// Tells the delegate when the selection has changed in the hex view.
@objc optional func selectionDidChangeInHexView(_ view: HexView)

}

/// A view that allows inspecting large data in hex view.
public class HexView: NSView {

public override var isFlipped: Bool {
Expand Down Expand Up @@ -35,12 +45,37 @@ public class HexView: NSView {
private var isMouseDragging = false
private var cursorBlinkState = false

/// The data provider of the receiver’s content.
public var dataProvider: HexViewDataProvider? {
didSet {
reloadData()
}
}

/// The receiver’s delegate.
public weak var delegate: HexViewDelegate?

/// The range of bytes selected in the receiver.
public var selectedRange: NSRange? {
// No selections and inserting points.
guard let visualSelectionEnd = visualSelectionEnd else {
return nil
}

// Has inserting points but no selections.
var dataSelectionEnd = drawingHelper.dataIndex(at: visualSelectionEnd)
guard let visualSelectionStart = visualSelectionStart else {
return .init(location: dataSelectionEnd, length: 1)
}

// Has selections.
let selectionEnd = max(visualSelectionEnd, visualSelectionStart)
let selectionStart = min(visualSelectionEnd, visualSelectionStart)
dataSelectionEnd = drawingHelper.dataIndex(at: selectionEnd)
let dataSelectionStart = drawingHelper.dataIndex(at: selectionStart)
return .init(location: dataSelectionStart, length: dataSelectionEnd - dataSelectionStart + 1)
}

deinit {
cursorBlinkTimer?.invalidate()
}
Expand Down Expand Up @@ -96,7 +131,6 @@ public class HexView: NSView {
}

// Gather drawing informations.
let gutterWidth = drawingHelper.gutterWidth
let lineHeight = drawingHelper.lineHeight
let charWidth = drawingHelper.charWidth

Expand Down Expand Up @@ -188,6 +222,8 @@ public class HexView: NSView {

setNeedsDisplay(viewportBounds)
startCursorBlinking()

delegate?.selectionDidChangeInHexView?(self)
}
}

Expand All @@ -204,10 +240,16 @@ public class HexView: NSView {

let point = convert(event.locationInWindow, from: nil)
if let hitComponent = drawingHelper.hitTest(at: point) {
visualSelectionEnd = clampCursorPosition(hitComponent.1)
let hitPosition = clampCursorPosition(hitComponent.1)
guard hitPosition != visualSelectionEnd else {
return
}
visualSelectionEnd = hitPosition
componentUnderCursor = hitComponent.0

setNeedsDisplay(viewportBounds)

delegate?.selectionDidChangeInHexView?(self)
}
}

Expand Down Expand Up @@ -256,7 +298,6 @@ public class HexView: NSView {
}

let lineHeight = drawingHelper.lineHeight
let gutterWidth = drawingHelper.gutterWidth
let viewportBounds = self.viewportBounds

func addLineLayer(for line: Int, forwards: Bool = true) {
Expand Down

0 comments on commit 4b3819e

Please sign in to comment.