Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
31 changes: 31 additions & 0 deletions LangSwitch/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Carbon
import Foundation
import AppKit
import IOKit.hid
import ServiceManagement


class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -18,6 +19,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let longPressThreshold: TimeInterval = 0.2;

func applicationDidFinishLaunching(_ notification: Notification) {
// launch at login
launchAtLogin()

// Create a status bar item with a system icon
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
statusBarItem?.button?.image = NSImage(systemSymbolName: "globe", accessibilityDescription: nil)
Expand All @@ -30,6 +34,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(withTitle: "Exit", action: #selector(exitAction), keyEquivalent: "")
statusBarItem?.menu = menu

// hide menu bar if the button was pressed once
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "hideStatusBarIcon") {
statusBarItem?.isVisible = false
}

NSApp.setActivationPolicy(.accessory)
NSApp.hide(nil)

Expand Down Expand Up @@ -93,6 +103,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
aboutWindow?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}

@objc func launchAtLogin() {
if #available(macOS 13.0, *) {
do {
if SMAppService.mainApp.status == .enabled {
// do nothing
print("Login item already registered.")
} else {
try SMAppService.mainApp.register()
}
} catch {
print("Failed to enable login item: \(error)")
}
} else {
// Fallback on earlier versions
print("Login item functionality is not available on this version of macOS.")
}
}


@objc func openGitHub() {
Expand Down Expand Up @@ -138,6 +166,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@objc func hideStatusBarIcon() {
statusBarItem?.isVisible = false
let userDefaults = UserDefaults.standard
userDefaults.set(true, forKey: "hideStatusBarIcon")
UserDefaults.standard.synchronize()
}

@objc func exitAction() {
Expand Down