Skip to content

Commit

Permalink
#78 Ability to hide menubar item
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Dec 29, 2023
1 parent 91e5c13 commit bc34267
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
45 changes: 45 additions & 0 deletions Loop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {

private let loopManager = LoopManager()
private let windowDragManager = WindowDragManager()
private var canOpenSettings = false

private var launchedAsLoginItem: Bool {
guard let event = NSAppleEventManager.shared().currentAppleEvent else { return false }
return
event.eventID == kAEOpenApplication &&
event.paramDescriptor(forKeyword: keyAEPropData)?.enumCodeValue == keyAELaunchedAsLogInItem
}

func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.setActivationPolicy(.accessory)
Expand All @@ -22,6 +30,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
IconManager.refreshCurrentAppIcon()
loopManager.startObservingKeys()
windowDragManager.addObservers()

if self.launchedAsLoginItem {
// Ensures Loop's settings window isn't opened upon login
// (cause MANY system processes are already currently initializing at this point)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.canOpenSettings = true
}
} else {
self.canOpenSettings = true
}
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
Expand All @@ -31,4 +49,31 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
return false
}

func applicationWillBecomeActive(_ notification: Notification) {
guard canOpenSettings else { return }
self.openSettings()
}

// Mostly taken from https://github.com/Wouter01/SwiftUI-WindowManagement
func openSettings() {
for window in NSApp.windows where window.toolbar?.items != nil {
window.close()
}

let eventSource = CGEventSource(stateID: .hidSystemState)
let keyCommand = CGEvent(keyboardEventSource: eventSource, virtualKey: 0x2B, keyDown: true)
guard let keyCommand else { return }

keyCommand.flags = .maskCommand
let event = NSEvent(cgEvent: keyCommand)
guard let event else { return }

NSApp.sendEvent(event)

for window in NSApp.windows where window.toolbar?.items != nil {
window.orderFrontRegardless()
window.center()
}
}
}
1 change: 1 addition & 0 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Defaults
// Add variables for default values (which are stored even then the app is closed)
extension Defaults.Keys {
static let launchAtLogin = Key<Bool>("launchAtLogin", default: false)
static let hideMenuBarIcon = Key<Bool>("hideMenuBarIcon", default: false)
static let currentIcon = Key<String>("currentIcon", default: "AppIcon-Classic")
static let timesLooped = Key<Int>("timesLooped", default: 0)
static let windowSnapping = Key<Bool>("windowSnapping", default: false) // BETA
Expand Down
11 changes: 9 additions & 2 deletions Loop/LoopApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
import SwiftUI
import MenuBarExtraAccess
import SettingsAccess
import Defaults

@main
struct LoopApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let aboutViewController = AboutViewController()
@State var isMenubarItemPresented: Bool = false
@Default(.hideMenuBarIcon) var hideMenuBarIcon

var body: some Scene {
Settings {
SettingsView()
}

MenuBarExtra("Loop", image: "empty") {
MenuBarExtra("Loop", image: "empty", isInserted: Binding.constant(!hideMenuBarIcon)) {
#if DEBUG
MenuBarHeaderText("DEV BUILD: \(Bundle.main.appVersion) (\(Bundle.main.appBuild))")
#endif
Expand Down Expand Up @@ -79,7 +81,12 @@ struct LoopApp: App {
}
.menuBarExtraStyle(.menu)
.menuBarExtraAccess(isPresented: $isMenubarItemPresented) { statusItem in
guard let button = statusItem.button else { return }
guard
let button = statusItem.button,
button.subviews.count == 0
else {
return
}

let view = NSHostingView(rootView: MenuBarIconView())
view.frame.size = NSSize(width: 26, height: 22)
Expand Down
12 changes: 12 additions & 0 deletions Loop/Settings/GeneralSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct GeneralSettingsView: View {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

@Default(.launchAtLogin) var launchAtLogin
@Default(.hideMenuBarIcon) var hideMenuBarIcon
@Default(.useSystemAccentColor) var useSystemAccentColor
@Default(.customAccentColor) var customAccentColor
@Default(.useGradient) var useGradient
Expand All @@ -38,6 +39,17 @@ struct GeneralSettingsView: View {
try? SMAppService().unregister()
}
}

VStack(alignment: .leading) {
Toggle("Hide menubar icon", isOn: $hideMenuBarIcon)

if hideMenuBarIcon {
Text("Re-open Loop again to see this window.")
.font(.caption)
.foregroundColor(.secondary)
.textSelection(.enabled)
}
}
}

Section {
Expand Down

0 comments on commit bc34267

Please sign in to comment.