|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | + |
| 5 | +using Avalonia; |
| 6 | +using Avalonia.Controls; |
| 7 | +using Avalonia.Interactivity; |
| 8 | + |
| 9 | +namespace SourceGit.Views |
| 10 | +{ |
| 11 | + public partial class RemoteProtocolSwitcher : UserControl |
| 12 | + { |
| 13 | + public static readonly StyledProperty<string> UrlProperty = |
| 14 | + AvaloniaProperty.Register<RemoteProtocolSwitcher, string>(nameof(Url)); |
| 15 | + |
| 16 | + public string Url |
| 17 | + { |
| 18 | + get => GetValue(UrlProperty); |
| 19 | + set => SetValue(UrlProperty, value); |
| 20 | + } |
| 21 | + |
| 22 | + public static readonly StyledProperty<string> ActiveProtocolProperty = |
| 23 | + AvaloniaProperty.Register<RemoteProtocolSwitcher, string>(nameof(ActiveProtocol)); |
| 24 | + |
| 25 | + public string ActiveProtocol |
| 26 | + { |
| 27 | + get => GetValue(ActiveProtocolProperty); |
| 28 | + set => SetValue(ActiveProtocolProperty, value); |
| 29 | + } |
| 30 | + |
| 31 | + public RemoteProtocolSwitcher() |
| 32 | + { |
| 33 | + InitializeComponent(); |
| 34 | + } |
| 35 | + |
| 36 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 37 | + { |
| 38 | + base.OnPropertyChanged(change); |
| 39 | + |
| 40 | + if (change.Property == UrlProperty) |
| 41 | + { |
| 42 | + _protocols.Clear(); |
| 43 | + |
| 44 | + var url = Url ?? string.Empty; |
| 45 | + if (url.StartsWith("https://", StringComparison.Ordinal) && Uri.TryCreate(url, UriKind.Absolute, out var uri)) |
| 46 | + { |
| 47 | + var host = uri.Host; |
| 48 | + var serverName = uri.Port == 443 ? host : $"{host}:{uri.Port}"; |
| 49 | + var route = uri.AbsolutePath.TrimStart('/'); |
| 50 | + |
| 51 | + _protocols.Add(url); |
| 52 | + _protocols.Add($"git@{serverName}:{route}"); |
| 53 | + |
| 54 | + SetCurrentValue(ActiveProtocolProperty, "HTTPS"); |
| 55 | + SetCurrentValue(IsVisibleProperty, true); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + var match = REG_SSH_FORMAT().Match(url); |
| 60 | + if (match.Success) |
| 61 | + { |
| 62 | + var host = match.Groups[1].Value; |
| 63 | + var repo = match.Groups[2].Value; |
| 64 | + |
| 65 | + _protocols.Add($"https://{host}/{repo}"); |
| 66 | + _protocols.Add(url); |
| 67 | + |
| 68 | + SetCurrentValue(ActiveProtocolProperty, "SSH"); |
| 69 | + SetCurrentValue(IsVisibleProperty, true); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + SetCurrentValue(IsVisibleProperty, false); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void OnOpenDropdownMenu(object sender, RoutedEventArgs e) |
| 78 | + { |
| 79 | + if (sender is Button btn && _protocols.Count > 0) |
| 80 | + { |
| 81 | + var menu = new ContextMenu(); |
| 82 | + menu.Placement = PlacementMode.BottomEdgeAlignedLeft; |
| 83 | + |
| 84 | + foreach (var protocol in _protocols) |
| 85 | + { |
| 86 | + var dup = protocol; |
| 87 | + var item = new MenuItem() { Header = dup }; |
| 88 | + item.Click += (_, _) => Url = protocol; |
| 89 | + menu.Items.Add(item); |
| 90 | + } |
| 91 | + |
| 92 | + menu.Open(btn); |
| 93 | + } |
| 94 | + |
| 95 | + e.Handled = true; |
| 96 | + } |
| 97 | + |
| 98 | + [GeneratedRegex(@"^git@([\w\.\-]+):(.+)$")] |
| 99 | + private static partial Regex REG_SSH_FORMAT(); |
| 100 | + private List<string> _protocols = []; |
| 101 | + } |
| 102 | +} |
0 commit comments