From f5e6a53a8b42648664380392c8d0993a085e1bc3 Mon Sep 17 00:00:00 2001 From: Renset Date: Tue, 25 Jun 2024 01:22:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Fix=20copy=20table=20as?= =?UTF-8?q?=20JSON.=20Use=20plain=20button=20style.=20Display=20copy=20but?= =?UTF-8?q?ton=20for=20code=20block=20if=20language=20name=20is=20missing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- macai/UI/Chat/CodeView.swift | 50 +++++++++++++++++------------------ macai/UI/Chat/TableView.swift | 33 ++++++++++++++++++++--- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/macai/UI/Chat/CodeView.swift b/macai/UI/Chat/CodeView.swift index 110ac75..eddd188 100644 --- a/macai/UI/Chat/CodeView.swift +++ b/macai/UI/Chat/CodeView.swift @@ -20,40 +20,40 @@ struct CodeView: View { var body: some View { VStack { VStack { - if lang != "" { + HStack { HStack { - HStack { + if lang != "" { Text(lang) .fontWeight(.bold) - Spacer() - // Flat button to copy code to clipboard - // Display Check mark if copied - if isHovered { - Button(action: { - copyToClipboard(code?.string ?? "") - withAnimation{ - isCopied = true - } - DispatchQueue.main.asyncAfter(deadline: .now() + 1) { - withAnimation { - isCopied = false - } + } + Spacer() + // Flat button to copy code to clipboard + // Display Check mark if copied + if isHovered { + Button(action: { + copyToClipboard(code?.string ?? "") + withAnimation{ + isCopied = true + } + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + withAnimation { + isCopied = false } - }) { - Image(systemName: isCopied ? "checkmark" : "doc.on.doc") - .frame(height: 12) } - .buttonStyle(PlainButtonStyle()) + }) { + Image(systemName: isCopied ? "checkmark" : "doc.on.doc") + .frame(height: 12) } + .buttonStyle(PlainButtonStyle()) } - .padding(6) } - .background( - colorScheme == .dark - ? Color(NSColor(red: 30 / 255, green: 30 / 255, blue: 30 / 255, alpha: 1)) - : Color(NSColor(red: 220 / 255, green: 220 / 255, blue: 220 / 255, alpha: 1)) - ) + .padding(6) } + .background( + colorScheme == .dark + ? Color(NSColor(red: 30 / 255, green: 30 / 255, blue: 30 / 255, alpha: 1)) + : Color(NSColor(red: 220 / 255, green: 220 / 255, blue: 220 / 255, alpha: 1)) + ) AttributedText(code ?? NSAttributedString(string: "")) .textSelection(.enabled) diff --git a/macai/UI/Chat/TableView.swift b/macai/UI/Chat/TableView.swift index 43d2be0..6177421 100644 --- a/macai/UI/Chat/TableView.swift +++ b/macai/UI/Chat/TableView.swift @@ -57,6 +57,8 @@ struct TableRowView: View { struct TableView: View { let header: [String] let tableData: [[String]] + @State private var isCopied = false + @State private var isCopiedJSON = false private func copyTableToClipboard() { let headerString = header.joined(separator: "\t") @@ -98,17 +100,40 @@ struct TableView: View { VStack { HStack { Spacer() - Button(action: copyTableToClipboard) { + Button(action: { + copyTableAsJSONToClipboard() + withAnimation{ + isCopiedJSON = true + } + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + withAnimation { + isCopiedJSON = false + } + } + }) { + Image(systemName: isCopiedJSON ? "checkmark" : "doc.on.doc") Text("JSON") - Image(systemName: "doc.on.doc") } .padding(0) + .padding(.trailing, 16) + .buttonStyle(PlainButtonStyle()) - Button(action: copyTableToClipboard) { - Image(systemName: "doc.on.doc") + Button(action: { + copyTableToClipboard() + withAnimation{ + isCopied = true + } + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + withAnimation { + isCopied = false + } + } + }) { + Image(systemName: isCopied ? "checkmark" : "doc.on.doc") } .padding(0) + .buttonStyle(PlainButtonStyle()) } .padding(.bottom, 2) VStack(spacing: 0) {