uses
  uCPUMem;

var
  tmr:TTimer;
  imgpbbgs,imgpbs,lblCores,charts;
  MiniMode,ShowChart:boolean;

//打开任务管理器
procedure OpenTaskManager(sender:TObject);
begin
  s:=GetSystemDir+'\taskmgr.exe';
  ShellExecute(0,'open',s,'','',1);
end;

procedure ChangeMiniMode(value:boolean);
var
  i:integer;
begin
  //if MiniMode=value then exit;
  MiniMode:=value;
  if MiniMode then
    SetSettingInt('MiniMode',1)
  else
    SetSettingInt('MiniMode',0);
  BeginUpdate;
  for i:=0 to cpuCount-1 do
  begin
    imgpbbgs[i].visible:=not ShowChart;
    imgpbs[i].visible:=not ShowChart;
    lblCores[i].visible:=not ShowChart;
    charts[i].visible:=ShowChart;
  end;
  imgpbbgRAM.visible:=not ShowChart;
  imgpbRAM.visible:=not ShowChart;
  chartRAM.visible:=ShowChart;
  lblCPUHz.visible:=not ShowChart;
  lblCores[0].FontStyle.ShadowStyle.Enabled:=ShowChart;
  lblRAM.FontStyle.ShadowStyle.Enabled:=ShowChart;
  
  if ShowChart then
  begin
    lblCores[0].visible:=true;
    imgbg.height:=43+12;
    lblCores[0].Top:=4;
    lblRAM.top:=4;
    lblRAM.left:=95;
    if MiniMode then
    begin
      for i:=1 to cpuCount-1 do
        charts[i].visible:=false;
      charts[0].width:=90;
      charts[0].BringFront;
    end
    else
    begin
      charts[0].width:=round(90/cpuCount);
    end;
    lblCore1.BringFront;
  end
  else
  begin
    if cpuCount>1 then
      for i:=1 to cpuCount-1 do
      begin
        lblCores[i].visible:=not MiniMode;
        imgpbbgs[i].visible:=not MiniMode;
        imgpbs[i].visible:=not MiniMode;
      end;
    lblCores[0].top:=24;
    lblRAM.left:=8;
    lblRAM.caption:='RAM  ';
    if MiniMode then
    begin
      imgbg.height:=43+12
      lblRAM.top:=lblCore1.top+12;
      imgpbbgRAM.top:=imgpbbg1.top+12;
      lblCore1.caption:='CPU';
    end
    else
    begin
      imgbg.height:=43+12*cpuCount;
      lblRAM.top:=lblCore1.top+cpuCount*12;
      imgpbbgRAM.top:=imgpbbg1.top+cpuCount*12;
       lblCore1.caption:='Core1';
   end;
    imgpbRAM.top:=imgpbbgRAM.top;
  end;
  imgInfo.top:=imgbg.height-17;
  EndUpdate;
  Widget.Height:=imgbg.height;
  RefreshSidebarOrder;
  BeginUpdate;
  
  EndUpdateAll;
end;

procedure ChangeStyle(aStyle:string);
var
  aFillStyle:TxspFillStyle;
  i:integer;
begin
  case aStyle of
    'GradientFill': aFillStyle:=xsfGradientFill;
    'GradienCurve': aFillStyle:=xsfGradientCurve;
    'ColorFill':    aFillStyle:=xsfColorFill;
    'ColorCurve':   aFillStyle:=xsfColorCurve;
  else
    aFillStyle:=xsfGradientFill;
  end;
  for i:=0 to cpuCount-1 do
    charts[i].FillStyle:=aFillStyle;
  chartRAM.FillStyle:=aFillStyle;
  SetSetting('FillStyle',aStyle);  
end;

procedure SwitchStyle;
begin
  ShowChart:=not ShowChart;
  ChangeMiniMode(MiniMode);
  if ShowChart then
    SetSettingInt('ShowChart',1)
  else
    SetSettingInt('ShowChart',0);
end;

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

procedure OntmrTimer(sender);
var
  i,j:Int64;
  s:string;
