Skip to content

Commit

Permalink
Dynamic status bar padding #5
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 11, 2024
1 parent 5b0219b commit 02c0e6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions Edit/Modules/DocumentContent/DocumentStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SwiftUI
public final class EditorStateModel {
public var cursors: [Cursor] = []
public var visibleFrame = CGRect.zero
public var contentInsets = EdgeInsets()

public init() {
}
Expand Down
2 changes: 2 additions & 0 deletions Edit/Modules/Editor/EditorContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Status
import Theme
import UIUtility

@MainActor
struct EditorContent<Content: View>: View {
@Environment(EditorStateModel.self) private var model
@Environment(\.theme) private var theme
Expand All @@ -30,5 +31,6 @@ struct EditorContent<Content: View>: View {
.background(Color(theme.color(for: .background, context: context)))
.environment(\.documentCursors, model.cursors)
.environment(\.editorVisibleRect, model.visibleFrame)
.environment(\.statusBarPadding, model.contentInsets)
}
}
31 changes: 25 additions & 6 deletions Edit/Modules/Editor/EditorContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import UIUtility
public final class EditorContentViewController: NSViewController {
public typealias ShouldChangeTextHandler = (NSRange, String?) -> Bool

private let editorScrollView = OverlayOnlyScrollView()
private let editorScrollView = EditorScrollView()
let sourceViewController: SourceViewController
let editorState: EditorStateModel
let textSystem: TextViewSystem
Expand All @@ -30,8 +30,14 @@ public final class EditorContentViewController: NSViewController {

addChild(sourceViewController)

observer.contentBoundsChangedHandler = { [weak self] in self?.handleScrollChange($0.documentVisibleRect) }
observer.frameChangedHandler = { [weak self] in self?.handleScrollChange($0.documentVisibleRect) }
// these all have to be weak, because even though we own the scroll view, it gets injected into the view heirarchy, and can outlive this object.

observer.contentBoundsChangedHandler = { [weak self] in self?.handleVisibleRectChanged($0.documentVisibleRect) }
observer.frameChangedHandler = { [weak self] in self?.handleVisibleRectChanged($0.documentVisibleRect) }

editorScrollView.scrollerThicknessChangedHandler = { [weak self] in
self?.handleLayoutChanged()
}
}

@available(*, unavailable)
Expand All @@ -40,10 +46,12 @@ public final class EditorContentViewController: NSViewController {
}

public override func loadView() {
editorScrollView.hasVerticalScroller = true
editorScrollView.hasHorizontalScroller = true
editorScrollView.drawsBackground = true
editorScrollView.backgroundColor = .black
editorScrollView.hasVerticalScroller = true
editorScrollView.hasHorizontalScroller = true
editorScrollView.verticalScroller = ObservableScroller()
editorScrollView.horizontalScroller = ObservableScroller()

// allow the scroll view to use the entire height of a full-content windows
editorScrollView.automaticallyAdjustsContentInsets = false
Expand Down Expand Up @@ -73,8 +81,19 @@ public final class EditorContentViewController: NSViewController {
set { editorState.selectedRanges = newValue }
}

private func handleScrollChange(_ rect: CGRect) {
private func handleVisibleRectChanged(_ rect: CGRect) {
contentVisibleRectChanged(rect)
editorState.visibleFrame = rect
}

private func handleLayoutChanged() {
let margins = EdgeInsets(
top: 0.0,
leading: 0.0,
bottom: editorScrollView.horizontalMargin,
trailing: editorScrollView.verticalMargin
)

editorState.contentInsets = margins
}
}

0 comments on commit 02c0e6b

Please sign in to comment.