-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repo // Add a Settings window to help users change keyLayouts.
- Loading branch information
Showing
8 changed files
with
172 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
import Foundation | ||
|
||
@objc public extension SquirrelInputController { static var basicKeyboardLayoutNameVerified: String { | ||
let result = Properties.shared.basicKeyboardLayout | ||
return (TISInputSource.generate(from: result) != nil) ? result : "com.apple.keylayout.ABC" | ||
} | ||
public var sessionControllers: Set<SquirrelInputController> = .init() | ||
|
||
func overrideKeyboard() { | ||
DispatchQueue.main.async { [self] in | ||
self.client()?.overrideKeyboard(withKeyboardNamed: Self.basicKeyboardLayoutNameVerified) | ||
@objc public extension SquirrelInputController { | ||
static var isBasicKeyboardLayoutDefinedValidInPlist: Bool { | ||
TISInputSource.generate(from: Properties.shared.basicKeyboardLayout) != nil | ||
} | ||
|
||
static var basicKeyboardLayoutNameVerified: String? { | ||
let result = Properties.shared.basicKeyboardLayout | ||
return (TISInputSource.generate(from: result) != nil) ? result : nil | ||
} | ||
|
||
func overrideKeyboard() { | ||
DispatchQueue.main.async { [self] in | ||
if let verified = Self.basicKeyboardLayoutNameVerified { | ||
client()?.overrideKeyboard(withKeyboardNamed: verified) | ||
} | ||
} | ||
} | ||
|
||
func registerSessionControllerIntoSet() { | ||
sessionControllers.insert(self) | ||
} | ||
|
||
func removeSessionControllerFromSet() { | ||
sessionControllers.remove(self) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Cocoa | ||
|
||
@objc class SquirrelSettingsWindow: NSWindowController { | ||
@IBOutlet var basicKeyboardLayoutButton: NSPopUpButton! | ||
@IBOutlet var theWindow: NSWindow! | ||
|
||
@objc static var shared: SquirrelSettingsWindow? | ||
|
||
override func windowDidLoad() { | ||
super.windowDidLoad() | ||
window = theWindow | ||
initiateKeyLayoutDropdownButton() | ||
} | ||
|
||
@objc static func show() { | ||
if shared == nil { | ||
shared = .init(windowNibName: "SquirrelSettingsWindow") | ||
} | ||
guard let shared = shared else { return } | ||
shared.window?.center() | ||
shared.window?.orderFrontRegardless() // 逼著屬性視窗往最前方顯示 | ||
shared.window?.level = .statusBar | ||
if #available(macOS 10.10, *) { | ||
shared.window?.titlebarAppearsTransparent = true | ||
} | ||
NSApp.setActivationPolicy(.accessory) | ||
NSApp.activate(ignoringOtherApps: true) | ||
} | ||
} | ||
|
||
extension SquirrelSettingsWindow { | ||
func initiateKeyLayoutDropdownButton() { | ||
var usKeyboardLayoutItem: NSMenuItem? | ||
var chosenBaseKeyboardLayoutItem: NSMenuItem? | ||
basicKeyboardLayoutButton.menu?.removeAllItems() | ||
let basicKeyboardLayoutID = Properties.shared.basicKeyboardLayout | ||
|
||
for source in IMKHelper.allowedBasicLayoutsAsTISInputSources { | ||
guard let source = source else { | ||
basicKeyboardLayoutButton.menu?.addItem(NSMenuItem.separator()) | ||
continue | ||
} | ||
let menuItem = NSMenuItem() | ||
menuItem.title = source.vChewingLocalizedName | ||
menuItem.representedObject = source.identifier | ||
if source.identifier == "com.apple.keylayout.US" { usKeyboardLayoutItem = menuItem } | ||
if basicKeyboardLayoutID == source.identifier { chosenBaseKeyboardLayoutItem = menuItem } | ||
basicKeyboardLayoutButton.menu?.addItem(menuItem) | ||
} | ||
|
||
basicKeyboardLayoutButton.select(chosenBaseKeyboardLayoutItem ?? usKeyboardLayoutItem) | ||
} | ||
|
||
@IBAction func updateBasicKeyboardLayoutAction(_: Any) { | ||
if let sourceID = basicKeyboardLayoutButton.selectedItem?.representedObject as? String { | ||
Properties.shared.basicKeyboardLayout = sourceID | ||
sessionControllers.forEach { | ||
$0.overrideKeyboard() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21225" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct"> | ||
<dependencies> | ||
<deployment identifier="macosx"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21225"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<objects> | ||
<customObject id="-2" userLabel="File's Owner" customClass="SquirrelSettingsWindow" customModule="Squirrel" customModuleProvider="target"> | ||
<connections> | ||
<outlet property="basicKeyboardLayoutButton" destination="SAs-ky-Hj4" id="Cm4-11-q9O"/> | ||
<outlet property="theWindow" destination="F0z-JX-Cv5" id="Y9w-af-H2Y"/> | ||
</connections> | ||
</customObject> | ||
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/> | ||
<customObject id="-3" userLabel="Application" customClass="NSObject"/> | ||
<window title="Squirrel Settings" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" titlebarAppearsTransparent="YES" id="F0z-JX-Cv5" userLabel="SquirrelSettingsWindow"> | ||
<windowStyleMask key="styleMask" titled="YES" closable="YES" fullSizeContentView="YES"/> | ||
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/> | ||
<rect key="contentRect" x="196" y="240" width="381" height="82"/> | ||
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="875"/> | ||
<view key="contentView" id="se5-gp-TjO"> | ||
<rect key="frame" x="0.0" y="0.0" width="381" height="82"/> | ||
<autoresizingMask key="autoresizingMask"/> | ||
<subviews> | ||
<stackView distribution="fill" orientation="horizontal" alignment="top" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" fixedFrame="YES" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="icc-tr-Rcw"> | ||
<rect key="frame" x="20" y="20" width="330" height="20"/> | ||
<subviews> | ||
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="1E1-vm-e4q"> | ||
<rect key="frame" x="-2" y="4" width="71" height="16"/> | ||
<textFieldCell key="cell" lineBreakMode="clipping" title="KeyLayout:" id="5TS-Z1-9jq"> | ||
<font key="font" metaFont="system"/> | ||
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/> | ||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/> | ||
</textFieldCell> | ||
</textField> | ||
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="SAs-ky-Hj4"> | ||
<rect key="frame" x="72" y="-4" width="262" height="25"/> | ||
<constraints> | ||
<constraint firstAttribute="width" constant="255" id="rSG-jt-qHe"/> | ||
</constraints> | ||
<popUpButtonCell key="cell" type="push" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" id="EYT-Eu-Ij3"> | ||
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/> | ||
<font key="font" metaFont="menu"/> | ||
<menu key="menu" id="EN3-AD-X99"/> | ||
</popUpButtonCell> | ||
<connections> | ||
<action selector="updateBasicKeyboardLayoutAction:" target="-2" id="omK-oP-bLn"/> | ||
</connections> | ||
</popUpButton> | ||
</subviews> | ||
<visibilityPriorities> | ||
<integer value="1000"/> | ||
<integer value="1000"/> | ||
</visibilityPriorities> | ||
<customSpacing> | ||
<real value="3.4028234663852886e+38"/> | ||
<real value="3.4028234663852886e+38"/> | ||
</customSpacing> | ||
</stackView> | ||
</subviews> | ||
</view> | ||
<point key="canvasLocation" x="-1.5" y="16"/> | ||
</window> | ||
</objects> | ||
</document> |