> For the complete documentation index, see [llms.txt](https://betty-dev.gitbook.io/cutting-edge-swiftui-apps/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://betty-dev.gitbook.io/cutting-edge-swiftui-apps/new-window/single-unique-window.md).

# Single Unique Window

MacOS

You're likely already familiar with WindowGroup, which is a great way to build the main interface of your app, and can generate multiple windows to allow different perspectives into your app's data.&#x20;

Window declares a single, unique window for your app.&#x20;

{% tabs %}
{% tab title="Unique Window" %}

```swift
// Add Window in the Main App files
// choose Mac target

import SwiftUI

@main
struct FrenchKit2022App: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }

        Window("Paris c'est aussi la FrenchKit !", id: "Sales") {
            ParisDetailsChart()
        }
    }
}

```

{% endtab %}

{% tab title="Second Tab" %}
// Visible also in the App Menu

![](https://1097234039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fj3vybST3gHmvCcIboaVS%2Fuploads%2FWoeoXcDZdwYn4JwRxoEC%2Fmenu.png?alt=media\&token=b2b1955b-7f1b-454e-859a-aa0efdfe1541)
{% endtab %}

{% tab title="Shortcut, position" %}
// We can assign a key keyboard shortcut to open the window  (CMD)

```swift
import SwiftUI

@main
struct FrenchKit2022App: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        Window("Paris c'est aussi la FrenchKit !", id: "Sales") {
            ParisDetailsChart()
        }
        .keyboardShortcut("9")
        .defaultPosition(.topLeading)
        .defaultSize(width: 420, height: 250)
    }
}
```

{% endtab %}

{% tab title="Open" %}
// Open programmatically this window

-> Open `WinesTab`&#x20;

**import** Charts

**import** SwiftUI

<br>

**struct** Wines: Identifiable {

&#x20;   **var** name: String

&#x20;   **var** sales: Int

&#x20;  &#x20;

&#x20;   **var** id: String { name }

}

<br>

**let** sales: \[Wines] = \[

&#x20;   .init(name: "Margaux", sales: 500),

&#x20;   .init(name: "Médoc", sales: 460),

&#x20;   .init(name: "Blaye", sales: 300),

&#x20;   .init(name: "St Estèphe", sales: 290),

&#x20;   .init(name: "Sauternes", sales: 250),

&#x20;   .init(name: "Pessac-Léognan", sales: 20)

]

<br>

**struct** WinesTab: View {

&#x20;   <mark style="color:orange;">@Environment(\\.openWindow)</mark> <mark style="color:orange;"></mark><mark style="color:orange;">**var**</mark> <mark style="color:orange;"></mark><mark style="color:orange;">openWindow</mark>

&#x20;  &#x20;

&#x20;   **var** body: **some** View {

&#x20;       Chart(sales) { element **in**

&#x20;           BarMark (

&#x20;               x: .value("Sales", element.sales),

&#x20;               y: .value("Name", element.name)

&#x20;           )

&#x20;       }

&#x20;       .padding(20)

&#x20;       <mark style="color:orange;">.toolbar {</mark>

&#x20;           <mark style="color:orange;">Button {</mark>

&#x20;               <mark style="color:orange;">openWindow(id: "Sales")</mark>

&#x20;           <mark style="color:orange;">} label: {</mark>

&#x20;               <mark style="color:orange;">Image(systemName: "dollarsign")</mark>

&#x20;           <mark style="color:orange;">}</mark>

&#x20;       <mark style="color:orange;">}</mark>

&#x20;   }

}

<br>

**struct** WinesTab\_Previews: PreviewProvider {

&#x20;   **static** **var** previews: **some** View {

&#x20;       WinesTab()

&#x20;   }

}

```swift
import Charts
import SwiftUI

struct Wines: Identifiable {
    var name: String
    var sales: Int
    
    var id: String { name }
}

let sales: [Wines] = [
    .init(name: "Margaux", sales: 500),
    .init(name: "Médoc", sales: 460),
    .init(name: "Blaye", sales: 300),
    .init(name: "St Estèphe", sales: 290),
    .init(name: "Sauternes", sales: 250),
    .init(name: "Pessac-Léognan", sales: 20)
]

struct WinesTab: View {
    @Environment(\.openWindow) var openWindow
    
    var body: some View {
        Chart(sales) { element in
            BarMark (
                x: .value("Sales", element.sales),
                y: .value("Name", element.name)
            )
        }
        .padding(20)
        .toolbar {
            Button {
                openWindow(id: "Sales")
            } label: {
                Image(systemName: "dollarsign")
            }
        }
    }
}

struct WinesTab_Previews: PreviewProvider {
    static var previews: some View {
        WinesTab()
    }
}
```

{% endtab %}
{% endtabs %}
