Git Product home page Git Product logo

Comments (9)

orchetect avatar orchetect commented on September 25, 2024 1

Oh yeah, that's a menu-based MenuBarExtra.

SwiftUI treats standard menus differently. Even though you're using a View, for menu items SwiftUI strips out everything except Buttons (Text and/or Image labels) and any view modifiers. Which means SwiftUI won't setup the internals for SettingsAccess to work.

Inside a menu, SettingsLink is sufficient to open Settings but it won't let you run custom code before or after it's clicked.

I'm not sure there is a solution for this specific scenario. I've tried a few approaches and nothing is sticking yet.

I will add a note to the readme in the interim that this is an unsupported use case until a solution can be worked out.

from settingsaccess.

orchetect avatar orchetect commented on September 25, 2024

Usually this happens if compiling with Xcode 14 (it needs Xcode 15).

from settingsaccess.

orchetect avatar orchetect commented on September 25, 2024

I've pushed release 1.1.0 which will now only compile on Xcode 15. That was originally intended and was a bit of an oversight to have allowed compiling on Xcode 14 which is where the error came from.

from settingsaccess.

castdrian avatar castdrian commented on September 25, 2024

this does not resolve the issue, it had been compiled with xcode 15 already, updating to the next minor still throws this exception

from settingsaccess.

orchetect avatar orchetect commented on September 25, 2024

I need more details about where you're using .openSettingsAccess() and where you're calling openSettings().

What it most likely means is the view that is getting .openSettingsAccess() is not being created or persisted.

from settingsaccess.

castdrian avatar castdrian commented on September 25, 2024

it's being chained onto mainmenuview in app.swift here

@main
struct ishare: App {
    @Default(.menuBarIcon) var menubarIcon
    @Default(.showMainMenu) var showMainMenu
    @StateObject private var appState = AppState()
    @NSApplicationDelegateAdaptor private var appDelegate : AppDelegate
    
    var body: some Scene {
        MenuBarExtra() {
            MainMenuView().openSettingsAccess()
        }
    label: {
        switch menubarIcon {
            case .DEFAULT: Image(nsImage: GlyphIcon)
            case .APPICON: Image(nsImage: AppIcon)
            case .SYSTEM: Image(systemName: "photo.on.rectangle.angled")
        }
    }
    .menuBarExtraAccess(isPresented: $showMainMenu)
        Settings {
            SettingsMenuView()
        }
    }
}

within MainMenuview the env is declared:

struct MainMenuView: View {    
    @StateObject private var availableContentProvider = AvailableContentProvider()
    @Environment(\.openSettings) private var openSettings
    
    var body: some View {
    ...

and then the method is called within a button

Button {
                NSApplication.shared.activate(ignoringOtherApps: true)
                openSettings()
} label: {
                Image(systemName: "gearshape")
                Label("Settings", image: String())
}.keyboardShortcut("s")

from settingsaccess.

orchetect avatar orchetect commented on September 25, 2024

Actually I think I've found solution. I'll try to push it today or tomorrow, just want to polish it up.

from settingsaccess.

orchetect avatar orchetect commented on September 25, 2024

Ok try release 1.2.0. The Demo project and readme are updated, but this is the gist:

  • Remove .openSettingsAccess() from your MenuBarExtra menu view
  • Inside MainMenuView, instead of your Button, use this:
    SettingsLink {
        Image(systemName: "gearshape")
        Text("Settings")
    } preAction: {
        NSApp.activate(ignoringOtherApps: true)
    } postAction: {
        // empty
    }
    .keyboardShortcut("s")

from settingsaccess.

castdrian avatar castdrian commented on September 25, 2024

lovely, whatever you did works much better than the actual SettingsLink component <3

from settingsaccess.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.