Skip to content

Commit

Permalink
ScaledImageView
Browse files Browse the repository at this point in the history
Added `imageURL` to `ScaledImageView` and `ScaledImageButton` to support images stored in a Swift Package's bundle.
  • Loading branch information
Appracatappra committed Dec 15, 2023
1 parent 64f71f6 commit b5533d5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Appracatappra/LogManager",
"state" : {
"revision" : "aa1c1a9d15ea104a590df766af3f81da5afabab7",
"version" : "1.0.2"
"revision" : "bc21e80add4315c28e9461b8f583cfd79e4c74f8",
"version" : "1.0.3"
}
},
{
"identity" : "soundmanager",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Appracatappra/SoundManager",
"state" : {
"revision" : "e9cf3a1dfaa3830eb8bed9efb834add05c76aad2",
"version" : "1.0.1"
"revision" : "04a17d306bea71729877696fa16a55cfb84f8e1a",
"version" : "1.0.3"
}
},
{
"identity" : "swiftletutilities",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Appracatappra/SwiftletUtilities",
"state" : {
"revision" : "7cc84ffb53bc9886b51c0bf2de3d308dd64a71cd",
"version" : "1.1.7"
"revision" : "4d6bf1f7d694af526dc834cf9b0acfc28949cbc3",
"version" : "1.1.12"
}
}
],
Expand Down
14 changes: 10 additions & 4 deletions Sources/SwiftUIKit/Components/ScaledImageButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public struct ScaledImageButton: View {
/// The name of the image to display.
public var imageName = ""

/// The optional URL to an image, if not `nil` this will override the `imageName` property.
/// - Remark: This property is being used to support images stored in a Swift Package bundle.
public var imageURL:URL? = nil

/// The scale for the image button as a percentage.
public var scale:Float = 1.0

Expand Down Expand Up @@ -69,15 +73,17 @@ public struct ScaledImageButton: View {
/// Creates a new insstance of the button.
/// - Parameters:
/// - imageName: The name of the image to display.
/// - imageURL: A `URL` representing the souce of an image.
/// - scale: The scale for the image button as a percentage.
/// - glowOnFocus: If `true`, apply a glow to the button when it is in focus.
/// - isEnabled: If `true`, the button is enabled.
/// - soundSource: The sound source for the button.
/// - buttonSound: The clicked sound for the button.
/// - focusSound: The focused sound for the button.
/// - action: The action to take when the button is clicked.
public init(imageName: String, scale: Float = 1.0, glowOnFocus: Bool = true, isEnabled: Bool = true, soundSource: SwiftUIKit.Source = ScaledImageButton.defaultSoundSource, buttonSound: String = ScaledImageButton.defaultButtonSound, focusSound: String = ScaledImageButton.defaultButtonFocusSound, action: buttonAction? = nil) {
public init(imageName: String = "", imageURL:URL? = nil, scale: Float = 1.0, glowOnFocus: Bool = true, isEnabled: Bool = true, soundSource: SwiftUIKit.Source = ScaledImageButton.defaultSoundSource, buttonSound: String = ScaledImageButton.defaultButtonSound, focusSound: String = ScaledImageButton.defaultButtonFocusSound, action: buttonAction? = nil) {
self.imageName = imageName
self.imageURL = imageURL
self.scale = scale
self.glowOnFocus = glowOnFocus
self.isEnabled = isEnabled
Expand All @@ -91,11 +97,11 @@ public struct ScaledImageButton: View {
/// The contents of the button.
public var body: some View {
if !isEnabled {
ScaledImageView(imageName: imageName, scale: scale)
ScaledImageView(imageName: imageName, imageURL: imageURL, scale: scale)
.opacity(0.50)
} else {
#if os(tvOS)
ScaledImageView(imageName: imageName, scale: scale)
ScaledImageView(imageName: imageName, imageURL: imageURL, scale: scale)
.scaleEffect(isFocused ? CGFloat(1.2) : CGFloat(1.0))
.focusable(true) { newState in
isFocused = newState
Expand Down Expand Up @@ -143,7 +149,7 @@ public struct ScaledImageButton: View {
}
}
}) {
ScaledImageView(imageName: imageName, scale: scale)
ScaledImageView(imageName: imageName, imageURL: imageURL, scale: scale)
}
#endif
}
Expand Down
37 changes: 32 additions & 5 deletions Sources/SwiftUIKit/Components/ScaledImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public struct ScaledImageView: View {
/// The name of the image to display.
public var imageName = ""

/// The optional URL to an image, if not `nil` this will override the `imageName` property.
/// - Remark: This property is being used to support images stored in a Swift Package bundle.
public var imageURL:URL? = nil

/// The scale to show the image at as a percentage.
public var scale:Float = 1.0

Expand All @@ -31,10 +35,12 @@ public struct ScaledImageView: View {
/// Creates a new instance of the control.
/// - Parameters:
/// - imageName: he name of the image to display.
/// - imageURL: A `URL` representing the souce of an image.
/// - scale: The scale to show the image at as a percentage.
/// - ignoreSafeArea: If `true`, the image will ignore the safe area. This is useful to flood a background all the way to the edges of the device.
public init(imageName: String, scale: Float = 1.0, ignoreSafeArea: Bool = true) {
public init(imageName: String = "", imageURL:URL? = nil, scale: Float = 1.0, ignoreSafeArea: Bool = true) {
self.imageName = imageName
self.imageURL = imageURL
self.scale = scale
self.ignoreSafeArea = ignoreSafeArea
}
Expand All @@ -43,7 +49,26 @@ public struct ScaledImageView: View {
/// The contents of the control.
public var body: some View {
#if os(macOS)
if let sourceImage = NSImage.asset(named: imageName, atScale: imageScale) {
if let imageURL {
let sourceImage = NSImage.scaledImage(bundleURL: imageURL, scale: imageScale)
contents(sourceImage: sourceImage)
} else {
let sourceImage = NSImage.asset(named: imageName, atScale: imageScale)
contents(sourceImage: sourceImage)
}
#else
if let imageURL {
let sourceImage = UIImage.scaledImage(bundleURL: imageURL, scale: imageScale)
contents(sourceImage: sourceImage)
} else {
let sourceImage = UIImage.asset(named: imageName, atScale: imageScale)
contents(sourceImage: sourceImage)
}
#endif
}
#if os(macOS)
@ViewBuilder func contents(sourceImage:NSImage?) -> some View {
if let sourceImage {
if ignoreSafeArea {
Image(nsImage: sourceImage)
.ignoresSafeArea()
Expand All @@ -54,8 +79,10 @@ public struct ScaledImageView: View {
Text("`\(imageName)` Missing")
.foregroundColor(Color.white)
}
#else
if let sourceImage = UIImage.asset(named: imageName, atScale: imageScale) {
}
#else
@ViewBuilder func contents(sourceImage:UIImage?) -> some View {
if let sourceImage {
if ignoreSafeArea {
Image(uiImage: sourceImage)
.ignoresSafeArea()
Expand All @@ -66,8 +93,8 @@ public struct ScaledImageView: View {
Text("`\(imageName)` Missing")
.foregroundColor(Color.white)
}
#endif
}
#endif
}

#Preview {
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftUIKit/Components/WordArtView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
//

import SwiftUI

#if !os(macOS)
import UIKit
#endif

/// `WordArtView` displays text in the given font at the given size and rotation with the defined gradient.
public struct WordArtView: View {
Expand Down

0 comments on commit b5533d5

Please sign in to comment.