Flow.Launcher/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs

109 lines
2.9 KiB
C#
Raw Permalink Normal View History

2022-10-13 12:14:32 +00:00
using System;
2022-10-08 08:33:39 +00:00
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Flow.Launcher.Infrastructure.UserSettings
{
#region Base
2022-10-15 06:18:54 +00:00
public abstract class ShortcutBaseModel
{
public string Key { get; set; }
2022-10-13 11:49:44 +00:00
public override bool Equals(object obj)
2022-10-08 08:33:39 +00:00
{
2022-10-15 06:29:19 +00:00
return obj is ShortcutBaseModel other &&
2022-10-13 11:49:44 +00:00
Key == other.Key;
2022-10-08 08:33:39 +00:00
}
2022-10-13 11:49:44 +00:00
public override int GetHashCode()
2022-10-08 08:33:39 +00:00
{
2022-11-03 17:13:29 +00:00
return Key.GetHashCode();
}
2022-10-15 06:18:54 +00:00
}
public class BaseCustomShortcutModel : ShortcutBaseModel
2022-10-15 06:18:54 +00:00
{
public string Value { get; set; }
public BaseCustomShortcutModel(string key, string value)
2022-10-15 06:18:54 +00:00
{
Key = key;
Value = value;
}
public void Deconstruct(out string key, out string value)
{
key = Key;
2022-10-13 16:42:05 +00:00
value = Value;
}
public static implicit operator (string Key, string Value)(BaseCustomShortcutModel shortcut)
{
return (shortcut.Key, shortcut.Value);
}
public static implicit operator BaseCustomShortcutModel((string Key, string Value) shortcut)
{
return new BaseCustomShortcutModel(shortcut.Key, shortcut.Value);
}
}
2022-10-15 06:18:54 +00:00
public class BaseBuiltinShortcutModel : ShortcutBaseModel
2022-10-15 06:18:54 +00:00
{
public string Description { get; set; }
public string LocalizedDescription => PublicApi.Instance.GetTranslation(Description);
public BaseBuiltinShortcutModel(string key, string description)
2022-10-15 06:18:54 +00:00
{
Key = key;
Description = description;
}
}
#endregion
#region Custom Shortcut
public class CustomShortcutModel : BaseCustomShortcutModel
{
[JsonIgnore]
public Func<string> Expand { get; set; } = () => { return string.Empty; };
[JsonConstructor]
public CustomShortcutModel(string key, string value) : base(key, value)
{
Expand = () => { return Value; };
}
}
#endregion
#region Builtin Shortcut
public class BuiltinShortcutModel : BaseBuiltinShortcutModel
{
[JsonIgnore]
public Func<string> Expand { get; set; } = () => { return string.Empty; };
public BuiltinShortcutModel(string key, string description, Func<string> expand) : base(key, description)
{
Expand = expand ?? (() => { return string.Empty; });
}
}
public class AsyncBuiltinShortcutModel : BaseBuiltinShortcutModel
{
[JsonIgnore]
public Func<Task<string>> ExpandAsync { get; set; } = () => { return Task.FromResult(string.Empty); };
public AsyncBuiltinShortcutModel(string key, string description, Func<Task<string>> expandAsync) : base(key, description)
{
ExpandAsync = expandAsync ?? (() => { return Task.FromResult(string.Empty); });
}
}
#endregion
}