Skip to content

Commit 3e4474a

Browse files
committed
Preference menu item for idempotent activation
The default behavior: If an app is not in the foreground then activating it brings it to the fore. Activating it again sends it to the background. This change introduces a preference menu item that keeps the above deafult behavior and allows repeated activations to keep the application in the foreground. The alternate behavior is the default of some other, similar apps, like the obsolete 'Apptivate'.
1 parent 9f59d45 commit 3e4474a

5 files changed

Lines changed: 34 additions & 3 deletions

File tree

Thor/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3333
DefaultsKeys.DelayInterval.key: 0.3,
3434
DefaultsKeys.EnableShortcut.key: true,
3535
DefaultsKeys.enableMenuBarIcon.key: true,
36-
DefaultsKeys.enableMenuBarIconShowHideKey.key: true
36+
DefaultsKeys.enableMenuBarIconShowHideKey.key: true,
37+
DefaultsKeys.backgroundWhenActive.key: true
3738
])
3839

3940
NSApp.setActivationPolicy(.accessory)

Thor/Base.lproj/Main.storyboard

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,20 @@
380380
<action selector="toggleEnableShortcut:" target="LfM-H7-njo" id="wyA-Z5-TGh"/>
381381
</connections>
382382
</button>
383+
<button translatesAutoresizingMaskIntoConstraints="NO" id="new-bg-background-when-active-btn">
384+
<rect key="frame" x="45" y="351" width="280" height="18"/>
385+
<buttonCell key="cell" type="check" title="Repeated activation toggles app between foreground and background" bezelStyle="regularSquare" imagePosition="left" state="off" inset="2" id="new-bg-background-when-active-cell">
386+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
387+
<font key="font" size="13" name="STHeitiSC-Light"/>
388+
</buttonCell>
389+
<constraints>
390+
<constraint firstAttribute="width" constant="290" id="new-bg-background-when-active-width"/>
391+
<constraint firstAttribute="height" constant="28" id="new-bg-background-when-active-height"/>
392+
</constraints>
393+
<connections>
394+
<action selector="toggleBackgroundWhenActive:" target="LfM-H7-njo" id="new-bg-background-when-active-action"/>
395+
</connections>
396+
</button>
383397
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="prA-Il-0qM">
384398
<rect key="frame" x="83" y="13" width="200" height="32"/>
385399
<buttonCell key="cell" type="push" title="Quit Thor" bezelStyle="rounded" alignment="center" borderStyle="border" inset="2" id="2nX-9N-OOj">
@@ -496,7 +510,10 @@
496510
<constraint firstItem="hWC-9z-cv3" firstAttribute="top" secondItem="ySw-Wg-Pgf" secondAttribute="bottom" constant="1" id="R7Z-q7-uy1"/>
497511
<constraint firstItem="XJy-eX-prE" firstAttribute="leading" secondItem="Crp-4s-mEo" secondAttribute="leading" constant="47" id="TPK-B1-dpv"/>
498512
<constraint firstItem="Lcx-bw-woE" firstAttribute="top" secondItem="d3U-w9-jNg" secondAttribute="bottom" constant="35" id="YWI-qf-QpZ"/>
499-
<constraint firstItem="d3U-w9-jNg" firstAttribute="top" secondItem="7sy-3X-TvI" secondAttribute="bottom" constant="35" id="Yns-4x-PP5"/>
513+
<constraint firstItem="d3U-w9-jNg" firstAttribute="top" secondItem="new-bg-background-when-active-btn" secondAttribute="bottom" constant="35" id="Yns-4x-PP5"/>
514+
<constraint firstItem="new-bg-background-when-active-btn" firstAttribute="top" secondItem="7sy-3X-TvI" secondAttribute="bottom" constant="35" id="new-bg-background-when-active-top"/>
515+
<constraint firstItem="new-bg-background-when-active-btn" firstAttribute="leading" secondItem="7sy-3X-TvI" secondAttribute="leading" id="new-bg-background-when-active-leading"/>
516+
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="new-bg-background-when-active-btn" secondAttribute="trailing" constant="20" symbolic="YES" id="new-bg-background-when-active-trailing"/>
500517
<constraint firstItem="7sy-3X-TvI" firstAttribute="leading" secondItem="ych-hK-1qT" secondAttribute="leading" id="dgY-s3-y2Y"/>
501518
<constraint firstItem="ych-hK-1qT" firstAttribute="top" secondItem="Lcx-bw-woE" secondAttribute="bottom" constant="35" id="dt7-ok-vmd"/>
502519
<constraint firstItem="cIc-ga-45L" firstAttribute="top" secondItem="ych-hK-1qT" secondAttribute="bottom" constant="20" id="e9X-KM-xgc"/>
@@ -519,6 +536,7 @@
519536
<outlet property="btnEnableMenuBarIconShowHideKey" destination="Lcx-bw-woE" id="ZxA-zv-c8A"/>
520537
<outlet property="btnEnableShortcut" destination="7sy-3X-TvI" id="qGM-kS-fk6"/>
521538
<outlet property="btnLaunchAtLogin" destination="XJy-eX-prE" id="xKD-J0-ALg"/>
539+
<outlet property="btnBackgroundWhenActive" destination="new-bg-background-when-active-btn" id="new-bg-background-when-active-outlet"/>
522540
<outlet property="btnShortcutDeactivateKey" destination="0zk-yi-rDJ" id="OzF-KE-TX0"/>
523541
<outlet property="slider" destination="9mT-en-uZb" id="ZmK-N7-ClV"/>
524542
</connections>

