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

feat: add preference option to change MiniSim icon #126

Merged
merged 1 commit into from
Sep 1, 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
4 changes: 4 additions & 0 deletions MiniSim.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
76AC9AF62A0EA82C00864A8B /* CustomCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76AC9AF52A0EA82C00864A8B /* CustomCommands.swift */; };
76AC9AF92A0EB50800864A8B /* SymbolPicker in Frameworks */ = {isa = PBXBuildFile; productRef = 76AC9AF82A0EB50800864A8B /* SymbolPicker */; };
76B70F7E2B0D361A009D87A4 /* UserDefaultsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76B70F7D2B0D361A009D87A4 /* UserDefaultsTests.swift */; };
76C1396A2C849A3F006CD80C /* MenuIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76C139692C849A3F006CD80C /* MenuIcons.swift */; };
76E4451229D4391000039025 /* Onboarding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E4451129D4391000039025 /* Onboarding.swift */; };
76E4451429D4403F00039025 /* NSNotificationName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E4451329D4403F00039025 /* NSNotificationName.swift */; };
76F04A11298A5AE000BF9CA3 /* ADB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76F04A10298A5AE000BF9CA3 /* ADB.swift */; };
Expand Down Expand Up @@ -165,6 +166,7 @@
76AC9AF52A0EA82C00864A8B /* CustomCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCommands.swift; sourceTree = "<group>"; };
76B70F742B0D359D009D87A4 /* MiniSimTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MiniSimTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
76B70F7D2B0D361A009D87A4 /* UserDefaultsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultsTests.swift; sourceTree = "<group>"; };
76C139692C849A3F006CD80C /* MenuIcons.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuIcons.swift; sourceTree = "<group>"; };
76E4451129D4391000039025 /* Onboarding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Onboarding.swift; sourceTree = "<group>"; };
76E4451329D4403F00039025 /* NSNotificationName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSNotificationName.swift; sourceTree = "<group>"; };
76F04A10298A5AE000BF9CA3 /* ADB.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADB.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -225,6 +227,7 @@
762CF1E12981DDD400099999 /* Extensions */ = {
isa = PBXGroup;
children = (
76C139692C849A3F006CD80C /* MenuIcons.swift */,
762CF1DF2981968F00099999 /* String+match.swift */,
7645D5022983186100019227 /* NSMenuItem+ImageInit.swift */,
7630B26C2986B4FD00D8B57D /* KeyboardShortcuts.swift */,
Expand Down Expand Up @@ -629,6 +632,7 @@
7630B26D2986B4FD00D8B57D /* KeyboardShortcuts.swift in Sources */,
76059BF52AD4361C0008D38B /* SetupPreferences.swift in Sources */,
7684FAAF29D202F500230BB0 /* AndroidHomeError.swift in Sources */,
76C1396A2C849A3F006CD80C /* MenuIcons.swift in Sources */,
55CDB0782B1B6D24002418D7 /* TerminalApps.swift in Sources */,
7645D4BE2982A1B100019227 /* DeviceService.swift in Sources */,
765ABF382A8BECD900A063CB /* ExecuteCommand.swift in Sources */,
Expand Down
Binary file removed MiniSim/.DS_Store
Binary file not shown.
Binary file removed MiniSim/Assets.xcassets/.DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions MiniSim/Assets.xcassets/box.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions MiniSim/Assets.xcassets/ipad.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scale" : "1x"
},
{
"filename" : "menu_icon.png",
"filename" : "iphone@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
Expand Down
28 changes: 28 additions & 0 deletions MiniSim/Extensions/MenuIcons.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import AppKit
import Foundation

enum MenuImage: String, CaseIterable {
case iphone
case ipad
case box

var image: NSImage? {
guard let itemImage = NSImage(named: self.rawValue) else {
return nil
}
itemImage.size = size
itemImage.isTemplate = true
return itemImage
}

var size: NSSize {
switch self {
case .box:
return NSSize(width: 16.5, height: 15)
case .ipad:
return NSSize(width: 19, height: 14)
case .iphone:
return NSSize(width: 11, height: 19)
}
}
}
6 changes: 6 additions & 0 deletions MiniSim/Extensions/UserDefaults+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extension UserDefaults {
static let enableiOSSimulators = "enableiOSSimulators"
static let enableAndroidEmulators = "enableAndroidEmulators"
static let preferedTerminal = "preferedTerminal"
static let menuImage = "menuImage"
}

@objc public dynamic var androidHome: String? {
Expand All @@ -28,6 +29,11 @@ extension UserDefaults {
set { set(newValue, forKey: Keys.isOnboardingFinished) }
}

@objc public dynamic var menuImage: String {
get { string(forKey: Keys.menuImage) ?? "iphone" }
set { set(newValue, forKey: Keys.menuImage) }
}

public var parameters: Data? {
get { object(forKey: Keys.parameters) as? Data }
set { set(newValue, forKey: Keys.parameters) }
Expand Down
15 changes: 10 additions & 5 deletions MiniSim/MiniSim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MiniSim: NSObject {
@objc let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

private var isOnboardingFinishedObserver: NSKeyValueObservation?
private var menuImageObserver: NSKeyValueObservation?

private lazy var onboarding = Onboarding()

Expand All @@ -32,6 +33,7 @@ class MiniSim: NSObject {

deinit {
isOnboardingFinishedObserver?.invalidate()
menuImageObserver?.invalidate()
NotificationCenter.default.removeObserver(self, name: .commandDidSucceed, object: nil)
NotificationCenter.default.removeObserver(self, name: .deviceDeleted, object: nil)
}
Expand Down Expand Up @@ -98,6 +100,10 @@ class MiniSim: NSObject {
self.onboarding.showPopOver(button: self.statusItem.button)
}
}
menuImageObserver = UserDefaults.standard.observe(\.menuImage, options: .new) { _, _ in
self.setMenuImage()
}

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(
self,
Expand All @@ -123,11 +129,10 @@ class MiniSim: NSObject {

private func setMenuImage() {
if let button = statusItem.button {
button.toolTip = "MiniSim"
let itemImage = NSImage(named: "menu_icon")
itemImage?.size = NSSize(width: 9, height: 16)
itemImage?.isTemplate = true
button.image = itemImage
button.toolTip = "MiniSim"
let itemImage = MenuImage(rawValue: UserDefaults.standard.menuImage)?.image
itemImage?.isTemplate = true
button.image = itemImage
}
}

Expand Down
23 changes: 23 additions & 0 deletions MiniSim/Views/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import LaunchAtLogin
import Settings
import ShellOut
import SwiftUI

struct Preferences: View {
@State var menuImageSelected: String = "iphone"
@State private var preferedTerminal: Terminal

init() {
Expand All @@ -24,6 +26,20 @@ struct Preferences: View {

var body: some View {
Settings.Container(contentWidth: 400) {
Settings.Section(title: "Icon:") {
Picker("", selection: $menuImageSelected) {
ForEach(MenuImage.allCases, id: \.self) { image in
Image(nsImage: NSImage(imageLiteralResourceName: image.rawValue))
.tag(image.rawValue)
}
}
.fixedSize(horizontal: true, vertical: false)
.onChange(of: menuImageSelected) { _ in
UserDefaults.standard.menuImage = menuImageSelected
}
Text("The icon displayed in the Menu Bar.")
.descriptionText()
}
Settings.Section(title: "Preferred Terminal:") {
Picker("", selection: $preferedTerminal) {
let availableTerminal = Terminal.allCases.filter { checkAppIsInstalled(appName: $0) }
Expand Down Expand Up @@ -57,6 +73,13 @@ struct Preferences: View {
}
}
.frame(minWidth: 650, minHeight: 450)
.onAppear {
MenuImage.allCases.forEach { image in
if UserDefaults.standard.menuImage == image.rawValue {
menuImageSelected = image.rawValue
}
}
}
}

func checkAppIsInstalled(appName: Terminal) -> Bool {
Expand Down
Loading