Skip to content

Commit

Permalink
searchcombobox improvements +
Browse files Browse the repository at this point in the history
load fpgas from documents directory
  • Loading branch information
HendrikMennen committed Aug 1, 2024
1 parent 55450c8 commit 6ff05da
Show file tree
Hide file tree
Showing 19 changed files with 1,253 additions and 29 deletions.
7 changes: 7 additions & 0 deletions OneWare.sln
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneWare.Vhdl.UnitTests", "t
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneWare.WaveFormViewer.Tests", "tests\OneWare.WaveFormViewer.Tests\OneWare.WaveFormViewer.Tests.csproj", "{AAC4D6FA-4F94-4616-93B3-3BB47E4A0BB1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneWare.UniversalFpgaProjectSystem.Tests", "tests\OneWare.UniversalFpgaProjectSystem.Tests\OneWare.UniversalFpgaProjectSystem.Tests.csproj", "{484F3E48-EF14-4801-B896-41BDB67A54E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -378,6 +380,10 @@ Global
{AAC4D6FA-4F94-4616-93B3-3BB47E4A0BB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAC4D6FA-4F94-4616-93B3-3BB47E4A0BB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAC4D6FA-4F94-4616-93B3-3BB47E4A0BB1}.Release|Any CPU.Build.0 = Release|Any CPU
{484F3E48-EF14-4801-B896-41BDB67A54E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{484F3E48-EF14-4801-B896-41BDB67A54E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{484F3E48-EF14-4801-B896-41BDB67A54E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{484F3E48-EF14-4801-B896-41BDB67A54E6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F08B36E8-AB80-42CD-BD47-6B05E96DA390} = {0761690C-7DA0-4554-9F6B-211088412DCD}
Expand Down Expand Up @@ -437,5 +443,6 @@ Global
{3174F07C-F96E-4695-B05D-CE91845CF292} = {EB783E04-C3C8-45F8-B810-24798DAE2450}
{8EF6E36F-C63A-4F2A-99CF-8F3AE2A5C946} = {EB783E04-C3C8-45F8-B810-24798DAE2450}
{AAC4D6FA-4F94-4616-93B3-3BB47E4A0BB1} = {EB783E04-C3C8-45F8-B810-24798DAE2450}
{484F3E48-EF14-4801-B896-41BDB67A54E6} = {EB783E04-C3C8-45F8-B810-24798DAE2450}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/OneWare.Cyc5000/Cyc5000Fpga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class Cyc5000Fpga : FpgaBase
{
public Cyc5000Fpga()
{
LoadFromJson("avares://OneWare.Cyc5000/Assets/Cyc5000.json");
LoadFromJsonAsset("avares://OneWare.Cyc5000/Assets/Cyc5000.json");
}
}
10 changes: 10 additions & 0 deletions src/OneWare.Essentials/Controls/SearchComboBox.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,14 @@
</Setter>
</ControlTheme>
</Styles.Resources>

<Style Selector="ComboBoxItem:selected /template/ ContentPresenter">
<Setter Property="Background" Value="{DynamicResource ThemeControlHighlightLowBrush}"/>
</Style>
<Style Selector="ComboBoxItem:searchResult /template/ ContentPresenter">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush3}"/>
</Style>
<Style Selector="ComboBoxItem:searchResult:selected /template/ ContentPresenter">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush2}"/>
</Style>
</Styles>
107 changes: 97 additions & 10 deletions src/OneWare.Essentials/Controls/SearchComboBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,50 @@ public class SearchComboBox : ComboBox
{
private TextBox? _searchBox;

public static readonly StyledProperty<bool> IsInteractiveProperty =
AvaloniaProperty.Register<SearchComboBox, bool>(nameof(IsInteractive), true);

public bool IsInteractive
{
get => GetValue(IsInteractiveProperty);
set => SetValue(IsInteractiveProperty, value);
}

protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
{
return new SearchComboBoxItem();
}

private int _resultIndex = -1;

public int ResultIndex
{
get => _resultIndex;
set
{
_resultIndex = value;
ResultItem = ContainerFromIndex(value) as SearchComboBoxItem;
ScrollIntoView(value);
}
}

private SearchComboBoxItem? _resultItem;

public SearchComboBoxItem? ResultItem
{
get => _resultItem;
private set
{
if(_resultItem != null) _resultItem.IsSearchResult = false;
_resultItem = value;

if (_resultItem != null)
{
_resultItem.IsSearchResult = true;
}
}
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Expand All @@ -18,39 +62,82 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

_searchBox!.TextChanged += (sender, args) =>
{
SelectedItem = Items.FirstOrDefault(x =>
x?.ToString()?.StartsWith(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase) ?? false);
var item = Items.FirstOrDefault(x =>
x?.ToString()?.StartsWith(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase)
?? x?.ToString()?.Contains(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase)
?? false);

if (IsInteractive)
{
SelectedItem = item;
}
else
{
if (item != null)
{
ResultIndex = Items.IndexOf(item);
}
}

_searchBox.Focus();
};

this.DropDownOpened += (sender, args) =>
{
_searchBox.Text = string.Empty;
_searchBox.Focus();
};
}

protected override void OnKeyDown(KeyEventArgs e)
{
if(_searchBox == null) return;

if (e.Key == Key.Down)
{
if (SelectedIndex < Items.Count - 1)
SelectedIndex++;
if (IsInteractive)
{
if (SelectedIndex < Items.Count - 1)
SelectedIndex++;
}
else
{
if (ResultIndex < Items.Count - 1)
ResultIndex++;
}

e.Handled = true;

_searchBox?.Focus();
_searchBox.Focus();
return;
}
if (e.Key == Key.Up)
{
if (SelectedIndex > 0)
SelectedIndex--;
if (IsInteractive)
{
if (SelectedIndex > 0)
SelectedIndex--;
}
else
{
if (ResultIndex > 0)
ResultIndex--;
}

e.Handled = true;

_searchBox?.Focus();
_searchBox.Focus();
return;
}
if (e.Key == Key.Enter && !IsInteractive)
{
SelectedIndex = ResultIndex;
_searchBox.Text = string.Empty;
}
if (e.Key == Key.Space && _searchBox.IsFocused)
{
return;
}

base.OnKeyDown(e);
}
}
20 changes: 20 additions & 0 deletions src/OneWare.Essentials/Controls/SearchComboBoxItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Controls;

namespace OneWare.Essentials.Controls;

public class SearchComboBoxItem : ComboBoxItem
{
protected override Type StyleKeyOverride => typeof(ComboBoxItem);

private bool _isSearchResult;

public bool IsSearchResult
{
get => _isSearchResult;
set
{
_isSearchResult = value;
((IPseudoClasses)Classes).Set(":searchResult", value);
}
}
}
2 changes: 1 addition & 1 deletion src/OneWare.IceBreaker/IceBreakerFpga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class IceBreakerFpga : FpgaBase
{
public IceBreakerFpga()
{
LoadFromJson("avares://OneWare.IceBreaker/Assets/IceBreakerV1.0e.json");
LoadFromJsonAsset("avares://OneWare.IceBreaker/Assets/IceBreakerV1.0e.json");
}
}
2 changes: 1 addition & 1 deletion src/OneWare.Max10/Max10Fpga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class Max10Fpga : FpgaBase
{
public Max10Fpga()
{
LoadFromJson("avares://OneWare.Max10/Assets/Max10.json");
LoadFromJsonAsset("avares://OneWare.Max10/Assets/Max10.json");
}
}
2 changes: 1 addition & 1 deletion src/OneWare.Max1000/Max1000Fpga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class Max1000Fpga : FpgaBase
{
public Max1000Fpga()
{
LoadFromJson("avares://OneWare.Max1000/Assets/Max1000.json");
LoadFromJsonAsset("avares://OneWare.Max1000/Assets/Max1000.json");
}
}
2 changes: 1 addition & 1 deletion src/OneWare.TangNano9K/TangNano9kFpga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class TangNano9KFpga : FpgaBase
{
public TangNano9KFpga()
{
LoadFromJson("avares://OneWare.TangNano9K/Assets/TangNano9K.json");
LoadFromJsonAsset("avares://OneWare.TangNano9K/Assets/TangNano9K.json");
}
}
18 changes: 15 additions & 3 deletions src/OneWare.UniversalFpgaProjectSystem/Fpga/FpgaBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ protected FpgaBase(Dictionary<string, string>? properties = null)

public IReadOnlyDictionary<string, string> Properties { get; }

protected void LoadFromJson(string path)
protected void LoadFromJsonAsset(string path)
{
var stream = AssetLoader.Open(new Uri(path));
using var stream = AssetLoader.Open(new Uri(path));
using var reader = new StreamReader(stream);
LoadFromJson(reader.ReadToEnd());
}

protected void LoadFromJsonFile(string path)
{
using var stream = File.OpenRead(path);
using var reader = new StreamReader(stream);
LoadFromJson(reader.ReadToEnd());
}

var properties = JsonNode.Parse(stream);
private void LoadFromJson(string json)
{
var properties = JsonNode.Parse(json);

if (properties == null) return;

Expand Down
9 changes: 9 additions & 0 deletions src/OneWare.UniversalFpgaProjectSystem/Fpga/FpgaLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OneWare.UniversalFpgaProjectSystem.Fpga;

public static class FpgaLoader
{
public static IFpga LoadFromPath(string path)
{
return new GenericFpga(path);
}
}
9 changes: 9 additions & 0 deletions src/OneWare.UniversalFpgaProjectSystem/Fpga/GenericFpga.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OneWare.UniversalFpgaProjectSystem.Fpga;

public class GenericFpga : FpgaBase
{
public GenericFpga(string jsonPath)
{
LoadFromJsonFile(jsonPath);
}
}
5 changes: 5 additions & 0 deletions src/OneWare.UniversalFpgaProjectSystem/Models/FpgaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ private void AddInterface(FpgaInterface fpgaInterface)
var model = new FpgaInterfaceModel(fpgaInterface, this);
InterfaceModels.Add(fpgaInterface.Name, model);
}

public override string ToString()
{
return Fpga.Name;
}
}
41 changes: 41 additions & 0 deletions src/OneWare.UniversalFpgaProjectSystem/Services/FpgaService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using OneWare.Essentials.Extensions;
using OneWare.Essentials.Services;
using OneWare.UniversalFpgaProjectSystem.Fpga;
using OneWare.UniversalFpgaProjectSystem.Models;
using Prism.Ioc;
Expand All @@ -8,6 +9,17 @@ namespace OneWare.UniversalFpgaProjectSystem.Services;

public class FpgaService
{
private readonly ILogger _logger;
public FpgaService(IPaths paths, ILogger logger)
{
FpgaDirectory = Path.Combine(paths.DocumentsDirectory, "Hardware", "FPGA");
Directory.CreateDirectory(FpgaDirectory);

_logger = logger;
}

public string FpgaDirectory { get; }

public Dictionary<IFpga, Type> CustomFpgaViewModels { get; } = new();

public Dictionary<IFpgaExtension, Type> FpgaExtensionViewModels { get; } = new();
Expand Down Expand Up @@ -89,4 +101,33 @@ public void RegisterPreCompileStep<T>() where T : IFpgaPreCompileStep
if (provider != null) return ContainerLocator.Container.Resolve(provider) as INodeProvider;
return null;
}

public void LoadGenericFpgas()
{
foreach (var fpga in Fpgas.ToArray())
{
if (fpga is GenericFpga)
{
Fpgas.Remove(fpga);
CustomFpgaViewModels.Remove(fpga);
}
}

try
{
foreach (var directory in Directory.GetDirectories(FpgaDirectory))
{
var fpgaFile = Path.Combine(directory, "fpga.json");
if (File.Exists(fpgaFile))
{
var fpga = FpgaLoader.LoadFromPath(fpgaFile);
RegisterFpga(fpga);
}
}
}
catch (Exception e)
{
_logger.Error(e.Message, e);
}
}
}
Loading

0 comments on commit 6ff05da

Please sign in to comment.