Skip to content

Commit

Permalink
Added PlotWindow, refactored Pages, implemented simple DarkMode.
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankenApps committed Aug 1, 2020
1 parent ad6adf6 commit 6418725
Show file tree
Hide file tree
Showing 19 changed files with 769 additions and 281 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*.VC.VC.opendb
*.sln

# Exclude Visual Studio Code user options.
.vscode/

# Exclude StyleCop cache files.
[Ss]tyle[Cc]op.[Cc]ache

Expand Down
20 changes: 20 additions & 0 deletions App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Markup.Xaml.Styling;
using Avalonia.Styling;
using MqttDebugger.ViewModels;
using MqttDebugger.Views;
using System;

namespace MqttDebugger
{
Expand All @@ -11,8 +14,25 @@ public class App : Application
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
Current.Styles[1] = FluentLight;
}

public static Styles FluentDark = new Styles
{
new StyleInclude(new Uri("avares://ControlCatalog/Styles"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/FluentDark.xaml")
},
};

public static Styles FluentLight = new Styles
{
new StyleInclude(new Uri("avares://ControlCatalog/Styles"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/FluentLight.xaml")
},
};

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
Expand Down
Binary file added Assets/Images/plot_window-hovered.afdesign
Binary file not shown.
Binary file added Assets/Images/plot_window-hovered.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/Images/plot_window.afdesign
Binary file not shown.
Binary file added Assets/Images/plot_window.ico
Binary file not shown.
Binary file added Assets/Images/plot_window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 71 additions & 10 deletions ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia.Controls.Notifications;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Threading;
using MqttDebugger.Models;
using MqttDebugger.Views;
using MQTTnet;
Expand All @@ -21,6 +22,7 @@
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MqttDebugger.ViewModels
{
Expand Down Expand Up @@ -310,6 +312,32 @@ public string FileOutputFolder
}
}

/// <summary>
/// Wether the Log window should autoscroll.
/// </summary>
private bool autoscrollMessageLog = true;
public bool AutoscrollMessageLog
{
get => autoscrollMessageLog;
set
{
this.RaiseAndSetIfChanged(ref autoscrollMessageLog, value);
}
}

/// <summary>
/// Indicates if the message log should be preserved when disconnecting from the server.
/// </summary>
private bool preserveMessageLog = false;
public bool PreserveMessageLog
{
get => preserveMessageLog;
set
{
this.RaiseAndSetIfChanged(ref preserveMessageLog, value);
}
}

// Create reactive commands.
public ReactiveCommand<Unit, Unit> StartServerCommand { get; }
public ReactiveCommand<Unit, Unit> StopServerCommand { get; }
Expand All @@ -318,17 +346,20 @@ public string FileOutputFolder
public ReactiveCommand<Unit, Unit> ConnectToServerCommand { get; }
public ReactiveCommand<Unit, Unit> SendMessageCommand { get; }
public ReactiveCommand<Unit, Unit> ClearMessageLogCommand { get; }

public ReactiveCommand<Unit, Unit> ToggleAutoscrollCommand { get; }
public ReactiveCommand<Unit, Unit> TogglePreserveLogCommand { get; }


// For notification handling.
private MainWindow _window;
private IManagedNotificationManager _notificationManager;

// Create default instances of client and server
// Create default instances of client and server.
private Server mqttServer = new Server();
private Client mqttClient = new Client();
private MqttMessageOptions mqttMessageOptions = new MqttMessageOptions();

// The actual client and server isnstances as provided in MQTTnet.
private IMqttServer server;
private IMqttClient client;
private Thread listenForMessagesThread;
Expand All @@ -343,6 +374,8 @@ public MainWindowViewModel(MainWindow window, IManagedNotificationManager notifi
ConnectToServerCommand = ReactiveCommand.Create(ConnectToServer);
SendMessageCommand = ReactiveCommand.Create(SendMessage);
ClearMessageLogCommand = ReactiveCommand.Create(ClearMessageLog);
ToggleAutoscrollCommand = ReactiveCommand.Create(ToggleAutoscroll);
TogglePreserveLogCommand = ReactiveCommand.Create(TogglePreserveLog);

// Copy references for window and notificationManager
_notificationManager = notificationManager;
Expand Down Expand Up @@ -473,12 +506,14 @@ private async void StopServer()
/// <summary>
/// Restarts or Starts the MQTT-Server (depends on current state).
/// </summary>
private void RestartServer()
private async void RestartServer()
{
if (server.IsStarted)
{
NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Information", $"Restarting the server...", NotificationType.Information));
StopServer();
// Should be improved by making StopServer() awaitable.
await Task.Delay(300);
StartServer();
}
else
Expand All @@ -503,8 +538,11 @@ private async void ConnectToServer()
ClientConnectionButtonText = "Connect";
IsConnectedToServer = false;
MqttMessageText = string.Empty;
// One might also keep those message in the queue...
ReceivedMessages = string.Empty;

if (PreserveMessageLog == false)
{
ReceivedMessages = string.Empty;
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -588,6 +626,9 @@ private async void SendMessage()
}
}

/// <summary>
/// Listen to incoming messages on the subscribed topics.
/// </summary>
private async void ListenForMessages()
{
List<MqttTopicFilter> topicFilters = new List<MqttTopicFilter>();
Expand All @@ -604,10 +645,6 @@ private async void ListenForMessages()

await client.SubscribeAsync(topicFilters.ToArray());
}
/* else if (topicsAsString.Length == 1)
{
await client.SubscribeAsync(mqttMessageOptions.FilterByTopic);
}*/
else
{
NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Can not subscribe to chosen topic. Please change the topic in the general tab.", NotificationType.Error));
Expand Down Expand Up @@ -639,7 +676,12 @@ private async void ListenForMessages()
}
ReceivedMessage += "\n";
ReceivedMessages += ReceivedMessage;
//_window.ScrollTextToEnd(); // Does not work.
if (AutoscrollMessageLog)
{
// A short timeout is needed, so that the scroll viewer will scroll to the new end of its content.
Thread.Sleep(10);
Dispatcher.UIThread.InvokeAsync(_window.ScrollTextToEnd);
}
}
});
}
Expand All @@ -655,9 +697,28 @@ private void ResetServerSettings()
StopServer();
}

/// <summary>
/// Remove all past messages from the log window.
/// </summary>
private void ClearMessageLog()
{
ReceivedMessages = string.Empty;
}

/// <summary>
/// Toggles autoscroll property of the message log.
/// </summary>
private void ToggleAutoscroll()
{
AutoscrollMessageLog = !AutoscrollMessageLog;
}

/// <summary>
/// Toggles the option to preserve the log (e.g. prevent the log window from being emptied when disconnecting).
/// </summary>
private void TogglePreserveLog()
{
PreserveMessageLog = !PreserveMessageLog;
}
}
}
Loading

0 comments on commit 6418725

Please sign in to comment.