Skip to content

Instantly share code, notes, and snippets.

@jancassio
Last active January 13, 2022 18:37
Show Gist options
  • Save jancassio/e439caa24aa0ab5bec5fc5fd5123bf81 to your computer and use it in GitHub Desktop.
Save jancassio/e439caa24aa0ab5bec5fc5fd5123bf81 to your computer and use it in GitHub Desktop.

DebugView Modifier

Extends View's protocol including some additional modifiers to help debugging SwiftUI

Example

import SwiftUI

struct ContentView: View {
  @State private var count: Int = 0
  var body: some View {
    VStack {
      HStack {
        Text("Hello World")
        Spacer()
        Button { count += 1 } label: { 
          Text("Action")
          .debugBackground()
        }
      }
      .padding()
      .debugBorder()
    }
    .debugPrint("[ContentView]: count = \(count)")
  }
}

See also

License

MIT

/*
DebugModifier.swift
MIT License
Copyright (c) 2021 Jan Cassio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import Foundation
import SwiftUI
extension View {
/// Perform modifications during rendering.
/// It works only when the environment flag `DEBUG` is enabled, otherwise, any modification will be made.
/// - Parameter modifier: Modifiers to be performed during the rendering phase.
/// - Returns: The current view.
func debugUsing<T: View>(_ modifier: (Self) -> T) -> some View {
#if DEBUG
return modifier(self)
#else
return self
#endif
}
/// Performs an action during rendering.
/// Works only when the environment flag `DEBUG` is enabled, otherwise, any closure will be called.
/// - Parameter closure: A closure to be performed during view rendering phase.
/// - Returns: The current view.
func debugPerforming(_ closure: () -> Void) -> some View {
#if DEBUG
closure()
#endif
return self
}
/// Prints a console message during View's rendering phase.
/// - Parameter item: Any printable value.
/// - Returns: The current view.
func debugPrint(_ item: Any...) -> some View {
debugPerforming {
print(item)
}
}
/// Draws a border around the current view.
///
/// It helps to check the boundaries of some view by drawing a border around it.
///
/// - Parameters:
/// - color: The boder's color, default is `Color.red`.
/// - width: The border's stroke size, default is `1.0`.
/// - Returns: A modified view with a border drawend around its boundaries.
func debugBorder(color: Color = .red, width: CGFloat = 1.0) -> some View {
debugUsing { $0.border(color, width: width) }
}
/// Draws a solid color view at background.
///
/// It allows to see the how much space a view takes from the available space provided by the parent's view.
///
/// - Parameter color: The background's view, default is `Color.red`.
/// - Returns: A modified view with a background that fills the area used by the view.
func debugBackground(_ color: Color = .red.opacity(0.45)) -> some View {
debugUsing { $0.background(color) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment