3

I am attempting to create an application in MonoMac/Xamarin.Mac that does not have a dock icon, nor a visible window when it launches and only an icon in the top-right menu bar.

I have set LSUIElement = 1 (tried both String and Boolean types) in Info.plist, but the status menu icon isn't displayed at all when the application launches. The only way I can get it to appear is by removing the LSUIElement flag, although then the dock icon becomes visible.

The snippet I am using is:

public partial class AppDelegate : NSApplicationDelegate
{
    public AppDelegate ()
    {
    }

    public override void FinishedLaunching (NSObject notification)
    {
        // Construct menu that will be displayed when tray icon is clicked
        var notifyMenu = new NSMenu();
        var exitMenuItem = new NSMenuItem("Quit", 
                                          (a,b) => { System.Environment.Exit(0); }); // Just add 'Quit' command
        notifyMenu.AddItem(exitMenuItem);

        // Display tray icon in upper-right-hand corner of the screen
        var sItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
        sItem.Menu = notifyMenu;
        sItem.Image = NSImage.FromStream(System.IO.File.OpenRead(
            NSBundle.MainBundle.ResourcePath + @"/menu_connected.png"));
        sItem.HighlightMode = true;

        // Remove the system tray icon from upper-right hand corner of the screen
        // (works without adjusting the LSUIElement setting in Info.plist)
        NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Accessory;
    }
}

Does anyone know of a good way of creating a windowless application in MonoMac that doesn't have a dock icon and only a menu bar icon?

Thanks,

BB

BB1
  • 304
  • 2
  • 8
  • Do you happen to still have the solution that @TheNextman provided? I am desperately looking for a way to accomplish this. – vargonian Apr 13 '14 at 05:11

1 Answers1

6

Try specifying a .xib for the "Main nib file name'

I did this some time ago, and it works fine for me. Something like this:

  • New monomac project
  • Created an 'AppController' class in C#:

    [Register("AppController")]
    public partial class AppController : NSObject
    {
        public AppController()
        {
    
        }
    
        public override void AwakeFromNib()
        {
            var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
            statusItem.Menu = statusMenu;
            statusItem.Image = NSImage.ImageNamed("f3bfd_Untitled-thumb");
            statusItem.HighlightMode = true;
        }
    
  • In MainMenu.xib, I deleted the application menu

  • In MainMenu.xib, I added an custom object and set it's type to AppController
  • I created my status menu in the .xib and then connected it to the AppController with an outlet
  • In info.plist, I add the "Application is agent (UIElement)" value as a string and set it to "1"

Try it out with a .xib. If it doesn't work, maybe I can share my project for you to take apart.

TheNextman
  • 12,428
  • 2
  • 36
  • 75
  • I'd love to seean example if you wouldn't mind sharing. There's not much on thsi subject online and I'm struggling with the same. Thanks! – Eli Gassert Jul 16 '13 at 20:55
  • 1
    @EliGassert Try this: pastelink.me/dl/395e5a#sthash.iEGMVNju.dpuf – TheNextman Jul 17 '13 at 10:37
  • @TheNextman your test solution works great, thank you very much! – BB1 Jul 19 '13 at 22:42
  • @TheNextman Your pastelink isn't working anymore. Still have that somewhere? I'd love to use it as a go-by if possible. – robertmiles3 Oct 10 '13 at 18:22
  • It appears that the new link is no longer working as well. Could we get a new one? This looks like exactly what I need. – vargonian Apr 08 '14 at 14:55
  • @TheNextman While your answer is greatly appreciated and seems to represent a good start, it is lacking crucial information. When you say you added a "custom object" to MainMenu.xib, what does this mean exactly? From the object library in Xcode (the area where I can select UI controls to add), the closest I see is a type called "Object" represented by a blue cube. Is this what you mean? After adding this, I tried setting its "Type" via the "Identity and Type" group in the pane in the top right of the Xcode window, but there is no "AppController" type. – vargonian Apr 12 '14 at 18:17
  • 1
    The AppController type is registered in the `Register` declaration at the top of the class in the example. It probably won't show in the drop down list but you can just type it in. – TheNextman Apr 13 '14 at 01:42
  • 1
    Sorry but I don't have the sample project anymore to post – TheNextman Apr 13 '14 at 01:43
  • @TheNextman Still, a few questions: 1) What does it mean to Delete the application menu? This is in Xcode, I assume? Do you delete the entire "Main Menu" object or just the menu item with the application's name (i.e. the furthest left one, next to the File menu)? 2) When you "added a custom object" to MainMenu.xib, what type of object *specifically* is this (i.e. from the Object Library), and is this also in Xcode Interface Builder? 3) How do you "set its type to AppController"? I see a "Type" dropdown in Xcode with lots of values, but "AppController" is not one of these values. – vargonian Apr 13 '14 at 04:40
  • @TheNextman 4) How do you create a "status menu in the xib"? Again, is this from Xcode Interface Builder? I look at all the options in the Object Browser, and I don't see one called "Status Menu". Thank you very much for your answers. I have a project whose core is working very well but figuring this Mac/Xamarin UI stuff out is my last bottleneck. – vargonian Apr 13 '14 at 05:11
  • Please @TheNextman. I've been struggling with this for weeks, and I know there's just a tiny piece that I'm missing that you could trivially answer. – vargonian Apr 23 '14 at 19:49
  • @EliGassert as you can see from my frantic comments, I've been spending weeks desperately trying to find this solution that TheNextman could probably answer in 60 seconds. Do you happen to have the sample project that he provided? For some reason, *both* of his links are no longer valid. – vargonian Apr 24 '14 at 00:25
  • 2
    I created a public gist with the content of all the files. https://gist.github.com/grexican/11252656 Also, here's the file as a .zip that I'll keep in a public share of mine so it won't go stale like the other download links: https://letscrate.com/f/toadsoftware/public/MacStatusMenuExample.zip – Eli Gassert Apr 24 '14 at 12:24
  • @EliGassert Thank you so much, this is exactly what I needed! I do have a confession, though. Apparently my solution was working just fine, but I couldn't tell because the Xamarin app menu is so long that it hid the status item (so I thought it was not displaying; I'm on a 13" MacBook Pro so space is limited). I found this out because the solution you provided had the same effect, until I clicked outside of Xamarin's window. In any case, it most certainly answered the question and I appreciate it immensely! – vargonian Apr 24 '14 at 13:38