Single Unique Window
MacOS
Last updated
MacOS
Last updated
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.
Window declares a single, unique window for your app.
// 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()
}
}
}
// Visible also in the App Menu
// We can assign a key keyboard shortcut to open the window (CMD)
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)
}
}
// Open programmatically this window
-> Open WinesTab
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()
}
}
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()
}
}