Skip to content

Commit 3f18ada

Browse files
authored
Merge pull request #11 from nodes-app/10-add-scrollbar-configuration
10 add scrollbar configuration
2 parents ddabf2f + 32c7d19 commit 3f18ada

2 files changed

Lines changed: 84 additions & 25 deletions

File tree

‎Sources/MarkdownEngine/Configuration/MarkdownEditorConfiguration.swift‎

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public struct MarkdownEditorConfiguration: Sendable {
3939
public var paragraph: ParagraphStyle
4040
public var overscroll: OverscrollPolicy
4141
public var dragSelection: DragSelectionPolicy
42-
public var contentInsets: ContentInsets
42+
public var safeAreaInsets: SafeAreaInsets
43+
public var scrollers: ScrollersPolicy
44+
public var textInsets: TextInsets
4345

4446
public init(
4547
theme: MarkdownEditorTheme = .default,
@@ -57,7 +59,9 @@ public struct MarkdownEditorConfiguration: Sendable {
5759
paragraph: ParagraphStyle = .default,
5860
overscroll: OverscrollPolicy = .default,
5961
dragSelection: DragSelectionPolicy = .default,
60-
contentInsets: ContentInsets = .default
62+
safeAreaInsets: SafeAreaInsets = .default,
63+
scrollers: ScrollersPolicy = .default,
64+
textInsets: TextInsets = .default
6165
) {
6266
self.theme = theme
6367
self.services = services
@@ -74,12 +78,58 @@ public struct MarkdownEditorConfiguration: Sendable {
7478
self.paragraph = paragraph
7579
self.overscroll = overscroll
7680
self.dragSelection = dragSelection
77-
self.contentInsets = contentInsets
81+
self.safeAreaInsets = safeAreaInsets
82+
self.scrollers = scrollers
83+
self.textInsets = textInsets
7884
}
7985

8086
public static let `default` = MarkdownEditorConfiguration()
8187
}
8288

89+
// MARK: - Scroll bars
90+
91+
/// Scroll bar visibility. Default: vertical only, autohide on.
92+
public struct ScrollersPolicy: Sendable {
93+
public var hasVerticalScroller: Bool
94+
public var hasHorizontalScroller: Bool
95+
public var autohidesScrollers: Bool
96+
97+
public init(
98+
hasVerticalScroller: Bool = true,
99+
hasHorizontalScroller: Bool = false,
100+
autohidesScrollers: Bool = true
101+
) {
102+
self.hasVerticalScroller = hasVerticalScroller
103+
self.hasHorizontalScroller = hasHorizontalScroller
104+
self.autohidesScrollers = autohidesScrollers
105+
}
106+
107+
public static let `default` = ScrollersPolicy()
108+
/// No scrollers (use with a custom scroll overlay).
109+
public static let hidden = ScrollersPolicy(hasVerticalScroller: false, hasHorizontalScroller: false)
110+
/// Vertical only — same as `.default`.
111+
public static let vertical = ScrollersPolicy(hasVerticalScroller: true, hasHorizontalScroller: false)
112+
/// Both axes (code-heavy / wide content).
113+
public static let both = ScrollersPolicy(hasVerticalScroller: true, hasHorizontalScroller: true)
114+
/// Vertical, no auto-hide.
115+
public static let alwaysVisible = ScrollersPolicy(hasVerticalScroller: true, autohidesScrollers: false)
116+
}
117+
118+
// MARK: - Text insets
119+
120+
/// Margins inside the text view (`NSTextView.textContainerInset`). Scroll bar stays at the outer edge.
121+
public struct TextInsets: Sendable {
122+
public var horizontal: CGFloat
123+
public var vertical: CGFloat
124+
125+
public init(horizontal: CGFloat = 0, vertical: CGFloat = 5) {
126+
self.horizontal = horizontal
127+
self.vertical = vertical
128+
}
129+
130+
public static let `default` = TextInsets()
131+
}
132+
83133
// MARK: - Marker visibility
84134

85135
/// How Markdown syntax markers (e.g. `**`, `*`, `$`) are visualized when
@@ -415,25 +465,13 @@ public struct DragSelectionPolicy: Sendable {
415465
public static let `default` = DragSelectionPolicy()
416466
}
417467

418-
// MARK: - Content insets
468+
// MARK: - Safe-area insets
419469

420-
/// Inset (in points) applied to the editor's enclosing scroll view's content
421-
/// area on each edge.
422-
///
423-
/// Use this when the editor is hosted inside a layout that needs reserved
424-
/// space at one of the edges — for example, a translucent toolbar or header
425-
/// bar that the document should scroll *underneath*. The default is zero on
426-
/// every edge so the editor fills its container exactly; embedders that
427-
/// want padding opt in explicitly.
428-
public struct ContentInsets: Sendable {
429-
/// Top inset in points. Common use: reserving space for a translucent
430-
/// title / toolbar bar.
470+
/// Reserves space on the scroll view for system overlays (e.g. a translucent toolbar to scroll underneath). Maps to `NSScrollView.contentInsets`; scroll bar follows the inset.
471+
public struct SafeAreaInsets: Sendable {
431472
public var top: CGFloat
432-
/// Leading (left in NSEdgeInsets) inset in points.
433473
public var leading: CGFloat
434-
/// Trailing (right in NSEdgeInsets) inset in points.
435474
public var trailing: CGFloat
436-
/// Bottom inset in points.
437475
public var bottom: CGFloat
438476

439477
public init(
@@ -448,5 +486,5 @@ public struct ContentInsets: Sendable {
448486
self.bottom = bottom
449487
}
450488

451-
public static let `default` = ContentInsets()
489+
public static let `default` = SafeAreaInsets()
452490
}

‎Sources/MarkdownEngine/TextView/NativeTextViewWrapper.swift‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ public struct NativeTextViewWrapper: NSViewRepresentable {
100100
public func makeNSView(context: Context) -> NSScrollView {
101101
let scrollView = ClampedScrollView()
102102
scrollView.borderType = .noBorder
103-
scrollView.hasVerticalScroller = false
104-
scrollView.hasHorizontalScroller = false
103+
scrollView.hasVerticalScroller = configuration.scrollers.hasVerticalScroller
104+
scrollView.hasHorizontalScroller = configuration.scrollers.hasHorizontalScroller
105+
scrollView.autohidesScrollers = configuration.scrollers.autohidesScrollers
105106
scrollView.drawsBackground = false
106107
scrollView.automaticallyAdjustsContentInsets = false
107108
scrollView.contentInsets = NSEdgeInsets(
108-
top: configuration.contentInsets.top,
109-
left: configuration.contentInsets.leading,
110-
bottom: configuration.contentInsets.bottom,
111-
right: configuration.contentInsets.trailing
109+
top: configuration.safeAreaInsets.top,
110+
left: configuration.safeAreaInsets.leading,
111+
bottom: configuration.safeAreaInsets.bottom,
112+
right: configuration.safeAreaInsets.trailing
112113
)
113114

114115
// Let NSTextView auto-initialize its own TextKit 2 stack via init(frame:).
@@ -121,6 +122,10 @@ public struct NativeTextViewWrapper: NSViewRepresentable {
121122
}
122123
textContainer.lineFragmentPadding = 0
123124
textContainer.widthTracksTextView = true
125+
textView.textContainerInset = NSSize(
126+
width: configuration.textInsets.horizontal,
127+
height: configuration.textInsets.vertical
128+
)
124129
textContainer.heightTracksTextView = false
125130

126131
let layoutDelegate = MarkdownLayoutManagerDelegate()
@@ -223,6 +228,22 @@ public struct NativeTextViewWrapper: NSViewRepresentable {
223228
if let bottomTextView = nsView.documentView as? NativeTextView {
224229
bottomTextView.onPasteImage = onPasteImage
225230
}
231+
if nsView.hasVerticalScroller != configuration.scrollers.hasVerticalScroller {
232+
nsView.hasVerticalScroller = configuration.scrollers.hasVerticalScroller
233+
}
234+
if nsView.hasHorizontalScroller != configuration.scrollers.hasHorizontalScroller {
235+
nsView.hasHorizontalScroller = configuration.scrollers.hasHorizontalScroller
236+
}
237+
if nsView.autohidesScrollers != configuration.scrollers.autohidesScrollers {
238+
nsView.autohidesScrollers = configuration.scrollers.autohidesScrollers
239+
}
240+
let desiredTextInset = NSSize(
241+
width: configuration.textInsets.horizontal,
242+
height: configuration.textInsets.vertical
243+
)
244+
if textView.textContainerInset != desiredTextInset {
245+
textView.textContainerInset = desiredTextInset
246+
}
226247
// Refresh services/theme when the embedder hands us a new configuration
227248
// (e.g. when the available wiki-link targets change). Cheap pointer-/
228249
// value-based comparison; full equality isn't required because the

0 commit comments

Comments
 (0)