Commit 13c57fa6 authored by shrabbit's avatar shrabbit
Browse files

Add color picker and numeric updown controls, update settings application logic

parent b476e7e0
Loading
Loading
Loading
Loading
+15 −7
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WatermarkClock"
        xmlns:num="clr-namespace:NumericUpDownLib;assembly=NumericUpDownLib"
        mc:Ignorable="d"
        Title="WatermarkClock - shRabbit" Height="500" Width="520"
        ResizeMode="CanResize">
@@ -51,23 +52,30 @@
                              SelectedIndex="1"/>

                    <TextBlock Text="边距:" VerticalAlignment="Center" Grid.Row="1"/>
                    <TextBox x:Name="TbPadding" Grid.Row="1" Grid.Column="1" Margin="0,4" Text="16"/>
                    <num:UIntegerUpDown x:Name="NudPadding" Grid.Row="1" Grid.Column="1" Margin="0,4"
                                       Value="16" MinValue="0" MaxValue="200" StepSize="1"/>

                    <TextBlock Text="字体大小:" VerticalAlignment="Center" Grid.Row="2"/>
                    <TextBox x:Name="TbFontSize" Grid.Row="2" Grid.Column="1" Margin="0,4" Text="28"/>
                    <num:DoubleUpDown x:Name="NudFontSize" Grid.Row="2" Grid.Column="1" Margin="0,4"
                                      Value="28" MinValue="8" MaxValue="200" StepSize="1"/>

                    <TextBlock Text="字体:" VerticalAlignment="Center" Grid.Row="3"/>
                    <ComboBox x:Name="CbFontFamily" Grid.Row="3" Grid.Column="1" Margin="0,4"
                              IsEditable="True" IsTextSearchEnabled="True"/>

                    <TextBlock Text="文字颜色:" VerticalAlignment="Center" Grid.Row="4"/>
                    <TextBox x:Name="TbTextColor" Grid.Row="4" Grid.Column="1" Margin="0,4" Text="#FFFFFF"/>
                    <Button x:Name="BtnColor" Grid.Row="4" Grid.Column="1" Margin="0,4"
                            Width="80" Height="26" HorizontalAlignment="Left"
                            Click="BtnColor_Click" ToolTip="点击选择颜色">
                        <Button.Background>
                            <SolidColorBrush Color="#FFFFFF"/>
                        </Button.Background>
                    </Button>

                    <TextBlock Text="刷新间隔:" VerticalAlignment="Center" Grid.Row="5"/>
                    <StackPanel Orientation="Horizontal" Grid.Row="5" Grid.Column="1" Margin="0,4">
                        <TextBox x:Name="TbRefreshInterval" Width="80" Text="16"/>
                        <TextBlock Text="ms" VerticalAlignment="Center" Margin="4,0,0,0"/>
                    </StackPanel>
                    <num:UIntegerUpDown x:Name="NudRefreshInterval" Grid.Row="5" Grid.Column="1" Margin="0,4"
                                        Value="16" MinValue="1" MaxValue="10000" StepSize="1"
                                        FormatString="0 ms"/>
                </Grid>
            </GroupBox>

