diff --git a/QRCode-Generator/Constants.swift b/QRCode-Generator/Constants.swift index c2f9507..293b4d4 100644 --- a/QRCode-Generator/Constants.swift +++ b/QRCode-Generator/Constants.swift @@ -24,7 +24,7 @@ enum ImageConstants { static let close = "xmark.circle.fill" static let hide = "eye.slash.fill" static let history = "clock" - static let noImagePlaceHolder = "xmark.circle" + static let noImagePlaceHolder = "photo.artframe" static let qrcode = "qrcode" static let settings = "gearshape.fill" static let show = "eye" diff --git a/QRCode-Generator/Extensions/NSColor+HexString.swift b/QRCode-Generator/Extensions/NSColor+HexString.swift deleted file mode 100644 index 777df18..0000000 --- a/QRCode-Generator/Extensions/NSColor+HexString.swift +++ /dev/null @@ -1,53 +0,0 @@ -// -// NSColor+Hex.swift -// QRCode-Generator -// -// Created by Ibrahim Hassan on 28/03/23. -// - -import Cocoa - -extension NSColor { - var hexString: String { - let red = Int(round(self.redComponent * 0xFF)) - let green = Int(round(self.greenComponent * 0xFF)) - let blue = Int(round(self.blueComponent * 0xFF)) - let hexString = NSString(format: "#%02X%02X%02X", red, green, blue) - - return hexString as String - } -} - -extension NSColor { - /// Initialises NSColor from a hexadecimal string. Color is clear if string is invalid. - /// - Parameter fromHex: supported formats are "#RGB", "#RGBA", "#RRGGBB", "#RRGGBBAA", with or without the # character - convenience init(fromHex:String) { - var r = 0, g = 0, b = 0, a = 255 - let offset = fromHex.hasPrefix("#") ? 1 : 0 - let ch = fromHex.map{$0} - - switch(ch.count - offset) { - case 8: - a = 16 * (ch[offset+6].hexDigitValue ?? 0) + (ch[offset+7].hexDigitValue ?? 0) - fallthrough - case 6: - r = 16 * (ch[offset+0].hexDigitValue ?? 0) + (ch[offset+1].hexDigitValue ?? 0) - g = 16 * (ch[offset+2].hexDigitValue ?? 0) + (ch[offset+3].hexDigitValue ?? 0) - b = 16 * (ch[offset+4].hexDigitValue ?? 0) + (ch[offset+5].hexDigitValue ?? 0) - break - case 4: - a = 16 * (ch[offset+3].hexDigitValue ?? 0) + (ch[offset+3].hexDigitValue ?? 0) - fallthrough - case 3: // Three digit #0D3 is the same as six digit #00DD33 - r = 16 * (ch[offset+0].hexDigitValue ?? 0) + (ch[offset+0].hexDigitValue ?? 0) - g = 16 * (ch[offset+1].hexDigitValue ?? 0) + (ch[offset+1].hexDigitValue ?? 0) - b = 16 * (ch[offset+2].hexDigitValue ?? 0) + (ch[offset+2].hexDigitValue ?? 0) - break - default: - a = 255 - break - } - - self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: CGFloat(a)/255) - } -} diff --git a/QRCode-Generator/Helpers/ConfigurationHelper.swift b/QRCode-Generator/Helpers/ConfigurationHelper.swift index ea928a7..c8aca07 100644 --- a/QRCode-Generator/Helpers/ConfigurationHelper.swift +++ b/QRCode-Generator/Helpers/ConfigurationHelper.swift @@ -7,11 +7,14 @@ import Combine import SwiftUI +import Foundation class ConfigurationHelper { private let colorFileName = "Color.txt" private let iconImageName = "icon" private let iconSize = "IconSize.txt" + + private var isLight: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.aqua } private var colorPath: URL? { FileManager.sharedContainerURL?.appendingPathComponent(colorFileName) @@ -27,19 +30,22 @@ class ConfigurationHelper { //MARK: - Icon Color func getColor() -> Color { - var colorString = "" + var colorData: Data + var color: NSColor? guard let colorPath else { return Color.black } do { - colorString = try String(contentsOf: colorPath) + colorData = try Data(contentsOf: colorPath) + color = try NSKeyedUnarchiver + .unarchiveTopLevelObjectWithData(colorData) as? NSColor } catch { print (error) } - return Color(NSColor(fromHex: colorString)) + return Color(color ?? (isLight ? NSColor.black : NSColor.white)) } func saveColor(_ color: Color) { @@ -48,7 +54,12 @@ class ConfigurationHelper { } do { - try NSColor(color).hexString.write(to: colorPath, atomically: true, encoding: .utf8) + let data = try NSKeyedArchiver.archivedData( + withRootObject: NSColor(color), + requiringSecureCoding: true + ) + + try data.write(to: colorPath) } catch { print (error) } diff --git a/QRCode-Generator/Views/QRCodeView.swift b/QRCode-Generator/Views/QRCodeView.swift index 17b6cb6..4f8af9f 100644 --- a/QRCode-Generator/Views/QRCodeView.swift +++ b/QRCode-Generator/Views/QRCodeView.swift @@ -20,6 +20,7 @@ struct QRCodeView: View { Image(systemName: ImageConstants.noImagePlaceHolder) .resizable() .scaledToFit() + .padding() Text(Strings.copyValidURL) .padding() @@ -30,6 +31,7 @@ struct QRCodeView: View { .resizable() .interpolation(.none) .scaledToFit() + .padding() .contextMenu { Button { OpenPanelManager.showPanel(image: qrCodeImage, url: viewModel.lastValidURL ?? "") @@ -93,6 +95,7 @@ struct QRCodeView: View { } .frame(width: 320, height: 400) .onChange(of: colorScheme) { newValue in + viewModel.forceRefresh() (NSApplication.shared.delegate as? AppDelegate)?.updateColor() } } diff --git a/QRtist.xcodeproj/project.pbxproj b/QRtist.xcodeproj/project.pbxproj index ba70422..ef96324 100644 --- a/QRtist.xcodeproj/project.pbxproj +++ b/QRtist.xcodeproj/project.pbxproj @@ -29,7 +29,6 @@ EDBB6ED829D2F20400A37F1C /* ConfigurationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6ED729D2F20400A37F1C /* ConfigurationView.swift */; }; EDBB6EDA29D325AF00A37F1C /* ConfigurationHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6ED929D325AF00A37F1C /* ConfigurationHelper.swift */; }; EDBB6EDC29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6EDB29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift */; }; - EDBB6EDE29D326E000A37F1C /* NSColor+HexString.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6EDD29D326E000A37F1C /* NSColor+HexString.swift */; }; EDBB6EE029D359F300A37F1C /* NSImage+Resize.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6EDF29D359F300A37F1C /* NSImage+Resize.swift */; }; EDBB6F0229D4082A00A37F1C /* TimerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6F0129D4082A00A37F1C /* TimerManager.swift */; }; /* End PBXBuildFile section */ @@ -59,7 +58,6 @@ EDBB6ED729D2F20400A37F1C /* ConfigurationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationView.swift; sourceTree = ""; }; EDBB6ED929D325AF00A37F1C /* ConfigurationHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationHelper.swift; sourceTree = ""; }; EDBB6EDB29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CIImage+GetTintWithIcon.swift"; sourceTree = ""; }; - EDBB6EDD29D326E000A37F1C /* NSColor+HexString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSColor+HexString.swift"; sourceTree = ""; }; EDBB6EDF29D359F300A37F1C /* NSImage+Resize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSImage+Resize.swift"; sourceTree = ""; }; EDBB6F0129D4082A00A37F1C /* TimerManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerManager.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -133,7 +131,6 @@ EDBB6EDB29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift */, EDBB6ECD29D2E9C700A37F1C /* FileManager+SharedContainer.swift */, ED0EC66529DC015600226C3C /* FileManager+TemporaryFilePath.swift */, - EDBB6EDD29D326E000A37F1C /* NSColor+HexString.swift */, EDBB6EDF29D359F300A37F1C /* NSImage+Resize.swift */, EDBB6EC929D2E93600A37F1C /* NSImage+SavePNG.swift */, ED203B372A14A15F00314C9D /* String+GetFileName.swift */, @@ -228,7 +225,6 @@ EDBB6EE029D359F300A37F1C /* NSImage+Resize.swift in Sources */, EDBB6ECA29D2E93600A37F1C /* NSImage+SavePNG.swift in Sources */, ED0EC66C29DC1BA700226C3C /* ColorViewModel.swift in Sources */, - EDBB6EDE29D326E000A37F1C /* NSColor+HexString.swift in Sources */, ED203B3A2A14DD9800314C9D /* OpenPanelManager.swift in Sources */, EDBB6F0229D4082A00A37F1C /* TimerManager.swift in Sources */, ED0EC66E29DC311800226C3C /* String+IsValidColor.swift in Sources */, diff --git a/QRtist.xcodeproj/project.xcworkspace/xcuserdata/ibrahimhassan.xcuserdatad/UserInterfaceState.xcuserstate b/QRtist.xcodeproj/project.xcworkspace/xcuserdata/ibrahimhassan.xcuserdatad/UserInterfaceState.xcuserstate index 3433b63..fdba15f 100644 Binary files a/QRtist.xcodeproj/project.xcworkspace/xcuserdata/ibrahimhassan.xcuserdatad/UserInterfaceState.xcuserstate and b/QRtist.xcodeproj/project.xcworkspace/xcuserdata/ibrahimhassan.xcuserdatad/UserInterfaceState.xcuserstate differ