Skip to content

Commit

Permalink
💾 Feat(Dashboard): Optimized PluginsLaunchWindow.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Feb 24, 2024
1 parent 9d2773b commit 069443d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Common.BasicHelper.Utils.Extensions;
using KitX.Dashboard.Managers;
using KitX.Dashboard.Names;
using KitX.Dashboard.Network.PluginsNetwork;
using KitX.Dashboard.Views;
using KitX.Shared.Device;
using Serilog;
using System;
Expand Down Expand Up @@ -115,7 +115,7 @@ private static void UpdateDefaultDeviceInfo()
.ResetIPv6(NetworkHelper.GetInterNetworkIPv6())
;
DefaultDeviceInfo.PluginsServerPort = ConstantTable.PluginsServerPort;
DefaultDeviceInfo.PluginsCount = PluginsServer.Instance.PluginConnectors.Count;
DefaultDeviceInfo.PluginsCount = ViewInstances.PluginInfos.Count;
DefaultDeviceInfo.IsMainDevice = ConstantTable.IsMainMachine;
DefaultDeviceInfo.DevicesServerPort = ConstantTable.DevicesServerPort;
DefaultDeviceInfo.DevicesServerBuildTime = ConstantTable.ServerBuildTime;
Expand Down Expand Up @@ -230,7 +230,7 @@ private void MultiDevicesBroadCastReceive()
{
var location = $"{nameof(DevicesDiscoveryServer)}.{nameof(MultiDevicesBroadCastReceive)}";

IPEndPoint multicast = new(IPAddress.Any, 0);
var multicast = new IPEndPoint(IPAddress.Any, 0);
UdpReceiver?.Client.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress,
Expand Down
4 changes: 2 additions & 2 deletions KitX Dashboard/Network/NetworkHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Common.BasicHelper.Core.Shell;
using Common.BasicHelper.Utils.Extensions;
using KitX.Dashboard.Converters;
using KitX.Dashboard.Network.PluginsNetwork;
using KitX.Dashboard.Views;
using KitX.Shared.Device;
using Serilog;
using System;
Expand Down Expand Up @@ -221,6 +221,6 @@ from nic in NetworkInterface.GetAllNetworkInterfaces()
PluginsServerPort = ConstantTable.PluginsServerPort,
DevicesServerPort = ConstantTable.DevicesServerPort,
DevicesServerBuildTime = new(),
PluginsCount = PluginsServer.Instance.PluginConnectors.Count,
PluginsCount = ViewInstances.PluginInfos.Count,
};
}
6 changes: 3 additions & 3 deletions KitX Dashboard/Network/PluginsNetwork/PluginsServer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Fleck;
using KitX.Dashboard.Configuration;
using KitX.Dashboard.Services;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
Expand All @@ -16,9 +16,9 @@ public class PluginsServer : ConfigFetcher

private WebSocketServer? _server;

private readonly ObservableCollection<PluginConnector> _connectors = [];
private readonly List<PluginConnector> _connectors = [];

public ObservableCollection<PluginConnector> PluginConnectors => _connectors;
public List<PluginConnector> PluginConnectors => _connectors;

public PluginsServer()
{
Expand Down
27 changes: 21 additions & 6 deletions KitX Dashboard/ViewModels/PluginsLaunchWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using KitX.Dashboard.Views;
using Avalonia;
using Avalonia.Controls;
using KitX.Dashboard.Views;
using KitX.Shared.Plugin;
using ReactiveUI;
using System.Collections.ObjectModel;
using Avalonia;
using System;
using Avalonia.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace KitX.Dashboard.ViewModels;

Expand All @@ -27,6 +29,8 @@ public override void InitEvents()
PluginInfos.CollectionChanged += (_, _) =>
{
PluginsCount = $"{PluginInfos.Count}";
this.RaisePropertyChanged(nameof(SearchItems));
};
}

Expand All @@ -45,7 +49,7 @@ public string PluginsCount

public static double NoPlugins_TipHeight => PluginInfos.Count == 0 ? 40 : 0;

private int selectedPluginIndex = 0;
private int selectedPluginIndex = -1;

