Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Removed UIKit dependencies from many files #464

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions OpoLua.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@
D8C679BA27483B5600BD8720 /* DirectoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8C679B927483B5600BD8720 /* DirectoryViewController.swift */; };
D8C679BD27484BF400BD8720 /* examples in Resources */ = {isa = PBXBuildFile; fileRef = D8C679BC27484BF400BD8720 /* examples */; };
D8C712A4275AF50D00D83A3E /* ConcurrentQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8C712A3275AF50D00D83A3E /* ConcurrentQueue.swift */; };
D8CCACAE27B2FE3D008C8122 /* NSLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8CCACAD27B2FE3D008C8122 /* NSLock.swift */; };
D8CCACB027B2FE59008C8122 /* NSRecursiveLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8CCACAF27B2FE59008C8122 /* NSRecursiveLock.swift */; };
D8D184F527BACAB5004DDFFD /* SoundViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D184F427BACAB5004DDFFD /* SoundViewController.swift */; };
D8D22C1F2CA4E6B1003E2D5E /* Metadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D22C1E2CA4E6AF003E2D5E /* Metadata.swift */; };
D8D44C49279A0B87007340B5 /* TaskManagerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D44C48279A0B87007340B5 /* TaskManagerViewController.swift */; };
Expand Down Expand Up @@ -125,6 +123,8 @@
DBE2917E27932C5400EE824B /* ClockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE2917D27932C5300EE824B /* ClockView.swift */; };
DBE291822795F86200EE824B /* UIKeyModifierFlags.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE291812795F86100EE824B /* UIKeyModifierFlags.swift */; };
DBE91525276D25AA00BDB8F5 /* UIKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE91524276D25A900BDB8F5 /* UIKey.swift */; };
DBEBA4462CCCF039009335CA /* CommonImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBEBA4452CCCF033009335CA /* CommonImage.swift */; };
DBEBA4482CCCF7EA009335CA /* ViewBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBEBA4472CCCF7E7009335CA /* ViewBase.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -215,8 +215,6 @@
D8C679B927483B5600BD8720 /* DirectoryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectoryViewController.swift; sourceTree = "<group>"; };
D8C679BC27484BF400BD8720 /* examples */ = {isa = PBXFileReference; lastKnownFileType = folder; path = examples; sourceTree = "<group>"; };
D8C712A3275AF50D00D83A3E /* ConcurrentQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcurrentQueue.swift; sourceTree = "<group>"; };
D8CCACAD27B2FE3D008C8122 /* NSLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSLock.swift; sourceTree = "<group>"; };
D8CCACAF27B2FE59008C8122 /* NSRecursiveLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSRecursiveLock.swift; sourceTree = "<group>"; };
D8D184F427BACAB5004DDFFD /* SoundViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundViewController.swift; sourceTree = "<group>"; };
D8D22C1E2CA4E6AF003E2D5E /* Metadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Metadata.swift; sourceTree = "<group>"; };
D8D44C48279A0B87007340B5 /* TaskManagerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskManagerViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -257,6 +255,8 @@
DBE2917D27932C5300EE824B /* ClockView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClockView.swift; sourceTree = "<group>"; };
DBE291812795F86100EE824B /* UIKeyModifierFlags.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKeyModifierFlags.swift; sourceTree = "<group>"; };
DBE91524276D25A900BDB8F5 /* UIKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKey.swift; sourceTree = "<group>"; };
DBEBA4452CCCF033009335CA /* CommonImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommonImage.swift; sourceTree = "<group>"; };
DBEBA4472CCCF7E7009335CA /* ViewBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewBase.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
Expand Down Expand Up @@ -308,6 +308,7 @@
D861E8DC274A81B4003C8D3D /* Directory.swift */,
D884A3D22778E335008954A0 /* FileSystem.swift */,
D80D65A7279DD2060020F994 /* Icon.swift */,
DBEBA4452CCCF033009335CA /* CommonImage.swift */,
D884A3D6277914CF008954A0 /* Installer.swift */,
D86724F12771672A009C7246 /* InterpreterThread.swift */,
D8FE6D902C92251B0058CE2E /* Legal.swift */,
Expand Down Expand Up @@ -360,6 +361,7 @@
D8F4835C278D3D31008AF80C /* PixelView.swift */,
DB6B438327A55D1200625788 /* RootView.swift */,
D8553767278F535A00ED67C0 /* SettingsView.swift */,
DBEBA4472CCCF7E7009335CA /* ViewBase.swift */,
D85F597027ACB682006AAF3B /* WizardFooterView.swift */,
);
path = Views;
Expand All @@ -376,9 +378,7 @@
D8EB6FF9275995DB00FC139F /* Dictionary.swift */,
D89A60792756A2CC00C9757F /* FileManager.swift */,
D80925602772939A00121E12 /* Graphics+CoreGraphics.swift */,
D8CCACAD27B2FE3D008C8122 /* NSLock.swift */,
D84ADB172771DAB9001ABFB4 /* NSNotification.swift */,
D8CCACAF27B2FE59008C8122 /* NSRecursiveLock.swift */,
D85699C527A9948F00E6F6DD /* PsiLuaEnv+Utilities.swift */,
D8BA096F275196CA00F4730D /* String.swift */,
D80A9CBF27A4AF9200E49AD8 /* TaskManager+UIKit.swift */,
Expand Down Expand Up @@ -622,6 +622,7 @@
D8F4835D278D3D31008AF80C /* PixelView.swift in Sources */,
D8FE60032757106300711717 /* SecureLocation.swift in Sources */,
D82D2FCC27AB4FBC001A4283 /* RecursiveDirectoryMonitor.swift in Sources */,
DBEBA4482CCCF7EA009335CA /* ViewBase.swift in Sources */,
D8632832279C240600B63625 /* UID.swift in Sources */,
D8891353274C186E00794D55 /* Canvas.swift in Sources */,
D82A37B827A9B43F00D8F5F4 /* DirectoryMonitor.swift in Sources */,
Expand All @@ -640,7 +641,6 @@
D884A3D7277914CF008954A0 /* Installer.swift in Sources */,
D8B2757527B6840D00DAAFC0 /* RateLimiter.swift in Sources */,
D8C3BD632774BEA1003B1AD0 /* ErrorViewController.swift in Sources */,
D8CCACAE27B2FE3D008C8122 /* NSLock.swift in Sources */,
D8535A2A277139D900DFCE29 /* Tone.swift in Sources */,
D884A3D32778E335008954A0 /* FileSystem.swift in Sources */,
D8DCD03E277AC9B300760302 /* WindowServer.swift in Sources */,
Expand All @@ -667,7 +667,6 @@
D81C12A8279EF0C0000E70D0 /* AllProgramsViewController.swift in Sources */,
D84ADB162771D937001ABFB4 /* Device.swift in Sources */,
D89A60802756AE8800C9757F /* LibraryViewController.swift in Sources */,
D8CCACB027B2FE59008C8122 /* NSRecursiveLock.swift in Sources */,
D80D65B0279E31220020F994 /* Configuration.swift in Sources */,
D8D95506279B7B100049A3C3 /* UIColor.swift in Sources */,
D8891351274C184C00794D55 /* UIImage.swift in Sources */,
Expand All @@ -683,6 +682,7 @@
D85F597127ACB682006AAF3B /* WizardFooterView.swift in Sources */,
D89F8CEA27AEC48B009F25E5 /* UIViewController.swift in Sources */,
D8D6701527A72B2100D12E96 /* DestinationPickerViewController.swift in Sources */,
DBEBA4462CCCF039009335CA /* CommonImage.swift in Sources */,
D8EE4D86279E4D2900F91150 /* ProgramDetector.swift in Sources */,
DBE2917E27932C5400EE824B /* ClockView.swift in Sources */,
D80A9CC227A4B66400E49AD8 /* SourceViewController.swift in Sources */,
Expand Down
33 changes: 0 additions & 33 deletions OpoLua/Extensions/NSRecursiveLock.swift

