mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 10:16:24 +02:00
[AdvancedPaste] Custom Actions (#34395)
* [AdvancedPaste] Custom Actions * Renamed pipe name to make spellcheck happy * Improved settings page for Custom Actions * UI improvements, disabled standard paste actions when no clipboard text, update clipboard text and gpo state every second * Bug fixes, single query/prompt box, Ctrl+num shortcuts for custom actions, error box * Spellcheck issue * Bug fixes and used Advanced Paste Window as wait indicator for keyboard shortcuts * Improvements to PromptBox, incluing show error message as tooltip * Refactoring * Fixed issue where ESC sometimes didn't close paste window * Update src/settings-ui/Settings.UI/Strings/en-us/Resources.resw Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> --------- Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
This commit is contained in:
183
src/settings-ui/Settings.UI.Library/AdvancedPasteCustomAction.cs
Normal file
183
src/settings-ui/Settings.UI.Library/AdvancedPasteCustomAction.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
public sealed class AdvancedPasteCustomAction : INotifyPropertyChanged, ICloneable
|
||||
{
|
||||
private int _id;
|
||||
private string _name = string.Empty;
|
||||
private string _prompt = string.Empty;
|
||||
private HotkeySettings _shortcut = new();
|
||||
private bool _isShown;
|
||||
private bool _canMoveUp;
|
||||
private bool _canMoveDown;
|
||||
private bool _isValid;
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public int Id
|
||||
{
|
||||
get => _id;
|
||||
set
|
||||
{
|
||||
if (_id != value)
|
||||
{
|
||||
_id = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set
|
||||
{
|
||||
if (_name != value)
|
||||
{
|
||||
_name = value;
|
||||
OnPropertyChanged();
|
||||
UpdateIsValid();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("prompt")]
|
||||
public string Prompt
|
||||
{
|
||||
get => _prompt;
|
||||
set
|
||||
{
|
||||
if (_prompt != value)
|
||||
{
|
||||
_prompt = value;
|
||||
OnPropertyChanged();
|
||||
UpdateIsValid();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("shortcut")]
|
||||
public HotkeySettings Shortcut
|
||||
{
|
||||
get => _shortcut;
|
||||
set
|
||||
{
|
||||
if (_shortcut != value)
|
||||
{
|
||||
// We null-coalesce here rather than outside this branch as we want to raise PropertyChanged when the setter is called
|
||||
// with null; the ShortcutControl depends on this.
|
||||
_shortcut = value ?? new();
|
||||
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("isShown")]
|
||||
public bool IsShown
|
||||
{
|
||||
get => _isShown;
|
||||
set
|
||||
{
|
||||
if (_isShown != value)
|
||||
{
|
||||
_isShown = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public bool CanMoveUp
|
||||
{
|
||||
get => _canMoveUp;
|
||||
set
|
||||
{
|
||||
if (_canMoveUp != value)
|
||||
{
|
||||
_canMoveUp = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public bool CanMoveDown
|
||||
{
|
||||
get => _canMoveDown;
|
||||
set
|
||||
{
|
||||
if (_canMoveDown != value)
|
||||
{
|
||||
_canMoveDown = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public bool IsValid
|
||||
{
|
||||
get => _isValid;
|
||||
private set
|
||||
{
|
||||
if (_isValid != value)
|
||||
{
|
||||
_isValid = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public string ToJsonString() => JsonSerializer.Serialize(this);
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
AdvancedPasteCustomAction clone = new();
|
||||
clone.Update(this);
|
||||
return clone;
|
||||
}
|
||||
|
||||
public void Update(AdvancedPasteCustomAction other)
|
||||
{
|
||||
Id = other.Id;
|
||||
Name = other.Name;
|
||||
Prompt = other.Prompt;
|
||||
Shortcut = other.GetShortcutClone();
|
||||
IsShown = other.IsShown;
|
||||
CanMoveUp = other.CanMoveUp;
|
||||
CanMoveDown = other.CanMoveDown;
|
||||
}
|
||||
|
||||
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
private HotkeySettings GetShortcutClone()
|
||||
{
|
||||
object shortcut = null;
|
||||
if (Shortcut.TryToCmdRepresentable(out string shortcutString))
|
||||
{
|
||||
_ = HotkeySettings.TryParseFromCmd(shortcutString, out shortcut);
|
||||
}
|
||||
|
||||
return (shortcut as HotkeySettings) ?? new HotkeySettings();
|
||||
}
|
||||
|
||||
private void UpdateIsValid()
|
||||
{
|
||||
IsValid = !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(Prompt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
public sealed class AdvancedPasteCustomActions
|
||||
{
|
||||
private static readonly JsonSerializerOptions _serializerOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
};
|
||||
|
||||
[JsonPropertyName("value")]
|
||||
public ObservableCollection<AdvancedPasteCustomAction> Value { get; set; } = [];
|
||||
|
||||
public AdvancedPasteCustomActions()
|
||||
{
|
||||
}
|
||||
|
||||
public string ToJsonString() => JsonSerializer.Serialize(this, _serializerOptions);
|
||||
}
|
||||
@@ -20,6 +20,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
PasteAsPlainTextShortcut = DefaultPasteAsPlainTextShortcut;
|
||||
PasteAsMarkdownShortcut = new();
|
||||
PasteAsJsonShortcut = new();
|
||||
CustomActions = new();
|
||||
ShowCustomPreview = true;
|
||||
SendPasteKeyCombination = true;
|
||||
CloseAfterLosingFocus = false;
|
||||
@@ -47,6 +48,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
[JsonPropertyName("paste-as-json-hotkey")]
|
||||
public HotkeySettings PasteAsJsonShortcut { get; set; }
|
||||
|
||||
[JsonPropertyName("custom-actions")]
|
||||
[CmdConfigureIgnoreAttribute]
|
||||
public AdvancedPasteCustomActions CustomActions { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user