const
  dialOffset = -125;
  intRotateStep = 5;
  intNoAnimThresh = 4;
  
var
  tmr,tmrcpu:TTimer;
  //DataFileName:string;
  Angle:integer;
  Animate:boolean;
  skin:string;
  TotalMem:integer;

procedure OntmrcpuTimer(sender)
var
  i:integer;
begin
  if imgCpu.ImageStyle.Angle>Angle then
  begin
    i:=imgCpu.ImageStyle.Angle-intRotateStep;
    if i<=Angle then 
    begin
    	imgCpu.ImageStyle.Angle:=Angle;
    	tmrcpu.Enabled:=false;
    end
    else
      imgCpu.ImageStyle.Angle:=i;
  end
  else
  begin
    i:=imgCpu.ImageStyle.Angle+intRotateStep;
    if i>=Angle then 
    begin
    	imgCpu.ImageStyle.Angle:=Angle;
    	tmrcpu.Enabled:=false;
    end
    else
      imgCpu.ImageStyle.Angle:=i;
  end;
end;

//
procedure OntmrTimer(sender)
var
  i:integer;
  FreeMem:Int64;
  s:string;
begin
  BeginUpdate;
  i:=system.Machine.cpus.usagePercentage;  
  if i<10 then
	  s:='0'+inttostr(i)+'%'
	else
	  s:=inttostr(i)+'%';
  if s<>lblcpu.caption then
  begin
  	lblcpu.caption:=s;
  	i:=round(i*2.5)+dialOffset;
  	if not Animate or (abs(i-imgCpu.ImageStyle.Angle)<=intNoAnimThresh) then
  		imgCpu.ImageStyle.Angle:=i
  	else
  	begin
  	  Angle:=i;
  	  if abs(i-imgCpu.ImageStyle.Angle)<=60 then
  	    tmrcpu.interval:=100
  	  else
  	    tmrcpu.interval:=80;
  	  tmrcpu.Enabled:=true;
  	end;
  end;

  if TotalMem> 0  then 
	begin
		FreeMem:=system.Machine.availableMemory;
	  i:=round((TotalMem-FreeMem)*100/TotalMem); 
	  if i<10 then
  	  s:='0'+inttostr(i)+'%'
  	else
  	  s:=inttostr(i)+'%';
		if s<>lblmem.caption then
    begin
    	lblmem.caption:=s;
    	imgmem.ImageStyle.Angle:=round(i*2.5)+dialOffset;
    end;
    imgglass.hint:=format('可用:%d MB(%d%s)'#13#10'总内存:%d MB'#13#10#13#10'(双击打开任务管理器)',[FreeMem,100-i,'%',TotalMem]);			
	end;
	
  EndUpdate;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=132;
  Widget.Height:=103;
  imgbg.FileName:=WidgetPath+skin+'\back.png';
  imgCpu.FileName:=WidgetPath+skin+'\dial.png';
  imgcpu.left:=10;  imgcpu.top:=20;
  imgCpu.width:=72; imgcpu.height:=72;
  imgMem.FileName:=WidgetPath+skin+'\dial_sml.png';
  imgMem.left:=71;  imgMem.top:=8;
  imgMem.width:=50; imgMem.height:=50;
  imgdot.FileName:=WidgetPath+skin+'\dialdot.png';
  imgglass.FileName:=WidgetPath+skin+'\glass.png';
  lblcpu.left:=30;  lblcpu.top:=77;
  lblmem.left:=83;  lblmem.top:=43;
  lblcpu.fontstyle.font.size:=7;
  lblmem.fontstyle.font.size:=lblcpu.fontstyle.font.size;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.Width:=202;
  Widget.Height:=159;
  imgbg.FileName:=WidgetPath+skin+'\back_lrg.png';
  imgCpu.FileName:=WidgetPath+skin+'\dial_lrg.png';
  imgcpu.left:=19;  imgcpu.top:=32;
  imgCpu.width:=98; imgcpu.height:=98;
  imgMem.FileName:=WidgetPath+skin+'\dial_lrg_sml.png';
  imgMem.left:=108;  imgMem.top:=13;
  imgMem.width:=70; imgMem.height:=70;
  imgdot.FileName:=WidgetPath+skin+'\dialdot_lrg.png';
  imgglass.FileName:=WidgetPath+skin+'\glass_lrg.png';
  lblcpu.left:=55;  lblcpu.top:=108;
  lblmem.left:=128;  lblmem.top:=66;
  lblcpu.fontstyle.font.size:=8;
  lblmem.fontstyle.font.size:=lblcpu.fontstyle.font.size;
  EndUpdate;
end;

procedure ChangeSkin(value:string);
begin
  //if value=skin then exit;
  if trim(value)<>'' then skin:=value
  else
    case skin of
      'default':  skin:='black';
      'black':    skin:='car meter';
      'car meter':skin:='white';
      'white':    skin:='default';
    end;
  if Docking then 
    OnWidgetDock
  else
    OnWidgetUnDock;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  skin:=GetSetting('bg','default');
  Angle:=dialOffset;
  //Animate:=false;
  Animate:=GetSettingInt('Smooth',1)=1;

  BeginUpdate;
  imgCpu.ImageStyle.Angle:=dialOffset;
  imgMem.ImageStyle.Angle:=dialOffset;
  if skin<>'default' then
    ChangeSkin(skin);
  EndUpdate;

  tmr:=TTimer.Create(Widget);
  //tmr.Enabled:=false;
  tmr.Interval:=1000;
  tmr.OnTimer:='OntmrTimer';
	
	TotalMem:=system.Machine.totalMemory;
	system.Machine.Cpus.Active:=true;
	 
  tmrcpu:=TTimer.Create(Widget);
  tmrcpu.Enabled:=false;
  tmrcpu.Interval:=50;
  tmrcpu.OnTimer:='OntmrcpuTimer';

  AutoAdjustMem:=0;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
	 system.Machine.Cpus.Active:=false;
end;

procedure OpenTaskManager;
begin
  s:=GetSystemDir+'\taskmgr.exe';
  ShellExecute(0,'open',s,nil,nil,1);
end;

