Commit de01dcf2 authored by shrabbit's avatar shrabbit
Browse files

Add HwInfo template for CPU usage and integrate into TextTemplateEngine

parent bbeeca8a
Loading
Loading
Loading
Loading
+7 −10
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ namespace WatermarkClock
    public partial class MainWindow
    {
        private readonly Context _ctx;
        private readonly TextTemplateEngine _engine;
        private WatermarkClockWindow _watermarkWindow;
        private DispatcherTimer _refreshTimer;
        private DispatcherTimer _rotationTimer;
@@ -24,6 +25,10 @@ namespace WatermarkClock
            InitializeComponent();

            _ctx = new Context();
            _engine = new TextTemplateEngine
            {
                Templates = { new BaseTime(), new BaseFunc(), new HwInfo() }
            };
            InitComboBox();

            // 从命令行参数或默认路径加载配置
@@ -177,11 +182,7 @@ namespace WatermarkClock
            }

            var template = _ctx.Templates[_currentTemplateIndex].Text;
            var engine = new TextTemplateEngine
            {
                Templates = { new BaseTime(), new BaseFunc() }
            };
            _ctx.Output = engine.Replace(template);
            _ctx.Output = _engine.Replace(template);
        }

        private void BtnApply_Click(object sender, RoutedEventArgs e)
@@ -247,11 +248,7 @@ namespace WatermarkClock

        private void MenuTemplateSyntax_Click(object sender, RoutedEventArgs e)
        {
            var engine = new TextTemplateEngine
            {
                Templates = { new BaseTime(), new BaseFunc() }
            };
            var variables = engine.GetVariables().ToList();
            var variables = _engine.GetVariables().ToList();

            var dlg = new Window
            {
+48 −0
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace WatermarkClock.Templates
{
    public class HwInfo : ITextTemplate
    {
        private PerformanceCounter _systemCpuCounter;
        private bool _run;
        private DateTime _lastGet;
        private float _lastValue;
        
        public string Replace(string input)
        {
            return input.Replace("{cpu}", GetCpuUsage());
        }

        private string GetCpuUsage()
        {
            if ((DateTime.Now - _lastGet).TotalSeconds > 1)
            {
                PerformanceCounter counter;

                try
                {
                    counter = _systemCpuCounter = _systemCpuCounter ??
                              new PerformanceCounter("Processor Information", "% Processor Utility", "_Total");
                }
                catch (Exception e)
                {
                    counter = _systemCpuCounter = _systemCpuCounter ??
                              new PerformanceCounter("Processor", "% Processor Time", "_Total");
                }

                _lastGet = DateTime.Now;
                return (_lastValue = counter.NextValue()).ToString("F1");
            }
            
            return _lastValue.ToString("F1");
        }

        public IEnumerable<TemplateVariable> GetVariables()
        {
            yield return new TemplateVariable("{cpu}", "中央处理器使用率");
        }
    }
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@
        <Compile Include="MarkPosition.cs" />
        <Compile Include="NotifyIconWrapper.cs" />
        <Compile Include="TemplateItem.cs" />
        <Compile Include="Templates\HwInfo.cs" />
        <Compile Include="TemplateVariable.cs" />
        <Compile Include="Templates\BaseFunc.cs" />
        <Compile Include="Templates\BaseTime.cs" />