Skip to content

Commit

Permalink
run swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ShlomoCode committed Aug 11, 2024
1 parent 6dafb65 commit 148e81f
Show file tree
Hide file tree
Showing 35 changed files with 652 additions and 659 deletions.
85 changes: 43 additions & 42 deletions DockDoor/logic/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
//

import Cocoa
import SwiftUI
import Defaults
import Settings
import Sparkle
import SwiftUI

class SettingsWindowControllerDelegate: NSObject, NSWindowDelegate {
func windowDidBecomeKey(_ notification: Notification) {
func windowDidBecomeKey(_: Notification) {
NSApp.setActivationPolicy(.regular) // Show dock icon on open settings window
}
func windowWillClose(_ notification: Notification) {

func windowWillClose(_: Notification) {
NSApp.setActivationPolicy(.accessory) // Hide dock icon back
}
}
Expand All @@ -28,9 +28,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
private var appClosureObserver: AppClosureObserver?
private var keybindHelper: KeybindHelper?
private var statusBarItem: NSStatusItem?

private var updaterController: SPUStandardUpdaterController

// settings
private var firstTimeWindow: NSWindow?
private lazy var settingsWindowController = SettingsWindowController(
Expand All @@ -39,32 +39,32 @@ class AppDelegate: NSObject, NSApplicationDelegate {
AppearanceSettingsViewController(),
WindowSwitcherSettingsViewController(),
PermissionsSettingsViewController(),
UpdatesSettingsViewController(updater: updaterController.updater)
UpdatesSettingsViewController(updater: updaterController.updater),
]
)
private let settingsWindowControllerDelegate = SettingsWindowControllerDelegate()

override init() {
self.updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
self.updaterController.startUpdater()
updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
updaterController.startUpdater()
super.init()

if let zoomButton = settingsWindowController.window?.standardWindowButton(.zoomButton) {
zoomButton.isEnabled = false
}

settingsWindowController.window?.delegate = settingsWindowControllerDelegate
}
func applicationDidFinishLaunching(_ aNotification: Notification) {

func applicationDidFinishLaunching(_: Notification) {
NSApplication.shared.setActivationPolicy(.accessory) // Hide the menubar and dock icons

if Defaults[.showMenuBarIcon] {
self.setupMenuBar()
setupMenuBar()
} else {
self.removeMenuBar()
removeMenuBar()
}

if !Defaults[.launched] {
handleFirstTimeLaunch()
} else {
Expand All @@ -75,12 +75,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
self.openSettingsWindow(nil)

func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows _: Bool) -> Bool {
openSettingsWindow(nil)
return false
}

func setupMenuBar() {
guard statusBarItem == nil else { return }
let icon = NSImage(systemSymbolName: "door.right.hand.open", accessibilityDescription: nil)!
Expand All @@ -89,80 +89,81 @@ class AppDelegate: NSObject, NSApplicationDelegate {
button.image = icon
button.action = #selector(statusBarButtonClicked(_:))
button.target = self

// Create Menu Items
let openSettingsMenuItem = NSMenuItem(title: String(localized: "Open Settings"), action: #selector(openSettingsWindow(_:)), keyEquivalent: "")
openSettingsMenuItem.target = self
let quitMenuItem = NSMenuItem(title: String(localized: "Quit DockDoor"), action: #selector(quitAppWrapper), keyEquivalent: "q")
quitMenuItem.target = self

// Create the Menu
let menu = NSMenu()
menu.addItem(openSettingsMenuItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(quitMenuItem)

button.menu = menu
}
}

func removeMenuBar() {
guard let statusBarItem = statusBarItem else { return }
NSStatusBar.system.removeStatusItem(statusBarItem)
self.statusBarItem = nil
}
@objc func statusBarButtonClicked(_ sender: Any?) {

@objc func statusBarButtonClicked(_: Any?) {
// Show the menu
if let button = statusBarItem?.button {
button.menu?.popUp(positioning: nil, at: NSPoint(x: 0, y: button.bounds.maxY), in: button)
}
}

@objc private func quitAppWrapper() {
quitApp()
}
@objc func openSettingsWindow(_ sender: Any?) {

@objc func openSettingsWindow(_: Any?) {
settingsWindowController.show()
}

func quitApp() {
NSApplication.shared.terminate(nil)
}

func restartApp() {
// we use -n to open a new instance, to avoid calling applicationShouldHandleReopen
// we use Bundle.main.bundlePath in case of multiple DockDoor versions on the machine
Process.launchedProcess(launchPath: "/usr/bin/open", arguments: ["-n", Bundle.main.bundlePath])
self.quitApp()
quitApp()
}

private func handleFirstTimeLaunch() {
let contentView = FirstTimeView()

// Save that the app has launched
Defaults[.launched] = true

// Create a hosting controller
let hostingController = NSHostingController(rootView: contentView)

// Create the settings window
firstTimeWindow = NSWindow(
contentRect: NSRect(origin: .zero, size: NSSize(width: 400, height: 400)),
styleMask: [.titled, .closable, .resizable],
backing: .buffered, defer: false)
backing: .buffered, defer: false
)
firstTimeWindow?.center()
firstTimeWindow?.setFrameAutosaveName("DockDoor Permissions")
firstTimeWindow?.contentView = hostingController.view
firstTimeWindow?.title = "DockDoor Permissions"

// Make the window key and order it front
firstTimeWindow?.makeKeyAndOrderFront(nil)

// Calculate the preferred size of the SwiftUI view
let preferredSize = hostingController.view.fittingSize

// Resize the window to fit the content view
firstTimeWindow?.setContentSize(preferredSize)
}
Expand Down
Loading

0 comments on commit 148e81f

Please sign in to comment.