public int SelectedPluginIndex
{
Expand All @@ -59,7 +63,7 @@ public int SelectedPluginIndex

public PluginInfo? SelectedPluginInfo
{
get => (SelectedPluginIndex >= 0 && SelectedPluginIndex < PluginInfos.Count) ? PluginInfos[SelectedPluginIndex] : null;
get => SelectedPluginIndexInRange(SelectedPluginIndex) ? PluginInfos[SelectedPluginIndex] : null;
set
{
if (value is null) return;
Expand Down Expand Up @@ -88,6 +92,17 @@ public Parameter? SelectedParameter

public static ObservableCollection<PluginInfo> PluginInfos => ViewInstances.PluginInfos;

public static IEnumerable<string> SearchItems => PluginInfos.Select(x => x.Name).Distinct();
//.Concat(
// PluginInfos.SelectMany(
// x => x.SimpleDescription.Select(x => x.Value)
// )
//).Concat(
// PluginInfos.SelectMany(
// x => x.ComplexDescription.Select(x => x.Value)
// )
//).Distinct();

public string? SearchingText { get; set; }

private Vector scrollViewerOffset = new(0, 0);
Expand Down
15 changes: 12 additions & 3 deletions KitX Dashboard/Views/PluginsLaunchWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
ExtendClientAreaChromeHints="NoChrome"
ExtendClientAreaToDecorationsHint="True"
Icon="avares://KitX.Dashboard.Assets/KitX-Icon-32x32.png"
KeyDown="PluginsLaunchWindow_KeyDown"
PointerPressed="PluginsLaunchWindow_PointerPressed"
Topmost="True"
TransparencyLevelHint="Mica"
WindowStartupLocation="CenterScreen"
Expand All @@ -35,10 +33,14 @@
<Grid Height="80"
Margin="0,10,0,0"
DockPanel.Dock="Top">
<AutoCompleteBox Width="400"
<AutoCompleteBox x:Name="MainAutoCompleteBox"
Width="400"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FilterMode="Contains"
Focusable="True"
IsTextCompletionEnabled="True"
ItemsSource="{Binding SearchItems}"
Text="{Binding SearchingText}"
Watermark="{DynamicResource Text_Lib_Search_In_Local}"/>
</Grid>
Expand All @@ -63,6 +65,7 @@
</Grid>

<ScrollViewer x:Name="PluginsScrollViewer"
Focusable="True"
HorizontalScrollBarVisibility="Disabled"
Offset="{Binding ScrollViewerOffset}">
<ScrollViewer.Transitions>
Expand Down Expand Up @@ -98,6 +101,7 @@
<Button.Styles>
<Style Selector="Button.selected">
<Setter Property="RenderTransform" Value="scale(1.2)"/>
<Setter Property="BorderBrush" Value="{DynamicResource ThemePrimaryAccent}"/>
</Style>
</Button.Styles>
<Button.Transitions>
Expand All @@ -107,6 +111,11 @@
<CubicEaseInOut/>
</TransformOperationsTransition.Easing>
</TransformOperationsTransition>
<BrushTransition Property="BorderBrush" Duration="0:0:0.25">
<BrushTransition.Easing>
<CubicEaseInOut/>
</BrushTransition.Easing>
</BrushTransition>
</Transitions>
</Button.Transitions>
<Border Width="50"
Expand Down
128 changes: 93 additions & 35 deletions KitX Dashboard/Views/PluginsLaunchWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public partial class PluginsLaunchWindow : Window

private bool pluginsLaunchWindowDisplayed = false;

private int? previousSelectedPluginIndex = null;

public PluginsLaunchWindow()
{
var location = $"{nameof(PluginsLaunchWindow)}.ctor";
Expand All @@ -26,7 +28,7 @@ public PluginsLaunchWindow()

DataContext = viewModel;

OnHide(() => pluginsLaunchWindowDisplayed = false);
OnHideAction = () => pluginsLaunchWindowDisplayed = false;

EventService.OnExiting += Close;

Expand All @@ -42,9 +44,55 @@ public PluginsLaunchWindow()
}
}

if (this.FindControl<AutoCompleteBox>("MainAutoCompleteBox") is AutoCompleteBox box)
{
//box.AttachedToVisualTree += (_, _) => box.Focus();
//box.TextChanged += (_, _) =>
//{
// if (box.Text?.Equals("`") ?? false)
// box.Text = "";
//};
//box.KeyDown += (_, e) =>
//{
// if (e.PhysicalKey == PhysicalKey.Backquote)
// e.Handled = true;
//};
}

if (this.FindControl<ScrollViewer>("PluginsScrollViewer") is ScrollViewer viewer)
{
viewer.KeyDown += PluginsScrollViewer_KeyDown;
}

RegisterGlobalHotKey();
}

private void PluginsScrollViewer_KeyDown(object? sender, KeyEventArgs e)
{
if (sender is not ScrollViewer viewer) return;

if (viewer.IsFocused == false) return;

switch (e.Key)
{
case Key.Left:
viewModel.SelectLeftOne(Width, sender);
break;
case Key.Right:
viewModel.SelectRightOne(Width, sender);
break;
case Key.Up:
viewModel.SelectUpOne(Width, sender);
break;
case Key.Down:
viewModel.SelectDownOne(Width, sender);
break;
case Key.Enter:
viewModel.SelectPluginInfo();
break;
}
}

private void RegisterGlobalHotKey()
{
Instances.KeyHookManager?.RegisterHotKeyHandler(nameof(PluginsLaunchWindow), codes =>
Expand Down Expand Up @@ -79,51 +127,59 @@ private void RegisterGlobalHotKey()
});
}

