SwiftUI列表在macOS上:启用选择、双击和拖拽。

huangapple go评论84阅读模式
英文:

SwiftUI List on macOS: Enable Selection, double click and dragging

问题

以下是翻译好的代码部分:

struct ContentView: View {
	
	var items = ["项目1", "项目2", "项目3"]
	
	@State private var selection: String? = nil
	
	var body: some View {
		List(selection: self.$selection) {
			ForEach(self.items, id: \.self) { item in
				Text(item)
			}
		}
		.contextMenu(forSelectionType: String.self) { items in
			Button("测试") {}
		} primaryAction: { items in
			print(items)
		}
	}
}
struct ContentView: View {
	
	var items = ["项目1", "项目2", "项目3"]
	
	@State private var selection: String? = nil
	
	var body: some View {
		List(selection: self.$selection) {
			ForEach(self.items, id: \.self) { item in
				Text(item)
					.draggable(item)
			}
		}
		.contextMenu(forSelectionType: String.self) { items in
			Button("测试") {}
		} primaryAction: { items in
			print(items)
		}
	}
}

希望这有帮助。

英文:

I have a simple List with using the new contextMenu(forSelectionType: ..., primaryAction: ) to enable a double click:

struct ContentView: View {
	
	var items = ["Item1", "Item2", "Item3"]
	
	@State private var selection: String? = nil
	
	var body: some View {
		List(selection: self.$selection) {
			ForEach(self.items, id: \.self) { item in
				Text(item)
			}
		}
		.contextMenu(forSelectionType: String.self) { items in
			Button("Test") {}
		} primaryAction: { items in
			print(items)
		}
	}
}

This works well.

But I also want to provide a drag option, so that I can drag out the selected items (actually in my use case I am displaying files and I want to enable the option to drag those files to the Finder or other apps). For my example I will just use the String:

struct ContentView: View {
	
	var items = ["Item1", "Item2", "Item3"]
	
	@State private var selection: String? = nil
	
	var body: some View {
		List(selection: self.$selection) {
			ForEach(self.items, id: \.self) { item in
				Text(item)
					.draggable(item)
			}
		}
		.contextMenu(forSelectionType: String.self) { items in
			Button("Test") {}
		} primaryAction: { items in
			print(items)
		}
	}
}

After now adding the draggable-modifier, the selection of the item is not working anymore. The same is happening when using the older onDrag-modifier.

Does anyone has an idea how to provide selection and dragging?

答案1

得分: 0

Oh,我刚刚在这里找到了解决方案:https://developer.apple.com/forums/thread/664469(我在上次搜索中没有看到这个帖子)。所以解决方案将是:

struct ContentView: View {
	
	var items = ["Item1", "Item2", "Item3"]
	
	@State private var selection: String? = nil
	
	var body: some View {
		List(selection: self.$selection) {
			ForEach(self.items, id: \.self) { item in
				Text(item)
					.itemProvider {
						return NSItemProvider(object: item as NSString)
					}
			}
		}
		.contextMenu(forSelectionType: String.self) { items in
			Button("Test") {}
		} primaryAction: { items in
			print(items)
		}
	}
}
英文:

Oh, I just found the solution here: https://developer.apple.com/forums/thread/664469 (I didn't saw this thread in my last search). So the solution will be:

struct ContentView: View {
	
	var items = ["Item1", "Item2", "Item3"]
	
	@State private var selection: String? = nil
	
	var body: some View {
		List(selection: self.$selection) {
			ForEach(self.items, id: \.self) { item in
				Text(item)
					.itemProvider {
						return NSItemProvider(object: item as NSString)
					}
			}
		}
		.contextMenu(forSelectionType: String.self) { items in
			Button("Test") {}
		} primaryAction: { items in
			print(items)
		}
	}
}

huangapple
  • 本文由 发表于 2023年7月20日 20:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定