Skip to content

Commit

Permalink
feat: add ability to choose different status item icons (#19)
Browse files Browse the repository at this point in the history
* feat: add ability to choose different status item icons
  • Loading branch information
acheronfail authored Feb 18, 2019
1 parent d7d0323 commit 258b2ce
Show file tree
Hide file tree
Showing 42 changed files with 141 additions and 18 deletions.
33 changes: 32 additions & 1 deletion PixelPicker/AppDelegate+Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ let focusModifiers: [(String, NSEvent.ModifierFlags)] = [
("⇧ Shift ", .shift)
]

// The available status item images that the user may pick from.
let statusItemImages: [(String, String)] = [
("Magnifying Glass (Default)", "icon-default"),
("Palette", "icon-palette"),
("Dropper", "icon-dropper"),
("Magnifying Glass Dropper", "icon-mag-dropper"),
("Magnifying Glass Dropper Flat", "icon-mag-dropper-flat")
]

extension AppDelegate: NSMenuDelegate {
// Unregister the activating shortcut when the menu is opened/closed so it can't be called when
// setting a new shortcut. Also start a run loop observer so we know when the modifierFlags have
Expand Down Expand Up @@ -75,11 +84,12 @@ extension AppDelegate: NSMenuDelegate {
contextMenu.removeAllItems()

let pickItem = contextMenu.addItem(withTitle: "Pick a pixel!", action: #selector(showPicker), keyEquivalent: "")
pickItem.image = ICON
pickItem.image = PPState.shared.statusItemImage(withName: PPState.shared.statusItemImageName)

buildRecentPicks()

contextMenu.addItem(.separator())
buildAppIconMenu()
buildShowGridMenu()
buildColorSpaceItem()
buildColorFormatsMenu()
Expand All @@ -95,6 +105,27 @@ extension AppDelegate: NSMenuDelegate {
contextMenu.addItem(withTitle: "Quit \(APP_NAME)", action: #selector(quitApplication), keyEquivalent: "")
}

// Choose the status item icon.
private func buildAppIconMenu() {
let submenu = NSMenu()
for (name, imageName) in statusItemImages {
let item = submenu.addItem(withTitle: name, action: #selector(selectAppIcon(_:)), keyEquivalent: "")
item.representedObject = imageName
item.state = PPState.shared.statusItemImageName == imageName ? .on : .off
item.image = PPState.shared.statusItemImage(withName: imageName)
}

let item = contextMenu.addItem(withTitle: "App Icon", action: nil, keyEquivalent: "")
item.submenu = submenu
}

@objc private func selectAppIcon(_ sender: NSMenuItem) {
if let imageName = sender.representedObject as? String {
menuBarItem.image = PPState.shared.statusItemImage(withName: imageName)
PPState.shared.statusItemImageName = imageName
}
}

// Choose whether to always draw a grid, never draw one, or only draw one when in focus mode.
private func buildShowGridMenu() {
let submenu = NSMenu()
Expand Down
5 changes: 1 addition & 4 deletions PixelPicker/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import MASShortcut
import CleanroomLogger

// The app's menu bar item.
let ICON = setupMenuBarIcon(NSImage(named: NSImage.Name(stringLiteral: "icon")))

@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {

// This controller manages the pixel picker itself.
Expand Down Expand Up @@ -38,7 +35,7 @@ let ICON = setupMenuBarIcon(NSImage(named: NSImage.Name(stringLiteral: "icon")))
func applicationDidFinishLaunching(_ aNotification: Notification) {
contextMenu.delegate = self

menuBarItem.image = ICON
menuBarItem.image = PPState.shared.statusItemImage(withName: PPState.shared.statusItemImageName)
menuBarItem.action = #selector(onMenuClick)
menuBarItem.sendAction(on: [.leftMouseUp, .rightMouseUp])

Expand Down
23 changes: 23 additions & 0 deletions PixelPicker/Assets.xcassets/icon-dropper.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "dropper.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "dropper-1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "dropper-2.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "mag_dropper_flat.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "mag_dropper_flat-1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "mag_dropper_flat-2.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "mag_dropper.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "mag_dropper-1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "mag_dropper-2.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions PixelPicker/Assets.xcassets/icon-palette.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "palette.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "palette-1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "palette-2.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions PixelPicker/PPState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import CleanroomLogger
// Whether the color format should be uppercased or not.
var useUppercase: Bool = false

// The chosen icon for the status item (the icon in the menu bar).
var statusItemImageName: String = "icon-default"

// The shortcut that activates the pixel picker.
var activatingShortcut: MASShortcut?

Expand Down Expand Up @@ -65,13 +68,23 @@ import CleanroomLogger
recentPicks.append(color)
}

// Returns the chosen image for the Status Item in the menu bar.
func statusItemImage(withName name: String) -> NSImage? {
if let img = NSImage(named: NSImage.Name(stringLiteral: name)) {
return setupMenuBarIcon(img)
}

return setupMenuBarIcon(NSImage(named: NSImage.Name(stringLiteral: "icon-default")))
}

/**
* Below are methods related to saving/loading state from disk.
*/

func resetState() {
paschaModeEnabled = false
useUppercase = false
statusItemImageName = "icon-default"
focusModeModifier = .control
activatingShortcut = nil
chosenFormat = .genericHex
Expand Down Expand Up @@ -105,6 +118,8 @@ import CleanroomLogger
paschaModeEnabled = value.bool ?? false
case "useUppercase":
useUppercase = value.bool ?? false
case "statusItemImageName":
statusItemImageName = value.string ?? "icon-default"
case "focusModeModifier":
focusModeModifier = NSEvent.ModifierFlags(rawValue: value.uInt ?? NSEvent.ModifierFlags.control.rawValue)
case "activatingShortcut":
Expand Down Expand Up @@ -160,6 +175,7 @@ import CleanroomLogger
let json: JSON = [
"paschaModeEnabled": paschaModeEnabled,
"useUppercase": useUppercase,
"statusItemImageName": statusItemImageName,
"focusModeModifier": focusModeModifier.rawValue,
"activatingShortcut": shortcutData,
"magnificationLevel": magnificationLevel,
Expand Down
Binary file added Resources/dropper.afdesign
Binary file not shown.
Binary file added Resources/dropper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Resources/mag.iconset/icon_128x128.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_128x128@2x.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_16x16.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_16x16@2x.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_256x256.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_256x256@2x.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_32x32.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_32x32@2x.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_512x512.png
Binary file not shown.
Binary file removed Resources/mag.iconset/icon_512x512@2x.png
Diff not rendered.
13 changes: 0 additions & 13 deletions Resources/mag.svg
Diff not rendered.
Binary file added Resources/mag_dropper.afdesign
Binary file not shown.
Binary file added Resources/mag_dropper.png
Binary file added Resources/mag_dropper_flat.afdesign
Binary file not shown.
Binary file added Resources/mag_dropper_flat.png
Binary file added Resources/palette.afdesign
Binary file not shown.
Binary file added Resources/palette.png

0 comments on commit 258b2ce

Please sign in to comment.