using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Win32;
namespace XFEToolBox.QuickTools;
public sealed partial class QuickToolPage
{
private void BuildClipboard()
{
var history = ClipboardHistory.Shared; var search = Field("ClipboardSearch"); search.ToolTip = "搜索片段内容";
var filter = Choices(["全部片段", "固定片段", "普通历史"]); var entries = List(); entries.DisplayMemberPath = ""; entries.Name = "ClipboardEntries"; entries.Height = 315;
var factory = new FrameworkElementFactory(typeof(StackPanel)); factory.SetValue(MarginProperty, new Thickness(6, 9, 6, 9));
var snippet = new FrameworkElementFactory(typeof(TextBlock)); snippet.SetBinding(TextBlock.TextProperty, new Binding("Display") { Mode = BindingMode.OneWay }); snippet.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); snippet.SetValue(MaxHeightProperty, 45d); factory.AppendChild(snippet);
var source = new FrameworkElementFactory(typeof(TextBlock)); source.SetBinding(TextBlock.TextProperty, new Binding("Source") { Mode = BindingMode.OneWay, StringFormat = "来源 · {0}" }); source.SetValue(TextBlock.FontSizeProperty, 11d); source.SetValue(MarginProperty, new Thickness(0, 5, 0, 0)); source.SetValue(OpacityProperty, .65); factory.AppendChild(source);
entries.ItemTemplate = new DataTemplate { VisualTree = factory };
var text = Field("ClipboardText", Context.Text ?? "", true); text.Height = 240;
var metadata = Hint("选择左侧片段查看内容,或直接在这里新建。"); var state = Label("记录已关闭", 16); var count = Hint("");
void Refresh()
{
var selected = entries.SelectedItem; string query = search.Text;
entries.ItemsSource = history.Entries.Where(e => e.Text.Contains(query, StringComparison.OrdinalIgnoreCase) && (filter.SelectedIndex == 0 || (filter.SelectedIndex == 1 ? e.Pinned : !e.Pinned))).ToArray();
if (selected is not null && entries.Items.Contains(selected)) entries.SelectedItem = selected;
state.Text = history.Enabled ? "● 正在记录" : "○ 记录已关闭"; count.Text = $"{entries.Items.Count} 条片段 · 固定 {history.Entries.Count(e => e.Pinned)} 条";
}
history.Changed += Refresh; cleanup.Add(() => history.Changed -= Refresh); search.TextChanged += (_, _) => Refresh(); filter.SelectionChanged += (_, _) => Refresh();
entries.SelectionChanged += (_, _) => { if (entries.SelectedItem is ClipRecord record) { text.Text = record.Text; metadata.Text = $"{(record.Pinned ? "★ 固定片段" : "普通历史")} · {record.Source} · {record.Captured:MM-dd HH:mm:ss}"; } };
var recording = Actions(state, ActionButton("开启记录", () => { history.Start(); status.Text = "已开启本次运行的记录,不读取此前的剪贴板。"; }), ActionButton("暂停记录", () => { history.Stop(); status.Text = "已暂停记录。"; }));
body.Children.Add(recording);
var library = Section("片段库", search, filter, entries, count);
var editor = Section("片段详情", metadata, text, Actions(Primary("复制为纯文本", () => { Clipboard.SetText(text.Text); status.Text = "已复制纯文本。"; }), ActionButton("粘贴到先前窗口", () => { Clipboard.SetText(text.Text); SystemActions.PasteTo(Context.PreviousWindow); status.Text = "已发送粘贴。"; })), Actions(ActionButton("新建片段", () => { entries.SelectedItem = null; text.Clear(); metadata.Text = "新片段 · 输入后保存"; }), ActionButton("保存为固定片段", () => { if (string.IsNullOrWhiteSpace(text.Text)) throw new InvalidOperationException("请先输入片段。"); history.Add(text.Text, "手动片段", true); status.Text = "已加密保存固定片段。"; })), Actions(ActionButton("固定 / 取消固定", () => { if (entries.SelectedItem is ClipRecord r) history.TogglePin(r); }), ActionButton("删除选中", () => { if (entries.SelectedItem is ClipRecord r) history.Delete(r); })));
body.Children.Add(Split(library, editor, 1, 1.6));
var excluded = Box(history.ExcludedProcesses);
body.Children.Add(new Expander { Header = "隐私与清理 / 图片另存", Content = Stack(Hint("普通历史只保存在内存;固定片段使用当前用户 DPAPI 加密。默认不记录,开启后请主动暂停。排除进程名称使用逗号分隔。"), excluded, Actions(ActionButton("保存排除名单", () => { history.ExcludedProcesses = excluded.Text; history.SavePreferences(); status.Text = "排除名单已保存。"; }), ActionButton("清空普通历史", () => { history.ClearHistory(); status.Text = "已清空普通历史,保留固定片段。"; }), ActionButton("图片另存 PNG", () => { var bitmap = Clipboard.GetImage() ?? throw new InvalidOperationException("剪贴板没有图片。"); var dialog = new SaveFileDialog { Filter = "PNG 图片|*.png", FileName = "剪贴板.png", OverwritePrompt = false }; if (dialog.ShowDialog(Window.GetWindow(this)) == true) QrService.Save(bitmap, dialog.FileName); }))) });
Refresh(); if (history.LastError.Length > 0) status.Text = history.LastError;
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Win32;
namespace XFEToolBox.QuickTools;
public sealed partial class QuickToolPage
{
private void BuildClipboard()
{
var history = ClipboardHistory.Shared; var search = Field("ClipboardSearch"); search.ToolTip = "搜索片段内容";
var filter = Choices(["全部片段", "固定片段", "普通历史"]); var entries = List(); entries.DisplayMemberPath = ""; entries.Name = "ClipboardEntries"; entries.Height = 315;
var factory = new FrameworkElementFactory(typeof(StackPanel)); factory.SetValue(MarginProperty, new Thickness(6, 9, 6, 9));
var snippet = new FrameworkElementFactory(typeof(TextBlock)); snippet.SetBinding(TextBlock.TextProperty, new Binding("Display") { Mode = BindingMode.OneWay }); snippet.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); snippet.SetValue(MaxHeightProperty, 45d); factory.AppendChild(snippet);
var source = new FrameworkElementFactory(typeof(TextBlock)); source.SetBinding(TextBlock.TextProperty, new Binding("Source") { Mode = BindingMode.OneWay, StringFormat = "来源 · {0}" }); source.SetValue(TextBlock.FontSizeProperty, 11d); source.SetValue(MarginProperty, new Thickness(0, 5, 0, 0)); source.SetValue(OpacityProperty, .65); factory.AppendChild(source);
entries.ItemTemplate = new DataTemplate { VisualTree = factory };
var text = Field("ClipboardText", Context.Text ?? "", true); text.Height = 240;
var metadata = Hint("选择左侧片段查看内容,或直接在这里新建。"); var state = Label("记录已关闭", 16); var count = Hint("");
void Refresh()
{
var selected = entries.SelectedItem; string query = search.Text;
entries.ItemsSource = history.Entries.Where(e => e.Text.Contains(query, StringComparison.OrdinalIgnoreCase) && (filter.SelectedIndex == 0 || (filter.SelectedIndex == 1 ? e.Pinned : !e.Pinned))).ToArray();
if (selected is not null && entries.Items.Contains(selected)) entries.SelectedItem = selected;
state.Text = history.Enabled ? "● 正在记录" : "○ 记录已关闭"; count.Text = $"{entries.Items.Count} 条片段 · 固定 {history.Entries.Count(e => e.Pinned)} 条";
}
history.Changed += Refresh; cleanup.Add(() => history.Changed -= Refresh); search.TextChanged += (_, _) => Refresh(); filter.SelectionChanged += (_, _) => Refresh();
entries.SelectionChanged += (_, _) => { if (entries.SelectedItem is ClipRecord record) { text.Text = record.Text; metadata.Text = $"{(record.Pinned ? "★ 固定片段" : "普通历史")} · {record.Source} · {record.Captured:MM-dd HH:mm:ss}"; } };
var recording = Actions(state, ActionButton("开启记录", () => { history.Start(); status.Text = "已开启本次运行的记录,不读取此前的剪贴板。"; }), ActionButton("暂停记录", () => { history.Stop(); status.Text = "已暂停记录。"; }));
body.Children.Add(recording);
var library = Section("片段库", search, filter, entries, count);
var editor = Section("片段详情", metadata, text, Actions(Primary("复制为纯文本", () => { Clipboard.SetText(text.Text); status.Text = "已复制纯文本。"; }), ActionButton("粘贴到先前窗口", () => { Clipboard.SetText(text.Text); SystemActions.PasteTo(Context.PreviousWindow); status.Text = "已发送粘贴。"; })), Actions(ActionButton("新建片段", () => { entries.SelectedItem = null; text.Clear(); metadata.Text = "新片段 · 输入后保存"; }), ActionButton("保存为固定片段", () => { if (string.IsNullOrWhiteSpace(text.Text)) throw new InvalidOperationException("请先输入片段。"); history.Add(text.Text, "手动片段", true); status.Text = "已加密保存固定片段。"; })), Actions(ActionButton("固定 / 取消固定", () => { if (entries.SelectedItem is ClipRecord r) history.TogglePin(r); }), ActionButton("删除选中", () => { if (entries.SelectedItem is ClipRecord r) history.Delete(r); })));
body.Children.Add(Split(library, editor, 1, 1.6));
var excluded = Box(history.ExcludedProcesses);
body.Children.Add(new Expander { Header = "隐私与清理 / 图片另存", Content = Stack(Hint("普通历史只保存在内存;固定片段使用当前用户 DPAPI 加密。默认不记录,开启后请主动暂停。排除进程名称使用逗号分隔。"), excluded, Actions(ActionButton("保存排除名单", () => { history.ExcludedProcesses = excluded.Text; history.SavePreferences(); status.Text = "排除名单已保存。"; }), ActionButton("清空普通历史", () => { history.ClearHistory(); status.Text = "已清空普通历史,保留固定片段。"; }), ActionButton("图片另存 PNG", () => { var bitmap = Clipboard.GetImage() ?? throw new InvalidOperationException("剪贴板没有图片。"); var dialog = new SaveFileDialog { Filter = "PNG 图片|*.png", FileName = "剪贴板.png", OverwritePrompt = false }; if (dialog.ShowDialog(Window.GetWindow(this)) == true) QrService.Save(bitmap, dialog.FileName); }))) });
Refresh(); if (history.LastError.Length > 0) status.Text = history.LastError;
}
}