Skip to content

Commit

Permalink
Merge pull request #9 from humblehacker/master
Browse files Browse the repository at this point in the history
Deliver notification on input source change
  • Loading branch information
Hyunje Jun authored Sep 14, 2017
2 parents f761414 + f409744 commit 67dae03
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "shpakovski/MASShortcut" "2.1.2"
github "shpakovski/MASShortcut" "2.3.6"
6 changes: 3 additions & 3 deletions kawa/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
var preferenceWindowController: PreferenceWindowController!
var justLaunched: Bool = true
var launchedForTheFirstTime: Bool = Settings.get(Settings.launchedForTheFirstTime, withDefaultValue: true)
var launchedForTheFirstTime: Bool = Settings.get(.launchedForTheFirstTime, withDefaultValue: true)

func applicationDidFinishLaunching(_ aNotification: Notification) {
InputSourceManager.initialize()
preferenceWindowController = instantiatePreferenceWindowController()
StatusBar.initWithPreferenceWindowController(preferenceWindowController)
LaunchOnStartup.setLaunchAtStartup(Settings.get(Settings.launchOnStartup, withDefaultValue: true))
LaunchOnStartup.setLaunchAtStartup(Settings.get(.launchOnStartup, withDefaultValue: true))

if launchedForTheFirstTime {
Settings.set(Settings.launchedForTheFirstTime, toValue: false)
Settings.set(.launchedForTheFirstTime, toValue: false)
}
}

