Skip to content

Commit

Permalink
Fix PluginSettingsEditor<T> synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityGhost committed Jul 26, 2022
1 parent 8c321e6 commit 2f3d957
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 126 deletions.
2 changes: 0 additions & 2 deletions OpenTabletDriver.Benchmarks/Output/MacOSInteropBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using OpenTabletDriver.Desktop.Interop;
using OpenTabletDriver.Desktop.Interop.Input.Absolute;
using OpenTabletDriver.Desktop.Interop.Input.Relative;
using OpenTabletDriver.Platform.Pointer;

namespace OpenTabletDriver.Benchmarks.Output
Expand Down
2 changes: 0 additions & 2 deletions OpenTabletDriver.Benchmarks/Output/WindowsInteropBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using OpenTabletDriver.Desktop.Interop;
using OpenTabletDriver.Desktop.Interop.Input.Absolute;
using OpenTabletDriver.Desktop.Interop.Input.Relative;
using OpenTabletDriver.Platform.Pointer;

namespace OpenTabletDriver.Benchmarks.Output
Expand Down
3 changes: 1 addition & 2 deletions OpenTabletDriver.Console/ProgramCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using OpenTabletDriver.Desktop.Profiles;
using OpenTabletDriver.Desktop.Reflection;
using OpenTabletDriver.Output;
using OpenTabletDriver.Tablet;
using static System.Console;

