Skip to content

Commit

Permalink
fix: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gluonfield committed Apr 6, 2024
1 parent cd8990c commit 5b23d89
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
61 changes: 61 additions & 0 deletions Enchanted/Helpers/Accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class Accessibility {

func getSelectedText() -> String? {
if let text = getSelectedTextAX(), text.count > 1 {
print("getSelectedTextAX", text)
return text
}

Expand All @@ -58,6 +59,7 @@ final class Accessibility {


func getSelectedTextViaCopy(retryAttempts: Int = 1) -> String? {

let pasteboard = NSPasteboard.general
let originalContents = pasteboard.pasteboardItems?.compactMap { $0.string(forType: .string) } ?? []
pasteboard.clearContents()
Expand Down Expand Up @@ -85,6 +87,65 @@ final class Accessibility {
}

return newContent


print("via copy invocation")
DispatchQueue.main.async {
print("one", NSPasteboard.general.string(forType: .string))
sleep(1)
print("two", NSPasteboard.general.string(forType: .string))
sleep(1)
print("three", NSPasteboard.general.string(forType: .string))
sleep(1)
print("four", NSPasteboard.general.string(forType: .string))

}
// let pasteboard = NSPasteboard.general
// let originalContents = pasteboard.pasteboardItems?.compactMap { $0.string(forType: .string) } ?? []
// print("step 1", pasteboard.string(forType: .string))
// pasteboard.clearContents()
// pasteboard.setString("", forType: .string)
// print("step 2", pasteboard.string(forType: .string))
// usleep(1000000)
// sleep(1)
// var attempts = 0
// var newContent: String?


// while attempts < retryAttempts && newContent == nil {

// simulateCopyKeyPress()
// usleep(100000)


// print("step 3", pasteboard.string(forType: .string))
// newContent = pasteboard.string(forType: .string)
// print("pasteboard content", newContent)
// print("step 3.5", pasteboard.string(forType: .string))
// if let newContent = newContent, !newContent.isEmpty {
// break
// } else {
// newContent = nil
// }
// attempts += 1
// }

// if newContent == nil {
// pasteboard.clearContents()
// for item in originalContents {
// pasteboard.setString(item, forType: .string)
// }
// }

// defer {
// pasteboard.clearContents()
// }
//
// print("step 4", pasteboard.string(forType: .string))
// print("returning via copy method", newContent)

return nil
// return newContent
}

func simulateCopyKeyPress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct CompletionsEditor: View {

private func requestAccessibility() {
Task {
print("Requesting accessibility")
await Accessibility.shared.showAccessibilityInstructionsWindow()
Accessibility.shared.simulateCopyKeyPress()
}
Expand Down
8 changes: 4 additions & 4 deletions Enchanted/UI/macOS/Components/CompletionPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ struct PanelCompletionsView: View {
HStack(alignment: .center) {
switch completionMode {
case .completionsInApp:
Text("Response in **App**.")
Text("Respond in **App**.")
case .completionsInCurrentWindow:
Text("Response in **Window**.")
Text("Respond in **Window**.")
case .completionsInWindowDelayed:
Text("Response in **Window** with trigger.")
Text("Respond in **Window** with trigger.")
}

Text("SPACE")
Expand All @@ -115,7 +115,7 @@ struct PanelCompletionsView: View {
}
.padding()
.background {
RoundedRectangle(cornerRadius: 8).fill(.ultraThinMaterial)
RoundedRectangle(cornerRadius: 8).fill(.ultraThickMaterial)
}
.onKeyboardShortcut(key: .space, modifiers: []) {
withAnimation {
Expand Down
2 changes: 1 addition & 1 deletion Enchanted/UI/macOS/Components/PromptPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct PromptPanelView: View {
}
.padding(12)
.background {
RoundedRectangle(cornerRadius: 8).fill(.ultraThinMaterial)
RoundedRectangle(cornerRadius: 8).fill(.ultraThickMaterial)
}
}
.frame(minWidth: 500, maxWidth: 500)
Expand Down
10 changes: 6 additions & 4 deletions Enchanted/UI/macOS/PromptPanel/PanelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Carbon
import AsyncAlgorithms

final actor Printer {
func print(_ message: String) {
func write(_ message: String) {
print("clipboard printing", message)
Clipboard.shared.setString(message)
usleep(50000)
Accessibility.simulatePasteCommand()
Expand Down Expand Up @@ -56,15 +57,16 @@ class PanelManager: NSObject, NSApplicationDelegate {
}

print("printing: \((sentencesToConsume)) \(Date())")
await printer.print(sentencesToConsume)
await printer.write(sentencesToConsume)
lastPrintApplication = NSWorkspace.shared.runningApplications.first{$0.isActive}
}
}


@MainActor
@objc func togglePanel() {
if panel == nil {
let accessibilityStatus = Accessibility.shared.checkAccessibility()
if !accessibilityStatus {
Accessibility.shared.showAccessibilityInstructionsWindow()
}

Expand All @@ -82,7 +84,7 @@ class PanelManager: NSObject, NSApplicationDelegate {
self?.hidePanel()
}

return
return
}

hidePanel()
Expand Down

0 comments on commit 5b23d89

Please sign in to comment.