Skip to content

Commit 14cc191

Browse files
authored
Merge pull request #33 from lo-cafe/develop
Adds `backgroundColor` parameter to override Theme's default
2 parents f5c076b + 0666597 commit 14cc191

File tree

3 files changed

+177
-161
lines changed

3 files changed

+177
-161
lines changed

Sources/CodeEditor/CodeEditor.swift

Lines changed: 114 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -205,128 +205,136 @@ public struct CodeEditor: View {
205205
* Configures a CodeEditor View with the given parameters.
206206
*
207207
* - Parameters:
208-
* - source: A binding to a String that holds the source code to be
209-
* edited (or displayed).
210-
* - selection: A binding to the selected range of the `source`.
211-
* - language: Optionally set a language (e.g. `.swift`), otherwise
212-
* Highlight.js will attempt to detect the language.
213-
* - theme: The name of the theme to use, defaults to "pojoaque".
214-
* - fontSize: On macOS this Binding can be used to persist the size of
215-
* the font in use. At runtime this is combined with the
216-
* theme to produce the full font information. (optional)
217-
* - flags: Configure whether the text is editable and/or selectable
218-
* (defaults to both).
219-
* - indentStyle: Optionally insert a configurable amount of spaces if the
220-
* user hits "tab".
221-
* - autoPairs: A mapping of open/close characters, where the close
222-
* characters are automatically injected when the user enters
223-
* the opening character. For example: `[ "{": "}" ]` would
224-
* automatically insert the closing "}" if the user enters
225-
* "{". If no value is given, the default mapping for the
226-
* language is used.
227-
* - inset: The editor can be inset in the scroll view. Defaults to
228-
* 8/8.
229-
* - autoscroll: If enabled, the editor automatically scrolls to the respective
230-
* region when the `selection` is changed programatically.
208+
* - source: A binding to a String that holds the source code to be
209+
* edited (or displayed).
210+
* - selection: A binding to the selected range of the `source`.
211+
* - language: Optionally set a language (e.g. `.swift`), otherwise
212+
* Highlight.js will attempt to detect the language.
213+
* - theme: The name of the theme to use, defaults to "pojoaque".
214+
* - fontSize: On macOS this Binding can be used to persist the size of
215+
* the font in use. At runtime this is combined with the
216+
* theme to produce the full font information. (optional)
217+
* - flags: Configure whether the text is editable and/or selectable
218+
* (defaults to both).
219+
* - indentStyle: Optionally insert a configurable amount of spaces if the
220+
* user hits "tab".
221+
* - autoPairs: A mapping of open/close characters, where the close
222+
* characters are automatically injected when the user enters
223+
* the opening character. For example: `[ "{": "}" ]` would
224+
* automatically insert the closing "}" if the user enters
225+
* "{". If no value is given, the default mapping for the
226+
* language is used.
227+
* - inset: The editor can be inset in the scroll view. Defaults to
228+
* 8/8.
229+
* - autoscroll: If enabled, the editor automatically scrolls to the respective
230+
* region when the `selection` is changed programatically.
231+
* - backgroundColor: Overrides theme's background color.
231232
*/
232-
public init(source : Binding<String>,
233-
selection : Binding<Range<String.Index>>? = nil,
234-
language : Language? = nil,
235-
theme : ThemeName = .default,
236-
fontSize : Binding<CGFloat>? = nil,
237-
flags : Flags = .defaultEditorFlags,
238-
indentStyle : IndentStyle = .system,
239-
autoPairs : [ String : String ]? = nil,
240-
inset : CGSize? = nil,
241-
allowsUndo : Bool = true,
242-
autoscroll : Bool = true)
233+
public init(source : Binding<String>,
234+
selection : Binding<Range<String.Index>>? = nil,
235+
language : Language? = nil,
236+
theme : ThemeName = .default,
237+
fontSize : Binding<CGFloat>? = nil,
238+
flags : Flags = .defaultEditorFlags,
239+
indentStyle : IndentStyle = .system,
240+
autoPairs : [ String : String ]? = nil,
241+
inset : CGSize? = nil,
242+
allowsUndo : Bool = true,
243+
autoscroll : Bool = true,
244+
backgroundColor: NSColor? = nil)
243245
{
244-
self.source = source
245-
self.selection = selection
246-
self.fontSize = fontSize
247-
self.language = language
248-
self.themeName = theme
249-
self.flags = flags
250-
self.indentStyle = indentStyle
251-
self.inset = inset ?? CGSize(width: 8, height: 8)
252-
self.autoPairs = autoPairs
253-
?? language.flatMap({ CodeEditor.defaultAutoPairs[$0] })
254-
?? [:]
255-
self.allowsUndo = allowsUndo
256-
self.autoscroll = autoscroll
246+
self.source = source
247+
self.selection = selection
248+
self.fontSize = fontSize
249+
self.language = language
250+
self.themeName = theme
251+
self.flags = flags
252+
self.indentStyle = indentStyle
253+
self.inset = inset ?? CGSize(width: 8, height: 8)
254+
self.autoPairs = autoPairs
255+
?? language.flatMap({ CodeEditor.defaultAutoPairs[$0] })
256+
?? [:]
257+
self.allowsUndo = allowsUndo
258+
self.autoscroll = autoscroll
259+
self.backgroundColor = backgroundColor
257260
}
258261

259262
/**
260263
* Configures a read-only CodeEditor View with the given parameters.
261264
*
262265
* - Parameters:
263-
* - source: A String that holds the source code to be displayed.
264-
* - language: Optionally set a language (e.g. `.swift`), otherwise
265-
* Highlight.js will attempt to detect the language.
266-
* - theme: The name of the theme to use, defaults to "pojoaque".
267-
* - fontSize: On macOS this Binding can be used to persist the size of
268-
* the font in use. At runtime this is combined with the
269-
* theme to produce the full font information. (optional)
270-
* - flags: Configure whether the text is selectable
271-
* (defaults to both).
272-
* - indentStyle: Optionally insert a configurable amount of spaces if the
273-
* user hits "tab".
274-
* - autoPairs: A mapping of open/close characters, where the close
275-
* characters are automatically injected when the user enters
276-
* the opening character. For example: `[ "{": "}" ]` would
277-
* automatically insert the closing "}" if the user enters
278-
* "{". If no value is given, the default mapping for the
279-
* language is used.
280-
* - inset: The editor can be inset in the scroll view. Defaults to
281-
* 8/8.
266+
* - source: A String that holds the source code to be displayed.
267+
* - language: Optionally set a language (e.g. `.swift`), otherwise
268+
* Highlight.js will attempt to detect the language.
269+
* - theme: The name of the theme to use, defaults to "pojoaque".
270+
* - fontSize: On macOS this Binding can be used to persist the size of
271+
* the font in use. At runtime this is combined with the
272+
* theme to produce the full font information. (optional)
273+
* - flags: Configure whether the text is selectable
274+
* (defaults to both).
275+
* - indentStyle: Optionally insert a configurable amount of spaces if the
276+
* user hits "tab".
277+
* - autoPairs: A mapping of open/close characters, where the close
278+
* characters are automatically injected when the user enters
279+
* the opening character. For example: `[ "{": "}" ]` would
280+
* automatically insert the closing "}" if the user enters
281+
* "{". If no value is given, the default mapping for the
282+
* language is used.
283+
* - inset: The editor can be inset in the scroll view. Defaults to
284+
* 8/8.
285+
* - backgroundColor: Overrides theme's background color.
282286
*/
283287
@inlinable
284-
public init(source : String,
285-
language : Language? = nil,
286-
theme : ThemeName = .default,
287-
fontSize : Binding<CGFloat>? = nil,
288-
flags : Flags = .defaultViewerFlags,
289-
indentStyle : IndentStyle = .system,
290-
autoPairs : [ String : String ]? = nil,
291-
inset : CGSize? = nil,
292-
allowsUndo : Bool = true)
288+
public init(source : String,
289+
language : Language? = nil,
290+
theme : ThemeName = .default,
291+
fontSize : Binding<CGFloat>? = nil,
292+
flags : Flags = .defaultViewerFlags,
293+
indentStyle : IndentStyle = .system,
294+
autoPairs : [ String : String ]? = nil,
295+
inset : CGSize? = nil,
296+
allowsUndo : Bool = true,
297+
backgroundColor: NSColor? = nil)
293298
{
294299
assert(!flags.contains(.editable), "Editing requires a Binding")
295-
self.init(source : .constant(source),
296-
language : language,
297-
theme : theme,
298-
fontSize : fontSize,
299-
flags : flags.subtracting(.editable),
300-
indentStyle : indentStyle,
301-
autoPairs : autoPairs,
302-
inset : inset,
303-
allowsUndo : allowsUndo)
300+
self.init(source : .constant(source),
301+
language : language,
302+
theme : theme,
303+
fontSize : fontSize,
304+
flags : flags.subtracting(.editable),
305+
indentStyle : indentStyle,
306+
autoPairs : autoPairs,
307+
inset : inset,
308+
allowsUndo : allowsUndo,
309+
backgroundColor: backgroundColor)
304310
}
305311

306-
private var source : Binding<String>
307-
private var selection : Binding<Range<String.Index>>?
308-
private var fontSize : Binding<CGFloat>?
309-
private let language : Language?
310-
private let themeName : ThemeName
311-
private let flags : Flags
312-
private let indentStyle : IndentStyle
313-
private let autoPairs : [ String : String ]
314-
private let inset : CGSize
315-
private let allowsUndo : Bool
316-
private let autoscroll : Bool
312+
private var source : Binding<String>
313+
private var selection : Binding<Range<String.Index>>?
314+
private var fontSize : Binding<CGFloat>?
315+
private let language : Language?
316+
private let themeName : ThemeName
317+
private let flags : Flags
318+
private let indentStyle : IndentStyle
319+
private let autoPairs : [ String : String ]
320+
private let inset : CGSize
321+
private let allowsUndo : Bool
322+
private let autoscroll : Bool
323+
private let backgroundColor : NSColor?
317324

318325
public var body: some View {
319-
UXCodeTextViewRepresentable(source : source,
320-
selection : selection,
321-
language : language,
322-
theme : themeName,
323-
fontSize : fontSize,
324-
flags : flags,
325-
indentStyle : indentStyle,
326-
autoPairs : autoPairs,
327-
inset : inset,
328-
allowsUndo : allowsUndo,
329-
autoscroll : autoscroll)
326+
UXCodeTextViewRepresentable(source : source,
327+
selection : selection,
328+
language : language,
329+
theme : themeName,
330+
fontSize : fontSize,
331+
flags : flags,
332+
indentStyle : indentStyle,
333+
autoPairs : autoPairs,
334+
inset : inset,
335+
allowsUndo : allowsUndo,
336+
autoscroll : autoscroll,
337+
backgroundColor: backgroundColor)
330338
}
331339
}
332340

