Skip to content

Commit

Permalink
ADD support for drag board
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradSun committed Sep 29, 2023
1 parent af594f0 commit b01a808
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
Binary file modified Docs/PasteShow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions PasteShow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 2.1;
PRODUCT_BUNDLE_IDENTIFIER = com.nuwastone.PasteShow;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -325,7 +325,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 2.1;
PRODUCT_BUNDLE_IDENTIFIER = com.nuwastone.PasteShow;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
30 changes: 28 additions & 2 deletions PasteShow/PasteboardManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ enum ItemType: String {
case Other = "Other"
}

enum PasteboardType: String, CaseIterable {
case General = "General"
case Drag = "Drag"
}

class PasteInfoList: ObservableObject {
struct PasteInfo {
var itemType = ItemType.Other
var sourceURL = URL(string: "")
var copiedItems = [[String: Data]]()
}

@Published var boardType = "General"
@Published var infoList = [PasteInfo]()

func appendInfo(source: URL, items: [[String: Data]], type: ItemType) {
Expand All @@ -40,11 +46,12 @@ class PasteboardManager {
static let shared = PasteboardManager()
var changeCount = 0
var pasteInfo = PasteInfoList()
private let pasteboard = NSPasteboard.general

private var pasteboard = NSPasteboard.init(name: .general)
private var observerTimer = Timer()

private init() {
setupObserverTimer()
setupPasteboardType(type: pasteInfo.boardType)
}

private func setupObserverTimer() {
Expand Down Expand Up @@ -102,6 +109,25 @@ class PasteboardManager {
pasteInfo.appendInfo(source: sourceURL!, items: copiedItems, type: itemType)
}

private func getBoardType(_ type: String) -> NSPasteboard.Name {
let pasteboardType = ["General": NSPasteboard.Name.general,
"Drag": NSPasteboard.Name.drag,
"Font": NSPasteboard.Name.font]

guard let name = pasteboardType[type] else {
return .general
}
return name
}

func setupPasteboardType(type: String) {
let boardType = getBoardType(type)
pasteInfo.infoList.removeAll()
print("setup \(boardType)")
pasteboard = NSPasteboard.init(name: boardType)
setupObserverTimer()
}

func setDataWithoutReserve(data: String, forType type: NSPasteboard.PasteboardType) {
pasteboard.clearContents()
pasteboard.setString(data, forType: type)
Expand Down
20 changes: 20 additions & 0 deletions PasteShow/SplitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct SidebarView: View {
}
}
}
.toolbar {
PickerView()
}
}
}

Expand Down Expand Up @@ -138,3 +141,20 @@ struct SourceView: View {
}
}
}

struct PickerView: View {
@EnvironmentObject var info: PasteInfoList

var body: some View {
Picker(selection: $info.boardType) {
ForEach(Array(PasteboardType.allCases.enumerated()), id: \.1.rawValue) { index, type in
Text(type.rawValue)
}
} label: {
Text("\(info.boardType)")
}
.onReceive(info.$boardType) { type in
PasteboardManager.shared.setupPasteboardType(type: type)
}
}
}

0 comments on commit b01a808

Please sign in to comment.