Commit 06046923 authored by shrabbit's avatar shrabbit
Browse files

主界面、模板提示

parent 28513f53
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
+5 −2
Original line number Diff line number Diff line
namespace WatermarkClock
using System.Collections.Generic;

namespace WatermarkClock
{
    public interface ITextTemplate
    {
        string Replace(string input);
        IEnumerable<TemplateVariable> GetVariables();
    }
}
+87 −70
Original line number Diff line number Diff line
@@ -7,6 +7,22 @@
        mc:Ignorable="d"
        Title="WatermarkClock - shRabbit" Height="500" Width="520"
        ResizeMode="CanResize">
    <DockPanel>
        <!-- 菜单栏 -->
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="文件(_F)">
                <MenuItem Header="应用设置(_A)" Click="BtnApply_Click"/>
                <Separator/>
                <MenuItem Header="隐藏到托盘(_H)" Click="BtnHide_Click"/>
                <Separator/>
                <MenuItem Header="退出(_X)" Click="MenuExit_Click"/>
            </MenuItem>
            <MenuItem Header="帮助(_H)">
                <MenuItem Header="模板语法(_T)" Click="MenuTemplateSyntax_Click"/>
            </MenuItem>
        </Menu>

        <!-- 主内容 -->
        <Grid Margin="12">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
@@ -51,7 +67,7 @@
            </GroupBox>

            <!-- 模板列表 -->
        <GroupBox Header="模板列表(支持变量:{DatetimeF} {year} {month} {Day} {Hour} {Minute} {Second} {Week} 等)" Grid.Row="1" Margin="0,8,0,0" Padding="8">
            <GroupBox Header="模板列表" Grid.Row="1" Margin="0,8,0,0" Padding="8">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
@@ -87,4 +103,5 @@
                <Button Content="隐藏到托盘" Width="90" Click="BtnHide_Click"/>
            </StackPanel>
        </Grid>
    </DockPanel>
</Window>
+53 −7
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ namespace WatermarkClock

        private void ApplySettings()
        {
            // 解析设置值
            if (CbPosition.SelectedItem is MarkPosition pos)
                _ctx.MarkPosition = pos;

@@ -60,17 +59,13 @@ namespace WatermarkClock
            if (int.TryParse(TbRefreshInterval.Text, out var refreshMs) && refreshMs > 0)
                _ctx.RefreshInterval = refreshMs;

            // 创建/更新水印窗口
            if (_watermarkWindow == null)
            {
                _watermarkWindow = new WatermarkClockWindow(_ctx);
                _watermarkWindow.Show();
            }

            // 重启刷新定时器
            StartRefreshTimer();

            // 重启轮换定时器
            StartRotationTimer();
        }

@@ -91,13 +86,11 @@ namespace WatermarkClock

            if (_ctx.Templates.Count <= 1)
            {
                // 单模板直接渲染
                _currentTemplateIndex = 0;
                RenderCurrentTemplate();
                return;
            }

            // 先渲染当前模板
            RenderCurrentTemplate();
            ScheduleNextRotation();
        }
@@ -193,6 +186,59 @@ namespace WatermarkClock
            Hide();
        }

        private void MenuExit_Click(object sender, RoutedEventArgs e)
        {
            _notifyIcon?.Dispose();
            Application.Current.Shutdown();
        }

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

            var dlg = new Window
            {
                Title = "模板语法",
                Width = 450,
                Height = 400,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Owner = this,
                ResizeMode = ResizeMode.CanResize
            };

            var grid = new System.Windows.Controls.Grid();
            var dg = new System.Windows.Controls.DataGrid
            {
                AutoGenerateColumns = false,
                IsReadOnly = true,
                HeadersVisibility = System.Windows.Controls.DataGridHeadersVisibility.Column,
                ItemsSource = variables
            };

            var colName = new System.Windows.Controls.DataGridTextColumn
            {
                Header = "变量",
                Width = new System.Windows.Controls.DataGridLength(1, System.Windows.Controls.DataGridLengthUnitType.Star),
                Binding = new System.Windows.Data.Binding("Name")
            };
            var colDesc = new System.Windows.Controls.DataGridTextColumn
            {
                Header = "说明",
                Width = new System.Windows.Controls.DataGridLength(2, System.Windows.Controls.DataGridLengthUnitType.Star),
                Binding = new System.Windows.Data.Binding("Description")
            };

            dg.Columns.Add(colName);
            dg.Columns.Add(colDesc);
            grid.Children.Add(dg);
            dlg.Content = grid;
            dlg.ShowDialog();
        }

        protected override void OnClosed(EventArgs e)
        {
            _refreshTimer?.Stop();
+10 −2
Original line number Diff line number Diff line
namespace WatermarkClock.Templates
using System.Collections.Generic;

namespace WatermarkClock.Templates
{
    public class BaseFunc : ITextTemplate
    {
        public string Replace(string input) => input
            .Replace("{{", "{")
            .Replace("}}", "}");

        public IEnumerable<TemplateVariable> GetVariables()
        {
            yield return new TemplateVariable("{{", "转义输出 { 字符");
            yield return new TemplateVariable("}}", "转义输出 } 字符");
        }
    }
}
Loading