begin
  BeginUpdate;
  if cpuCount>0 then
  try
    if ShowChart then
    begin
      lblCores[0].caption:='CPU:'+inttostr(system.Machine.cpus.usagePercentage)+'%'
      if MiniMode then
      begin
        chart1.AddItem(system.Machine.cpus.usagePercentage);
      end
      else
      begin
        for i:=0 to cpuCount-1 do
        begin
          j:=system.Machine.cpus.items[i].usagePercentage;
          charts[i].AddItem(j);
          charts[i].hint:='核心'++inttostr(i+1)+' : '+inttostr(j)+'%';
        end;  
      end;
    end
    else
    begin
      if MiniMode then
      begin
        j:=system.Machine.cpus.items[i].usagePercentage;
      	lblCores[0].caption:='CPU       '+inttostr(j)+'%'
        imgpbs[0].width:=round(j/2);
        imgpbs[0].FileName:=GetPercentName(j);
      end
      else
      for i:=0 to cpuCount-1 do
      begin
        j:=system.Machine.cpus.items[i].usagePercentage;
      	lblCores[i].caption:='Core'+inttostr(i+1)+'   '+inttostr(j)+'%';
        imgpbs[i].width:=round(j/2);
        imgpbs[i].FileName:=GetPercentName(j);
      end;
    end;  
  except

  end;
  //memory
  if TotalMem<=0 then exit;
  i:=round((TotalMem-system.Machine.availableMemory)*100/TotalMem); // 100-GetPhysMemoryPercent;
  if i<10 then
	  s:='0'+inttostr(i)+'%'
	else
	  s:=inttostr(i)+'%';
  if ShowChart then
  begin
  	chartRAM.AddItem(i);
  	lblRAM.caption:='RAM'+#13#10+s;
  end
  else
  begin
    imgpbRAM.width:=round(i/2);
    imgpbRAM.FileName:=GetPercentName(i);
  	s:='RAM     '+s;  
  	lblRAM.caption:=s;
  end;
  EndUpdate;
end;

procedure InitIni;
var
  i:integer;
  aFillStyle:TxspFillStyle;
  s:string;
begin
  MiniMode:=GetSettingInt('MiniMode',0)=1;
  ShowChart:=GetSettingInt('ShowChart',1)=1;

  s:=GetSetting('FillStyle','GradientFill');
  case s of
    'GradienCurve': begin aFillStyle:=xsfGradientCurve; nChartStyle_GradienCurve.checked:=true; end;
    'ColorFill':    begin aFillStyle:=xsfColorFill; nChartStyle_ColorFill.checked:=true; end;
    'ColorCurve':   begin aFillStyle:=xsfColorCurve; nChartStyle_ColorCurve.checked:=true; end;
  else
    aFillStyle:=xsfGradientFill;
  end;
  for i:=0 to cpuCount-1 do
    charts[i].FillStyle:=aFillStyle;
  chartRAM.FillStyle:=aFillStyle;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  lst:TStrings;
  s:string;
  i:integer;
  r:Real;