This file was deleted.

103 changes: 0 additions & 103 deletions OpoLua/Extensions/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,109 +22,6 @@ import UIKit

extension UIImage {

static func clockMedium() -> UIImage {
return UIImage(named: "ClockMedium")!
}

static func clockMediumC() -> UIImage {
return UIImage(named: "ClockMediumC")!
}

static func dataIcon() -> UIImage {
return UIImage(named: "DataIcon")!
}

static func dataIconC() -> UIImage {
return UIImage(named: "DataIconC")!
}

static func epocLogo() -> UIImage {
return UIImage(named: "EpocLogo")!
}

static func epocLogoC() -> UIImage {
return UIImage(named: "EpocLogoC")!
}

static func folderIcon() -> UIImage {
return UIImage(named: "FolderIcon")!
}

static func folderIconC() -> UIImage {
return UIImage(named: "FolderIconC")!
}

static func installerIcon() -> UIImage {
return UIImage(named: "InstallerIcon")!
}

static func installerIconC() -> UIImage {
return UIImage(named: "InstallerIconC")!
}

static func oplIcon() -> UIImage {
return UIImage(named: "OplIcon")!
}

static func oplIconC() -> UIImage {
return UIImage(named: "OplIconC")!
}

static func opoIcon() -> UIImage {
return UIImage(named: "OpoIcon")!
}

static func opoIconC() -> UIImage {
return UIImage(named: "OpoIconC")!
}

static func paintIcon() -> UIImage {
return UIImage(named: "PaintIcon")!
}

static func paintIconC() -> UIImage {
return UIImage(named: "PaintIconC")!
}

static func recordIcon() -> UIImage {
return UIImage(named: "RecordIcon")!
}

static func recordIconC() -> UIImage {
return UIImage(named: "RecordIconC")!
}

static func textIcon() -> UIImage {
return UIImage(named: "TextIcon")!
}

static func unknownAppIcon() -> UIImage {
return UIImage(named: "UnknownAppIcon")!
}

static func unknownAppIconC() -> UIImage {
return UIImage(named: "UnknownAppIconC")!
}

static func unknownFileIcon() -> UIImage {
return UIImage(named: "UnknownFileIcon")!
}

static func unknownFileIconC() -> UIImage {
return UIImage(named: "UnknownFileIconC")!
}

static func ditherPattern() -> UIImage {
return UIImage(named: "DitherPattern")!
}

static func emptyImage(with size: CGSize) -> UIImage? {
UIGraphicsBeginImageContext(size)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}

func scale(_ scale: CGFloat) -> UIImage? {
let size = CGSize(width: size.width * scale, height: size.height * scale)
UIGraphicsBeginImageContext(size)
Expand Down
5 changes: 5 additions & 0 deletions OpoLua/Extensions/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
// SOFTWARE.

import Foundation

#if canImport(UIKit)
import UIKit
#endif

extension URL {

Expand Down Expand Up @@ -77,9 +80,11 @@ _Please provide details of the program you were running, and what you were doing
}

var localizedName: String {
#if canImport(UIKit)
if self == FileManager.default.documentsUrl {
return UIDevice.current.localizedDocumentsName
}
#endif
return FileManager.default.displayName(atPath: path)
}

Expand Down
16 changes: 12 additions & 4 deletions OpoLua/Model/ApplicationMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit
import Foundation

fileprivate let appInfoCache = FileMetadataCache<ApplicationMetadata>()
fileprivate let fileTypeCache = FileMetadataCache<PsiLuaEnv.FileType>()
Expand All @@ -34,7 +34,15 @@ struct ApplicationMetadata {

var caption: String {
for language in Locale.preferredLanguages {
if let caption = captions.first(where: { $0.locale.languageCode == language }) {
// This conditional only exists because locale.languageCode deprecation on iOS is different to when it was
// deprecated on macos (or our target platform versions are a bit different). Eventually both platforms
// should use the (newer) macos definition below.
#if os(iOS)
let predicate: (PsiLuaEnv.LocalizedString) -> Bool = { $0.locale.languageCode == language }
#else
let predicate: (PsiLuaEnv.LocalizedString) -> Bool = { $0.locale.language.languageCode?.identifier == language }
#endif
if let caption = captions.first(where: predicate) {
return caption.value
}
}
Expand All @@ -56,7 +64,7 @@ struct ApplicationMetadata {
return Icon(grayscaleImage: appIcon, colorImage: appIcon)
}

private func image(for size: Graphics.Size) -> UIImage? {
private func image(for size: Graphics.Size) -> CommonImage? {
var bitmap: Graphics.MaskedBitmap? = nil
for icon in icons {
guard icon.bitmap.size <= size && (icon.mask == nil || icon.bitmap.size == icon.mask?.size) else {
Expand All @@ -74,7 +82,7 @@ struct ApplicationMetadata {
return nil
}
if let cgImg = bitmap.cgImage {
return UIImage(cgImage: cgImg)
return CommonImage(cgImage: cgImg)
}
return nil
}
Expand Down
9 changes: 4 additions & 5 deletions OpoLua/Model/BitmapFont.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import Foundation
import CoreGraphics
import UIKit

struct BitmapFontInfo {
struct MetadataJson: Codable {
Expand Down Expand Up @@ -101,7 +100,7 @@ class BitmapFontRenderer {
init(font: BitmapFontInfo, embolden: Bool = false) {
self.font = font
self.boldWeight = embolden ? 1 : 0
self.img = UIImage(named: "fonts/\(font.bitmapName)/\(font.bitmapName)")!.cgImage!.inverted()!
self.img = CommonImage(named: "fonts/\(font.bitmapName)/\(font.bitmapName)")!.cgImage!.inverted()!
self.imagew = self.img.width
self.imageh = self.img.height
}
Expand Down Expand Up @@ -146,7 +145,7 @@ class BitmapFontRenderer {

private func individualImageForChar(_ char: Character) -> CGImage? {
let charName = Self.getCharName(char)
return UIImage(named: "fonts/\(font.bitmapName)/\(charName)")?.cgImage?.inverted()
return CommonImage(named: "fonts/\(font.bitmapName)/\(charName)")?.cgImage?.inverted()
}

func getCharWidth(_ char: Character) -> Int {
Expand Down Expand Up @@ -213,9 +212,9 @@ class BitmapFontRenderer {
// The resulting image needs to be black with white text so we can
// use the clip mask trick for drawing it, hence why we use these
// colours.
context.setFillColor(UIColor.black.cgColor)
context.setFillColor(CGColor(red: 0, green: 0, blue: 0, alpha: 1)) // black
context.fill(CGRect(x: 0, y: 0, width: context.width, height: context.height))
context.setFillColor(UIColor.white.cgColor)
context.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1)) // white
for i in 0...boldWeight {
let rect = CGRect(x: i, y: 0, width: img.width, height: context.height)
context.saveGState()
Expand Down
24 changes: 19 additions & 5 deletions OpoLua/Model/Border.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit
import Foundation
import CoreGraphics

extension CGContext {

Expand All @@ -29,16 +30,29 @@ extension CGContext {
print("No resource found for border type \(type) (\(filename).png)")
return
}
let image = UIImage(contentsOfFile: url.path)!
let image = CommonImage(contentsOfFile: url.path)!
// I don't really understand why we have to limit the inset size so agressively here, but
// limiting to half the frame size is not sufficient to avoid some weird artifacts
let inset = min(min(frame.width, frame.height) / 3, 10)
let button = image.resizableImage(withCapInsets: UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset), resizingMode: .stretch)
let view = UIImageView(image: button)
#if canImport(UIKit)
let button = image.resizableImage(withCapInsets: .init(top: inset, left: inset, bottom: inset, right: inset), resizingMode: .stretch)
#else
let button = image
button.capInsets = NSEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)
button.resizingMode = .stretch
#endif
let view = CommonImageView(image: button)

#if canImport(UIKit)
let layer = view.layer
#else
view.wantsLayer = true
let layer = view.layer!
#endif
saveGState()
self.translateBy(x: frame.origin.x, y: frame.origin.y)
view.frame = CGRect(origin: .zero, size: frame.size)
view.layer.render(in: self)
layer.render(in: self)
restoreGState()
}

Expand Down
Loading