Skip to content

Commit

Permalink
Fix #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsuhane committed May 11, 2019
1 parent 350afa5 commit 2288c89
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
4 changes: 4 additions & 0 deletions SwitchKey.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
7115A91622631E6A00DD386A /* InputSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 7115A91522631E6A00DD386A /* InputSource.m */; };
716404EE226A0FFB00E0F0A1 /* LoginServiceKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 716404ED226A0FFB00E0F0A1 /* LoginServiceKit.swift */; };
717050D4226204DE008A2686 /* SwitchKey.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 717050D3226204DE008A2686 /* SwitchKey.xcassets */; };
718A6EDF2285333700DB6B05 /* NSImage+extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 718A6EDE2285333700DB6B05 /* NSImage+extensions.swift */; };
71FECF2E22610BE600C51FD7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FECF2D22610BE600C51FD7 /* AppDelegate.swift */; };
71FECF3322610BE600C51FD7 /* SwitchKey.xib in Resources */ = {isa = PBXBuildFile; fileRef = 71FECF3122610BE600C51FD7 /* SwitchKey.xib */; };
/* End PBXBuildFile section */
Expand All @@ -20,6 +21,7 @@
7115A91522631E6A00DD386A /* InputSource.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InputSource.m; sourceTree = "<group>"; };
716404ED226A0FFB00E0F0A1 /* LoginServiceKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginServiceKit.swift; sourceTree = "<group>"; };
717050D3226204DE008A2686 /* SwitchKey.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = SwitchKey.xcassets; sourceTree = "<group>"; };
718A6EDE2285333700DB6B05 /* NSImage+extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSImage+extensions.swift"; sourceTree = "<group>"; };
71FECF2A22610BE600C51FD7 /* SwitchKey.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwitchKey.app; sourceTree = BUILT_PRODUCTS_DIR; };
71FECF2D22610BE600C51FD7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
71FECF3222610BE600C51FD7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/SwitchKey.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -66,6 +68,7 @@
7115A91522631E6A00DD386A /* InputSource.m */,
7115A91322631E6A00DD386A /* SwitchKey-Bridging-Header.h */,
716404ED226A0FFB00E0F0A1 /* LoginServiceKit.swift */,
718A6EDE2285333700DB6B05 /* NSImage+extensions.swift */,
);
path = SwitchKey;
sourceTree = "<group>";
Expand Down Expand Up @@ -148,6 +151,7 @@
files = (
71FECF2E22610BE600C51FD7 /* AppDelegate.swift in Sources */,
716404EE226A0FFB00E0F0A1 /* LoginServiceKit.swift in Sources */,
718A6EDF2285333700DB6B05 /* NSImage+extensions.swift in Sources */,
7115A91622631E6A00DD386A /* InputSource.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
12 changes: 7 additions & 5 deletions SwitchKey/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// AppDelegate.swift
// AppDelegate
// SwitchKey
//
// Created by Jinyu Li on 2019/03/16.
// Copyright © 2019 Jinyu Li. All rights reserved.
Expand Down Expand Up @@ -178,8 +178,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab

let element = AXUIElementCreateApplication(pid)
let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
AXObserverAddNotification(observer, element, kAXApplicationActivatedNotification as CFString, selfPtr)

AXObserverAddNotification(observer, element, NSAccessibility.Notification.applicationActivated.rawValue as CFString, selfPtr)
applicationObservers[pid] = observer
}
}
Expand Down Expand Up @@ -273,8 +272,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab
let itemCell = conditionTableView.makeView(withIdentifier: itemCellIdentifier, owner: nil) as! ConditionCell
itemCell.appIcon.image = item.applicationIcon
itemCell.appName.stringValue = item.applicationName
itemCell.inputSourceButton.image = item.inputSourceIcon
itemCell.inputSourceButton.image?.isTemplate = true

let icon = item.inputSourceIcon
itemCell.inputSourceButton.image = icon
itemCell.inputSourceButton.image?.isTemplate = icon.canTemplate()

itemCell.conditionEnabled.state = item.enabled ? .on : .off
return itemCell
} else {
Expand Down
4 changes: 2 additions & 2 deletions SwitchKey/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.1.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>7</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
62 changes: 62 additions & 0 deletions SwitchKey/NSImage+extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// NSImage+extensions.swift
// SwitchKey
//
// Created by Jinyu Li on 2019/05/10.
// Copyright © 2019 Jinyu Li. All rights reserved.
//

import Cocoa

extension NSImage {
// A dumb method:
// go over the pixels, and check the color range.
// an image can be safely templated if its color range is limited.
// luckily, our image is small, and the check is done once for each new icon.
func canTemplate() -> Bool {
guard let cgContext = CGContext(
data: nil,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
) else {
return false
}

let nsContext = NSGraphicsContext(cgContext: cgContext, flipped: false)
NSGraphicsContext.current = nsContext
draw(in: NSMakeRect(0, 0, size.width, size.height))

guard let pixel = cgContext.data else {
return false;
}

let width = cgContext.width;
var rmin:UInt32 = 255, rmax:UInt32 = 0
var gmin:UInt32 = 255, gmax:UInt32 = 0
var bmin:UInt32 = 255, bmax:UInt32 = 0
for y in 0 ... cgContext.height {
for x in 0 ... width {
let px = pixel.load(fromByteOffset: (y * width + x) * 4, as: UInt32.self)
let r = (px & 0x000000ff) >> 0
let g = (px & 0x0000ff00) >> 8
let b = (px & 0x00ff0000) >> 16
rmin = min(r, rmin)
rmax = max(r, rmax)
gmin = min(g, rmin)
gmax = max(g, rmax)
bmin = min(b, rmin)
bmax = max(b, rmax)
}
}

NSGraphicsContext.current = nil

let isTemplate = (abs(Int(rmax)-Int(rmin)) + abs(Int(gmax)-Int(gmin)) + abs(Int(bmax)-Int(bmin))) <= 9

return isTemplate
}
}

0 comments on commit 2288c89

Please sign in to comment.