mirror of https://github.com/2dust/v2rayN
parent
355a424be2
commit
bc3593871b
@ -0,0 +1,791 @@
|
||||
using DynamicData;
|
||||
using DynamicData.Binding;
|
||||
using MaterialDesignThemes.Wpf;
|
||||
using ReactiveUI;
|
||||
using ReactiveUI.Fody.Helpers;
|
||||
using Splat;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using v2rayN.Enums;
|
||||
using v2rayN.Handler;
|
||||
using v2rayN.Handler.Fmt;
|
||||
using v2rayN.Handler.Statistics;
|
||||
using v2rayN.Models;
|
||||
using v2rayN.Resx;
|
||||
using v2rayN.Views;
|
||||
|
||||
namespace v2rayN.ViewModels
|
||||
{
|
||||
public class ProfilesViewModel : ReactiveObject
|
||||
{
|
||||
#region private prop
|
||||
|
||||
private List<ProfileItem> _lstProfile;
|
||||
private string _serverFilter = string.Empty;
|
||||
private static Config _config;
|
||||
private NoticeHandler? _noticeHandler;
|
||||
private Dictionary<string, bool> _dicHeaderSort = new();
|
||||
private Action<EViewAction> _updateView;
|
||||
|
||||
#endregion private prop
|
||||
|
||||
#region ObservableCollection
|
||||
|
||||
private IObservableCollection<ProfileItemModel> _profileItems = new ObservableCollectionExtended<ProfileItemModel>();
|
||||
public IObservableCollection<ProfileItemModel> ProfileItems => _profileItems;
|
||||
|
||||
private IObservableCollection<SubItem> _subItems = new ObservableCollectionExtended<SubItem>();
|
||||
public IObservableCollection<SubItem> SubItems => _subItems;
|
||||
|
||||
private IObservableCollection<ComboItem> _servers = new ObservableCollectionExtended<ComboItem>();
|
||||
|
||||
[Reactive]
|
||||
public ProfileItemModel SelectedProfile { get; set; }
|
||||
|
||||
public IList<ProfileItemModel> SelectedProfiles { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public SubItem SelectedSub { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public SubItem SelectedMoveToGroup { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public ComboItem SelectedServer { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public string ServerFilter { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public bool BlServers { get; set; }
|
||||
|
||||
#endregion ObservableCollection
|
||||
|
||||
#region Menu
|
||||
|
||||
//servers delete
|
||||
public ReactiveCommand<Unit, Unit> EditServerCmd { get; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> RemoveServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> RemoveDuplicateServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> CopyServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> SetDefaultServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> ShareServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> SetDefaultMultipleServerCmd { get; }
|
||||
|
||||
//servers move
|
||||
public ReactiveCommand<Unit, Unit> MoveTopCmd { get; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> MoveUpCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> MoveDownCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> MoveBottomCmd { get; }
|
||||
|
||||
//servers ping
|
||||
public ReactiveCommand<Unit, Unit> MixedTestServerCmd { get; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> TcpingServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> RealPingServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> SpeedServerCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> SortServerResultCmd { get; }
|
||||
|
||||
//servers export
|
||||
public ReactiveCommand<Unit, Unit> Export2ClientConfigCmd { get; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> Export2ShareUrlCmd { get; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> AddSubCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> EditSubCmd { get; }
|
||||
|
||||
#endregion Menu
|
||||
|
||||
#region Init
|
||||
|
||||
public ProfilesViewModel(Action<EViewAction> updateView)
|
||||
{
|
||||
_updateView = updateView;
|
||||
|
||||
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
|
||||
_config = LazyConfig.Instance.GetConfig();
|
||||
MessageBus.Current.Listen<string>(Global.CommandRefreshProfiles).Subscribe(x => RefreshServersBiz());
|
||||
|
||||
SelectedProfile = new();
|
||||
SelectedSub = new();
|
||||
SelectedMoveToGroup = new();
|
||||
SelectedServer = new();
|
||||
|
||||
RefreshSubscriptions();
|
||||
RefreshServers();
|
||||
|
||||
#region WhenAnyValue && ReactiveCommand
|
||||
|
||||
var canEditRemove = this.WhenAnyValue(
|
||||
x => x.SelectedProfile,
|
||||
selectedSource => selectedSource != null && !selectedSource.indexId.IsNullOrEmpty());
|
||||
|
||||
this.WhenAnyValue(
|
||||
x => x.SelectedSub,
|
||||
y => y != null && !y.remarks.IsNullOrEmpty() && _config.subIndexId != y.id)
|
||||
.Subscribe(c => SubSelectedChanged(c));
|
||||
this.WhenAnyValue(
|
||||
x => x.SelectedMoveToGroup,
|
||||
y => y != null && !y.remarks.IsNullOrEmpty())
|
||||
.Subscribe(c => MoveToGroup(c));
|
||||
|
||||
this.WhenAnyValue(
|
||||
x => x.SelectedServer,
|
||||
y => y != null && !y.Text.IsNullOrEmpty())
|
||||
.Subscribe(c => ServerSelectedChanged(c));
|
||||
|
||||
this.WhenAnyValue(
|
||||
x => x.ServerFilter,
|
||||
y => y != null && _serverFilter != y)
|
||||
.Subscribe(c => ServerFilterChanged(c));
|
||||
|
||||
//servers delete
|
||||
EditServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
EditServer(false, EConfigType.Custom);
|
||||
}, canEditRemove);
|
||||
RemoveServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
RemoveServer();
|
||||
}, canEditRemove);
|
||||
RemoveDuplicateServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
RemoveDuplicateServer();
|
||||
});
|
||||
CopyServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
CopyServer();
|
||||
}, canEditRemove);
|
||||
SetDefaultServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
SetDefaultServer();
|
||||
}, canEditRemove);
|
||||
ShareServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
ShareServer();
|
||||
}, canEditRemove);
|
||||
SetDefaultMultipleServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
SetDefaultMultipleServer();
|
||||
}, canEditRemove);
|
||||
//servers move
|
||||
MoveTopCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
MoveServer(EMove.Top);
|
||||
}, canEditRemove);
|
||||
MoveUpCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
MoveServer(EMove.Up);
|
||||
}, canEditRemove);
|
||||
MoveDownCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
MoveServer(EMove.Down);
|
||||
}, canEditRemove);
|
||||
MoveBottomCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
MoveServer(EMove.Bottom);
|
||||
}, canEditRemove);
|
||||
|
||||
//servers ping
|
||||
MixedTestServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
ServerSpeedtest(ESpeedActionType.Mixedtest);
|
||||
});
|
||||
TcpingServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
ServerSpeedtest(ESpeedActionType.Tcping);
|
||||
}, canEditRemove);
|
||||
RealPingServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
ServerSpeedtest(ESpeedActionType.Realping);
|
||||
}, canEditRemove);
|
||||
SpeedServerCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
ServerSpeedtest(ESpeedActionType.Speedtest);
|
||||
}, canEditRemove);
|
||||
SortServerResultCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
SortServer(EServerColName.delayVal.ToString());
|
||||
});
|
||||
//servers export
|
||||
Export2ClientConfigCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
Export2ClientConfig();
|
||||
}, canEditRemove);
|
||||
Export2ShareUrlCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
Export2ShareUrl();
|
||||
}, canEditRemove);
|
||||
|
||||
//Subscription
|
||||
|
||||
AddSubCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
EditSub(true);
|
||||
});
|
||||
EditSubCmd = ReactiveCommand.Create(() =>
|
||||
{
|
||||
EditSub(false);
|
||||
});
|
||||
|
||||
#endregion WhenAnyValue && ReactiveCommand
|
||||
}
|
||||
|
||||
#endregion Init
|
||||
|
||||
#region Actions
|
||||
|
||||
private void Reload()
|
||||
{
|
||||
Locator.Current.GetService<MainWindowViewModel>()?.Reload();
|
||||
}
|
||||
|
||||
private void UpdateSpeedtestHandler(string indexId, string delay, string speed)
|
||||
{
|
||||
Application.Current?.Dispatcher.Invoke((Action)(() =>
|
||||
{
|
||||
SetTestResult(indexId, delay, speed);
|
||||
}));
|
||||
}
|
||||
|
||||
private void SetTestResult(string indexId, string delay, string speed)
|
||||
{
|
||||
if (Utils.IsNullOrEmpty(indexId))
|
||||
{
|
||||
_noticeHandler?.SendMessage(delay, true);
|
||||
_noticeHandler?.Enqueue(delay);
|
||||
return;
|
||||
}
|
||||
var item = _profileItems.Where(it => it.indexId == indexId).FirstOrDefault();
|
||||
if (item != null)
|
||||
{
|
||||
if (!Utils.IsNullOrEmpty(delay))
|
||||
{
|
||||
int.TryParse(delay, out int temp);
|
||||
item.delay = temp;
|
||||
item.delayVal = $"{delay} {Global.DelayUnit}";
|
||||
}
|
||||
if (!Utils.IsNullOrEmpty(speed))
|
||||
{
|
||||
item.speedVal = $"{speed} {Global.SpeedUnit}";
|
||||
}
|
||||
_profileItems.Replace(item, JsonUtils.DeepCopy(item));
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStatistics(ServerSpeedItem update)
|
||||
{
|
||||
try
|
||||
{
|
||||
Application.Current?.Dispatcher.Invoke((Action)(() =>
|
||||
{
|
||||
var item = _profileItems.Where(it => it.indexId == update.indexId).FirstOrDefault();
|
||||
if (item != null)
|
||||
{
|
||||
item.todayDown = Utils.HumanFy(update.todayDown);
|
||||
item.todayUp = Utils.HumanFy(update.todayUp);
|
||||
item.totalDown = Utils.HumanFy(update.totalDown);
|
||||
item.totalUp = Utils.HumanFy(update.totalUp);
|
||||
|
||||
if (SelectedProfile?.indexId == item.indexId)
|
||||
{
|
||||
var temp = JsonUtils.DeepCopy(item);
|
||||
_profileItems.Replace(item, temp);
|
||||
SelectedProfile = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
_profileItems.Replace(item, JsonUtils.DeepCopy(item));
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Actions
|
||||
|
||||
#region Servers && Groups
|
||||
|
||||
private void SubSelectedChanged(bool c)
|
||||
{
|
||||
if (!c)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_config.subIndexId = SelectedSub?.id;
|
||||
|
||||
RefreshServers();
|
||||
|
||||
_updateView(EViewAction.ProfilesFocus);
|
||||
}
|
||||
|
||||
private void ServerFilterChanged(bool c)
|
||||
{
|
||||
if (!c)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_serverFilter = ServerFilter;
|
||||
if (Utils.IsNullOrEmpty(_serverFilter))
|
||||
{
|
||||
RefreshServers();
|
||||
}
|
||||
}
|
||||
|
||||
public void RefreshServers()
|
||||
{
|
||||
MessageBus.Current.SendMessage("", Global.CommandRefreshProfiles);
|
||||
}
|
||||
|
||||
private void RefreshServersBiz()
|
||||
{
|
||||
var lstModel = LazyConfig.Instance.ProfileItems(_config.subIndexId, _serverFilter);
|
||||
|
||||
ConfigHandler.SetDefaultServer(_config, lstModel);
|
||||
|
||||
var lstServerStat = (_config.guiItem.enableStatistics ? StatisticsHandler.Instance.ServerStat : null) ?? [];
|
||||
var lstProfileExs = ProfileExHandler.Instance.ProfileExs;
|
||||
lstModel = (from t in lstModel
|
||||
join t2 in lstServerStat on t.indexId equals t2.indexId into t2b
|
||||
from t22 in t2b.DefaultIfEmpty()
|
||||
join t3 in lstProfileExs on t.indexId equals t3.indexId into t3b
|
||||
from t33 in t3b.DefaultIfEmpty()
|
||||
select new ProfileItemModel
|
||||
{
|
||||
indexId = t.indexId,
|
||||
configType = t.configType,
|
||||
remarks = t.remarks,
|
||||
address = t.address,
|
||||
port = t.port,
|
||||
security = t.security,
|
||||
network = t.network,
|
||||
streamSecurity = t.streamSecurity,
|
||||
subid = t.subid,
|
||||
subRemarks = t.subRemarks,
|
||||
isActive = t.indexId == _config.indexId,
|
||||
sort = t33 == null ? 0 : t33.sort,
|
||||
delay = t33 == null ? 0 : t33.delay,
|
||||
delayVal = t33?.delay != 0 ? $"{t33?.delay} {Global.DelayUnit}" : string.Empty,
|
||||
speedVal = t33?.speed != 0 ? $"{t33?.speed} {Global.SpeedUnit}" : string.Empty,
|
||||
todayDown = t22 == null ? "" : Utils.HumanFy(t22.todayDown),
|
||||
todayUp = t22 == null ? "" : Utils.HumanFy(t22.todayUp),
|
||||
totalDown = t22 == null ? "" : Utils.HumanFy(t22.totalDown),
|
||||
totalUp = t22 == null ? "" : Utils.HumanFy(t22.totalUp)
|
||||
}).OrderBy(t => t.sort).ToList();
|
||||
_lstProfile = JsonUtils.Deserialize<List<ProfileItem>>(JsonUtils.Serialize(lstModel));
|
||||
|
||||
Application.Current?.Dispatcher.Invoke((Action)(() =>
|
||||
{
|
||||
_profileItems.Clear();
|
||||
_profileItems.AddRange(lstModel);
|
||||
if (lstModel.Count > 0)
|
||||
{
|
||||
var selected = lstModel.FirstOrDefault(t => t.indexId == _config.indexId);
|
||||
if (selected != null)
|
||||
{
|
||||
SelectedProfile = selected;
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedProfile = lstModel[0];
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public void RefreshSubscriptions()
|
||||
{
|
||||
_subItems.Clear();
|
||||
|
||||
_subItems.Add(new SubItem { remarks = ResUI.AllGroupServers });
|
||||
foreach (var item in LazyConfig.Instance.SubItems().OrderBy(t => t.sort))
|
||||
{
|
||||
_subItems.Add(item);
|
||||
}
|
||||
if (_config.subIndexId != null && _subItems.FirstOrDefault(t => t.id == _config.subIndexId) != null)
|
||||
{
|
||||
SelectedSub = _subItems.FirstOrDefault(t => t.id == _config.subIndexId);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedSub = _subItems[0];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Servers && Groups
|
||||
|
||||
#region Add Servers
|
||||
|
||||
private int GetProfileItems(out List<ProfileItem> lstSelecteds, bool latest)
|
||||
{
|
||||
lstSelecteds = new List<ProfileItem>();
|
||||
if (SelectedProfiles == null || SelectedProfiles.Count <= 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
var orderProfiles = SelectedProfiles?.OrderBy(t => t.sort);
|
||||
if (latest)
|
||||
{
|
||||
foreach (var profile in orderProfiles)
|
||||
{
|
||||
var item = LazyConfig.Instance.GetProfileItem(profile.indexId);
|
||||
if (item is not null)
|
||||
{
|
||||
lstSelecteds.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lstSelecteds = JsonUtils.Deserialize<List<ProfileItem>>(JsonUtils.Serialize(orderProfiles));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void EditServer(bool blNew, EConfigType eConfigType)
|
||||
{
|
||||
ProfileItem item;
|
||||
if (blNew)
|
||||
{
|
||||
item = new()
|
||||
{
|
||||
subid = _config.subIndexId,
|
||||
configType = eConfigType,
|
||||
isSub = false,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Utils.IsNullOrEmpty(SelectedProfile?.indexId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
item = LazyConfig.Instance.GetProfileItem(SelectedProfile.indexId);
|
||||
if (item is null)
|
||||
{
|
||||
_noticeHandler?.Enqueue(ResUI.PleaseSelectServer);
|
||||
return;
|
||||
}
|
||||
eConfigType = item.configType;
|
||||
}
|
||||
bool? ret = false;
|
||||
if (eConfigType == EConfigType.Custom)
|
||||
{
|
||||
ret = (new AddServer2Window(item)).ShowDialog();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = (new AddServerWindow(item)).ShowDialog();
|
||||
}
|
||||
if (ret == true)
|
||||
{
|
||||
RefreshServers();
|
||||
if (item.indexId == _config.indexId)
|
||||
{
|
||||
Reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveServer()
|
||||
{
|
||||
if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (UI.ShowYesNo(ResUI.RemoveServer) == MessageBoxResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var exists = lstSelecteds.Exists(t => t.indexId == _config.indexId);
|
||||
|
||||
ConfigHandler.RemoveServer(_config, lstSelecteds);
|
||||
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
|
||||
|
||||
RefreshServers();
|
||||
if (exists)
|
||||
{
|
||||
Reload();
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveDuplicateServer()
|
||||
{
|
||||
var tuple = ConfigHandler.DedupServerList(_config, _config.subIndexId);
|
||||
RefreshServers();
|
||||
Reload();
|
||||
_noticeHandler?.Enqueue(string.Format(ResUI.RemoveDuplicateServerResult, tuple.Item1, tuple.Item2));
|
||||
}
|
||||
|
||||
private void CopyServer()
|
||||
{
|
||||
if (GetProfileItems(out List<ProfileItem> lstSelecteds, false) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (ConfigHandler.CopyServer(_config, lstSelecteds) == 0)
|
||||
{
|
||||
RefreshServers();
|
||||
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDefaultServer()
|
||||
{
|
||||
if (Utils.IsNullOrEmpty(SelectedProfile?.indexId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
SetDefaultServer(SelectedProfile.indexId);
|
||||
}
|
||||
|
||||
private void SetDefaultServer(string indexId)
|
||||
{
|
||||
if (Utils.IsNullOrEmpty(indexId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (indexId == _config.indexId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var item = LazyConfig.Instance.GetProfileItem(indexId);
|
||||
if (item is null)
|
||||
{
|
||||
_noticeHandler?.Enqueue(ResUI.PleaseSelectServer);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ConfigHandler.SetDefaultServerIndex(_config, indexId) == 0)
|
||||
{
|
||||
RefreshServers();
|
||||
Reload();
|
||||
}
|
||||
}
|
||||
|
||||
private void ServerSelectedChanged(bool c)
|
||||
{
|
||||
if (!c)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (SelectedServer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (Utils.IsNullOrEmpty(SelectedServer.ID))
|
||||
{
|
||||
return;
|
||||
}
|
||||
SetDefaultServer(SelectedServer.ID);
|
||||
}
|
||||
|
||||
public async void ShareServer()
|
||||
{
|
||||
var item = LazyConfig.Instance.GetProfileItem(SelectedProfile.indexId);
|
||||
if (item is null)
|
||||
{
|
||||
_noticeHandler?.Enqueue(ResUI.PleaseSelectServer);
|
||||
return;
|
||||
}
|
||||
var url = FmtHandler.GetShareUri(item);
|
||||
if (Utils.IsNullOrEmpty(url))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var img = QRCodeHelper.GetQRCode(url);
|
||||
var dialog = new QrcodeView()
|
||||
{
|
||||
imgQrcode = { Source = img },
|
||||
txtContent = { Text = url },
|
||||
};
|
||||
|
||||
await DialogHost.Show(dialog, "RootDialog");
|
||||
}
|
||||
|
||||
private void SetDefaultMultipleServer()
|
||||
{
|
||||
if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (ConfigHandler.AddCustomServer4Multiple(_config, lstSelecteds, out string indexId) != 0)
|
||||
{
|
||||
_noticeHandler?.Enqueue(ResUI.OperationFailed);
|
||||
return;
|
||||
}
|
||||
if (indexId == _config.indexId)
|
||||
{
|
||||
Reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
SetDefaultServer(indexId);
|
||||
}
|
||||
}
|
||||
|
||||
public void SortServer(string colName)
|
||||
{
|
||||
if (Utils.IsNullOrEmpty(colName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_dicHeaderSort.TryAdd(colName, true);
|
||||
_dicHeaderSort.TryGetValue(colName, out bool asc);
|
||||
if (ConfigHandler.SortServers(_config, _config.subIndexId, colName, asc) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_dicHeaderSort[colName] = !asc;
|
||||
RefreshServers();
|
||||
}
|
||||
|
||||
//move server
|
||||
private void MoveToGroup(bool c)
|
||||
{
|
||||
if (!c)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigHandler.MoveToGroup(_config, lstSelecteds, SelectedMoveToGroup.id);
|
||||
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
|
||||
|
||||
RefreshServers();
|
||||
SelectedMoveToGroup = new();
|
||||
//Reload();
|
||||
}
|
||||
|
||||
public void MoveServer(EMove eMove)
|
||||
{
|
||||
var item = _lstProfile.FirstOrDefault(t => t.indexId == SelectedProfile.indexId);
|
||||
if (item is null)
|
||||
{
|
||||
_noticeHandler?.Enqueue(ResUI.PleaseSelectServer);
|
||||
return;
|
||||
}
|
||||
|
||||
int index = _lstProfile.IndexOf(item);
|
||||
if (index < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (ConfigHandler.MoveServer(_config, ref _lstProfile, index, eMove) == 0)
|
||||
{
|
||||
RefreshServers();
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveServerTo(int startIndex, ProfileItemModel targetItem)
|
||||
{
|
||||
var targetIndex = _profileItems.IndexOf(targetItem);
|
||||
if (startIndex >= 0 && targetIndex >= 0 && startIndex != targetIndex)
|
||||
{
|
||||
if (ConfigHandler.MoveServer(_config, ref _lstProfile, startIndex, EMove.Position, targetIndex) == 0)
|
||||
{
|
||||
RefreshServers();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ServerSpeedtest(ESpeedActionType actionType)
|
||||
{
|
||||
if (actionType == ESpeedActionType.Mixedtest)
|
||||
{
|
||||
SelectedProfiles = _profileItems;
|
||||
}
|
||||
if (GetProfileItems(out List<ProfileItem> lstSelecteds, false) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//ClearTestResult();
|
||||
var coreHandler = Locator.Current.GetService<CoreHandler>();
|
||||
if (coreHandler != null)
|
||||
{
|
||||
new SpeedtestHandler(_config, coreHandler, lstSelecteds, actionType, UpdateSpeedtestHandler);
|
||||
}
|
||||
}
|
||||
|
||||
private void Export2ClientConfig()
|
||||
{
|
||||
var item = LazyConfig.Instance.GetProfileItem(SelectedProfile.indexId);
|
||||
if (item is null)
|
||||
{
|
||||
_noticeHandler?.Enqueue(ResUI.PleaseSelectServer);
|
||||
return;
|
||||
}
|
||||
MainFormHandler.Instance.Export2ClientConfig(item, _config);
|
||||
}
|
||||
|
||||
public void Export2ShareUrl()
|
||||
{
|
||||
if (GetProfileItems(out List<ProfileItem> lstSelecteds, true) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new();
|
||||
foreach (var it in lstSelecteds)
|
||||
{
|
||||
var url = FmtHandler.GetShareUri(it);
|
||||
if (Utils.IsNullOrEmpty(url))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
sb.Append(url);
|
||||
sb.AppendLine();
|
||||
}
|
||||
if (sb.Length > 0)
|
||||
{
|
||||
Utils.SetClipboardData(sb.ToString());
|
||||
_noticeHandler?.SendMessage(ResUI.BatchExportURLSuccessfully);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Add Servers
|
||||
|
||||
#region Subscription
|
||||
|
||||
private void EditSub(bool blNew)
|
||||
{
|
||||
SubItem item;
|
||||
if (blNew)
|
||||
{
|
||||
item = new();
|
||||
}
|
||||
else
|
||||
{
|
||||
item = LazyConfig.Instance.GetSubItem(_config.subIndexId);
|
||||
if (item is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
var ret = (new SubEditWindow(item)).ShowDialog();
|
||||
if (ret == true)
|
||||
{
|
||||
RefreshSubscriptions();
|
||||
SubSelectedChanged(true);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Subscription
|
||||
}
|
||||
}
|
@ -0,0 +1,302 @@
|
||||
<reactiveui:ReactiveUserControl
|
||||
x:Class="v2rayN.Views.ProfilesView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:base="clr-namespace:v2rayN.Base"
|
||||
xmlns:conv="clr-namespace:v2rayN.Converters"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:reactiveui="http://reactiveui.net"
|
||||
xmlns:resx="clr-namespace:v2rayN.Resx"
|
||||
xmlns:vms="clr-namespace:v2rayN.ViewModels"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
x:TypeArguments="vms:ProfilesViewModel"
|
||||
mc:Ignorable="d">
|
||||
<UserControl.Resources>
|
||||
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
|
||||
<conv:DelayColorConverter x:Key="DelayColorConverter" />
|
||||
</UserControl.Resources>
|
||||
<Grid>
|
||||
<DockPanel>
|
||||
<WrapPanel Margin="2" DockPanel.Dock="Top">
|
||||
<ListBox
|
||||
x:Name="lstGroup"
|
||||
MaxHeight="200"
|
||||
FontSize="{DynamicResource StdFontSize}"
|
||||
ItemContainerStyle="{StaticResource MyChipListBoxItem}"
|
||||
Style="{StaticResource MaterialDesignChoiceChipPrimaryOutlineListBox}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding remarks}" />
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<Button
|
||||
x:Name="btnEditSub"
|
||||
Width="30"
|
||||
Height="30"
|
||||
Margin="4,0"
|
||||
Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}">
|
||||
<materialDesign:PackIcon VerticalAlignment="Center" Kind="Edit" />
|
||||
</Button>
|
||||
<Button
|
||||
x:Name="btnAddSub"
|
||||
Width="30"
|
||||
Height="30"
|
||||
Margin="4,0"
|
||||
Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}">
|
||||
<materialDesign:PackIcon VerticalAlignment="Center" Kind="Plus" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
x:Name="btnAutofitColumnWidth"
|
||||
Width="30"
|
||||
Height="30"
|
||||
Margin="20,0"
|
||||
Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}">
|
||||
<materialDesign:PackIcon VerticalAlignment="Center" Kind="ArrowSplitVertical" />
|
||||
</Button>
|
||||
<TextBox
|
||||
x:Name="txtServerFilter"
|
||||
Width="200"
|
||||
Margin="4,0"
|
||||
VerticalContentAlignment="Center"
|
||||
materialDesign:HintAssist.Hint="{x:Static resx:ResUI.MsgServerTitle}"
|
||||
materialDesign:TextFieldAssist.HasClearButton="True"
|
||||
Style="{StaticResource DefTextBox}" />
|
||||
</WrapPanel>
|
||||
<DataGrid
|
||||
x:Name="lstProfiles"
|
||||
materialDesign:DataGridAssist.CellPadding="2,2"
|
||||
AutoGenerateColumns="False"
|
||||
BorderThickness="1"
|
||||
CanUserAddRows="False"
|
||||
CanUserResizeRows="False"
|
||||
CanUserSortColumns="False"
|
||||
EnableRowVirtualization="True"
|
||||
Focusable="True"
|
||||
GridLinesVisibility="All"
|
||||
HeadersVisibility="All"
|
||||
IsReadOnly="True"
|
||||
RowHeaderWidth="40"
|
||||
Style="{StaticResource DefDataGrid}">
|
||||
<DataGrid.InputBindings>
|
||||
<KeyBinding Command="ApplicationCommands.NotACommand" Gesture="Ctrl+C" />
|
||||
<KeyBinding Command="ApplicationCommands.NotACommand" Gesture="Ctrl+V" />
|
||||
<KeyBinding Command="ApplicationCommands.NotACommand" Gesture="Delete" />
|
||||
<KeyBinding Command="ApplicationCommands.NotACommand" Gesture="Enter" />
|
||||
</DataGrid.InputBindings>
|
||||
<DataGrid.ContextMenu>
|
||||
<ContextMenu Style="{StaticResource DefContextMenu}">
|
||||
<MenuItem
|
||||
x:Name="menuEditServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuEditServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuSetDefaultServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuSetDefaultServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuRemoveServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuRemoveServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuRemoveDuplicateServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuRemoveDuplicateServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuCopyServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuCopyServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuShareServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuShareServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuSetDefaultMultipleServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuSetDefaultMultipleServer}" />
|
||||
<Separator />
|
||||
<MenuItem
|
||||
x:Name="menuMixedTestServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuMixedTestServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuTcpingServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuTcpingServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuRealPingServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuRealPingServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuSpeedServer"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuSpeedServer}" />
|
||||
<MenuItem
|
||||
x:Name="menuSortServerResult"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuSortServerResult}" />
|
||||
<Separator />
|
||||
<MenuItem
|
||||
x:Name="menuMoveToGroup"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuMoveToGroup}">
|
||||
<MenuItem Height="Auto">
|
||||
<MenuItem.Header>
|
||||
<DockPanel>
|
||||
<ComboBox
|
||||
x:Name="cmbMoveToGroup"
|
||||
Width="200"
|
||||
materialDesign:HintAssist.Hint="{x:Static resx:ResUI.menuSubscription}"
|
||||
DisplayMemberPath="remarks"
|
||||
FontSize="{DynamicResource StdFontSize}"
|
||||
Style="{StaticResource MaterialDesignFilledComboBox}" />
|
||||
</DockPanel>
|
||||
</MenuItem.Header>
|
||||
</MenuItem>
|
||||
</MenuItem>
|
||||
<MenuItem Header="{x:Static resx:ResUI.menuMoveTo}">
|
||||
<MenuItem
|
||||
x:Name="menuMoveTop"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuMoveTop}" />
|
||||
<MenuItem
|
||||
x:Name="menuMoveUp"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuMoveUp}" />
|
||||
<MenuItem
|
||||
x:Name="menuMoveDown"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuMoveDown}" />
|
||||
<MenuItem
|
||||
x:Name="menuMoveBottom"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuMoveBottom}" />
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
x:Name="menuSelectAll"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Click="menuSelectAll_Click"
|
||||
Header="{x:Static resx:ResUI.menuSelectAll}" />
|
||||
<Separator />
|
||||
<MenuItem
|
||||
x:Name="menuExport2ClientConfig"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuExport2ClientConfig}" />
|
||||
<MenuItem
|
||||
x:Name="menuExport2ShareUrl"
|
||||
Height="{StaticResource MenuItemHeight}"
|
||||
Header="{x:Static resx:ResUI.menuExport2ShareUrl}" />
|
||||
</ContextMenu>
|
||||
</DataGrid.ContextMenu>
|
||||
<DataGrid.Resources>
|
||||
<Style BasedOn="{StaticResource MaterialDesignDataGridRow}" TargetType="DataGridRow">
|
||||
<EventSetter Event="MouseDoubleClick" Handler="LstProfiles_MouseDoubleClick" />
|
||||
</Style>
|
||||
<Style BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" TargetType="DataGridColumnHeader">
|
||||
<EventSetter Event="Click" Handler="LstProfiles_ColumnHeader_Click" />
|
||||
</Style>
|
||||
|
||||
<Style BasedOn="{StaticResource MaterialDesignDataGridCell}" TargetType="DataGridCell">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding isActive}" Value="True">
|
||||
<Setter Property="Background" Value="{DynamicResource MaterialDesign.Brush.Primary.Light}" />
|
||||
<Setter Property="Foreground" Value="Black" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesign.Brush.Primary.Light}" />
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</DataGrid.Resources>
|
||||
<DataGrid.Columns>
|
||||
<base:MyDGTextColumn
|
||||
Width="80"
|
||||
Binding="{Binding configType}"
|
||||
ExName="configType"
|
||||
Header="{x:Static resx:ResUI.LvServiceType}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="150"
|
||||
Binding="{Binding remarks}"
|
||||
ExName="remarks"
|
||||
Header="{x:Static resx:ResUI.LvRemarks}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="120"
|
||||
Binding="{Binding address}"
|
||||
ExName="address"
|
||||
Header="{x:Static resx:ResUI.LvAddress}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="60"
|
||||
Binding="{Binding port}"
|
||||
ExName="port"
|
||||
Header="{x:Static resx:ResUI.LvPort}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="100"
|
||||
Binding="{Binding network}"
|
||||
ExName="network"
|
||||
Header="{x:Static resx:ResUI.LvTransportProtocol}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="100"
|
||||
Binding="{Binding streamSecurity}"
|
||||
ExName="streamSecurity"
|
||||
Header="{x:Static resx:ResUI.LvTLS}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="100"
|
||||
Binding="{Binding subRemarks}"
|
||||
ExName="subRemarks"
|
||||
Header="{x:Static resx:ResUI.LvSubscription}" />
|
||||
<base:MyDGTextColumn
|
||||
Width="100"
|
||||
Binding="{Binding delayVal}"
|
||||
ExName="delayVal"
|
||||
Header="{x:Static resx:ResUI.LvTestDelay}">
|
||||
<DataGridTextColumn.ElementStyle>
|
||||
<Style TargetType="{x:Type TextBlock}">
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
<Setter Property="Foreground" Value="{Binding delay, Converter={StaticResource DelayColorConverter}}" />
|
||||
</Style>
|
||||
</DataGridTextColumn.ElementStyle>
|
||||
</base:MyDGTextColumn>
|
||||
<base:MyDGTextColumn
|
||||
Width="100"
|
||||
Binding="{Binding speedVal}"
|
||||
ExName="speedVal"
|
||||
Header="{x:Static resx:ResUI.LvTestSpeed}">
|
||||
<DataGridTextColumn.ElementStyle>
|
||||
<Style TargetType="{x:Type TextBlock}">
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
</Style>
|
||||
</DataGridTextColumn.ElementStyle>
|
||||
</base:MyDGTextColumn>
|
||||
|
||||
<base:MyDGTextColumn
|
||||
x:Name="colTodayUp"
|
||||
Width="100"
|
||||
Binding="{Binding todayUp}"
|
||||
ExName="todayUp"
|
||||
Header="{x:Static resx:ResUI.LvTodayUploadDataAmount}" />
|
||||
<base:MyDGTextColumn
|
||||
x:Name="colTodayDown"
|
||||
Width="100"
|
||||
Binding="{Binding todayDown}"
|
||||
ExName="todayDown"
|
||||
Header="{x:Static resx:ResUI.LvTodayDownloadDataAmount}" />
|
||||
<base:MyDGTextColumn
|
||||
x:Name="colTotalUp"
|
||||
Width="100"
|
||||
Binding="{Binding totalUp}"
|
||||
ExName="totalUp"
|
||||
Header="{x:Static resx:ResUI.LvTotalUploadDataAmount}" />
|
||||
<base:MyDGTextColumn
|
||||
x:Name="colTotalDown"
|
||||
Width="100"
|
||||
Binding="{Binding totalDown}"
|
||||
ExName="totalDown"
|
||||
Header="{x:Static resx:ResUI.LvTotalDownloadDataAmount}" />
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</DockPanel>
|
||||
</Grid>
|
||||
</reactiveui:ReactiveUserControl>
|
@ -0,0 +1,399 @@
|
||||
using ReactiveUI;
|
||||
using Splat;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using v2rayN.Base;
|
||||
using v2rayN.Enums;
|
||||
using v2rayN.Handler;
|
||||
using v2rayN.Models;
|
||||
using v2rayN.ViewModels;
|
||||
using Point = System.Windows.Point;
|
||||
|
||||
namespace v2rayN.Views
|
||||
{
|
||||
public partial class ProfilesView
|
||||
{
|
||||
private static Config _config;
|
||||
|
||||
public ProfilesView()
|
||||
{
|
||||
InitializeComponent();
|
||||
lstGroup.MaxHeight = Math.Floor(SystemParameters.WorkArea.Height * 0.20 / 40) * 40;
|
||||
|
||||
_config = LazyConfig.Instance.GetConfig();
|
||||
|
||||
Application.Current.Exit += Current_Exit;
|
||||
btnAutofitColumnWidth.Click += BtnAutofitColumnWidth_Click;
|
||||
txtServerFilter.PreviewKeyDown += TxtServerFilter_PreviewKeyDown;
|
||||
lstProfiles.PreviewKeyDown += LstProfiles_PreviewKeyDown;
|
||||
lstProfiles.SelectionChanged += lstProfiles_SelectionChanged;
|
||||
lstProfiles.LoadingRow += LstProfiles_LoadingRow;
|
||||
if (_config.uiItem.enableDragDropSort)
|
||||
{
|
||||
lstProfiles.AllowDrop = true;
|
||||
lstProfiles.PreviewMouseLeftButtonDown += LstProfiles_PreviewMouseLeftButtonDown;
|
||||
lstProfiles.MouseMove += LstProfiles_MouseMove;
|
||||
lstProfiles.DragEnter += LstProfiles_DragEnter;
|
||||
lstProfiles.Drop += LstProfiles_Drop;
|
||||
}
|
||||
|
||||
ViewModel = new ProfilesViewModel(UpdateViewHandler);
|
||||
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(ProfilesViewModel));
|
||||
|
||||
this.WhenActivated(disposables =>
|
||||
{
|
||||
this.OneWayBind(ViewModel, vm => vm.ProfileItems, v => v.lstProfiles.ItemsSource).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.SelectedProfile, v => v.lstProfiles.SelectedItem).DisposeWith(disposables);
|
||||
|
||||
this.OneWayBind(ViewModel, vm => vm.SubItems, v => v.lstGroup.ItemsSource).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.SelectedSub, v => v.lstGroup.SelectedItem).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.ServerFilter, v => v.txtServerFilter.Text).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.AddSubCmd, v => v.btnAddSub).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.EditSubCmd, v => v.btnEditSub).DisposeWith(disposables);
|
||||
|
||||
//servers delete
|
||||
this.BindCommand(ViewModel, vm => vm.EditServerCmd, v => v.menuEditServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.RemoveServerCmd, v => v.menuRemoveServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.RemoveDuplicateServerCmd, v => v.menuRemoveDuplicateServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.CopyServerCmd, v => v.menuCopyServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.SetDefaultServerCmd, v => v.menuSetDefaultServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.ShareServerCmd, v => v.menuShareServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.SetDefaultMultipleServerCmd, v => v.menuSetDefaultMultipleServer).DisposeWith(disposables);
|
||||
|
||||
//servers move
|
||||
this.OneWayBind(ViewModel, vm => vm.SubItems, v => v.cmbMoveToGroup.ItemsSource).DisposeWith(disposables);
|
||||
this.Bind(ViewModel, vm => vm.SelectedMoveToGroup, v => v.cmbMoveToGroup.SelectedItem).DisposeWith(disposables);
|
||||
|
||||
this.BindCommand(ViewModel, vm => vm.MoveTopCmd, v => v.menuMoveTop).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.MoveUpCmd, v => v.menuMoveUp).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.MoveDownCmd, v => v.menuMoveDown).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.MoveBottomCmd, v => v.menuMoveBottom).DisposeWith(disposables);
|
||||
|
||||
//servers ping
|
||||
this.BindCommand(ViewModel, vm => vm.MixedTestServerCmd, v => v.menuMixedTestServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.TcpingServerCmd, v => v.menuTcpingServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.RealPingServerCmd, v => v.menuRealPingServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.SpeedServerCmd, v => v.menuSpeedServer).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.SortServerResultCmd, v => v.menuSortServerResult).DisposeWith(disposables);
|
||||
|
||||
//servers export
|
||||
this.BindCommand(ViewModel, vm => vm.Export2ClientConfigCmd, v => v.menuExport2ClientConfig).DisposeWith(disposables);
|
||||
this.BindCommand(ViewModel, vm => vm.Export2ShareUrlCmd, v => v.menuExport2ShareUrl).DisposeWith(disposables);
|
||||
});
|
||||
|
||||
RestoreUI();
|
||||
}
|
||||
|
||||
#region Event
|
||||
|
||||
private void Current_Exit(object sender, ExitEventArgs e)
|
||||
{
|
||||
StorageUI();
|
||||
}
|
||||
|
||||
private void UpdateViewHandler(EViewAction action)
|
||||
{
|
||||
if (action == EViewAction.AdjustMainLvColWidth)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
AutofitColumnWidth();
|
||||
});
|
||||
}
|
||||
else if (action == EViewAction.ProfilesFocus)
|
||||
{
|
||||
lstProfiles.Focus();
|
||||
}
|
||||
}
|
||||
|
||||
private void lstProfiles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
|
||||
{
|
||||
ViewModel.SelectedProfiles = lstProfiles.SelectedItems.Cast<ProfileItemModel>().ToList();
|
||||
}
|
||||
|
||||
private void LstProfiles_LoadingRow(object? sender, DataGridRowEventArgs e)
|
||||
{
|
||||
e.Row.Header = $" {e.Row.GetIndex() + 1}";
|
||||
}
|
||||
|
||||
private void LstProfiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (_config.uiItem.doubleClick2Activate)
|
||||
{
|
||||
ViewModel?.SetDefaultServer();
|
||||
}
|
||||
else
|
||||
{
|
||||
ViewModel?.EditServer(false, EConfigType.Custom);
|
||||
}
|
||||
}
|
||||
|
||||
private void LstProfiles_ColumnHeader_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var colHeader = sender as DataGridColumnHeader;
|
||||
if (colHeader == null || colHeader.TabIndex < 0 || colHeader.Column == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var colName = ((MyDGTextColumn)colHeader.Column).ExName;
|
||||
ViewModel?.SortServer(colName);
|
||||
}
|
||||
|
||||
private void menuSelectAll_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
lstProfiles.SelectAll();
|
||||
}
|
||||
|
||||
private void LstProfiles_PreviewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
|
||||
{
|
||||
switch (e.Key)
|
||||
{
|
||||
case Key.A:
|
||||
menuSelectAll_Click(null, null);
|
||||
break;
|
||||
|
||||
case Key.C:
|
||||
ViewModel?.Export2ShareUrl();
|
||||
break;
|
||||
|
||||
case Key.D:
|
||||
ViewModel?.EditServer(false, EConfigType.Custom);
|
||||
break;
|
||||
|
||||
case Key.F:
|
||||
ViewModel?.ShareServer();
|
||||
break;
|
||||
|
||||
case Key.O:
|
||||
ViewModel?.ServerSpeedtest(ESpeedActionType.Tcping);
|
||||
break;
|
||||
|
||||
case Key.R:
|
||||
ViewModel?.ServerSpeedtest(ESpeedActionType.Realping);
|
||||
break;
|
||||
|
||||
case Key.T:
|
||||
ViewModel?.ServerSpeedtest(ESpeedActionType.Speedtest);
|
||||
break;
|
||||
|
||||
case Key.E:
|
||||
ViewModel?.ServerSpeedtest(ESpeedActionType.Mixedtest);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (e.Key is Key.Enter or Key.Return)
|
||||
{
|
||||
ViewModel?.SetDefaultServer();
|
||||
}
|
||||
else if (e.Key == Key.Delete)
|
||||
{
|
||||
ViewModel?.RemoveServer();
|
||||
}
|
||||
else if (e.Key == Key.T)
|
||||
{
|
||||
ViewModel?.MoveServer(EMove.Top);
|
||||
}
|
||||
else if (e.Key == Key.U)
|
||||
{
|
||||
ViewModel?.MoveServer(EMove.Up);
|
||||
}
|
||||
else if (e.Key == Key.D)
|
||||
{
|
||||
ViewModel?.MoveServer(EMove.Down);
|
||||
}
|
||||
else if (e.Key == Key.B)
|
||||
{
|
||||
ViewModel?.MoveServer(EMove.Bottom);
|
||||
}
|
||||
else if (e.Key == Key.Escape)
|
||||
{
|
||||
MessageBus.Current.SendMessage("true", Global.CommandStopSpeedTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void BtnAutofitColumnWidth_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
AutofitColumnWidth();
|
||||
}
|
||||
|
||||
private void AutofitColumnWidth()
|
||||
{
|
||||
foreach (var it in lstProfiles.Columns)
|
||||
{
|
||||
it.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
|
||||
}
|
||||
}
|
||||
|
||||
private void TxtServerFilter_PreviewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key is Key.Enter or Key.Return)
|
||||
{
|
||||
ViewModel?.RefreshServers();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Event
|
||||
|
||||
#region UI
|
||||
|
||||
private void RestoreUI()
|
||||
{
|
||||
var lvColumnItem = _config.uiItem.mainColumnItem.OrderBy(t => t.Index).ToList();
|
||||
var displayIndex = 0;
|
||||
foreach (var item in lvColumnItem)
|
||||
{
|
||||
foreach (MyDGTextColumn item2 in lstProfiles.Columns)
|
||||
{
|
||||
if (item2.ExName == item.Name)
|
||||
{
|
||||
if (item.Width < 0)
|
||||
{
|
||||
item2.Visibility = Visibility.Hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
item2.Width = item.Width;
|
||||
item2.DisplayIndex = displayIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!_config.guiItem.enableStatistics)
|
||||
{
|
||||
colTodayUp.Visibility =
|
||||
colTodayDown.Visibility =
|
||||
colTotalUp.Visibility =
|
||||
colTotalDown.Visibility = Visibility.Hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
colTodayUp.Visibility =
|
||||
colTodayDown.Visibility =
|
||||
colTotalUp.Visibility =
|
||||
colTotalDown.Visibility = Visibility.Visible;
|
||||
}
|
||||
}
|
||||
|
||||
private void StorageUI()
|
||||
{
|
||||
List<ColumnItem> lvColumnItem = new();
|
||||
for (int k = 0; k < lstProfiles.Columns.Count; k++)
|
||||
{
|
||||
var item2 = (MyDGTextColumn)lstProfiles.Columns[k];
|
||||
lvColumnItem.Add(new()
|
||||
{
|
||||
Name = item2.ExName,
|
||||
Width = item2.Visibility == Visibility.Visible ? Utils.ToInt(item2.ActualWidth) : -1,
|
||||
Index = item2.DisplayIndex
|
||||
});
|
||||
}
|
||||
_config.uiItem.mainColumnItem = lvColumnItem;
|
||||
ConfigHandler.SaveConfig(_config);
|
||||
}
|
||||
|
||||
#endregion UI
|
||||
|
||||
#region Drag and Drop
|
||||
|
||||
private Point startPoint = new();
|
||||
private int startIndex = -1;
|
||||
private string formatData = "ProfileItemModel";
|
||||
|
||||
/// <summary>
|
||||
/// Helper to search up the VisualTree
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="current"></param>
|
||||
/// <returns></returns>
|
||||
private static T? FindAncestor<T>(DependencyObject current) where T : DependencyObject
|
||||
{
|
||||
do
|
||||
{
|
||||
if (current is T)
|
||||
{
|
||||
return (T)current;
|
||||
}
|
||||
current = VisualTreeHelper.GetParent(current);
|
||||
}
|
||||
while (current != null);
|
||||
return null;
|
||||
}
|
||||
|
||||
private void LstProfiles_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
// Get current mouse position
|
||||
startPoint = e.GetPosition(null);
|
||||
}
|
||||
|
||||
private void LstProfiles_MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
// Get the current mouse position
|
||||
Point mousePos = e.GetPosition(null);
|
||||
Vector diff = startPoint - mousePos;
|
||||
|
||||
if (e.LeftButton == MouseButtonState.Pressed &&
|
||||
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
|
||||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
|
||||
{
|
||||
// Get the dragged Item
|
||||
if (sender is not DataGrid listView) return;
|
||||
var listViewItem = FindAncestor<DataGridRow>((DependencyObject)e.OriginalSource);
|
||||
if (listViewItem == null) return; // Abort
|
||||
// Find the data behind the ListViewItem
|
||||
ProfileItemModel item = (ProfileItemModel)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
|
||||
if (item == null) return; // Abort
|
||||
// Initialize the drag & drop operation
|
||||
startIndex = lstProfiles.SelectedIndex;
|
||||
DataObject dragData = new(formatData, item);
|
||||
DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Copy | DragDropEffects.Move);
|
||||
}
|
||||
}
|
||||
|
||||
private void LstProfiles_DragEnter(object sender, DragEventArgs e)
|
||||
{
|
||||
if (!e.Data.GetDataPresent(formatData) || sender != e.Source)
|
||||
{
|
||||
e.Effects = DragDropEffects.None;
|
||||
}
|
||||
}
|
||||
|
||||
private void LstProfiles_Drop(object sender, DragEventArgs e)
|
||||
{
|
||||
if (e.Data.GetDataPresent(formatData) && sender == e.Source)
|
||||
{
|
||||
// Get the drop Item destination
|
||||
if (sender is not DataGrid listView) return;
|
||||
var listViewItem = FindAncestor<DataGridRow>((DependencyObject)e.OriginalSource);
|
||||
if (listViewItem == null)
|
||||
{
|
||||
// Abort
|
||||
e.Effects = DragDropEffects.None;
|
||||
return;
|
||||
}
|
||||
// Find the data behind the Item
|
||||
ProfileItemModel item = (ProfileItemModel)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
|
||||
if (item == null) return;
|
||||
// Move item into observable collection
|
||||
// (this will be automatically reflected to lstView.ItemsSource)
|
||||
e.Effects = DragDropEffects.Move;
|
||||
|
||||
ViewModel?.MoveServerTo(startIndex, item);
|
||||
|
||||
startIndex = -1;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Drag and Drop
|
||||
}
|
||||
}
|
Loading…
Reference in new issue