Skip to content

Commit

Permalink
Merge pull request #381 from Planetable/api-console-controls
Browse files Browse the repository at this point in the history
Add basic console controls
  • Loading branch information
livid authored Aug 21, 2024
2 parents 709d790 + 9ce35bc commit 609ea08
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Planet/API/PlanetAPIConsoleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private struct AttributedConsoleView: NSViewRepresentable {
attributedLog.addAttribute(.foregroundColor, value: NSColor.textColor, range: NSRange(location: 0, length: attributedLog.length))

// Set base font
attributedLog.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: 12, weight: .regular), range: NSRange(location: 0, length: attributedLog.length))
attributedLog.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: self.viewModel.baseFontSize, weight: .regular), range: NSRange(location: 0, length: attributedLog.length))

// Match timestamp
let timestampRange = NSRange(location: 0, length: timestampString.count)
Expand All @@ -83,15 +83,15 @@ private struct AttributedConsoleView: NSViewRepresentable {
color = .textColor
}
attributedLog.addAttribute(.foregroundColor, value: color, range: statusCodeRange)
attributedLog.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: 12, weight: .bold), range: statusCodeRange)
attributedLog.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: self.viewModel.baseFontSize, weight: .bold), range: statusCodeRange)
}
}
}

// Match request method
if let methodRange = logText.range(of: log.requestURL.split(separator: " ").first ?? "") {
let nsRange = NSRange(methodRange, in: logText)
attributedLog.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: 12, weight: .semibold), range: nsRange)
attributedLog.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: self.viewModel.baseFontSize, weight: .semibold), range: nsRange)
}

attributedText.append(attributedLog)
Expand All @@ -101,7 +101,7 @@ private struct AttributedConsoleView: NSViewRepresentable {
let errorDescription = log.errorDescription + "\n"
let attributedErrorDescription = NSMutableAttributedString(string: errorDescription)
let errorRange = NSRange(location: 0, length: attributedErrorDescription.length)
attributedErrorDescription.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium), range: errorRange)
attributedErrorDescription.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: self.viewModel.baseFontSize, weight: .medium), range: errorRange)
attributedErrorDescription.addAttribute(.foregroundColor, value: NSColor.secondaryLabelColor, range: errorRange)
attributedText.append(attributedErrorDescription)
}
Expand Down
33 changes: 33 additions & 0 deletions Planet/API/PlanetAPIConsoleViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ import SwiftUI
class PlanetAPIConsoleViewModel: ObservableObject {
static let shared = PlanetAPIConsoleViewModel()
static let maxLength: Int = 2000
static let baseFontKey: String = "APIConsoleBaseFontSizeKey"

@Published var isShowingConsoleWindow = false
@Published private(set) var baseFontSize: CGFloat {
didSet {
UserDefaults.standard.set(baseFontSize, forKey: Self.baseFontKey)
}
}

@Published private(set) var logs: [
(
Expand All @@ -23,6 +29,11 @@ class PlanetAPIConsoleViewModel: ObservableObject {
] = []

init() {
var fontSize = CGFloat(UserDefaults.standard.float(forKey: Self.baseFontKey))
if fontSize == 0 {
fontSize = 12
}
baseFontSize = fontSize
}

@MainActor
Expand All @@ -34,4 +45,26 @@ class PlanetAPIConsoleViewModel: ObservableObject {
logs = Array(logs.suffix(Self.maxLength))
}
}

@MainActor
func decreaseFontSize() {
if baseFontSize > 9 {
baseFontSize -= 1
}
}

@MainActor
func increaseFontSize() {
baseFontSize += 1
}

@MainActor
func resetFontSize() {
baseFontSize = 12
}

@MainActor
func clearLogs() {
logs.removeAll()
}
}
48 changes: 48 additions & 0 deletions Planet/API/Window/PlanetAPIConsoleWindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

import Foundation
import SwiftUI


class PlanetAPIConsoleWindowManager: NSObject {
Expand Down Expand Up @@ -33,4 +34,51 @@ class PlanetAPIConsoleWindowManager: NSObject {
PlanetAPIConsoleViewModel.shared.isShowingConsoleWindow = false
}
}

@ViewBuilder
func consoleCommandMenu() -> some View {
Menu("API Console") {
Button {
Task { @MainActor in
PlanetAPIConsoleViewModel.shared.increaseFontSize()
}
} label: {
Text("Increase Font Size")
}
.keyboardShortcut("+", modifiers: [.command])
Button {
Task { @MainActor in
PlanetAPIConsoleViewModel.shared.decreaseFontSize()
}
} label: {
Text("Decrease Font Size")
}
.keyboardShortcut("-", modifiers: [.command])
Button {
Task { @MainActor in
PlanetAPIConsoleViewModel.shared.resetFontSize()
}
} label: {
Text("Reset to Default Size")
}
.keyboardShortcut("0", modifiers: [.command])
Divider()
Button {
Task { @MainActor in
PlanetAPIConsoleViewModel.shared.clearLogs()
}
} label: {
Text("Clear Console Output")
}
.keyboardShortcut("c", modifiers: [.command, .shift, .option])
Divider()
Button {
PlanetAPIConsoleWindowManager.shared.activate()
} label: {
Text("Open Console")
}
.keyboardShortcut("a", modifiers: [.command, .shift, .option])
}

}
}
11 changes: 6 additions & 5 deletions Planet/Helper/KeyboardShortcutHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ class KeyboardShortcutHelper: ObservableObject {

publishedFoldersMenus()

Button {
PlanetAPIConsoleWindowManager.shared.activate()
} label: {
Text("API Console")
}
apiConsoleMenus()

Divider()
}
Expand Down Expand Up @@ -473,4 +469,9 @@ class KeyboardShortcutHelper: ObservableObject {
self.serviceStore.updatePendingPublishings()
}
}

@ViewBuilder
private func apiConsoleMenus() -> some View {
PlanetAPIConsoleWindowManager.shared.consoleCommandMenu()
}
}
10 changes: 10 additions & 0 deletions PlanetLite/CroptopApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ struct CroptopApp: App {
@Environment(\.openURL) private var openURL
@ObservedObject private var updater: PlanetUpdater
@ObservedObject private var keyboardHelper: KeyboardShortcutHelper
@ObservedObject var apiController: PlanetAPIController

init() {
_updater = ObservedObject(wrappedValue: PlanetUpdater.shared)
_keyboardHelper = ObservedObject(wrappedValue: KeyboardShortcutHelper.shared)
_apiController = ObservedObject(wrappedValue: PlanetAPIController.shared)
}

var body: some Scene {
Expand All @@ -33,6 +35,7 @@ struct CroptopApp: App {
keyboardHelper.writerCommands()
fileCommands()
infoCommands()
consoleToolsCommands()
helperCommands()
SidebarCommands()
}
Expand Down Expand Up @@ -138,6 +141,13 @@ struct CroptopApp: App {
.disabled(!updater.canCheckForUpdates)
}
}

@CommandsBuilder
func consoleToolsCommands() -> some Commands {
CommandMenu("Tools") {
PlanetAPIConsoleWindowManager.shared.consoleCommandMenu()
}
}

@CommandsBuilder
func helperCommands() -> some Commands {
Expand Down

0 comments on commit 609ea08

Please sign in to comment.