Skip to content

Commit

Permalink
Merge pull request #6 from viordash/MultiProfile
Browse files Browse the repository at this point in the history
Multi profile
  • Loading branch information
viordash authored Jul 30, 2023
2 parents df921ca + f27975c commit 6089bd4
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace ShareClipbrd.Core.Configuration {
public interface ISystemConfiguration {
string HostAddress { get; }
string PartnerAddress { get; }
int SettingsProfile { get; }
}
}
12 changes: 5 additions & 7 deletions ShareClipbrd/ShareClipbrd.Core/Services/DataServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Buffers;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Net.Sockets;
using GuardNet;
Expand All @@ -17,9 +16,9 @@ public interface IDataServer {
public class DataServer : IDataServer {
readonly ISystemConfiguration systemConfiguration;
readonly IDialogService dialogService;
readonly CancellationTokenSource cts;
readonly IDispatchService dispatchService;
readonly IProgressService progressService;
CancellationTokenSource cts;

public DataServer(
ISystemConfiguration systemConfiguration,
Expand All @@ -35,8 +34,6 @@ IProgressService progressService
this.dialogService = dialogService;
this.dispatchService = dispatchService;
this.progressService = progressService;

cts = new CancellationTokenSource();
}

static async ValueTask<MemoryStream> HandleData(NetworkStream stream, int dataSize, CancellationToken cancellationToken) {
Expand Down Expand Up @@ -132,6 +129,8 @@ async ValueTask HandleClient(TcpClient tcpClient, CancellationToken cancellation
}

public void Start() {
cts?.Cancel();
cts = new CancellationTokenSource();
var cancellationToken = cts.Token;
Task.Run(async () => {
Expand All @@ -157,21 +156,20 @@ public void Start() {
Debug.WriteLine($"tcpServer stop");
tcpServer.Stop();
Debug.WriteLine($"tcpServer stopped");
} catch(SocketException ex) {
await dialogService.ShowError(ex);
} catch(ArgumentException ex) {
await dialogService.ShowError(ex);
}
}
Debug.WriteLine($"tcpServer stopped");
}, cancellationToken);
}

public void Stop() {
Debug.WriteLine($"tcpServer request to stop");
cts.Cancel();
cts?.Cancel();
}
}
}
4 changes: 4 additions & 0 deletions ShareClipbrd/ShareClipbrd.Core/ShareClipbrd.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<PackageReference Include="MessagePack" Version="2.5.108" />
</ItemGroup>

