Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > BotCompany Repo | #1031275 // dorkbox_installTrayIcon - takes tray name, image ID and menu item params [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 8310K of libraries. Click here for Pure Java version (5531L/35K).

!include once #1031274 // dorkbox SystemTray 

// trayName has to be unique for each tray you create per application (or globally?)
// Difference to installTrayIcon: menuItems can not be a PopupMenu
// first of menuItems can be a Runnable for left-click on icon
static dorkbox.systemTray.SystemTray dorkBox_installTrayIcon(S trayName, S imageID, S tooltip, O... menuItems) {
  // convenience parameter swap
  if (!isSnippetID(imageID) && isSnippetID(tooltip)) {
    S s = tooltip;
    tooltip = imageID;
    imageID = s;
  }
  
  ret dorkBox_installTrayIcon(trayName, imageIcon(imageID).getImage(), tooltip, menuItems);
}

static dorkbox.systemTray.SystemTray dorkBox_installTrayIcon(S trayName, Image image, S tooltip, O... _menuItems) null on exception {
  ret swing(() -> {
    Runnable leftClick = null;
    O[] menuItems = params_unpackList(_menuItems);
    if (first(menuItems) instanceof Runnable) {
      leftClick = (Runnable) menuItems[0];
      menuItems = dropFirst(menuItems);
    }
    
    dorkbox.systemTray.SystemTray systemTray = dorkbox.systemTray.SystemTray.get(trayName);
    systemTray.setImage(image);
    
    systemTray.setStatus(tooltip);
  
    // TODO if (leftClick != null) onLeftClick(trayIcon, _leftClick);

    var m = systemTray.getMenu();
    var x = menuItems;
    
    for i over x: {
      O o = x[i];
      if (eqOneOf(o, "***", "---", "===", ""))
        m.add(new dorkbox.systemTray.Separator());
      else if (o instanceof S && _get(x, i+1) instanceof Runnable)
        m.add(new dorkbox.systemTray.MenuItem((S) o, actionListener(get(x, ++i))));
      /*else if (o instanceof S || o instanceof Menu || o instanceof MenuItem)
        call(m, "add", o);*/
      else
        print("Unknown menu item: " + o);
    }

    ret systemTray;
  });
}

Author comment

Began life as a copy of #1003656

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031275
Snippet name: dorkbox_installTrayIcon - takes tray name, image ID and menu item params [dev.]
Eternal ID of this version: #1031275/9
Text MD5: 76cfe3200f156f8b6365b3a8cfe30e9c
Transpilation MD5: 3247493a2556da2e62712409bee93406
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-31 13:03:47
Source code size: 1832 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 164 / 240
Version history: 8 change(s)
Referenced in: [show references]