1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-09 13:10:24 +01:00

👌 Avoid using sender.tag

This commit is contained in:
2020-12-13 19:32:14 +01:00
parent e353fb7524
commit c7a0e25336
4 changed files with 27 additions and 9 deletions

View File

@@ -0,0 +1,15 @@
//
// PhpMenuItem.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 13/12/2020.
// Copyright © 2020 Nico Verbruggen. All rights reserved.
//
import Cocoa
class PhpMenuItem: NSMenuItem {
var version: String = ""
}

View File

@@ -36,8 +36,8 @@ class StatusMenu : NSMenu {
let version = App.shared.availablePhpVersions[index]
let action = #selector(MainMenu.switchToPhpVersion(sender:))
let brew = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)"
let menuItem = NSMenuItem(title: "\("mi_php_switch".localized) \(version) (\(brew))", action: (version == App.shared.currentVersion?.short) ? nil : action, keyEquivalent: "\(shortcutKey)")
menuItem.tag = index
let menuItem = PhpMenuItem(title: "\("mi_php_switch".localized) \(version) (\(brew))", action: (version == App.shared.currentVersion?.short) ? nil : action, keyEquivalent: "\(shortcutKey)")
menuItem.version = version
shortcutKey = shortcutKey + 1
self.addItem(menuItem)
}

View File

@@ -202,6 +202,7 @@ class MainMenu: NSObject, NSWindowDelegate {
self.waitAndExecute({
try! "<?php phpinfo();".write(toFile: "/tmp/phpmon_phpinfo.php", atomically: true, encoding: .utf8)
Shell.user.run("/usr/local/bin/php-cgi -q /tmp/phpmon_phpinfo.php > /tmp/phpmon_phpinfo.html")
}, {
NSWorkspace.shared.open(URL(string: "file:///private/tmp/phpmon_phpinfo.html")!)
})
}
@@ -236,11 +237,9 @@ class MainMenu: NSObject, NSWindowDelegate {
Actions.openValetConfigFolder()
}
@objc public func switchToPhpVersion(sender: AnyObject) {
@objc public func switchToPhpVersion(sender: PhpMenuItem) {
print("Switching to: PHP \(sender.version)")
self.setBusyImage()
// TODO: A wise man once said: using tags is not good. Fix this.
let index = sender.tag!
let version = App.shared.availablePhpVersions[index]
App.shared.busy = true
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
// Update the PHP version in the status bar
@@ -249,7 +248,7 @@ class MainMenu: NSObject, NSWindowDelegate {
self.update()
// Switch the PHP version
Actions.switchToPhpVersion(
version: version,
version: sender.version,
availableVersions: App.shared.availablePhpVersions
)
// Mark as no longer busy
@@ -260,8 +259,8 @@ class MainMenu: NSObject, NSWindowDelegate {
self.update()
// Send a notification that the switch has been completed
LocalNotification.send(
title: String(format: "notification.version_changed_title".localized, version),
subtitle: String(format: "notification.version_changed_desc".localized, version)
title: String(format: "notification.version_changed_title".localized, sender.version),
subtitle: String(format: "notification.version_changed_desc".localized, sender.version)
)
}
}