Thor/DefaultsKeys+Extension.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ extension DefaultsKeys {
1919
static let LaunchAtLoginKey = DefaultsKey<Bool>("LaunchAtLoginKey")
2020
static let enableMenuBarIcon = DefaultsKey<Bool>("enableMenuBarIcon")
2121
static let enableMenuBarIconShowHideKey = DefaultsKey<Bool>("enableMenuBarIconShowHideKey")
22+
static let backgroundWhenActive = DefaultsKey<Bool>("backgroundWhenActive")
2223

2324
}

Thor/SettingsViewController.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SettingsViewController: NSViewController {
1313

1414
@IBOutlet weak var btnLaunchAtLogin: NSButton!
1515
@IBOutlet weak var btnEnableShortcut: NSButton!
16+
@IBOutlet weak var btnBackgroundWhenActive: NSButton!
1617
@IBOutlet weak var btnEnableMenuBarIcon: NSButton!
1718
@IBOutlet weak var btnEnableMenuBarIconShowHideKey: NSButton!
1819
@IBOutlet weak var btnEnableDeactivateKey: NSButton!
@@ -28,6 +29,8 @@ class SettingsViewController: NSViewController {
2829

2930
btnEnableShortcut.state = defaults[.EnableShortcut] ? .on : .off
3031

32+
btnBackgroundWhenActive.state = defaults[.backgroundWhenActive] ? .on : .off
33+
3134
btnEnableMenuBarIcon.state = defaults[.enableMenuBarIcon] ? .on : .off
3235
btnEnableMenuBarIconShowHideKey.state = defaults[.enableMenuBarIconShowHideKey] ? .on : .off
3336

@@ -63,6 +66,11 @@ class SettingsViewController: NSViewController {
6366
}
6467
}
6568

69+
@IBAction func toggleBackgroundWhenActive(_ sender: Any) {
70+
let enable = btnBackgroundWhenActive.state == .on
71+
defaults[.backgroundWhenActive] = enable
72+
}
73+
6674
@IBAction func toggleEnableMenuBarIcon(_ sender: Any) {
6775
let enable = btnEnableMenuBarIcon.state == .on
6876

Thor/ShortcutMonitor.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ struct ShortcutMonitor {
2121
if let frontmostAppIdentifier = NSWorkspace.shared.frontmostApplication?.bundleIdentifier,
2222
let targetAppIdentifier = Bundle(url: app.appBundleURL)?.bundleIdentifier,
2323
frontmostAppIdentifier == targetAppIdentifier {
24-
NSRunningApplication.runningApplications(withBundleIdentifier: frontmostAppIdentifier).first?.hide()
24+
// Only hide if backgroundWhenActive is enabled
25+
if defaults[.backgroundWhenActive] {
26+
NSRunningApplication.runningApplications(withBundleIdentifier: frontmostAppIdentifier).first?.hide()
27+
}
2528
} else {
2629
if #available(macOS 10.15, *) {
2730
let configuration = NSWorkspace.OpenConfiguration()

0 commit comments

Comments
 (0)