const
  bgCount=46;
  
var
  tmr:TTimer;
  NetDevice:string;
  TotalSpeed:integer;
  iOldUp,iOldDown:integer;
  bgIndex:integer;

procedure Changebg;
begin
  bgIndex:=(bgIndex mod bgCount)+1;
  BeginUpdate;
  imgbg.FileName:=WidgetPath+format('images\bg%d.png',[bgIndex]);
  EndUpdate;
  SetSetting('bgIndex',bgIndex);
end;

function GetPercentName(iPercent:integer):string;
begin
  if iPercent>90 then
    result:='red'
  else if iPercent>80 then
    result:='roux'
  else
    result:='blue';
  result:=WidgetPath+'images\'+result+'.png';  
end;

function GetSize(value:Int64):string;
var
  i:integer;
  f:double;
  s:string;
begin
  //计算剩余容量
  i:=0;
  f:=value/1024;
  while f>1024 do
  begin
    inc(i);
    f:=f/1024;
  end;
  case i of
    0: s:='KB';
    1: s:='MB';
    2: s:='GB';
    3: s:='TB';
  else
    s:='MB';
  end;
  result:=format('%.1f %s',[f,s]);
end;

procedure OntmrTimer(sender)
var
  iup,idown,i:integer;
begin
  BeginUpdate;
  //net
  iup:=0; idown:=0;
  idown:=System.Machine.Network.InPerSec;
  iup:=System.Machine.Network.OutPerSec;
  if (TotalSpeed<idown) and (idown<2048000) then
    TotalSpeed:=idown;
  
  chartNetUp.AddItem(round((iup+iOldUp)*50/TotalSpeed));
  iOldUp:=iup;
  i:=round((idown+iOldDown)/TotalSpeed*50);
  iOldDown:=idown;
  if i>100 then i:=100
    else if i<0 then i:=0;
  chartNetDown.AddItem(i);

  if (iup>=1024) or (iup=0) then
    lblup.caption:=format('Up:%d k/s',[round(iup/1024)])
  else
    lblup.caption:=format('Up:%.1f k/s',[iup/1024]);
    
  if (idown>=1024) or (idown=0) then
    lbldown.caption:='Down:'+inttostr(idown/1024)+' k/s'
  else
    lbldown.caption:=format('Down:%.1f k/s',[idown/1024]);

  //下载总量
  with System.Machine.Network do
  begin
    lbldown.hint:=format('--- 下载 ---'#13#10'累计下载 : %s'#13#10'平均下载速度: %s/每秒'#13#10'最高下载速度: %s/每秒',
        [GetSize(ActiveAdapter.InTotal),GetSize(ActiveAdapter.AverageInPerSec), GetSize(ActiveAdapter.PeakInPerSec)]);
    lblup.hint:=format('--- 上传 ---'#13#10'累计上传 :%s'#13#10'平均上传速度: %s/每秒'#13#10'最高上传速度: %s/每秒',
        [GetSize(ActiveAdapter.OutTotal),  GetSize(ActiveAdapter.AverageOutPerSec),GetSize(ActiveAdapter.PeakOutPerSec)]);
    chartNetDown.hint:=format('IP    地址 : %s'#13#10'网卡地址 : %s'#13#10'%s'#13#10'%s',
        [IP,MacAddress,lbldown.hint,lblup.hint]);
  end;    
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  lst:TStrings;
begin
  iOldUp:=0;
  iOldDown:=0;
  NetDevice:=GetSetting('NetDevice','');
  
  chartNetUp.penStyle.Color:=$68E591;
  chartNetUp.penStyle.Opacity:=128;
  chartNetUp.bgPatternColor:=13540738;
  chartNetUp.bgOpacity:=26;
  chartNetUp.bgStyle:=xhsSmallGrid;
  chartNetUp.BorderStyle.Opacity:=128;
  chartNetUp.GradientStyle.ColorBegin:=$6AE697; //$68E591; //#97E66A
  chartNetUp.GradientStyle.ColorEnd  :=$B6FFD3; //$00FFFF; //#D3FFB6
  chartNetUp.GradientStyle.Opacity:=196;
  chartNetUp.GradientStyle.OpacityEnd:=255;
  chartNetUp.FillStyle:=xsfGradientCurve; //xsfColorCurve;

  chartNetDown.penStyle.Color:=13540738;
  chartNetDown.penStyle.Opacity:=128;
  chartNetDown.bgPatternColor:=13540738;
  chartNetDown.bgPatternOpacity:=0;
  chartNetDown.bgOpacity:=0;
  chartNetDown.BorderStyle.Opacity:=0;
  chartNetDown.bgStyle:=xhsClear;
  chartNetDown.GradientStyle.ColorBegin:=$E6A15A; //#5AA1E6
  chartNetDown.GradientStyle.ColorEnd:=$F7D3A7; //#A7D3F7
  chartNetDown.GradientStyle.Opacity:=196;
  chartNetDown.GradientStyle.OpacityEnd:=255;
  chartNetDown.FillStyle:=xsfGradientCurve; //xsfColorCurve;

  with System.Machine.Network do
  if NetDevice='' then
  begin
    if AvailableAdapterList.count>1 then
    begin
      NetDevice:=ActiveAdapterName;
      showtrayhint('警告','您的电脑有多个网络设备，如果默认的没有网速，请到小工具设置窗口选择你使用的网络设备！');
      SetSetting('NetDevice',NetDevice);
    end;
  end
  else
    System.Machine.Network.ActiveAdapterName:=NetDevice;
  BeginUpdate;
  TotalSpeed:=GetSetting('TotalSpeed',102400);
  chartNetUp.MaxValue:=100; //TotalSpeed;
  chartNetDown.MaxValue:=100; //TotalSpeed;
  EndUpdate;

  System.Machine.Network.Active:=true;
  tmr:=TTimer.Create(Widget);
  tmr.Interval:=1000;
  tmr.OnTimer:='OntmrTimer';
  AutoAdjustMem:=0;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.width:=137;
  Widget.Height:=50;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.width:=145;
  Widget.Height:=60;
  EndUpdate;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
end;
