From f6378e7b730165cbd144de2d2a0e5f226f264b09 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 24 Jan 2022 23:42:22 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20option=20to=20add=20a=20linke?= =?UTF-8?q?d=20site?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 6 + phpmon/Domain/Core/Base.lproj/Main.storyboard | 187 ++++++++++++++++-- phpmon/Domain/SiteList/AddSiteVC.swift | 71 +++++++ phpmon/Domain/SiteList/SiteListVC.swift | 22 +++ phpmon/Domain/SiteList/SiteListWC.swift | 41 +++- phpmon/Localizable.strings | 2 + 6 files changed, 313 insertions(+), 16 deletions(-) create mode 100644 phpmon/Domain/SiteList/AddSiteVC.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 0d0bc97..2423eb6 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -96,6 +96,8 @@ C48D6C71279CD2AC00F26D7E /* PhpVersionNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = C48D6C6F279CD2AC00F26D7E /* PhpVersionNumber.swift */; }; C48D6C72279CD2AC00F26D7E /* PhpVersionNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = C48D6C6F279CD2AC00F26D7E /* PhpVersionNumber.swift */; }; C48D6C75279CD3E400F26D7E /* PhpVersionNumberTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C48D6C73279CD3E400F26D7E /* PhpVersionNumberTest.swift */; }; + C493084A279F331F009C240B /* AddSiteVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4930849279F331F009C240B /* AddSiteVC.swift */; }; + C493084B279F331F009C240B /* AddSiteVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4930849279F331F009C240B /* AddSiteVC.swift */; }; C4998F0626175E7200B2526E /* HotKey in Frameworks */ = {isa = PBXBuildFile; productRef = C4998F0526175E7200B2526E /* HotKey */; }; C4998F0A2617633900B2526E /* PrefsWC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4998F092617633900B2526E /* PrefsWC.swift */; }; C4998F0B2617633900B2526E /* PrefsWC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4998F092617633900B2526E /* PrefsWC.swift */; }; @@ -269,6 +271,7 @@ C48D0CA225CC992000CC7490 /* StatsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsView.swift; sourceTree = ""; }; C48D6C6F279CD2AC00F26D7E /* PhpVersionNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpVersionNumber.swift; sourceTree = ""; }; C48D6C73279CD3E400F26D7E /* PhpVersionNumberTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhpVersionNumberTest.swift; sourceTree = ""; }; + C4930849279F331F009C240B /* AddSiteVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSiteVC.swift; sourceTree = ""; }; C4998F092617633900B2526E /* PrefsWC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefsWC.swift; sourceTree = ""; }; C4ACA38E25C754C100060C66 /* PhpExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpExtension.swift; sourceTree = ""; }; C4AF9F70275445FF00D44ED0 /* valet-config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "valet-config.json"; sourceTree = ""; }; @@ -506,6 +509,7 @@ C464ADAE275A7A69003FCD53 /* SiteListVC.swift */, C41E87192763D42300161EE0 /* SiteListVC+ContextMenu.swift */, C41CA5EC2774F8EE00A2C80E /* SiteListVC+Actions.swift */, + C4930849279F331F009C240B /* AddSiteVC.swift */, C464ADB1275A87CA003FCD53 /* SiteListCell.swift */, ); path = SiteList; @@ -931,6 +935,7 @@ C464ADAC275A7A3F003FCD53 /* SiteListWC.swift in Sources */, C464ADB2275A87CA003FCD53 /* SiteListCell.swift in Sources */, C4EE188422D3386B00E126E5 /* Constants.swift in Sources */, + C493084A279F331F009C240B /* AddSiteVC.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -948,6 +953,7 @@ C4EE55AC27708B9E001DF387 /* Preview.swift in Sources */, 54B48B60275F66AE006D90C5 /* Application.swift in Sources */, C4F780C825D80B75000DBC97 /* DateExtension.swift in Sources */, + C493084B279F331F009C240B /* AddSiteVC.swift in Sources */, C4D9ADC0277610E1007277F4 /* PhpSwitcher.swift in Sources */, C4F780CC25D80B75000DBC97 /* ActivePhpInstallation.swift in Sources */, C4F780B125D80B4D000DBC97 /* PhpExtension.swift in Sources */, diff --git a/phpmon/Domain/Core/Base.lproj/Main.storyboard b/phpmon/Domain/Core/Base.lproj/Main.storyboard index fb73779..8568c78 100644 --- a/phpmon/Domain/Core/Base.lproj/Main.storyboard +++ b/phpmon/Domain/Core/Base.lproj/Main.storyboard @@ -374,6 +374,12 @@ + + + + + + @@ -394,6 +400,7 @@ + @@ -411,28 +418,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + @@ -445,7 +601,7 @@ - + @@ -473,7 +629,7 @@ - + @@ -481,7 +637,7 @@ - + @@ -489,10 +645,10 @@ - + - + @@ -501,7 +657,7 @@ - + @@ -570,7 +726,7 @@ - + - + @@ -605,7 +761,7 @@ - + @@ -613,5 +769,6 @@ + diff --git a/phpmon/Domain/SiteList/AddSiteVC.swift b/phpmon/Domain/SiteList/AddSiteVC.swift new file mode 100644 index 0000000..9366528 --- /dev/null +++ b/phpmon/Domain/SiteList/AddSiteVC.swift @@ -0,0 +1,71 @@ +// +// AddSiteVC.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 24/01/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation +import Cocoa + +class AddSiteVC: NSViewController, NSTextFieldDelegate { + + @IBOutlet weak var pathControl: NSPathControl! + @IBOutlet weak var linkName: NSTextField! + @IBOutlet weak var previewText: NSTextField! + @IBOutlet weak var buttonSecure: NSButton! + + @IBOutlet weak var buttonCreateLink: NSButton! + + @IBAction func pressedCreateLink(_ sender: Any) { + let path = self.pathControl.url!.path + let name = self.linkName.stringValue + + // TODO: Check if the path still exists + Shell.run("cd '\(path)' && \(Paths.valet) link '\(name)'", requiresPath: true) + self.view.window!.close() + + // Reset search + App.shared.siteListWindowController? + .searchToolbarItem + .searchField.stringValue = "" + + // Add the new item and scrolls to it + App.shared.siteListWindowController? + .contentVC + .addedNewSite( + name: name, + secure: buttonSecure.state == .on + ) + } + + @IBAction func pressedCancel(_ sender: Any) { + self.view.window!.close() + } + + @IBAction func pressedSecure(_ sender: Any) { + updatePreview() + } + + func controlTextDidChange(_ obj: Notification) { + updateTextField() + } + + func updateTextField() { + self.linkName.stringValue = self.linkName.stringValue + .replacingOccurrences(of: " ", with: "-") + + buttonCreateLink.isEnabled = !self.linkName.stringValue.isEmpty + self.updatePreview() + } + + func updatePreview() { + previewText.stringValue = "site_list.add.folder_available" + .localized( + self.buttonSecure.state == .on ? "https" : "http", + self.linkName.stringValue, + Valet.shared.config.tld + ) + } +} diff --git a/phpmon/Domain/SiteList/SiteListVC.swift b/phpmon/Domain/SiteList/SiteListVC.swift index 039180b..c770abe 100644 --- a/phpmon/Domain/SiteList/SiteListVC.swift +++ b/phpmon/Domain/SiteList/SiteListVC.swift @@ -135,6 +135,28 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { } } + func addedNewSite(name: String, secure: Bool) { + waitAndExecute { + Valet.shared.reloadSites() + } completion: { [self] in + find(name, secure) + } + } + + private func find(_ name: String, _ secure: Bool = false) { + sites = Valet.shared.sites + searchedFor(text: "") + if let site = sites.enumerated().first(where: { $0.element.name == name }) { + DispatchQueue.main.async { + self.tableView.selectRowIndexes([site.offset], byExtendingSelection: false) + self.tableView.scrollRowToVisible(site.offset) + if (secure && !site.element.secured) { + self.toggleSecure() + } + } + } + } + // MARK: - Table View Delegate func numberOfRows(in tableView: NSTableView) -> Int { diff --git a/phpmon/Domain/SiteList/SiteListWC.swift b/phpmon/Domain/SiteList/SiteListWC.swift index 9f032c9..ec3f0cc 100644 --- a/phpmon/Domain/SiteList/SiteListWC.swift +++ b/phpmon/Domain/SiteList/SiteListWC.swift @@ -50,7 +50,46 @@ class SiteListWC: PMWindowController, NSSearchFieldDelegate, NSToolbarDelegate { // MARK: - Reload functionality - @IBAction func pressedReload(_ sender: Any) { + @IBAction func pressedReload(_ sender: Any?) { contentVC.reloadSites() } + + @IBAction func pressedAddLink(_ sender: Any?) { + selectFolder() + } + + // MARK: - Add a new site + + func selectFolder() { + let dialog = NSOpenPanel() + dialog.title = "Select a Folder" + dialog.showsResizeIndicator = true + dialog.showsHiddenFiles = false + dialog.allowsMultipleSelection = false + dialog.canChooseDirectories = true + dialog.canChooseFiles = false + + if (dialog.runModal() == NSApplication.ModalResponse.OK) { + let result = dialog.url + if (result != nil) { + let path: String = result!.path + self.showSitePopup(path) + } + } + } + + func showSitePopup(_ folder: String) { + let storyboard = NSStoryboard(name: "Main" , bundle : nil) + + let windowController = storyboard.instantiateController( + withIdentifier: "addSiteWindow" + ) as! NSWindowController + + let viewController = windowController.window!.contentViewController as! AddSiteVC + viewController.pathControl.url = URL(fileURLWithPath: folder) + viewController.linkName.stringValue = String(folder.split(separator: "/").last!) + viewController.updateTextField() + + self.window?.beginSheet(windowController.window!) + } } diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index bd3f063..893a286 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -69,6 +69,8 @@ "site_list.confirm_unlink" = "Are you sure you want to unlink '%@'?"; "site_link.confirm_link" = "No files will be removed. If needed, the site will need to be relinked via the command line."; +"site_list.add.folder_available" = "This site will be available via the following URL: %@://%@.%@"; + // SITE LIST ACTIONS "site_list.unlink" = "Unlink Directory";