<ItemGroup Label="InternalsVisibleTo">
<InternalsVisibleTo Include="ShareClipbrd.Core.Tests" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,42 @@ public void Setup() {

[Test]
public void HostAddress_Test() {
Settings.Default.HostAddress = "127.1.2.3:8081";
Settings.Default.HostAddress0 = "127.1.2.3:8081";
Settings.Default.HostAddress1 = "127.1.2.3:8181";
Settings.Default.HostAddress2 = "127.1.2.3:8281";
Settings.Default.SettingsProfile = 0;
Assert.That(testable.HostAddress, Is.EqualTo("127.1.2.3:8081"));
Settings.Default.SettingsProfile = 1;
Assert.That(testable.HostAddress, Is.EqualTo("127.1.2.3:8181"));
Settings.Default.SettingsProfile = 2;
Assert.That(testable.HostAddress, Is.EqualTo("127.1.2.3:8281"));
}

[Test]
public void PartnerAddress_Test() {
Settings.Default.PartnerAddress = "127.1.2.3:8082";
Settings.Default.PartnerAddress0 = "127.1.2.3:8082";
Settings.Default.PartnerAddress1 = "127.1.2.3:8182";
Settings.Default.PartnerAddress2 = "127.1.2.3:8282";
Settings.Default.SettingsProfile = 0;
Assert.That(testable.PartnerAddress, Is.EqualTo("127.1.2.3:8082"));
Settings.Default.SettingsProfile = 1;
Assert.That(testable.PartnerAddress, Is.EqualTo("127.1.2.3:8182"));
Settings.Default.SettingsProfile = 2;
Assert.That(testable.PartnerAddress, Is.EqualTo("127.1.2.3:8282"));
}

[Test]
public void SettingsProfile_Clamped_Range_Test() {
Settings.Default.SettingsProfile = -1;
Assert.That(testable.SettingsProfile, Is.EqualTo(0));
Settings.Default.SettingsProfile = 0;
Assert.That(testable.SettingsProfile, Is.EqualTo(0));
Settings.Default.SettingsProfile = 1;
Assert.That(testable.SettingsProfile, Is.EqualTo(1));
Settings.Default.SettingsProfile = 2;
Assert.That(testable.SettingsProfile, Is.EqualTo(2));
Settings.Default.SettingsProfile = 3;
Assert.That(testable.SettingsProfile, Is.EqualTo(2));
}
}
}
30 changes: 30 additions & 0 deletions ShareClipbrd/ShareClipbrdApp/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,40 @@
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ShareClipbrdApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="ShareClipbrdApp.Properties.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<ShareClipbrdApp.Properties.Settings>
<setting name="MainFormLocation" serializeAs="String">
<value>0, 0</value>
</setting>
<setting name="MainFormSize" serializeAs="String">
<value>100, 100</value>
</setting>
<setting name="HostAddress0" serializeAs="String">
<value>127.0.0.1:8090</value>
</setting>
<setting name="PartnerAddress0" serializeAs="String">
<value>127.0.0.1:8091</value>
</setting>
<setting name="HostAddress1" serializeAs="String">
<value />
</setting>
<setting name="PartnerAddress1" serializeAs="String">
<value />
</setting>
<setting name="HostAddress2" serializeAs="String">
<value />
</setting>
<setting name="PartnerAddress2" serializeAs="String">
<value />
</setting>
<setting name="SettingsProfile" serializeAs="String">
<value>0</value>
</setting>
</ShareClipbrdApp.Properties.Settings>
<ShareClipbrdApp.Properties.Settings1>
<setting name="MainFormLocation" serializeAs="String">
<value>0, 0</value>
Expand Down
26 changes: 23 additions & 3 deletions ShareClipbrd/ShareClipbrdApp/Configuration/SystemConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
using ShareClipbrd.Core.Configuration;
using System;
using ShareClipbrd.Core.Configuration;
using ShareClipbrdApp.Properties;