public PluginsLaunchWindow OnHide(Action onHideAction)
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
OnHideAction = onHideAction;
BeginMoveDrag(e);

return this;
base.OnPointerPressed(e);
}

private void PluginsLaunchWindow_PointerPressed(object? sender, PointerPressedEventArgs e) => BeginMoveDrag(e);

private void PluginsLaunchWindow_KeyDown(object? sender, KeyEventArgs e)
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Tab:
e.Handled = true;
break;
case Key.Escape:
Hide();
OnHideAction?.Invoke();
break;
case Key.Left:
viewModel.SelectLeftOne(
Width,
this.FindControl<ScrollViewer>("PluginsScrollViewer")
);
break;
case Key.Right:
viewModel.SelectRightOne(
Width,
this.FindControl<ScrollViewer>("PluginsScrollViewer")
);
break;
case Key.Up:
viewModel.SelectUpOne(
Width,
this.FindControl<ScrollViewer>("PluginsScrollViewer")
);
break;
case Key.Down:
viewModel.SelectDownOne(
Width,
this.FindControl<ScrollViewer>("PluginsScrollViewer")
);
break;
case Key.Enter:
viewModel.SelectPluginInfo();
}

switch (e.PhysicalKey)
{
case PhysicalKey.Backquote:
if (viewModel.SelectedPluginIndex == -1)
{
viewModel.SelectedPluginIndex = previousSelectedPluginIndex ?? 0;

if (this.FindControl<ScrollViewer>("PluginsScrollViewer") is ScrollViewer viewer)
viewer.Focus();
}
else
{
previousSelectedPluginIndex = viewModel.SelectedPluginIndex;

viewModel.SelectedPluginIndex = -1;

if (this.FindControl<AutoCompleteBox>("MainAutoCompleteBox") is AutoCompleteBox box)
box.Focus();
}
e.Handled = true;
break;
}

base.OnKeyDown(e);
}

protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);

//if (viewModel.SelectedPluginIndex == -1)
// if (this.FindControl<AutoCompleteBox>("MainAutoCompleteBox") is AutoCompleteBox box)
// box.Focus();
}

protected override void OnResized(WindowResizedEventArgs e)
Expand All @@ -141,12 +197,12 @@ protected override void OnResized(WindowResizedEventArgs e)
else
Width = (oneLineCount + 1) * basicWidth + addonWidth;
}

base.OnResized(e);
}

protected override void OnClosing(WindowClosingEventArgs e)
{
base.OnClosing(e);

if (!ConstantTable.Exiting)
{
e.Cancel = true;
Expand All @@ -155,5 +211,7 @@ protected override void OnClosing(WindowClosingEventArgs e)

OnHideAction?.Invoke();
}

base.OnClosing(e);
}
}

0 comments on commit 069443d

Please sign in to comment.