Expand Down
27 changes: 20 additions & 7 deletions kawa/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="13122.17" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11201"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13122.17"/>
<capability name="box content view" minToolsVersion="7.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down Expand Up @@ -138,22 +139,22 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<box autoresizesSubviews="NO" fixedFrame="YES" borderType="line" title="General Stuff" translatesAutoresizingMaskIntoConstraints="NO" id="Krr-XJ-miL">
<rect key="frame" x="17" y="166" width="416" height="113"/>
<rect key="frame" x="17" y="138" width="416" height="141"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" id="fmh-So-mUE">
<rect key="frame" x="1" y="1" width="414" height="97"/>
<rect key="frame" x="1" y="1" width="414" height="125"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="JXT-vT-3c2">
<rect key="frame" x="16" y="71" width="382" height="18"/>
<rect key="frame" x="16" y="99" width="382" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show Menubar Icon" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="0Xx-Sj-It1">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" setsMaxLayoutWidthAtFirstLayout="YES" translatesAutoresizingMaskIntoConstraints="NO" id="zub-x4-8eR">
<rect key="frame" x="35" y="36" width="363" height="34"/>
<rect key="frame" x="35" y="64" width="363" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="If you hide the icon you can access the preferences by starting Kawa again" id="ZP5-V3-u7u">
<font key="font" metaFont="systemBold"/>
Expand All @@ -162,13 +163,24 @@
</textFieldCell>
</textField>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="MzF-xa-7TO">
<rect key="frame" x="16" y="12" width="382" height="18"/>
<rect key="frame" x="16" y="40" width="382" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Launch Kawa on startup" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="Hpx-WQ-b8B">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ix0-UJ-v3V">
<rect key="frame" x="16" y="20" width="382" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show notification on input source change" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="qfa-bZ-zps">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="showNotification:" target="8tD-vA-uhk" id="mcp-CO-SfQ"/>
</connections>
</button>
</subviews>
</view>
</box>
Expand Down Expand Up @@ -196,6 +208,7 @@
<outlet property="projectPageLink" destination="yD8-rS-xQC" id="lQZ-K9-RD1"/>
<outlet property="quitAppButton" destination="13u-Dv-ipW" id="EEK-Sd-doA"/>
<outlet property="showMenubarIconCheckbox" destination="JXT-vT-3c2" id="972-vw-teN"/>
<outlet property="showNotificationCheckbox" destination="Ix0-UJ-v3V" id="HxT-bb-QrP"/>
</connections>
</viewController>
<customObject id="YAB-W8-ond" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
Expand Down
17 changes: 10 additions & 7 deletions kawa/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import Cocoa
class Settings {
static let defaults = UserDefaults.standard

static let showMenubarIcon = "show-menubar-icon"
static let launchOnStartup = "launch-on-startup"
static let launchedForTheFirstTime = "launched-for-the-first-time"
enum Setting: String {
case showMenubarIcon = "show-menubar-icon"
case launchOnStartup = "launch-on-startup"
case showNotification = "show-notification"
case launchedForTheFirstTime = "launched-for-the-first-time"
}

static func get<T>(_ key: String, withDefaultValue: T) -> T {
let val: T? = defaults.object(forKey: key) as? T
static func get<T>(_ key: Setting, withDefaultValue: T) -> T {
let val: T? = defaults.object(forKey: key.rawValue) as? T
if val != nil {
return val!
} else {
return withDefaultValue
}
}

static func set<T>(_ key: String, toValue: T) {
defaults.set((toValue as AnyObject), forKey: key)
static func set<T>(_ key: Setting, toValue: T) {
defaults.set((toValue as AnyObject), forKey: key.rawValue)
defaults.synchronize()
}
}
12 changes: 11 additions & 1 deletion kawa/ShortcutCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class ShortcutCellView: NSTableCellView {
}

func selectInput() {
inputSource?.select()
guard let inputSource = inputSource else { return }
inputSource.select()
showNotification(inputSource.name, icon: inputSource.icon)
}

func showNotification(_ message: String, icon: NSImage?) {
guard Settings.get(.showNotification, withDefaultValue: true) else { return }
let notification = NSUserNotification()
notification.informativeText = message
notification.contentImage = icon
NSUserNotificationCenter.default.deliver(notification)
}
}
2 changes: 1 addition & 1 deletion kawa/StatusBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StatusBar: NSObject {
static func initWithPreferenceWindowController(_ preferenceWindowController: PreferenceWindowController) {
self.preferenceWindowController = preferenceWindowController

if Settings.get(Settings.showMenubarIcon, withDefaultValue: true) {
if Settings.get(.showMenubarIcon, withDefaultValue: true) {
createStatusBarItem()
}
}
Expand Down
16 changes: 12 additions & 4 deletions kawa/SystemSettingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Cocoa
class SystemSettingViewController: NSViewController {
@IBOutlet var showMenubarIconCheckbox: NSButton!
@IBOutlet var launchOnStartupCheckbox: NSButton!
@IBOutlet weak var showNotificationCheckbox: NSButton!
@IBOutlet var quitAppButton: NSButton!
@IBOutlet var projectPageLink: HyperlinkTextField!

Expand All @@ -22,14 +23,17 @@ class SystemSettingViewController: NSViewController {

showMenubarIconCheckbox.target = self
showMenubarIconCheckbox.action = #selector(SystemSettingViewController.setShowMenubarIcon(_:))
var isOn: Bool = Settings.get(Settings.showMenubarIcon, withDefaultValue: true)
var isOn: Bool = Settings.get(.showMenubarIcon, withDefaultValue: true)
showMenubarIconCheckbox.state = isOn ? NSOnState : NSOffState

launchOnStartupCheckbox.target = self
launchOnStartupCheckbox.action = #selector(SystemSettingViewController.setLaunchOnStartup(_:))
isOn = Settings.get(Settings.launchOnStartup, withDefaultValue: true)
isOn = Settings.get(.launchOnStartup, withDefaultValue: true)
launchOnStartupCheckbox.state = isOn ? NSOnState : NSOffState

isOn = Settings.get(.showNotification, withDefaultValue: true)
showNotificationCheckbox.state = isOn ? NSOnState : NSOffState

let urlString = projectPageLink.stringValue
let url = URL(string: "https://" + urlString)
projectPageLink.setURL(url!)
Expand All @@ -41,7 +45,7 @@ class SystemSettingViewController: NSViewController {

func setShowMenubarIcon(_ sender: AnyObject) {
let isOn: Bool = showMenubarIconCheckbox.state == NSOnState
Settings.set(Settings.showMenubarIcon, toValue: isOn)
Settings.set(.showMenubarIcon, toValue: isOn)

if isOn {
StatusBar.createStatusBarItem()
Expand All @@ -52,7 +56,11 @@ class SystemSettingViewController: NSViewController {

func setLaunchOnStartup(_ sender: AnyObject) {
let isOn: Bool = launchOnStartupCheckbox.state == NSOnState
Settings.set(Settings.launchOnStartup, toValue: isOn)
Settings.set(.launchOnStartup, toValue: isOn)
LaunchOnStartup.setLaunchAtStartup(isOn)
}

@IBAction func showNotification(_ sender: NSButton) {
Settings.set(.showNotification, toValue: sender.state == NSOnState)
}
}

0 comments on commit 67dae03

Please sign in to comment.