namespace ShareClipbrdApp.Configuration {
public class SystemConfiguration : ISystemConfiguration {
public string HostAddress {
get {
return Settings.Default.HostAddress;
switch(SettingsProfile) {
case 1:
return Settings.Default.HostAddress1;
case 2:
return Settings.Default.HostAddress2;
default:
return Settings.Default.HostAddress0;
}
}
}
public string PartnerAddress {
get {
return Settings.Default.PartnerAddress;
switch(SettingsProfile) {
case 1:
return Settings.Default.PartnerAddress1;
case 2:
return Settings.Default.PartnerAddress2;
default:
return Settings.Default.PartnerAddress0;
}
}
}
public int SettingsProfile {
get {
return Math.Clamp(Settings.Default.SettingsProfile, 0, 2);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion ShareClipbrd/ShareClipbrdApp/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
SystemDecorations="BorderOnly"
CanResize="False"
TransparencyLevelHint="Transparent"
TransparencyBackgroundFallback="Black"
TransparencyBackgroundFallback="Black"
Opacity="0.5"

Opened="OnOpened"
Expand All @@ -34,6 +34,13 @@
<MenuItem Header="Paste" Click="MenuItemPaste_Click"></MenuItem>
<Separator />
<MenuItem Header="Settings" >

<ComboBox x:Name="edSettingsProfile" SelectionChanged ="edSettingsProfile_SelectionChanged">
<ComboBoxItem>Profile 1</ComboBoxItem>
<ComboBoxItem>Profile 2</ComboBoxItem>
<ComboBoxItem>Profile 3</ComboBoxItem>
</ComboBox>

<MenuItem Header="Host address">
<TextBox x:Name="edHostAddress" Width="250" Margin="0,0,-10,0" PropertyChanged="edHostAddress_PropertyChanged" />
</MenuItem>
Expand Down
50 changes: 44 additions & 6 deletions ShareClipbrd/ShareClipbrdApp/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Avalonia.Platform.Storage;
using GuardNet;
using ShareClipbrd.Core.Clipboard;
using ShareClipbrd.Core.Configuration;
using ShareClipbrd.Core.Services;
using ShareClipbrdApp.Components;
using ShareClipbrdApp.Helpers;
Expand All @@ -25,6 +26,7 @@ public partial class MainWindow : Window {
readonly IDataServer? dataServer;
readonly IDialogService? dialogService;
readonly IProgressService? progressService;
readonly ISystemConfiguration? systemConfiguration;

WriteableBitmap? progressBarBitmap;
public WriteableBitmap? ProgressBarBitmap {
Expand All @@ -38,11 +40,13 @@ public MainWindow(
IDataClient dataClient,
IDataServer dataServer,
IDialogService dialogService,
IProgressService progressService) : this() {
IProgressService progressService,
ISystemConfiguration? systemConfiguration) : this() {
Guard.NotNull(dataClient, nameof(dataClient));
Guard.NotNull(dataServer, nameof(dataServer));
Guard.NotNull(dialogService, nameof(dialogService));
Guard.NotNull(progressService, nameof(progressService));
Guard.NotNull(systemConfiguration, nameof(systemConfiguration));

progressBar = new TetrisProgressBar((int)Width - 6, (int)Height - 6, new Random().Next());
progressBarBitmap = new WriteableBitmap(new PixelSize(progressBar.Width, progressBar.Height), new Vector(1.0, 1.0),
Expand All @@ -55,6 +59,7 @@ public MainWindow(
this.dataServer = dataServer;
this.dialogService = dialogService;
this.progressService = progressService;
this.systemConfiguration = systemConfiguration;

SetProgressMode(ProgressMode.None);
SetProgress(100.0);
Expand All @@ -66,10 +71,11 @@ public MainWindow() {

void OnOpened(object sender, System.EventArgs e) {
WindowsHelper.LoadLocation(Settings.Default.MainFormLocation, this);
//dataServer?.Start();
edSettingsProfile.SelectedIndex = systemConfiguration!.SettingsProfile;
edHostAddress.Text = systemConfiguration!.HostAddress;
edPartnerAddress.Text = systemConfiguration!.PartnerAddress;

dataServer?.Start();
edHostAddress.Text = Settings.Default.HostAddress;
edPartnerAddress.Text = Settings.Default.PartnerAddress;
}

void OnClosing(object sender, WindowClosingEventArgs e) {
Expand Down Expand Up @@ -121,16 +127,48 @@ void MenuItemClose_Click(object sender, RoutedEventArgs e) {

private void edHostAddress_PropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) {
if(e.Property == TextBox.TextProperty) {
Settings.Default.HostAddress = edHostAddress.Text;
switch(systemConfiguration!.SettingsProfile) {
case 0:
Settings.Default.HostAddress0 = edHostAddress.Text;
break;
case 1:
Settings.Default.HostAddress1 = edHostAddress.Text;
break;
case 2:
Settings.Default.HostAddress2 = edHostAddress.Text;
break;
}
}
}

private void edPartnerAddress_PropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) {
if(e.Property == TextBox.TextProperty) {
Settings.Default.PartnerAddress = edPartnerAddress.Text;
switch(systemConfiguration!.SettingsProfile) {
case 0:
Settings.Default.PartnerAddress0 = edPartnerAddress.Text;
break;
case 1:
Settings.Default.PartnerAddress1 = edPartnerAddress.Text;
break;
case 2:
Settings.Default.PartnerAddress2 = edPartnerAddress.Text;
break;
}
}
}

private void edSettingsProfile_SelectionChanged(object? sender, SelectionChangedEventArgs e) {
Settings.Default.SettingsProfile = edSettingsProfile.SelectedIndex;
SettingsUpdated();
}

void SettingsUpdated() {
edHostAddress.Text = systemConfiguration!.HostAddress;
edPartnerAddress.Text = systemConfiguration!.PartnerAddress;
dataServer?.Stop();
dataServer?.Start();
}

void OnKeyDown(object sender, KeyEventArgs e) {
if(e.Key == Key.V && e.KeyModifiers == KeyModifiers.Control) {
TransmitClipboard();
Expand Down
Loading

0 comments on commit 6089bd4

Please sign in to comment.