namespace OpenTabletDriver.Console
Expand Down Expand Up @@ -351,7 +350,7 @@ public async Task EditSettings(string? editor = null)
{
await using (var fs = File.OpenRead(path))
{
var newSettings = Serialization.Deserialize<Settings>(fs);
var newSettings = Serialization.Deserialize<Settings>(fs)!;
await ApplySettings(newSettings);
await Out.WriteLineAsync("Settings were successfully applied.");
}
Expand Down
2 changes: 1 addition & 1 deletion OpenTabletDriver.Daemon/DriverDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public async Task Initialize()

private Collection<LogMessage> LogMessages { get; } = new Collection<LogMessage>();
private Collection<ITool> Tools { get; } = new Collection<ITool>();
#if !DEBUG
#if RELEASE
private SleepDetectionThread? SleepDetection { set; get; }
#endif

Expand Down
1 change: 0 additions & 1 deletion OpenTabletDriver.Desktop/RPC/RpcClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO.Pipes;
using System.Threading.Tasks;
using JetBrains.Annotations;
using OpenTabletDriver.Desktop.RPC.Messages;
using StreamJsonRpc;

Expand Down
3 changes: 0 additions & 3 deletions OpenTabletDriver.Tests/ConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using OpenTabletDriver.Components;
using OpenTabletDriver.Configurations;
using OpenTabletDriver.Desktop;
using OpenTabletDriver.Desktop.Interop.AppInfo;
using OpenTabletDriver.Tablet;
using Xunit;
using Xunit.Abstractions;
Expand Down
1 change: 0 additions & 1 deletion OpenTabletDriver.Tests/ReportParserProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTabletDriver.Components;
using OpenTabletDriver.Configurations.Parsers.XP_Pen;
using OpenTabletDriver.Desktop;
using OpenTabletDriver.Tablet;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using OpenTabletDriver.Tablet;

namespace OpenTabletDriver.Tools.udev.Comparers
Expand Down
1 change: 1 addition & 0 deletions OpenTabletDriver.UX.MacOS/OpenTabletDriver.UX.MacOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>$(FrameworkBase)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RuntimeIdentifiers>osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup Label="Project References">
Expand Down
26 changes: 5 additions & 21 deletions OpenTabletDriver.UX/Controls/Editors/PluginSettingsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
using Eto.Forms;
using OpenTabletDriver.Desktop.Reflection;
using OpenTabletDriver.UX.Components;
using OpenTabletDriver.UX.ViewModels;

namespace OpenTabletDriver.UX.Controls.Editors
{
public class PluginSettingsEditor : DesktopPanel
{
private readonly IControlBuilder _controlBuilder;
private readonly Placeholder _placeholder;

public PluginSettingsEditor(IControlBuilder controlBuilder)
{
_controlBuilder = controlBuilder;

Content = _placeholder = new Placeholder("No plugin selected.");
}

protected override void OnDataContextChanged(EventArgs e)
public PluginSettingsEditor(IControlBuilder controlBuilder, PluginSettings settings, Type type)
{
base.OnDataContextChanged(e);

if (DataContext is not SettingsViewModel { Settings: not null, Type: not null } model)
{
Content = _placeholder;
return;
}
DataContext = settings;

var enableToggle = new CheckBox
{
Text = "Enable"
};

enableToggle.CheckedBinding.BindDataContext((SettingsViewModel m) => m.Settings.Enable);
enableToggle.CheckedBinding.BindDataContext((PluginSettings s) => s.Enable);

var layout = new StackLayout
{
Expand All @@ -44,7 +28,7 @@ protected override void OnDataContextChanged(EventArgs e)
}
};

foreach (var control in _controlBuilder.Generate(model.Settings, model.Type))
foreach (var control in controlBuilder.Generate(settings, type))
{
var item = new StackLayoutItem(control);
layout.Items.Add(item);
Expand Down
74 changes: 0 additions & 74 deletions OpenTabletDriver.UX/Controls/Editors/PluginSettingsEditorList.cs

This file was deleted.

84 changes: 84 additions & 0 deletions OpenTabletDriver.UX/Controls/Editors/PluginSettingsPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections.Immutable;
using System.Reflection;
using Eto.Drawing;
using Eto.Forms;
using OpenTabletDriver.Desktop.Reflection;
using OpenTabletDriver.UX.Components;

namespace OpenTabletDriver.UX.Controls.Editors
{
public abstract class PluginSettingsPanel<T> : DesktopPanel where T : class
{
private readonly IPluginFactory _pluginFactory;
private readonly IControlBuilder _controlBuilder;
private readonly App _app;
private readonly ListBox<TypeInfo> _list;
private readonly Splitter _splitter;
private readonly Placeholder _placeholder;

protected PluginSettingsPanel(
IControlBuilder controlBuilder,
IPluginFactory pluginFactory,
App app,
IPluginManager pluginManager
)
{
_controlBuilder = controlBuilder;
_pluginFactory = pluginFactory;
_app = app;

_splitter = new Splitter
{
Panel1 = new Panel
{
MinimumSize = new Size(250, 0),
Content = _list = new ListBox<TypeInfo>
{
ItemKeyBinding = Binding.Property<TypeInfo, string>(t => t.FullName!),
ItemTextBinding = Binding.Property<TypeInfo, string>(t => t.GetFriendlyName() ?? t.FullName!)
}
},
Panel2 = _placeholder = new Placeholder("No plugin selected.")
};

pluginManager.AssembliesChanged += (_, _) => Refresh();
DataContextChanged += (_, _) => UpdateContent();
_list.SelectedIndexChanged += (_, _) => UpdateContent();

Refresh();
}

protected abstract PluginSettingsCollection? Settings { get; }

private void Refresh()
{
var items = _pluginFactory.GetMatchingTypes(typeof(T)).ToImmutableArray();
_list.DataStore = items;

if (items.Any())
{
Content = _splitter;
UpdateContent();
}
else
{
var button = new Button((_, _) => _app.ShowWindow<Windows.PluginManager>())
{
Text = "Show plugin manager..."
};

Content = new Placeholder("No plugins of this type are installed.", button);
}
}

private void UpdateContent()
{
var type = _list.SelectedItem;

if (Settings?.FromType(type) is PluginSettings settings)
_splitter.Panel2 = _controlBuilder.Build<PluginSettingsEditor>(settings, type);
else
_splitter.Panel2 = _placeholder;
}
}
}
15 changes: 10 additions & 5 deletions OpenTabletDriver.UX/Controls/FiltersPanel.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using OpenTabletDriver.Desktop.Profiles;
using OpenTabletDriver.Desktop.Reflection;
using OpenTabletDriver.Output;
using OpenTabletDriver.UX.Components;
using OpenTabletDriver.UX.Controls.Editors;

namespace OpenTabletDriver.UX.Controls
{
public class FiltersPanel : DesktopPanel
public class FiltersPanel : PluginSettingsPanel<IDevicePipelineElement>
{
public FiltersPanel(IControlBuilder controlBuilder)
public FiltersPanel(
IControlBuilder controlBuilder,
IPluginFactory pluginFactory,
App app,
IPluginManager pluginManager
) : base(controlBuilder, pluginFactory, app, pluginManager)
{
var editor = controlBuilder.Build<PluginSettingsEditorList<IDevicePipelineElement>>();
editor.DataContextBinding.BindDataContext((Profile p) => p.Filters);
Content = editor;
}

protected override PluginSettingsCollection? Settings => (DataContext as Profile)?.Filters;
}
}
15 changes: 12 additions & 3 deletions OpenTabletDriver.UX/Controls/ProfilePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenTabletDriver.Desktop.Contracts;
using OpenTabletDriver.Desktop.Profiles;
using OpenTabletDriver.UX.Components;
using Application = Eto.Forms.Application;

namespace OpenTabletDriver.UX.Controls
{
Expand Down Expand Up @@ -32,7 +33,7 @@ public ProfilePanel(IDriverDaemon daemon, IControlBuilder controlBuilder, App ap
{
Pages =
{
logPage
placeholderPage
}
};

Expand All @@ -50,6 +51,8 @@ public ProfilePanel(IDriverDaemon daemon, IControlBuilder controlBuilder, App ap

DataContextChanged += delegate
{
var prevPage = tabControl.SelectedPage;

var pages = tabControl.Pages;
pages.Clear();

Expand All @@ -74,14 +77,20 @@ public ProfilePanel(IDriverDaemon daemon, IControlBuilder controlBuilder, App ap
pages.Add(mousePage);

pages.Add(toolsPage);
pages.Add(logPage);

if (prevPage != placeholderPage && pages.Contains(prevPage))
tabControl.SelectedPage = prevPage;
else
tabControl.SelectedIndex = 0;
}
else
{
pages.Add(placeholderPage);
pages.Add(logPage);

tabControl.SelectedPage = placeholderPage;
}

pages.Add(logPage);
};
}

Expand Down
Loading

0 comments on commit 2f3d957

Please sign in to comment.