Skip to content

Commit 2288c89

Browse files
committed
Fix #3.
1 parent 350afa5 commit 2288c89

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

SwitchKey.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
7115A91622631E6A00DD386A /* InputSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 7115A91522631E6A00DD386A /* InputSource.m */; };
1111
716404EE226A0FFB00E0F0A1 /* LoginServiceKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 716404ED226A0FFB00E0F0A1 /* LoginServiceKit.swift */; };
1212
717050D4226204DE008A2686 /* SwitchKey.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 717050D3226204DE008A2686 /* SwitchKey.xcassets */; };
13+
718A6EDF2285333700DB6B05 /* NSImage+extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 718A6EDE2285333700DB6B05 /* NSImage+extensions.swift */; };
1314
71FECF2E22610BE600C51FD7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FECF2D22610BE600C51FD7 /* AppDelegate.swift */; };
1415
71FECF3322610BE600C51FD7 /* SwitchKey.xib in Resources */ = {isa = PBXBuildFile; fileRef = 71FECF3122610BE600C51FD7 /* SwitchKey.xib */; };
1516
/* End PBXBuildFile section */
@@ -20,6 +21,7 @@
2021
7115A91522631E6A00DD386A /* InputSource.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InputSource.m; sourceTree = "<group>"; };
2122
716404ED226A0FFB00E0F0A1 /* LoginServiceKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginServiceKit.swift; sourceTree = "<group>"; };
2223
717050D3226204DE008A2686 /* SwitchKey.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = SwitchKey.xcassets; sourceTree = "<group>"; };
24+
718A6EDE2285333700DB6B05 /* NSImage+extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSImage+extensions.swift"; sourceTree = "<group>"; };
2325
71FECF2A22610BE600C51FD7 /* SwitchKey.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwitchKey.app; sourceTree = BUILT_PRODUCTS_DIR; };
2426
71FECF2D22610BE600C51FD7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2527
71FECF3222610BE600C51FD7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/SwitchKey.xib; sourceTree = "<group>"; };
@@ -66,6 +68,7 @@
6668
7115A91522631E6A00DD386A /* InputSource.m */,
6769
7115A91322631E6A00DD386A /* SwitchKey-Bridging-Header.h */,
6870
716404ED226A0FFB00E0F0A1 /* LoginServiceKit.swift */,
71+
718A6EDE2285333700DB6B05 /* NSImage+extensions.swift */,
6972
);
7073
path = SwitchKey;
7174
sourceTree = "<group>";
@@ -148,6 +151,7 @@
148151
files = (
149152
71FECF2E22610BE600C51FD7 /* AppDelegate.swift in Sources */,
150153
716404EE226A0FFB00E0F0A1 /* LoginServiceKit.swift in Sources */,
154+
718A6EDF2285333700DB6B05 /* NSImage+extensions.swift in Sources */,
151155
7115A91622631E6A00DD386A /* InputSource.m in Sources */,
152156
);
153157
runOnlyForDeploymentPostprocessing = 0;

SwitchKey/AppDelegate.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// AppDelegate.swift
3-
// AppDelegate
3+
// SwitchKey
44
//
55
// Created by Jinyu Li on 2019/03/16.
66
// Copyright © 2019 Jinyu Li. All rights reserved.
@@ -178,8 +178,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab
178178

179179
let element = AXUIElementCreateApplication(pid)
180180
let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
181-
AXObserverAddNotification(observer, element, kAXApplicationActivatedNotification as CFString, selfPtr)
182-
181+
AXObserverAddNotification(observer, element, NSAccessibility.Notification.applicationActivated.rawValue as CFString, selfPtr)
183182
applicationObservers[pid] = observer
184183
}
185184
}
@@ -273,8 +272,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab
273272
let itemCell = conditionTableView.makeView(withIdentifier: itemCellIdentifier, owner: nil) as! ConditionCell
274273
itemCell.appIcon.image = item.applicationIcon
275274
itemCell.appName.stringValue = item.applicationName
276-
itemCell.inputSourceButton.image = item.inputSourceIcon
277-
itemCell.inputSourceButton.image?.isTemplate = true
275+
276+
let icon = item.inputSourceIcon
277+
itemCell.inputSourceButton.image = icon
278+
itemCell.inputSourceButton.image?.isTemplate = icon.canTemplate()
279+
278280
itemCell.conditionEnabled.state = item.enabled ? .on : .off
279281
return itemCell
280282
} else {

SwitchKey/Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.1</string>
20+
<string>1.1.1</string>
2121
<key>CFBundleVersion</key>
22-
<string>1</string>
22+
<string>7</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

SwitchKey/NSImage+extensions.swift

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// NSImage+extensions.swift
3+
// SwitchKey
4+
//
5+
// Created by Jinyu Li on 2019/05/10.
6+
// Copyright © 2019 Jinyu Li. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
11+
extension NSImage {
12+
// A dumb method:
13+
// go over the pixels, and check the color range.
14+
// an image can be safely templated if its color range is limited.
15+
// luckily, our image is small, and the check is done once for each new icon.
16+
func canTemplate() -> Bool {
17+
guard let cgContext = CGContext(
18+
data: nil,
19+
width: Int(size.width),
20+
height: Int(size.height),
21+
bitsPerComponent: 8,
22+
bytesPerRow: 0,
23+
space: CGColorSpaceCreateDeviceRGB(),
24+
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
25+
) else {
26+
return false
27+
}
28+
29+
let nsContext = NSGraphicsContext(cgContext: cgContext, flipped: false)
30+
NSGraphicsContext.current = nsContext
31+
draw(in: NSMakeRect(0, 0, size.width, size.height))
32+
33+
guard let pixel = cgContext.data else {
34+
return false;
35+
}
36+
37+
let width = cgContext.width;
38+
var rmin:UInt32 = 255, rmax:UInt32 = 0
39+
var gmin:UInt32 = 255, gmax:UInt32 = 0
40+
var bmin:UInt32 = 255, bmax:UInt32 = 0
41+
for y in 0 ... cgContext.height {
42+
for x in 0 ... width {
43+
let px = pixel.load(fromByteOffset: (y * width + x) * 4, as: UInt32.self)
44+
let r = (px & 0x000000ff) >> 0
45+
let g = (px & 0x0000ff00) >> 8
46+
let b = (px & 0x00ff0000) >> 16
47+
rmin = min(r, rmin)
48+
rmax = max(r, rmax)
49+
gmin = min(g, rmin)
50+
gmax = max(g, rmax)
51+
bmin = min(b, rmin)
52+
bmax = max(b, rmax)
53+
}
54+
}
55+
56+
NSGraphicsContext.current = nil
57+
58+
let isTemplate = (abs(Int(rmax)-Int(rmin)) + abs(Int(gmax)-Int(gmin)) + abs(Int(bmax)-Int(bmin))) <= 9
59+
60+
return isTemplate
61+
}
62+
}

0 commit comments

Comments
 (0)