Skip to content

Commit

Permalink
Set default brew location based on platform.
Browse files Browse the repository at this point in the history
* If a brew location is not set, Brewlet will select the default location based on the current running platform.
* Refactor preferences to have radio buttons for Apple Silicon, Intel, and custom locations
  • Loading branch information
zachberger committed Nov 29, 2020
1 parent 88182f1 commit 2c85f17
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 30 deletions.
55 changes: 52 additions & 3 deletions Brewlet/PreferencesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ class PreferencesController: NSWindowController {
@IBOutlet weak var autoUpgrade: NSButton!
@IBOutlet weak var dontNotifyAvailable: NSButton!
@IBOutlet weak var brewPath: NSTextField!

@IBOutlet weak var intel: NSButton!
@IBOutlet weak var appleSilicon: NSButton!
@IBOutlet weak var custom: NSButton!

var delegate: PreferencesDelegate?

enum HomebrewPath: String {
case appleSilicon = "/opt/homebrew/bin/brew"
case intel = "/usr/local/bin/brew"
case custom = ""
}

override func windowDidLoad() {
super.windowDidLoad()

Expand Down Expand Up @@ -60,8 +69,20 @@ class PreferencesController: NSWindowController {
autoUpgrade.state = defaults.bool(forKey: "autoUpgrade") ? .on : .off
dontNotifyAvailable.state = defaults.bool(forKey: "dontNotify") ? .on : .off

let currentBrewPath = defaults.string(forKey: "brewPath") ?? "/usr/local/bin/brew"
brewPath.stringValue = currentBrewPath
#if arch(arm64)
let currentBrewPath = defaults.string(forKey: "brewPath") ?? HomebrewPath.appleSilicon.rawValue
#elseif arch(x86_64)
let currentBrewPath = defaults.string(forKey: "brewPath") ?? HomebrewPath.intel.rawValue
#endif
switch currentBrewPath {
case HomebrewPath.intel.rawValue:
intelSelected(nil)
case HomebrewPath.appleSilicon.rawValue:
appleSiliconSelected(nil)
default:
custom.state = .on
brewPath.stringValue = currentBrewPath
}
}

@IBAction func includeDependenciesPressed(_ sender: NSButton) {
Expand Down Expand Up @@ -96,6 +117,34 @@ class PreferencesController: NSWindowController {
delegate?.updateIntervalChanged(newInterval: seconds)
}

@IBAction func appleSiliconSelected(_ sender: Any?) {
appleSilicon.state = .on
intel.state = .off
custom.state = .off
brewPath.isEnabled = false
let path = HomebrewPath.appleSilicon.rawValue
brewPath.stringValue = path
delegate?.brewPathChanged(newPath: path)
}

@IBAction func intelSelected(_ sender: Any?) {
intel.state = .on
appleSilicon.state = .off
custom.state = .off
brewPath.isEnabled = false
let path = HomebrewPath.intel.rawValue
brewPath.stringValue = path
delegate?.brewPathChanged(newPath: path)
}

@IBAction func customSelected(_ sender: Any) {
custom.state = .on
appleSilicon.state = .off
intel.state = .off
brewPath.isEnabled = true
delegate?.brewPathChanged(newPath: brewPath.stringValue)
}

@IBAction func brewPathChanged(_ sender: NSTextField) {
// TODO: Validate that the path is valid
delegate?.brewPathChanged(newPath: sender.stringValue)
Expand Down
Loading

0 comments on commit 2c85f17

Please sign in to comment.