Skip to content

Commit d00eed3

Browse files
committed
Clean up code
1 parent c067ab1 commit d00eed3

File tree

10 files changed

+88
-119
lines changed

10 files changed

+88
-119
lines changed

Sources/SwiftUIKit/Data/MimeType.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import Foundation
1010
import UniformTypeIdentifiers
1111

12-
/**
13-
This enum represents a set of different MIME and file types.
14-
15-
Note that some types may be expected to be a different type,
16-
but are instead an `.application` type. For instance, `json`
17-
is a text format, but the mime type is `application/json`.
18-
*/
12+
/// This enum defines different MIME and file types.
13+
///
14+
/// Note that some types are expected to be a different type,
15+
/// but instead use `application`. For instance, `json` is a
16+
/// text format, but the mime type is `application/json`.
1917
public enum MimeType: Identifiable {
2018

2119
case

Sources/SwiftUIKit/Extensions/Text+Lines.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ import SwiftUI
1010

1111
public extension Text {
1212

13-
/**
14-
Force multiline rendering of a `Text` view where a text
15-
can become truncated even if there's space.
16-
*/
13+
/// Force text to render as multiline.
1714
func forceMultiline() -> some View {
1815
self.fixedSize(horizontal: false, vertical: true)
1916
}
20-
21-
/**
22-
Force single-line rendering of a `Text` view, where the
23-
text can become truncated even if there's space.
24-
*/
17+
18+
/// Force text to render as single line.
2519
func forceSingleLine() -> some View {
2620
self.fixedSize(horizontal: true, vertical: false)
2721
}

Sources/SwiftUIKit/Images/ImageRepresentable+Data.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,24 @@ public extension ImageRepresentable {
2424
return image.jpegData(compressionQuality: quality)
2525
}
2626
}
27+
28+
#if os(macOS)
29+
import AppKit
30+
import Cocoa
31+
import CoreGraphics
32+
33+
public extension ImageRepresentable {
34+
35+
/// Get the image's core graphics image representation.
36+
var cgImage: CGImage? {
37+
cgImage(forProposedRect: nil, context: nil, hints: nil)
38+
}
39+
40+
/// Get the image's JPEG data representation.
41+
func jpegData(compressionQuality: CGFloat) -> Data? {
42+
guard let image = cgImage else { return nil }
43+
let bitmap = NSBitmapImageRep(cgImage: image)
44+
return bitmap.representation(using: .jpeg, properties: [.compressionFactor: compressionQuality])
45+
}
46+
}
47+
#endif

Sources/SwiftUIKit/Images/ImageRepresentable+Resized.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import Foundation
1111
#if canImport(UIKit)
1212
import UIKit
1313

14-
public extension UIImage {
14+
public extension ImageRepresentable {
1515

1616
/// Create a resized copy of the image.
17-
func resized(to size: CGSize) -> UIImage? {
17+
func resized(to size: CGSize) -> ImageRepresentable? {
1818
UIGraphicsBeginImageContextWithOptions(size, false, scale)
1919
draw(in: CGRect(origin: CGPoint.zero, size: size))
2020
let result = UIGraphicsGetImageFromCurrentImageContext()
@@ -25,11 +25,11 @@ public extension UIImage {
2525
#elseif canImport(AppKit)
2626
import AppKit
2727

28-
public extension NSImage {
28+
public extension ImageRepresentable {
2929

3030
/// Create a resized copy of the image.
31-
func resized(to newSize: CGSize) -> NSImage? {
32-
let newImage = NSImage(size: newSize)
31+
func resized(to newSize: CGSize) -> ImageRepresentable? {
32+
let newImage = ImageRepresentable(size: newSize)
3333
newImage.lockFocus()
3434
let sourceRect = NSRect(x: 0, y: 0, width: size.width, height: size.height)
3535
let destRect = NSRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

Sources/SwiftUIKit/Images/NSImage+Data.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

Sources/SwiftUIKit/Previews/ProcessInfo+SwiftPreviewInspector.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.

Sources/SwiftUIKit/Previews/SwiftPreviewInspector.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// SwiftUIPreviewInspector.swift
3+
// SwiftUIKit
4+
//
5+
// Created by Daniel Saidi on 2022-11-01.
6+
// Copyright © 2022-2025 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
/// This protocol can be implemented by types that can check
12+
/// if the code is running in a SwiftUI preview.
13+
///
14+
/// The protocol is implemented by `ProcessInfo`.
15+
public protocol SwiftUIPreviewInspector {
16+
17+
/// Whether or not the code runs in a SwiftUI preview.
18+
var isSwiftUIPreview: Bool { get }
19+
}
20+
21+
public extension SwiftUIPreviewInspector {
22+
23+
/// Whether or not the code runs in a SwiftUI preview.
24+
var isSwiftUIPreview: Bool {
25+
ProcessInfo.isSwiftUIPreview
26+
}
27+
}
28+
29+
extension ProcessInfo: SwiftUIPreviewInspector {}
30+
31+
public extension ProcessInfo {
32+
33+
/// Whether or not the code runs in a SwiftUI preview.
34+
var isSwiftUIPreview: Bool {
35+
environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
36+
}
37+
38+
/// Whether or not the code runs in a SwiftUI preview.
39+
static var isSwiftUIPreview: Bool {
40+
processInfo.isSwiftUIPreview
41+
}
42+
}
43+
44+
#Preview {
45+
46+
VStack {
47+
Text("Preview.IsSwiftUIPreview", bundle: .module)
48+
.font(.title)
49+
Text("\(ProcessInfo.processInfo.isSwiftUIPreview ? "Preview.Yes" : "Preview.No")", bundle: .module)
50+
}
51+
}

Sources/SwiftUIKit/Sharing/ShareSheet.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
#if os(iOS)
1010
import SwiftUI
1111

12-
/**
13-
This sheet can be used to present `UIActivityViewController`
14-
modals for any kind of shareable content.
15-
16-
You can use a ``SheetContext`` to easily present this sheet
17-
as a modal sheet.
18-
*/
12+
/// This sheet can present a `UIActivityViewController` when
13+
/// sharing any kind of shareable content.
1914
public struct ShareSheet: UIViewControllerRepresentable {
2015

2116
public init(

Sources/SwiftUIKit/SwiftUIKit.docc/SwiftUIKit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ SwiftUIKit is available under the MIT license.
121121

122122
### Previews
123123

124-
- ``SwiftPreviewInspector``
124+
- ``SwiftUIPreviewInspector``
125125

126126
### Progress
127127

0 commit comments

Comments
 (0)