Skip to content

Commit

Permalink
Warning icon for editor warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Desbeers committed Oct 28, 2024
1 parent 9a79842 commit 84db6d1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
28 changes: 13 additions & 15 deletions Chord Provider/ChordProEditor/ChordProEditor+LineNumbersView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ extension ChordProEditor {
drawDirectiveIcon(
directive,
inRect: markerRect,
highlight: highlight
highlight: highlight,
warning: false
)
}
if highlight {
Expand All @@ -134,6 +135,8 @@ extension ChordProEditor {
let highlight = layoutManager.extraLineFragmentRect.minY == textView.currentParagraphRect?.minY
drawLineNumber(lineNumber, inRect: markerRect, highlight: highlight)
}
/// Set the internals of the editor
textView.parent?.runIntrospect(textView)

/// Draw the number of the line
func drawLineNumber(_ number: Int, inRect rect: NSRect, highlight: Bool) {
Expand All @@ -148,8 +151,14 @@ extension ChordProEditor {
attributes[NSAttributedString.Key.foregroundColor] = NSColor.secondaryLabelColor
}
if textView.log.map(\.lineNumber).contains(number) {
/// We have a warning, make the line number red
/// We have a warning, make the line number red and shw a warning icon
attributes[NSAttributedString.Key.foregroundColor] = NSColor.red
drawDirectiveIcon(
ChordPro.Directive.warning,
inRect: rect,
highlight: highlight,
warning: true
)
}
/// Define the rect of the string
var stringRect = rect
Expand All @@ -160,7 +169,7 @@ extension ChordProEditor {
NSString(string: "\(number)").draw(in: stringRect, withAttributes: attributes)
}
/// Draw the directive icon of the line
func drawDirectiveIcon(_ directive: ChordProDirective, inRect rect: NSRect, highlight: Bool) {
func drawDirectiveIcon(_ directive: ChordProDirective, inRect rect: NSRect, highlight: Bool, warning: Bool) {
var iconRect = rect
let imageAttachment = NSTextAttachment()
let imageConfiguration = NSImage.SymbolConfiguration(pointSize: font.pointSize * 0.7, weight: .medium)
Expand All @@ -169,7 +178,7 @@ extension ChordProEditor {
let imageString = NSMutableAttributedString(attachment: imageAttachment)
imageString.addAttribute(
.foregroundColor,
value: highlight ? NSColor.textColor : NSColor.secondaryLabelColor,
value: warning ? NSColor.red : highlight ? NSColor.textColor : NSColor.secondaryLabelColor,
range: NSRange(location: 0, length: imageString.length)
)
let imageSize = imageString.size()
Expand All @@ -179,17 +188,6 @@ extension ChordProEditor {
imageString.draw(in: iconRect)
}
}
/// Get optional directive argument inside the range
func getDirectiveArgument(nsRange: NSRange) -> String? {
var string: String?
textStorage.enumerateAttribute(.directiveArgument, in: nsRange) {values, _, _ in
if let value = values as? String {
string = value.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
return string
}
textView.parent?.runIntrospect(textView)
}
}
}
19 changes: 16 additions & 3 deletions Chord Provider/ChordProEditor/ChordProEditor+LogItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
// © 2024 Nick Berendsen
//

import Foundation
import SwiftUI

extension ChordProEditor {

/// An item for the log
struct LogItem: Equatable {
/// An item for the editor log
struct LogItem: Equatable, Identifiable {
/// Give it an unique ID
var id: UUID = UUID()
/// The date and time of the log item
var time: Date = .now
/// The type of log message
var type: LogItemType
/// The optional line number of the source
Expand All @@ -29,5 +31,16 @@ extension ChordProEditor {
case warning
/// An error log
case error
/// The color for an log message
var color: Color {
switch self {
case .notice:
return Color.blue
case .warning:
return Color.orange
case .error:
return Color.red
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension ChordProParser {
}
switch directive {

case .none:
case .none, .warning:
break

// MARK: Meta-data directives
Expand Down Expand Up @@ -173,7 +173,7 @@ extension ChordProParser {
let tabs = currentSection.lines.map(\.tab)
if let maxLength = tabs.max(by: { $1.count > $0.count })?.count {
for index in currentSection.lines.indices {
currentSection.lines[index].tab += (String(repeating: " ", count: maxLength - currentSection.lines[index].tab.count))
currentSection.lines[index].tab += String(repeating: " ", count: maxLength - currentSection.lines[index].tab.count)
}
}
processSection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension PDFBuild {
self.items = items
}

/// Draw the **page background color** element
/// Draw the **section** element
/// - Parameters:
/// - rect: The available rectangle
/// - calculationOnly: Bool if only the Bounding Rect should be calculated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ extension ChordPro.Directive {
button: "Strum",
help: "This directive indicates the end of the strum"
)
case .warning:
Details(
label: "Warning",
icon: "exclamationmark.triangle",
button: "None",
help: "A warning is found"
)
case .none:
Details(
label: "None",
Expand Down
3 changes: 2 additions & 1 deletion Chord Provider/Statics/ChordPro/ChordPro+Directive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ extension ChordPro {

/// This directive defines a tag for the song
case tag

/// A warning defined a directive to the editor can draw an icon for it
case warning
/// Not a directive
case none

Expand Down

0 comments on commit 84db6d1

Please sign in to comment.