Skip to content

Commit c688bcb

Browse files
committed
Use Constraint For Minimap ContentView Height
1 parent 8eef187 commit c688bcb

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
// A fast, efficient, text view for code.
1818
.package(
1919
url: "https://github.com/CodeEditApp/CodeEditTextView.git",
20-
from: "0.10.0"
20+
from: "0.10.1"
2121
),
2222
// tree-sitter languages
2323
.package(

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension TextViewController {
6969
func setUpConstraints() {
7070
guard let findViewController else { return }
7171

72-
let maxWidthConstraint = minimapView.widthAnchor.constraint(lessThanOrEqualToConstant: 140)
72+
let maxWidthConstraint = minimapView.widthAnchor.constraint(lessThanOrEqualToConstant: MinimapView.maxWidth)
7373
let relativeWidthConstraint = minimapView.widthAnchor.constraint(
7474
equalTo: view.widthAnchor,
7575
multiplier: 0.17

Sources/CodeEditSourceEditor/Minimap/MinimapView.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import CodeEditTextView
2525
///
2626
/// The minimap can be styled using an ``EditorTheme``. See ``setTheme(_:)`` for use and colors used by this view.
2727
public class MinimapView: FlippedNSView {
28+
static let maxWidth: CGFloat = 140.0
29+
2830
weak var textView: TextView?
2931

3032
/// The container scrollview for the minimap contents.
@@ -38,6 +40,7 @@ public class MinimapView: FlippedNSView {
3840

3941
/// Responder for a drag gesture on the ``documentVisibleView``.
4042
var documentVisibleViewPanGesture: NSPanGestureRecognizer?
43+
var contentViewHeightConstraint: NSLayoutConstraint?
4144

4245
/// The layout manager that uses the ``lineRenderer`` to render and layout lines.
4346
var layoutManager: TextLayoutManager?
@@ -162,6 +165,8 @@ public class MinimapView: FlippedNSView {
162165
// MARK: - Constraints
163166

164167
private func setUpConstraints() {
168+
let contentViewHeightConstraint = contentView.heightAnchor.constraint(equalToConstant: 1.0)
169+
self.contentViewHeightConstraint = contentViewHeightConstraint
165170
NSLayoutConstraint.activate([
166171
// Constrain to all sides
167172
scrollView.topAnchor.constraint(equalTo: topAnchor),
@@ -172,6 +177,7 @@ public class MinimapView: FlippedNSView {
172177
// Scrolling, but match width
173178
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
174179
contentView.trailingAnchor.constraint(equalTo: trailingAnchor),
180+
contentViewHeightConstraint,
175181

176182
// Y position set manually
177183
documentVisibleView.leadingAnchor.constraint(equalTo: leadingAnchor),
@@ -272,7 +278,8 @@ public class MinimapView: FlippedNSView {
272278
/// cached height.
273279
func updateContentViewHeight() {
274280
guard let estimatedContentHeight = layoutManager?.estimatedHeight(),
275-
let editorEstimatedHeight = textView?.layoutManager.estimatedHeight() else {
281+
let editorEstimatedHeight = textView?.layoutManager.estimatedHeight(),
282+
let contentViewHeightConstraint else {
276283
return
277284
}
278285
let overscrollAmount = textView?.overscrollAmount ?? 0.0
@@ -286,9 +293,12 @@ public class MinimapView: FlippedNSView {
286293
).pixelAligned
287294

288295
// Only update a frame if needed
289-
if contentView.frame.height != newFrame.height && height.isFinite && height < (textView?.frame.height ?? 0.0) {
290-
contentView.frame = newFrame
291-
layout()
296+
if contentViewHeightConstraint.constant != newFrame.height
297+
&& height.isFinite
298+
&& height < (textView?.frame.height ?? 0.0) {
299+
contentViewHeightConstraint.constant = newFrame.height
300+
contentViewHeightConstraint.isActive = true
301+
updateConstraints()
292302
}
293303
}
294304

Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ final class TextViewControllerTests: XCTestCase {
3636
)
3737

3838
controller.loadView()
39+
controller.view.frame = NSRect(x: 0, y: 0, width: 1000, height: 1000)
40+
controller.view.layoutSubtreeIfNeeded()
3941
}
4042

4143
// MARK: Capture Names
@@ -445,14 +447,18 @@ final class TextViewControllerTests: XCTestCase {
445447
// MARK: - Minimap
446448

447449
func test_minimapToggle() {
448-
controller.view.frame = NSRect(x: 0, y: 0, width: 1000, height: 1000)
449450
XCTAssertFalse(controller.minimapView.isHidden)
451+
XCTAssertEqual(controller.minimapView.frame.width, MinimapView.maxWidth)
452+
XCTAssertEqual(controller.textViewInsets.right, MinimapView.maxWidth)
450453

451454
controller.showMinimap = false
452455
XCTAssertTrue(controller.minimapView.isHidden)
456+
XCTAssertEqual(controller.textViewInsets.right, 0)
453457

454458
controller.showMinimap = true
455459
XCTAssertFalse(controller.minimapView.isHidden)
460+
XCTAssertEqual(controller.minimapView.frame.width, MinimapView.maxWidth)
461+
XCTAssertEqual(controller.textViewInsets.right, MinimapView.maxWidth)
456462
}
457463
}
458464
// swiftlint:enable all

0 commit comments

Comments
 (0)