maui-samples icon indicating copy to clipboard operation
maui-samples copied to clipboard

StatusBar Click Action in maccatalyst

Open mauisansan opened this issue 1 year ago • 0 comments

Description

For apps created with #193 File path: “8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/Platforms/MacCatalyst/TrayService.cs”

public void Initialize()
    {
        statusBarObj = Runtime.GetNSObject(Class.GetHandle("NSStatusBar"));
        systemStatusBarObj = statusBarObj.PerformSelector(new Selector("systemStatusBar"));
        statusBarItem = Runtime.GetNSObject(IntPtr_objc_msgSend_nfloat(systemStatusBarObj.Handle, Selector.GetHandle("statusItemWithLength:"), -1));
        statusBarButton = Runtime.GetNSObject(IntPtr_objc_msgSend(statusBarItem.Handle, Selector.GetHandle("button")));
        statusBarImage = Runtime.GetNSObject(IntPtr_objc_msgSend(ObjCRuntime.Class.GetHandle("NSImage"), Selector.GetHandle("alloc")));

        var imgPath = System.IO.Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "Platforms", "MacCatalyst", "trayicon.png");
        var imageFileStr = NSString.CreateNative(imgPath);
        var nsImagePtr = IntPtr_objc_msgSend_IntPtr(statusBarImage.Handle, Selector.GetHandle("initWithContentsOfFile:"), imageFileStr);

        void_objc_msgSend_IntPtr(statusBarButton.Handle, Selector.GetHandle("setImage:"), statusBarImage.Handle);
        void_objc_msgSend_bool(nsImagePtr, Selector.GetHandle("setTemplate:"), true);

        // Handle click
        void_objc_msgSend_IntPtr(statusBarButton.Handle, Selector.GetHandle("setTarget:"), this.Handle);
        void_objc_msgSend_IntPtr(statusBarButton.Handle, Selector.GetHandle("setAction:"), new Selector("handleButtonClick:").Handle);
    }

I understand that this is realized in ↑.

I do not get a notification when I click. Also, how can I add a menu (show/hide, exit) to the icon? Are there any web documents that can help me? I searched the web for thousands of hours, but could find no answers. Am I the only one who finds it very difficult to handle native code in MacCatalyst?

The execution environment is

mac: intel cpu os: Ventura 13.6.1 account: admin user .net: .net8.0.201 maui(vscode): v0.10.61

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

macOS

Affected platform versions

MAC OS: Ventura 13.6.1

mauisansan avatar Oct 02 '24 09:10 mauisansan