+15 −12
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ namespace WatermarkClock
        private DispatcherTimer _rotationTimer;
        private int _currentTemplateIndex;
        private NotifyIconWrapper _notifyIcon;
        private Color _selectedColor = Color.FromRgb(255, 255, 255);

        public MainWindow()
        {
@@ -41,10 +42,7 @@ namespace WatermarkClock
        private void LoadDefaults()
        {
            _ctx.Templates.Add(new TemplateItem("{DatetimeF}", 10));
            TbPadding.Text = "16";
            TbFontSize.Text = "28";
            TbTextColor.Text = "#FFFFFF";
            TbRefreshInterval.Text = "16";
            // NumericUpDown 控件已有默认值,无需额外设置
        }

        private void ApplySettings()
@@ -52,21 +50,17 @@ namespace WatermarkClock
            if (CbPosition.SelectedItem is MarkPosition pos)
                _ctx.MarkPosition = pos;

            if (int.TryParse(TbPadding.Text, out var padding))
                _ctx.Padding = padding;
            _ctx.Padding = (int)NudPadding.Value;
            _ctx.FontSize = NudFontSize.Value;

            if (double.TryParse(TbFontSize.Text, out var fontSize))
                _ctx.FontSize = fontSize;

            _ctx.TextColor = TbTextColor.Text;
            _ctx.TextColor = $"#{_selectedColor.A:X2}{_selectedColor.R:X2}{_selectedColor.G:X2}{_selectedColor.B:X2}";

            if (CbFontFamily.SelectedItem is FontFamily ff)
                _ctx.FontFamily = ff;
            else if (!string.IsNullOrWhiteSpace(CbFontFamily.Text))
                _ctx.FontFamily = new FontFamily(CbFontFamily.Text);

            if (int.TryParse(TbRefreshInterval.Text, out var refreshMs) && refreshMs > 0)
                _ctx.RefreshInterval = refreshMs;
            _ctx.RefreshInterval = (int)NudRefreshInterval.Value;

            if (_watermarkWindow == null)
            {
@@ -195,6 +189,15 @@ namespace WatermarkClock
            Hide();
        }

        private void BtnColor_Click(object sender, RoutedEventArgs e)
        {
            if (ColorPickerWPF.ColorPickerWindow.ShowDialog(out var color))
            {
                _selectedColor = color;
                BtnColor.Background = new SolidColorBrush(color);
            }
        }

        private void MenuExit_Click(object sender, RoutedEventArgs e)
        {
            _notifyIcon?.Dispose();
+47 −0
Original line number Diff line number Diff line
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WatermarkClock
{
    public class NotifyIconWrapper : IDisposable
    {
        private readonly NotifyIcon _notifyIcon;
        private readonly MainWindow _mainWindow;

        public NotifyIconWrapper(MainWindow mainWindow)
        {
            _mainWindow = mainWindow;

            _notifyIcon = new NotifyIcon
            {
                Icon = SystemIcons.Application,
                Text = "WatermarkClock",
                Visible = true
            };

            var menu = new ContextMenuStrip();
            menu.Items.Add("显示主窗口", null, OnShowClick);
            menu.Items.Add("-");
            menu.Items.Add("退出", null, OnExitClick);
            _notifyIcon.ContextMenuStrip = menu;
            _notifyIcon.DoubleClick += OnShowClick;
        }

        private void OnShowClick(object sender, EventArgs e)
        {
            _mainWindow.ShowFromTray();
        }

        private void OnExitClick(object sender, EventArgs e)
        {
            _notifyIcon.Visible = false;
            System.Windows.Application.Current.Shutdown();
        }

        public void Dispose()
        {
            _notifyIcon?.Dispose();
        }
    }
}
+52 −0
Original line number Diff line number Diff line
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WatermarkClock
{
    public class TemplateItem : INotifyPropertyChanged
    {
        private string _text;
        public string Text
        {
            get => _text;
            set
            {
                if (SetField(ref _text, value))
                    OnPropertyChanged();
            }
        }

        private int _durationSeconds = 10;
        public int DurationSeconds
        {
            get => _durationSeconds;
            set
            {
                if (SetField(ref _durationSeconds, value))
                    OnPropertyChanged();
            }
        }

        public TemplateItem() { }

        public TemplateItem(string text, int durationSeconds = 10)
        {
            _text = text;
            _durationSeconds = durationSeconds;
        }

        public override string ToString() => Text;

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    }
}
+16 −0
Original line number Diff line number Diff line
namespace WatermarkClock
{
    public class TemplateVariable
    {
        public string Name { get; }
        public string Description { get; }

        public TemplateVariable(string name, string description)
        {
            Name = name;
            Description = description;
        }

        public override string ToString() => Name;
    }
}
Loading