From b14d7bdf070273bf720f88b0783113feb3b18ebf Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 7 Feb 2022 00:46:00 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20multiple=20options=20for=20ic?= =?UTF-8?q?ons=20(#106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PHP next to icon (default) - PHP Elephant - No icon If you chose "no PHP text next to icon" earlier, that preference is remembered and migrated over. --- PHP Monitor.xcodeproj/project.pbxproj | 30 +++++-- .../Menu Bar Icons/Contents.json | 6 ++ .../MenuBar_elephant.imageset}/Contents.json | 0 .../Menu Bar Elephant.png | Bin .../Menu Bar Elephant@2x.png | Bin .../MenuBar_php.imageset}/Contents.json | 0 .../MenuBar_php.imageset}/Menu Bar.png | Bin .../MenuBar_php.imageset}/Menu Bar@2x.png | Bin .../Helpers/MenuBarImageGenerator.swift | 8 +- phpmon/Domain/Menu/MainMenu.swift | 2 +- phpmon/Domain/Preferences/MenuBarIcons.swift | 16 ++++ phpmon/Domain/Preferences/Preferences.swift | 31 ++++++- phpmon/Domain/Preferences/PrefsVC.swift | 9 +- .../Views/CheckboxPreferenceView.xib | 2 +- .../Views/SelectPreferenceView.swift | 83 ++++++++++++++++++ .../Views/SelectPreferenceView.xib | 72 +++++++++++++++ phpmon/Localizable.strings | 16 ++-- 17 files changed, 253 insertions(+), 22 deletions(-) create mode 100644 phpmon/Assets.xcassets/Menu Bar Icons/Contents.json rename phpmon/Assets.xcassets/{MB_Elephant.imageset => Menu Bar Icons/MenuBar_elephant.imageset}/Contents.json (100%) rename phpmon/Assets.xcassets/{MB_Elephant.imageset => Menu Bar Icons/MenuBar_elephant.imageset}/Menu Bar Elephant.png (100%) rename phpmon/Assets.xcassets/{MB_Elephant.imageset => Menu Bar Icons/MenuBar_elephant.imageset}/Menu Bar Elephant@2x.png (100%) rename phpmon/Assets.xcassets/{MB_PHP.imageset => Menu Bar Icons/MenuBar_php.imageset}/Contents.json (100%) rename phpmon/Assets.xcassets/{MB_PHP.imageset => Menu Bar Icons/MenuBar_php.imageset}/Menu Bar.png (100%) rename phpmon/Assets.xcassets/{MB_PHP.imageset => Menu Bar Icons/MenuBar_php.imageset}/Menu Bar@2x.png (100%) create mode 100644 phpmon/Domain/Preferences/MenuBarIcons.swift create mode 100644 phpmon/Domain/Preferences/Views/SelectPreferenceView.swift create mode 100644 phpmon/Domain/Preferences/Views/SelectPreferenceView.xib diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 7617b19..61b6521 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -14,8 +14,8 @@ 54B48B5F275F66AE006D90C5 /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B48B5E275F66AE006D90C5 /* Application.swift */; }; 54B48B60275F66AE006D90C5 /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B48B5E275F66AE006D90C5 /* Application.swift */; }; 54EAC806262F212B0092D14E /* GlobalKeybindPreference.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41CD0282628D8EE0065BBED /* GlobalKeybindPreference.swift */; }; - 54FCFD26276C883F004CE748 /* CheckboxPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54FCFD25276C883F004CE748 /* CheckboxPreferenceView.xib */; }; - 54FCFD27276C883F004CE748 /* CheckboxPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54FCFD25276C883F004CE748 /* CheckboxPreferenceView.xib */; }; + 54FCFD26276C883F004CE748 /* SelectPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54FCFD25276C883F004CE748 /* SelectPreferenceView.xib */; }; + 54FCFD27276C883F004CE748 /* SelectPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54FCFD25276C883F004CE748 /* SelectPreferenceView.xib */; }; 54FCFD2A276C8AA4004CE748 /* CheckboxPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54FCFD29276C8AA4004CE748 /* CheckboxPreferenceView.swift */; }; 54FCFD2B276C8AA4004CE748 /* CheckboxPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54FCFD29276C8AA4004CE748 /* CheckboxPreferenceView.swift */; }; 54FCFD2D276C8D67004CE748 /* HotkeyPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54FCFD2C276C8D67004CE748 /* HotkeyPreferenceView.xib */; }; @@ -24,6 +24,12 @@ 54FCFD31276C8DA4004CE748 /* HotkeyPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54FCFD2F276C8DA4004CE748 /* HotkeyPreferenceView.swift */; }; C405A4D024B9B9140062FAFA /* InternetAccessPolicy.strings in Resources */ = {isa = PBXBuildFile; fileRef = C405A4CE24B9B9130062FAFA /* InternetAccessPolicy.strings */; }; C405A4D124B9B9140062FAFA /* InternetAccessPolicy.plist in Resources */ = {isa = PBXBuildFile; fileRef = C405A4CF24B9B9140062FAFA /* InternetAccessPolicy.plist */; }; + C4068CA427B0780A00544CD5 /* CheckboxPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = C4068CA327B0780A00544CD5 /* CheckboxPreferenceView.xib */; }; + C4068CA527B0780A00544CD5 /* CheckboxPreferenceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = C4068CA327B0780A00544CD5 /* CheckboxPreferenceView.xib */; }; + C4068CA727B07A1300544CD5 /* SelectPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4068CA627B07A1300544CD5 /* SelectPreferenceView.swift */; }; + C4068CA827B07A1300544CD5 /* SelectPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4068CA627B07A1300544CD5 /* SelectPreferenceView.swift */; }; + C4068CAA27B0890D00544CD5 /* MenuBarIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4068CA927B0890D00544CD5 /* MenuBarIcons.swift */; }; + C4068CAB27B0890D00544CD5 /* MenuBarIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4068CA927B0890D00544CD5 /* MenuBarIcons.swift */; }; C40B24F127A3106D0018C7D2 /* ServicesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E67279DE0540010F296 /* ServicesView.swift */; }; C40B24F227A310770018C7D2 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E72279DFCF40010F296 /* Events.swift */; }; C40B24F327A310780018C7D2 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E72279DFCF40010F296 /* Events.swift */; }; @@ -235,12 +241,15 @@ 5420395E2613607600FB00FA /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; 54AB03252763858F00A29D5F /* Timer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = ""; }; 54B48B5E275F66AE006D90C5 /* Application.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = ""; }; - 54FCFD25276C883F004CE748 /* CheckboxPreferenceView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CheckboxPreferenceView.xib; sourceTree = ""; }; + 54FCFD25276C883F004CE748 /* SelectPreferenceView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SelectPreferenceView.xib; sourceTree = ""; }; 54FCFD29276C8AA4004CE748 /* CheckboxPreferenceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxPreferenceView.swift; sourceTree = ""; }; 54FCFD2C276C8D67004CE748 /* HotkeyPreferenceView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = HotkeyPreferenceView.xib; sourceTree = ""; }; 54FCFD2F276C8DA4004CE748 /* HotkeyPreferenceView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HotkeyPreferenceView.swift; sourceTree = ""; }; C405A4CE24B9B9130062FAFA /* InternetAccessPolicy.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = InternetAccessPolicy.strings; sourceTree = ""; }; C405A4CF24B9B9140062FAFA /* InternetAccessPolicy.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = InternetAccessPolicy.plist; sourceTree = ""; }; + C4068CA327B0780A00544CD5 /* CheckboxPreferenceView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CheckboxPreferenceView.xib; sourceTree = ""; }; + C4068CA627B07A1300544CD5 /* SelectPreferenceView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectPreferenceView.swift; sourceTree = ""; }; + C4068CA927B0890D00544CD5 /* MenuBarIcons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarIcons.swift; sourceTree = ""; }; C40C7F1D2772136000DDDCDC /* PhpEnv.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpEnv.swift; sourceTree = ""; }; C40C7F2727721FF600DDDCDC /* ActivePhpInstallation+Checks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ActivePhpInstallation+Checks.swift"; sourceTree = ""; }; C40C7F2F27722E8D00DDDCDC /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; @@ -378,6 +387,7 @@ 5420395826135DC100FB00FA /* PrefsVC.swift */, 5420395E2613607600FB00FA /* Preferences.swift */, C4C3ED4227834C5200AB15D8 /* CustomPrefs.swift */, + C4068CA927B0890D00544CD5 /* MenuBarIcons.swift */, C4DEB7D327A5D60B00834718 /* Stats.swift */, C41CD0272628D8E20065BBED /* Keybinds */, 54FCFD28276C88C0004CE748 /* Views */, @@ -401,8 +411,10 @@ 54FCFD28276C88C0004CE748 /* Views */ = { isa = PBXGroup; children = ( - 54FCFD25276C883F004CE748 /* CheckboxPreferenceView.xib */, + C4068CA327B0780A00544CD5 /* CheckboxPreferenceView.xib */, 54FCFD29276C8AA4004CE748 /* CheckboxPreferenceView.swift */, + 54FCFD25276C883F004CE748 /* SelectPreferenceView.xib */, + C4068CA627B07A1300544CD5 /* SelectPreferenceView.swift */, 54FCFD2C276C8D67004CE748 /* HotkeyPreferenceView.xib */, 54FCFD2F276C8DA4004CE748 /* HotkeyPreferenceView.swift */, ); @@ -852,9 +864,10 @@ C405A4D124B9B9140062FAFA /* InternetAccessPolicy.plist in Resources */, C44C1991276E44CB0072762D /* ProgressWindow.storyboard in Resources */, C4232EE52612526500158FC6 /* Credits.html in Resources */, - 54FCFD26276C883F004CE748 /* CheckboxPreferenceView.xib in Resources */, + 54FCFD26276C883F004CE748 /* SelectPreferenceView.xib in Resources */, C473319F2470923A009A0597 /* Localizable.strings in Resources */, C4F30B07278E195800755FCE /* brew-services.json in Resources */, + C4068CA427B0780A00544CD5 /* CheckboxPreferenceView.xib in Resources */, C4EC1E66279DE0380010F296 /* ServicesView.xib in Resources */, 54FCFD2D276C8D67004CE748 /* HotkeyPreferenceView.xib in Resources */, C405A4D024B9B9140062FAFA /* InternetAccessPolicy.strings in Resources */, @@ -866,9 +879,10 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 54FCFD27276C883F004CE748 /* CheckboxPreferenceView.xib in Resources */, + 54FCFD27276C883F004CE748 /* SelectPreferenceView.xib in Resources */, 54FCFD2E276C8D67004CE748 /* HotkeyPreferenceView.xib in Resources */, C4F780A825D80AE8000DBC97 /* php.ini in Resources */, + C4068CA527B0780A00544CD5 /* CheckboxPreferenceView.xib in Resources */, C43A8A2025D9D1D700591B77 /* brew.json in Resources */, C4AF9F72275445FF00D44ED0 /* valet-config.json in Resources */, C44C1992276E44CB0072762D /* ProgressWindow.storyboard in Resources */, @@ -921,6 +935,7 @@ C4AF9F7A2754499000D44ED0 /* Valet.swift in Sources */, 5420395926135DC100FB00FA /* PrefsVC.swift in Sources */, C43603A0275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */, + C4068CA727B07A1300544CD5 /* SelectPreferenceView.swift in Sources */, C49E171F27A5736E00787921 /* PMServicesView.swift in Sources */, C4EE55AD27708B9E001DF387 /* PMStatsView.swift in Sources */, C4C8E818276F54D8003AC782 /* App+ConfigWatch.swift in Sources */, @@ -954,6 +969,7 @@ C41CA5ED2774F8EE00A2C80E /* SiteListVC+Actions.swift in Sources */, C412E5FC25700D5300A1FB67 /* HomebrewPackage.swift in Sources */, C4D9ADBF277610E1007277F4 /* PhpSwitcher.swift in Sources */, + C4068CAA27B0890D00544CD5 /* MenuBarIcons.swift in Sources */, 54AB03262763858F00A29D5F /* Timer.swift in Sources */, C4C8E81B276F54E5003AC782 /* PhpConfigWatcher.swift in Sources */, C417DC74277614690015E6EE /* Helpers.swift in Sources */, @@ -1006,6 +1022,7 @@ C4D9ADC0277610E1007277F4 /* PhpSwitcher.swift in Sources */, C4F780CC25D80B75000DBC97 /* ActivePhpInstallation.swift in Sources */, C4F780B125D80B4D000DBC97 /* PhpExtension.swift in Sources */, + C4068CA827B07A1300544CD5 /* SelectPreferenceView.swift in Sources */, C4F780CE25D80B75000DBC97 /* LocalNotification.swift in Sources */, C40C7F2927721FF600DDDCDC /* ActivePhpInstallation+Checks.swift in Sources */, C4FBFC532616485F00CDB8E1 /* PhpVersionDetectionTest.swift in Sources */, @@ -1031,6 +1048,7 @@ C481F79726164A78004FBCFF /* PrefsVC.swift in Sources */, C41E871B2763D42300161EE0 /* SiteListVC+ContextMenu.swift in Sources */, C40C7F3127722E8D00DDDCDC /* Logger.swift in Sources */, + C4068CAB27B0890D00544CD5 /* MenuBarIcons.swift in Sources */, C4F30B09278E1A0E00755FCE /* CustomPrefs.swift in Sources */, C464ADB3275A87CA003FCD53 /* SiteListCell.swift in Sources */, C415D3E92770F692005EF286 /* AppDelegate+InterApp.swift in Sources */, diff --git a/phpmon/Assets.xcassets/Menu Bar Icons/Contents.json b/phpmon/Assets.xcassets/Menu Bar Icons/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/phpmon/Assets.xcassets/Menu Bar Icons/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/phpmon/Assets.xcassets/MB_Elephant.imageset/Contents.json b/phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_elephant.imageset/Contents.json similarity index 100% rename from phpmon/Assets.xcassets/MB_Elephant.imageset/Contents.json rename to phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_elephant.imageset/Contents.json diff --git a/phpmon/Assets.xcassets/MB_Elephant.imageset/Menu Bar Elephant.png b/phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_elephant.imageset/Menu Bar Elephant.png similarity index 100% rename from phpmon/Assets.xcassets/MB_Elephant.imageset/Menu Bar Elephant.png rename to phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_elephant.imageset/Menu Bar Elephant.png diff --git a/phpmon/Assets.xcassets/MB_Elephant.imageset/Menu Bar Elephant@2x.png b/phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_elephant.imageset/Menu Bar Elephant@2x.png similarity index 100% rename from phpmon/Assets.xcassets/MB_Elephant.imageset/Menu Bar Elephant@2x.png rename to phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_elephant.imageset/Menu Bar Elephant@2x.png diff --git a/phpmon/Assets.xcassets/MB_PHP.imageset/Contents.json b/phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_php.imageset/Contents.json similarity index 100% rename from phpmon/Assets.xcassets/MB_PHP.imageset/Contents.json rename to phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_php.imageset/Contents.json diff --git a/phpmon/Assets.xcassets/MB_PHP.imageset/Menu Bar.png b/phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_php.imageset/Menu Bar.png similarity index 100% rename from phpmon/Assets.xcassets/MB_PHP.imageset/Menu Bar.png rename to phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_php.imageset/Menu Bar.png diff --git a/phpmon/Assets.xcassets/MB_PHP.imageset/Menu Bar@2x.png b/phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_php.imageset/Menu Bar@2x.png similarity index 100% rename from phpmon/Assets.xcassets/MB_PHP.imageset/Menu Bar@2x.png rename to phpmon/Assets.xcassets/Menu Bar Icons/MenuBar_php.imageset/Menu Bar@2x.png diff --git a/phpmon/Domain/Helpers/MenuBarImageGenerator.swift b/phpmon/Domain/Helpers/MenuBarImageGenerator.swift index f250b0f..a68d15a 100644 --- a/phpmon/Domain/Helpers/MenuBarImageGenerator.swift +++ b/phpmon/Domain/Helpers/MenuBarImageGenerator.swift @@ -74,7 +74,13 @@ class MenuBarImageGenerator { let textImage = self.textToImage(text: text) // Then we'll fetch the image we want on the left - let iconImage = NSImage(named: "MB_PHP")! + var iconType = Preferences.preferences[.iconTypeToDisplay] as? String + if iconType == nil { + Log.warn("Invalid icon type found, using the default") + iconType = MenuBarIcon.iconPhp.rawValue + } + + let iconImage = NSImage(named: "MenuBar_\(iconType!)")! // We'll need to reference the width of the icon a bunch of times let iconWidthSize = iconImage.size.width diff --git a/phpmon/Domain/Menu/MainMenu.swift b/phpmon/Domain/Menu/MainMenu.swift index a3e86a9..ecb800c 100644 --- a/phpmon/Domain/Menu/MainMenu.swift +++ b/phpmon/Domain/Menu/MainMenu.swift @@ -68,7 +68,7 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate { */ func setStatusBarImage(version: String) { setStatusBar( - image: Preferences.isEnabled(.shouldDisplayPhpHintInIcon) + image: (Preferences.preferences[.iconTypeToDisplay] as! String != MenuBarIcon.noIcon.rawValue) ? MenuBarImageGenerator.textToImageWithIcon(text: version) : MenuBarImageGenerator.textToImage(text: version) ) diff --git a/phpmon/Domain/Preferences/MenuBarIcons.swift b/phpmon/Domain/Preferences/MenuBarIcons.swift new file mode 100644 index 0000000..dbe516a --- /dev/null +++ b/phpmon/Domain/Preferences/MenuBarIcons.swift @@ -0,0 +1,16 @@ +// +// MenuBarIcons.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 06/02/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation +import Cocoa + +enum MenuBarIcon: String, CaseIterable { + case iconPhp = "php" + case iconElephant = "elephant" + case noIcon = "none" +} diff --git a/phpmon/Domain/Preferences/Preferences.swift b/phpmon/Domain/Preferences/Preferences.swift index ea2b1a3..1d95ed9 100644 --- a/phpmon/Domain/Preferences/Preferences.swift +++ b/phpmon/Domain/Preferences/Preferences.swift @@ -14,7 +14,7 @@ import Foundation enum PreferenceName: String { case wasLaunchedBefore = "launched_before" case shouldDisplayDynamicIcon = "use_dynamic_icon" - case shouldDisplayPhpHintInIcon = "add_php_to_icon" + case iconTypeToDisplay = "icon_type_to_display" case fullPhpVersionDynamicIcon = "full_php_in_menu_bar" case autoServiceRestartAfterExtensionToggle = "auto_restart_after_extension_toggle" case autoComposerGlobalUpdateAfterSwitch = "auto_composer_global_update_after_switch" @@ -22,6 +22,13 @@ enum PreferenceName: String { case globalHotkey = "global_hotkey" } +/** + These are retired preferences that, if present, should be migrated. + */ +enum RetiredPreferenceName: String { + case shouldDisplayPhpHintInIcon = "add_php_to_icon" +} + /** These are internal stats. They NEVER get shared. */ @@ -64,7 +71,7 @@ class Preferences { UserDefaults.standard.register(defaults: [ /// Preferences PreferenceName.shouldDisplayDynamicIcon.rawValue: true, - PreferenceName.shouldDisplayPhpHintInIcon.rawValue: true, + PreferenceName.iconTypeToDisplay.rawValue: MenuBarIcon.iconPhp.rawValue, PreferenceName.fullPhpVersionDynamicIcon.rawValue: false, PreferenceName.autoServiceRestartAfterExtensionToggle.rawValue: true, PreferenceName.autoComposerGlobalUpdateAfterSwitch.rawValue: false, @@ -76,6 +83,7 @@ class Preferences { ]) if UserDefaults.standard.bool(forKey: PreferenceName.wasLaunchedBefore.rawValue) { + handleMigration() return } @@ -84,6 +92,23 @@ class Preferences { UserDefaults.standard.synchronize() } + /** + Sometimes preferences will change, and a migration is required to take the user's previous preference + and migrate it over to the new type. For example, the choice to disable the icon next to the version + number was once a boolean (do you want the icon? yes / no) but has now become a multi-faceted option. + */ + static func handleMigration() { + // If the user chose the "no icon" option, migrate it over + if ( + UserDefaults.standard.value(forKey: RetiredPreferenceName.shouldDisplayPhpHintInIcon.rawValue) != nil && + UserDefaults.standard.bool(forKey: RetiredPreferenceName.shouldDisplayPhpHintInIcon.rawValue) == false + ) { + Log.info("The preference where the user chose no icon has been migrated over.") + UserDefaults.standard.set(MenuBarIcon.noIcon.rawValue, forKey: PreferenceName.iconTypeToDisplay.rawValue) + UserDefaults.standard.removeObject(forKey: RetiredPreferenceName.shouldDisplayPhpHintInIcon.rawValue) + } + } + // MARK: - API static var preferences: [PreferenceName: Any?] { @@ -112,7 +137,6 @@ class Preferences { return [ // Part 1: Always Booleans .shouldDisplayDynamicIcon: UserDefaults.standard.bool(forKey: PreferenceName.shouldDisplayDynamicIcon.rawValue) as Any, - .shouldDisplayPhpHintInIcon: UserDefaults.standard.bool(forKey: PreferenceName.shouldDisplayPhpHintInIcon.rawValue) as Any, .fullPhpVersionDynamicIcon: UserDefaults.standard.bool(forKey: PreferenceName.fullPhpVersionDynamicIcon.rawValue) as Any, .autoServiceRestartAfterExtensionToggle: UserDefaults.standard.bool(forKey: PreferenceName.autoServiceRestartAfterExtensionToggle.rawValue) as Any, .autoComposerGlobalUpdateAfterSwitch: UserDefaults.standard.bool(forKey: PreferenceName.autoComposerGlobalUpdateAfterSwitch.rawValue) as Any, @@ -120,6 +144,7 @@ class Preferences { // Part 2: Always Strings .globalHotkey: UserDefaults.standard.string(forKey: PreferenceName.globalHotkey.rawValue) as Any, + .iconTypeToDisplay: UserDefaults.standard.string(forKey: PreferenceName.iconTypeToDisplay.rawValue) as Any, ] } diff --git a/phpmon/Domain/Preferences/PrefsVC.swift b/phpmon/Domain/Preferences/PrefsVC.swift index 13b1e9d..8f7fc9f 100644 --- a/phpmon/Domain/Preferences/PrefsVC.swift +++ b/phpmon/Domain/Preferences/PrefsVC.swift @@ -57,11 +57,12 @@ class PrefsVC: NSViewController { MainMenu.shared.refreshIcon() } ), - CheckboxPreferenceView.make( + SelectPreferenceView.make( sectionText: "", - descriptionText: "prefs.icon_hint_desc".localized, - checkboxText: "prefs.icon_hint_title".localized, - preference: .shouldDisplayPhpHintInIcon, + descriptionText: "prefs.icon_options_desc".localized, + options: MenuBarIcon.allCases.map({ return $0.rawValue }), + localizationPrefix: "prefs.icon_options", + preference: .iconTypeToDisplay, action: { MainMenu.shared.refreshIcon() } diff --git a/phpmon/Domain/Preferences/Views/CheckboxPreferenceView.xib b/phpmon/Domain/Preferences/Views/CheckboxPreferenceView.xib index 02aa72b..589e766 100644 --- a/phpmon/Domain/Preferences/Views/CheckboxPreferenceView.xib +++ b/phpmon/Domain/Preferences/Views/CheckboxPreferenceView.xib @@ -61,7 +61,7 @@ - + diff --git a/phpmon/Domain/Preferences/Views/SelectPreferenceView.swift b/phpmon/Domain/Preferences/Views/SelectPreferenceView.swift new file mode 100644 index 0000000..9e7b126 --- /dev/null +++ b/phpmon/Domain/Preferences/Views/SelectPreferenceView.swift @@ -0,0 +1,83 @@ +// +// SelectPreferenceView.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 06/02/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation +import Cocoa + +class SelectPreferenceView: NSView, XibLoadable { + + @IBOutlet weak var labelSection: NSTextField! + @IBOutlet weak var labelDescription: NSTextField! + @IBOutlet weak var popupButton: NSPopUpButton! + + var localizationPrefix: String = "" + var imagePrefix: String? = nil + + var options: [String] = [] { + didSet { + self.popupButton.removeAllItems() + self.options.forEach { value in + self.popupButton.addItem( + withTitle: "\(localizationPrefix).\(value)".localized + ) + } + + if imagePrefix == nil { + return + } + + self.popupButton.itemArray.enumerated().forEach { item in + print("\(imagePrefix!)_\(self.options[item.offset])") + item.element.image = NSImage(named: "\(imagePrefix!)_\(self.options[item.offset])") + } + } + } + + var action: (() -> Void)! + + var preference: PreferenceName! { + didSet { + let value = Preferences.preferences[preference] as! String + self.options.enumerated().forEach { option in + if option.element == value { + self.popupButton.selectItem(at: option.offset) + } + } + } + } + + static func make( + sectionText: String, + descriptionText: String, + options: [String], + localizationPrefix: String, + imagePrefix: String? = nil, + preference: PreferenceName, + action: @escaping () -> Void) -> NSView + { + let view = Self.createFromXib()! + + view.labelSection.stringValue = sectionText + view.labelDescription.stringValue = descriptionText + + view.localizationPrefix = localizationPrefix + view.imagePrefix = imagePrefix + view.options = options + view.preference = preference + view.action = action + + return view + } + + @IBAction func valueChanged(_ sender: Any) { + let index = self.popupButton.indexOfSelectedItem + Preferences.update(.iconTypeToDisplay, value: self.options[index]) + self.action() + } + +} diff --git a/phpmon/Domain/Preferences/Views/SelectPreferenceView.xib b/phpmon/Domain/Preferences/Views/SelectPreferenceView.xib new file mode 100644 index 0000000..0e5dbbc --- /dev/null +++ b/phpmon/Domain/Preferences/Views/SelectPreferenceView.xib @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index e10b864..579f880 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -133,22 +133,26 @@ "prefs.subtitle" = "Preferences"; "prefs.close" = "Close"; -"prefs.global_shortcut" = "Global shortcut:"; -"prefs.dynamic_icon" = "Icon customization:"; -"prefs.info_density" = "Info density:"; +"prefs.global_shortcut" = "Global Shortcut:"; +"prefs.dynamic_icon" = "Dynamic Icon:"; +"prefs.dynamic_icon" = "Icon Type:"; +"prefs.info_density" = "Info Density:"; "prefs.services" = "Services:"; "prefs.switcher" = "Switcher:"; "prefs.integrations" = "Integrations:"; +"prefs.icon_options.php" = "Display PHP Icon"; +"prefs.icon_options.elephant" = "Display Elephant Icon"; +"prefs.icon_options.none" = "Do Not Display Icon"; + +"prefs.icon_options_desc" = "This option decides which icon will be displayed next to the version number of the currently linked PHP version. If the Dynamic Icon option has been disabled, this will have no effect."; + "prefs.auto_restart_services_title" = "Auto-restart PHP-FPM"; "prefs.auto_restart_services_desc" = "When checked, will automatically restart PHP-FPM when you check or uncheck an extension. Slightly slower when enabled, but this applies the extension change immediately for all sites you're serving, no need to restart PHP-FPM manually."; "prefs.dynamic_icon_title" = "Display dynamic icon in menu bar"; "prefs.dynamic_icon_desc" = "If you uncheck this box, the truck icon will always be visible. If checked, it will display the major version number of the currently linked PHP version."; -"prefs.icon_hint_title" = "Display PHP hint next to version number"; -"prefs.icon_hint_desc" = "If you uncheck this box, the icon in the menu bar will only show the version number and not the PHP hint. This preference does not do anything if the dynamic icon has been disabled."; - "prefs.display_full_php_version" = "Display full PHP version everywhere"; "prefs.display_full_php_version_desc" = "Display the full version instead of the major version displayed in the menu bar and the dropdown menu. (This may be undesirable on smaller displays, so this is disabled by default.)";