Search code examples
swiftswiftuiappkitswiftui-list

`List` not rendering in `MenuBarExtra`


I'm new to Swift, SwiftUI, and AppKit.

I'm creating a menu bar app for macOS using MenuBarExtra.

Why are the Lists not rendering in MenuPanel?

// MacOSSoundUtilityApp.swift

import SwiftUI

@main
struct MacOSSoundUtilityApp: App {    
    var body: some Scene {
        MenuBarExtra("Utility App", systemImage: "hammer") {
            MenuPanel()
        }
        .menuBarExtraStyle(.menu)
    }
}
// MenuPanel.swift

import Foundation
import SwiftUI

struct SomeApp: Identifiable {
    let name: String
    let id = UUID()
}

struct MenuPanel: View {
    var applications = [SomeApp(name: "app 1"), SomeApp(name: "app 2"), SomeApp(name: "app 3")]
    
    var body: some View {
        VStack {
            Text("Utility App")
                .font(.title)
            List {
                Text("option1")
                Text("option2")
                Text("option3")
            }
            List (applications) {
                Text($0.name)
            }
            Text("Hello")
        }
    }
}

Solution

  • You'll want to use the window menu bar extra style instead of the menu menu bar extra style. The documentation describes the window style as "The styling and layout of controls is similar to that when contained in a normal window, compared to the menu-like layout that the menu style provides." (via https://developer.apple.com/documentation/swiftui/menubarextrastyle/window)

    So it would allow rendering of your custom list, vs the menu style has similar restrictions on what