begin
  MiniMode:=true;
  ShowChart:=true;
  imgpbbgs:=[imgpbbg1,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  imgpbs:=[imgpb1,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  lblCores:=[lblCore1,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  charts:=[chart1,nil,nil,nil,nil,nil,nil,nil,nil,nil];

  StartCPUMEMWatch;

  BeginUpdate;

  chart1.penStyle.Color:=13540738;
  chart1.penStyle.Opacity:=128;
  chart1.bgPatternColor:=13540738;
  chart1.bgOpacity:=26;
  chart1.bgStyle:=xhsSmallGrid;
  chart1.GradientStyle.ColorBegin:=15653950;
  chart1.GradientStyle.ColorEnd:=$00FFFF;
  chart1.GradientStyle.Opacity:=128;
  chart1.GradientStyle.OpacityEnd:=255;
  
  chartRAM.penStyle.Color:=13540738;
  chartRAM.penStyle.Opacity:=128;
  chartRAM.bgPatternColor:=13540738;
  chartRAM.bgOpacity:=26;
  chartRAM.bgStyle:=xhsSmallGrid;
  chartRAM.GradientStyle.ColorBegin:=15653950;
  chartRAM.GradientStyle.ColorEnd:=$00FFFF;
  chartRAM.GradientStyle.Opacity:=128;
  chartRAM.GradientStyle.OpacityEnd:=255;
  
  if cpuCount>10 then cpuCount:=10;
  lblCPUHz.caption:=inttostr(cpucount)+' '+system.Machine.CPUs.processorArchitecture;

  r:=90/cpuCount;
  charts[0].width:=round(r);
  if cpuCount>1 then
  begin
    for i:=1 to cpuCount-1 do
    begin
      imgpbbgs[i]:=TxspImage.Create(Widget);
      imgpbbgs[i].parent:=Widget;
      imgpbbgs[i].left:=imgpbbg1.left;
      imgpbbgs[i].top:=imgpbbg1.top+i*12;
      imgpbbgs[i].Stretch:=true;
      imgpbbgs[i].height:=6;
      imgpbbgs[i].width:=50;
      imgpbbgs[i].FileName:=WidgetPath+'images\bar0.png';
      SetAllowMoveWidget(imgpbbgs[i],true);

      imgpbs[i]:=TxspImage.Create(Widget);
      imgpbs[i].parent:=Widget;
      imgpbs[i].left:=imgpb1.left;
      imgpbs[i].top:=imgpb1.top+i*12;
      imgpbs[i].Stretch:=true;
      imgpbs[i].width:=0;
      imgpbs[i].height:=6;
      imgpbs[i].FileName:=WidgetPath+'images\bar1.png';
      imgpbs[i].autosize:=false;
      SetAllowMoveWidget(imgpbs[i],true);
      
      lblCores[i]:=TxspLabel.Create(Widget);
      lblCores[i].parent:=Widget;
      lblCores[i].left:=lblCore1.left;
      lblCores[i].top:=lblCore1.top+i*12;
      lblCores[i].caption:='Core'+inttostr(i+1);
      SetAllowMoveWidget(lblCores[i],true);
      
      charts[i]:=TxspChart.Create(Widget);
      charts[i].parent:=Widget;
      charts[i].visible:=ShowChart;
      charts[i].left:=charts[i-1].left+charts[i-1].width-1;
      charts[i].top:=3;
      if i<>cpuCount-1 then
        charts[i].width:=round(r)
      else
        charts[i].width:=90-charts[i].left+3;
      charts[i].height:=chart1.height;
      charts[i].penStyle.Color:=chart1.penStyle.Color;
      charts[i].penStyle.Opacity:=chart1.penStyle.Opacity;
      charts[i].bgPatternColor:=chart1.bgPatternColor;
      charts[i].bgOpacity:=chart1.bgOpacity;
      charts[i].bgStyle:=chart1.bgStyle;
      charts[i].GradientStyle.ColorBegin:=chart1.GradientStyle.ColorBegin;
      charts[i].GradientStyle.ColorEnd:=chart1.GradientStyle.ColorEnd;
      charts[i].GradientStyle.Opacity:=chart1.GradientStyle.Opacity;
      charts[i].GradientStyle.OpacityEnd:=chart1.GradientStyle.OpacityEnd;
      SetAllowMoveWidget(charts[i],true);
    end;
    lblRAM.top:=lblCore1.top+cpuCount*12;
    imgpbbgRAM.top:=imgpbbg1.top+cpuCount*12;
    imgpbRAM.top:=imgpbbgRAM.top;
  end;
  chartRAM.OnDblClick:='OpenTaskManager';
  for i:=0 to cpuCount-1 do
  begin
    charts[i].OnDblClick:='OpenTaskManager';
    lblCores[i].OnDblClick:='OpenTaskManager';
    imgpbs[i].OnDblClick:='OpenTaskManager';
    imgpbbgs[i].OnDblClick:='OpenTaskManager';
  end;
  lblCPUHz.caption:=inttostr(cpucount)+system.Machine.CPUs.processorArchitecture; //'x86';
  lblCores[0].BringFront;
  //imgbg.height:=43+12*cpuCount;
  //imgInfo.top:=imgbg.height-17;

  InitIni;

  ChangeMiniMode(MiniMode);
  EndUpdate;
  Widget.Height:=imgbg.height;


  tmr:=TTimer.Create(Widget);
  //tmr.Enabled:=false;
  tmr.Interval:=1000;
  tmr.OnTimer:='OntmrTimer';

  AutoAdjustMem:=0;
  RefreshSidebarOrder;
end;

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

//--- Widget事件，当打开鼠标移到Widget区域内时触发此事件 ---
procedure OnWidgetMouseEnter;
begin
  BeginUpdate;
  imgInfo.visible:=true;
  EndUpdate;
end;

//--- Widget事件，当打开鼠标移出Widget区域时触发此事件 ---
procedure OnWidgetMouseLeave;
begin
  BeginUpdate;
  imgInfo.visible:=false;
  EndUpdate;
end;