1

I writed a code in delphi to insert a status item to an system status bar, the code works good and status item displayed. I used an transparent icon and I should click exactly on icon (not icon rect) to open menu. But if I click on transparent area of icon, nothing happens! I write same code by XCode and every thing is ok.

Is any body has idea for this case? I want to open menu by click on any point of Status Icon (colored and transparent points).

the code:

procedure createStatusItem;
var
    app_delg: AppDelegate;
    statusBar: NSStatusBar;
    statusItem: NSStatusItem;
    menu: NSMenu;
    pImage: Pointer;
    nsImage: NSImage;
begin
    app_delg := TAppDelegate.Create;
    TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication()).setDelegate(AppDelegate(app_delg));
    statusBar := TNSStatusBar.Wrap(TNSStatusBar.OCClass.systemStatusBar);
    menu := TNSMenu.Wrap(TNSMenu.Alloc.initWithTitle(NSSTR('')));
    statusItem := statusBar.statusItemWithLength(NSVariableStatusItemLength);
    statusItem.retain;
    statusItem.setHighlightMode(true);
    statusItem.setAction(sel_getUid('onMenuClicked:'));
    statusItem.setMenu(menu);

    pImage := TNSImage.Alloc.initWithContentsOfFile(NSSTR('icon.ico'));
    nsImage := TNSImage.Wrap(pImage);
    statusItem.setImage(nsImage);
    nsImage.release;
end;

Thanks.

mh taqia
  • 3,506
  • 1
  • 24
  • 35

1 Answers1

0

When you set menu property, the action is not fired. So remove the setmenu line. Then you need a target. Create a delegate for it and set it as a target.

type
  IStatusBarDelegate = interface(NSObject)
    ['{5D8BF7A3-C695-45F8-A447-948524718E5F}']
    procedure StatusbarItemClick; cdecl;
  end;

  TStatusbarDelegate = class(TOCLocal)
  protected
    { TOCLocal }
    function GetObjectiveCClass: PTypeInfo; override;
  public
    procedure StatusbarItemClick; cdecl;
  end;

implementation

function TStatusbarDelegate.GetObjectiveCClass: PTypeInfo;
begin
  Result := TypeInfo(IStatusBarDelegate);
end;

procedure TStatusbarDelegate.StatusbarItemClick;
begin
  Showmessage('click');
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  statusBar: NSStatusBar;
  statusItem: NSStatusItem;
  statusBarDelegate : TStatusbarDelegate;
begin
  StatusBarDelegate := TStatusbarDelegate.Create;
  StatusBar := TNSStatusBar.Wrap(TNSStatusBar.OCClass.systemStatusBar);
  StatusBarItem := FStatusBar.statusItemWithLength(NSVariableStatusItemLength);
  StatusBarItem.retain;
  Statusbaritem.setHighlightMode(True);
  Statusbaritem.setTitle(StrToNSStr('test'));
  Statusbaritem.setEnabled(true);
  Statusbaritem.setToolTip(StrToNSStr('test tooltip'));
  Statusbaritem.setTarget(FStatusBarDelegate.GetObjectID);
  Statusbaritem.setAction(sel_getUid('StatusbarItemClick'));
 end;
Kick
  • 1
  • Thankyou, but It is not my answer, 1) your solution has not anything about graphical icon .2) It seems the problem is related to an osx bug! because my statusbar icon works as expected in El Capitan. – mh taqia Jan 28 '16 at 10:21