如何在基于文档的MacOS应用中处理不同的文档类型?

发布于 2025-01-23 08:01:34 字数 1058 浏览 2 评论 0原文

如何在一个MacOS文档应用中处理两种不同的自定义文档类型?

从MacOS文档应用模板开始,我定义了两种类型,这些类型也在info.plist中注册。plist:

extension UTType {
    static var test1: UTType {
        UTType(exportedAs: "com.exapmple.test1")
    }
}

extension UTType {
    static var test2: UTType {
        UTType(exportedAs: "com.example.test2")
    }
}

apple “ noreferrer”>文档说:

您的应用程序可以通过添加其他文档来支持多种文档类型 文档组场景:

但是显示的示例只有一种可以创建的类型,另一种仅读取(编辑器模式)。

如果我在主应用结构中执行此操作(基本上是模板中的样板:

@main
struct MultipleDocumentsApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: DocumentOne()) { file in
            Content1View(document: file.$document)
        }
        DocumentGroup(newDocument: DocumentTwo()) { file in
            Content2View(document: file.$document)
        }
    }
}

..所产生的新菜单看起来像这样,我只能创建类型1的文档:

​不同的新菜单项,用于我如何实现的任何想法?

How do I handle two different custom document types in one macOS document app?

Starting from the macOS Document App template I define two types, which are also registered in the info.plist :

extension UTType {
    static var test1: UTType {
        UTType(exportedAs: "com.exapmple.test1")
    }
}

extension UTType {
    static var test2: UTType {
        UTType(exportedAs: "com.example.test2")
    }
}

Apple documentation says:

Your app can support multiple document types by adding additional
document group scenes:

But the shown example has only one type that can be created, the other is read only (Editor mode).

If I do this in the main app struct (which is basically boilerplate from the template:

@main
struct MultipleDocumentsApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: DocumentOne()) { file in
            Content1View(document: file.$document)
        }
        DocumentGroup(newDocument: DocumentTwo()) { file in
            Content2View(document: file.$document)
        }
    }
}

..the resulting New menu looks like this, and I can only create documents of type 1:

enter image description here

Obviously I would need two different New... menu items for the two doc types. Any ideas how I can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2025-01-30 08:01:34

它仍然是基于nsdocumentController,因此Pros& cons也相同 - 我们对默认文档类型(对于其他所有内容)进行了自动处理 - 回到编码。

因此,您所做的一切都是正确的,唯一的剩余就是在编程中添加其他(非默认)类型的新文档的创建。

这是方法的主要部分:

Button("New Document2") {
  let dc = NSDocumentController.shared
  if let newDocument = try? dc.makeUntitledDocument(ofType: "com.example2.plain-text") {
    dc.addDocument(newDocument)
    newDocument.makeWindowControllers()
    newDocument.showWindows()
  }
}

完成

It is still NSDocumentController based, so pros&cons are also the same - we have automatic handling for default document type, for everything else - back to coding.

So all you've done is correct, the only left is to add, programmatically, creation of new document of other (non default) type.

Here is main part of approach:

Button("New Document2") {
  let dc = NSDocumentController.shared
  if let newDocument = try? dc.makeUntitledDocument(ofType: "com.example2.plain-text") {
    dc.addDocument(newDocument)
    newDocument.makeWindowControllers()
    newDocument.showWindows()
  }
}

Complete findings and code is here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文