XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace XFEToolBox.QuickTools;

public sealed partial class QuickToolPage
{
    private void BuildCalculator()
    {
        var pad = new CalculatorPad(); if (!string.IsNullOrWhiteSpace(Context.Text)) pad.SetExpression(Context.Text);
        var expression = Field("ExpressionInput", pad.Expression); expression.TextAlignment = TextAlignment.Right; expression.FontSize = 18;
        var output = Field("CalculationResult", "0", false, true); output.FontSize = 38; output.MinHeight = 66; output.MaxHeight = 86; output.TextAlignment = TextAlignment.Right; output.BorderThickness = new Thickness(0); output.Background = Brushes.Transparent;
        bool rendering = false;
        expression.TextChanged += (_, _) => { if (!rendering) pad.SetExpression(expression.Text); };
        void PressPad(string key)
        {
            try { pad.Press(key); rendering = true; expression.Text = pad.Expression; output.Text = pad.Display; expression.CaretIndex = expression.Text.Length; status.Text = key == "=" ? "计算完成 · 点击历史记录可再次使用" : "支持鼠标键盘 · Enter 计算 · Esc 清空"; }
            finally { rendering = false; }
        }
        expression.PreviewKeyDown += async (_, e) => { if (e.Key is Key.Enter or Key.Escape) { string key = e.Key == Key.Enter ? "=" : "C"; e.Handled = true; await Run(() => { PressPad(key); return Task.CompletedTask; }); } };
        var keyboard = new UniformGrid { Columns = 4, Name = "CalculatorKeypad" };
        foreach (string key in new[] { "%", "CE", "C", "⌫", "1/x", "x²", "√", "÷", "7", "8", "9", "×", "4", "5", "6", "−", "1", "2", "3", "+", "+/−", "0", ".", "=" })
        {
            var b = key == "=" ? Primary(key, () => PressPad(key)) : ActionButton(key, () => PressPad(key));
            b.MinWidth = 0; b.Height = 53; b.Margin = new Thickness(3); b.HorizontalAlignment = HorizontalAlignment.Stretch; b.FontSize = char.IsDigit(key[0]) && key != "1/x" ? 23 : 19; b.Focusable = false;
            b.ToolTip = key switch { "%" => "当前表达式除以 100", "CE" => "清除当前数字", "C" => "清空表达式", "⌫" => "退格", "=" => "计算 · Enter", _ => key };
            if (key is "+" or "−" or "×" or "÷") { b.Background = Tint; b.Foreground = Accent; }
            keyboard.Children.Add(b);
        }
        var history = List(); history.Name = "CalculationHistory"; history.ItemsSource = pad.History; history.Height = 345;
        history.SelectionChanged += (_, _) => { if (history.SelectedItem is CalculationRecord record) { pad.SetExpression(record.Result); expression.Text = record.Result; output.Text = record.Result; } };
        var caption = Hint("标准计算 · 可编辑表达式");
        var calculator = Card(Stack(caption, expression, output, keyboard));
        void FitKeypad()
        {
            bool compact = ActualHeight > 0 && ActualHeight < 700;
            caption.Visibility = compact ? Visibility.Collapsed : Visibility.Visible;
            calculator.Padding = new Thickness(compact ? 10 : 16);
            expression.Padding = new Thickness(10, compact ? 2 : 8, 10, compact ? 2 : 8);
            expression.MinHeight = compact ? 34 : 42; expression.Height = compact ? 34 : double.NaN;
            expression.Margin = new Thickness(0, compact ? 0 : 6, 0, compact ? 4 : 10);
            output.Padding = new Thickness(10, compact ? 0 : 8, 10, compact ? 0 : 8);
            output.MinHeight = compact ? 44 : 66; output.Height = compact ? 44 : double.NaN;
            output.FontSize = compact ? 30 : 38; output.Margin = new Thickness(0, 4, 0, compact ? 4 : 10);
            foreach (Button key in keyboard.Children)
            {
                key.MinHeight = 0;
                key.Height = compact ? Math.Clamp((ActualHeight - 290) / 6 - 6, 24, 45) : 53;
                key.Padding = new Thickness(6, 2, 6, 2);
                key.FontSize = compact ? 18 : (key.Content is string t && char.IsDigit(t[0]) && t != "1/x" ? 23 : 19);
            }
        }
        SizeChanged += (_, _) => FitKeypad();
        var standard = Split(calculator, Section("计算历史", Hint("仅保留本次窗口的 30 条记录"), history, Actions(ActionButton("清空历史", () => pad.History.Clear()), Copy(output))), 1.8, 1, 800);
        var tabs = new TabControl(); tabs.Items.Add(new TabItem { Header = "计算器", Content = standard });
        var conversionResult = Field("ConversionResult", "", true, true);
        var unitValue = Field("UnitValue", "1"); var unitType = Choices(["容量", "长度", "温度", "时间", "重量"]); var fromUnit = Choices(["GiB", "MiB", "GB", "MB", "KB", "B"]); var toUnit = Choices(["GiB", "MiB", "GB", "MB", "KB", "B"], 1);
        unitType.SelectionChanged += (_, _) => { string[] options = (string)unitType.SelectedItem switch { "长度" => ["m", "cm", "mm", "km", "ft", "in", "mi"], "温度" => ["C", "F", "K"], "时间" => ["s", "min", "h", "day", "ms"], "重量" => ["kg", "g", "lb"], _ => ["GiB", "MiB", "GB", "MB", "KB", "B"] }; fromUnit.ItemsSource = options; toUnit.ItemsSource = options; fromUnit.SelectedIndex = 0; toUnit.SelectedIndex = 1; };
        var units = Section("单位换算", Hint("选择类型和单位,无需记忆命令语法"), unitType, unitValue, Actions(fromUnit, Label("→"), toUnit), Primary("换算", () => Result(Calculator.ConvertUnit($"{unitValue.Text} {fromUnit.SelectedItem} to {toUnit.SelectedItem}"), conversionResult)));
        var number = Field("BaseValue", "FF"); var from = Choices(["2", "8", "10", "16", "36"], 3); var to = Choices(["2", "8", "10", "16", "36"], 2);
        tabs.Items.Add(new TabItem { Header = "单位 / 进制", Content = Stack(Split(units, Section("进制转换", Hint("支持大整数 · 2–36 进制"), number, Actions(from, Label("→"), to), Primary("转换进制", () => Result(Calculator.ConvertBase(number.Text, int.Parse((string)from.SelectedItem), int.Parse((string)to.SelectedItem)), conversionResult)))), conversionResult, Copy(conversionResult)) });
        var timestamp = Box(DateTimeOffset.Now.ToUnixTimeSeconds().ToString()); var dateA = Box(DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss zzz")); var dateB = Box(DateTimeOffset.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss zzz")); var dateResult = Box("", true, true);
        tabs.Items.Add(new TabItem { Header = "日期 / 时间戳", Content = Stack(Split(Section("时间戳", Hint("自动识别秒 / 毫秒;也可输入日期"), timestamp, Primary("转换时间戳", () => Result(Calculator.Timestamp(timestamp.Text), dateResult))), Section("日期间隔", dateA, dateB, Primary("计算日期差", () => Result(Calculator.DateDifference(dateA.Text, dateB.Text), dateResult)))), dateResult, Copy(dateResult)) });
        body.Children.Add(tabs);
    }
}