Sources/CodeEditor/UXCodeTextView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import Highlightr
2929
final class UXCodeTextView: UXTextView {
3030

3131
fileprivate let highlightr = Highlightr()
32+
33+
var customBackgroundColor: NSColor? = nil
3234

3335
private var hlTextStorage : CodeAttributedString? {
3436
return textStorage as? CodeAttributedString
@@ -245,7 +247,7 @@ final class UXCodeTextView: UXTextView {
245247
guard let highlightr = highlightr,
246248
highlightr.setTheme(to: newTheme.rawValue),
247249
let theme = highlightr.theme else { return false }
248-
self.backgroundColor = theme.themeBackgroundColor
250+
self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor
249251
if let font = theme.codeFont, font !== self.font { self.font = font }
250252
return true
251253
}
@@ -264,7 +266,7 @@ final class UXCodeTextView: UXTextView {
264266
theme.codeFont = theme.codeFont? .withSize(newSize)
265267
theme.boldCodeFont = theme.boldCodeFont? .withSize(newSize)
266268
theme.italicCodeFont = theme.italicCodeFont?.withSize(newSize)
267-
self.backgroundColor = theme.themeBackgroundColor
269+
self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor
268270
if let font = theme.codeFont, font !== self.font { self.font = font }
269271
return true
270272
}

0 commit comments

Comments
 (0)