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

63
LINES

< > BotCompany Repo | #1026796 - Combined Process+System CPU % Host Tray Icon

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 795K of libraries. Click here for Pure Java version (7713L/40K).

!7

cmodule CombinedCPUTrayIcon > DynBigNumber {
  transient BufferedImage trayImage;
  transient TrayIcon trayIcon; 
  
  switchable S processColor = "ADFF2F"; // green yellow
  switchable S restColor = "6666FF";
  switchable S backgroundColor = "000000";
  switchable int size = goodSystemTrayIconSize().x;
  switchable double interval = 1;
  switchable bool postToServer;
  
  transient double systemLoad;
  transient int systemPercent;
  
  // these are for the process
  transient double loadRelativeToAllCores, recentLoad;
  transient RollingAverage avg = new(5); // =10 seconds
  
  start {
    setDescription("STEFAN'S OS/SYSTEM CPU %");
    setToolTip("Percentage is per core (total: " + numberOfCores()*100 + "%)");
    doEvery(interval, r actualUpdate);
    ownResource(tempTrayIcon(trayIcon = installTrayIcon(
      trayImage = newBufferedImage(size, size, colorFromHex(backgroundColor)),
      "Stefan's OS + System CPU",
      rThread dm_activateOSAndModule)));
    dm_registerAs_direct combinedCPUTrayIcon();
  }
  
  void grabSystemLoad {
    systemLoad = systemCPULoad();
    systemPercent = toIntPercent_ceil(systemLoad*numberOfCores());
    
    if (postToServer)
      dm_postCPULoadToServer(systemPercent);
  }
  
  void actualUpdate {
    grabSystemLoad();
    recentLoad = processCPULoad();
    avg.add(recentLoad);
    loadRelativeToAllCores = avg!;
    setValue(
      toIntPercent_ceil(loadRelativeToAllCores*numberOfCores()) + " / " +
      systemPercent + " %");

    int w = trayImage.getWidth(), h = trayImage.getHeight();
    copyImagePart(trayImage, 1, 0, trayImage, 0, 0, w-1, h);
    int y1 = iround(h*(1 - loadRelativeToAllCores));
    int y2 = min(y1, iround(h*(1 - systemLoad)));
    fillRect(trayImage, w-1, 0, 1, y2, colorFromHex(backgroundColor));
    fillRect(trayImage, w-1, y2, 1, y1-y2, colorFromHex(restColor));
    fillRect(trayImage, w-1, y1, 1, h-y1, colorFromHex(processColor));
    setTrayIconImage(trayIcon, trayImage);
    vmBus_send newCombinedCPUTrayImage(module(), trayImage);
  }
  
  // API
  
  BufferedImage getImage() { ret trayImage; }
}

Author comment

Began life as a copy of #1020927

download  show line numbers  debug dex   

Travelled to 3 computer(s): mqqgnosmbjvj, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026796
Snippet name: Combined Process+System CPU % Host Tray Icon
Eternal ID of this version: #1026796/12
Text MD5: e0810b7c40dd7b7dc8911b09c4661bef
Transpilation MD5: 0009ac653411f892b5fd3f34ee5df607
Author: stefan
Category: javax / modules
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-11 14:44:08
Source code size: 2160 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 28 / 1794
Version history: 11 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1026796 & http://1026796.tinybrain.de