Skip to content

Commit

Permalink
Merge pull request #4 from Novack/main
Browse files Browse the repository at this point in the history
Added some convenience tools and goodies
  • Loading branch information
amilich authored Sep 3, 2023
2 parents 896a909 + 15fa51f commit cc170c7
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 31 deletions.
112 changes: 107 additions & 5 deletions skiffWindowsApp/Skiff Desktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using ToastNotifications.Position;
using ToastNotifications.Core;
using CustomNotificationsExample.CustomMessage;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Skiff_Desktop
{
Expand All @@ -16,15 +18,17 @@ namespace Skiff_Desktop
/// </summary>
public partial class MainWindow : Window
{
// Counter and action not yet updated. We need messaging from server or webview.
// Use placeholder tray menu items to demo this.
public int UnreadCount { get; private set; }
public Action UnreadCounterChanged;
public int UnreadCount { get; private set; }
public HttpClient HttpClient { get; private set; }

private string baseURL = "https://app.skiff.com/";
private Notifier _notifier;
private TrayController _trayController;
private MessageProcessor _messageProcessor;
private PreferencesController _preferencesController;

private WindowState _prevState;


public MainWindow()
Expand All @@ -46,8 +50,69 @@ public MainWindow()
cfg.DisplayOptions.TopMost = true;
});

_trayController = new TrayController(this);
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Skiff-Mail", "1.0"));

_preferencesController = new PreferencesController(this);
_trayController = new TrayController(this, _preferencesController);
_messageProcessor = new MessageProcessor(this);

StateChanged += OnWindowStateChanged;
SizeChanged += OnWindowSizeChanged;
RestoreWindow();
ApplyWindowPreferences();
}

internal void RestoreWindow()
{
var windowData = _preferencesController.WindowData;
if (windowData != null)
{
Top = windowData.Top;
Left = windowData.Left;
Width = windowData.Width;
Height = windowData.Height;
WindowState = windowData.Maximized ? WindowState.Maximized : WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
}

private void ApplyWindowPreferences()
{
if (_preferencesController.StartMinimized)
{
WindowState = WindowState.Minimized;
if (_preferencesController.MinimizeToTray)
Hide();
}
}

private void SaveWindowData()
{
var windowData = new WindowData()
{
Top = Top,
Left = Left,
Width = Width,
Height = Height,
Maximized = WindowState == WindowState.Maximized
};
_preferencesController.SetWindowPosAndState(windowData);
}

private void OnWindowStateChanged(object? sender, EventArgs e)
{
if (WindowState != WindowState.Minimized)
SaveWindowData();
}

private void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
{
if (WindowState != WindowState.Minimized)
SaveWindowData();
}

internal void ShowToastNotification(string title, string message)
Expand All @@ -58,6 +123,9 @@ internal void ShowToastNotification(string title, string message)

protected override void OnClosed(EventArgs e)
{
if (WindowState != WindowState.Minimized)
SaveWindowData();

_notifier.Dispose();
base.OnClosed(e);
}
Expand Down Expand Up @@ -113,7 +181,8 @@ private void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWind
}
}
}
private void OpenInDefaultBrowser(string uri)

internal void OpenInDefaultBrowser(string uri)
{
try
{
Expand Down Expand Up @@ -145,4 +214,37 @@ internal void UpdateUnreadCount(int newTotal)
UnreadCounterChanged?.Invoke();
}
}

[Serializable]
class WindowData
{
public double Top { get; set; }
public double Left { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public bool Maximized { get; set; }

public override string ToString()
{
return $"{Top} {Left} {Width} {Height} {Maximized}";
}

public static WindowData Parse(string strData)
{
if (string.IsNullOrEmpty(strData))
return null;

string[] values = strData.Split(' ');
WindowData windowData = new()
{
Top = double.Parse(values[0]),
Left = double.Parse(values[1]),
Width = double.Parse(values[2]),
Height = double.Parse(values[3]),
Maximized = bool.Parse(values[4])
};

return windowData;
}
}
}
77 changes: 77 additions & 0 deletions skiffWindowsApp/Skiff Desktop/PreferencesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace Skiff_Desktop
{
internal class PreferencesController
{
public bool LaunchOnStartup { get; private set; }
public bool StartMinimized { get; private set; }
public bool MinimizeToTray { get; private set; }
public bool CloseToTray { get; private set; }
public WindowData WindowData { get; private set; }

public Version Version { get; private set; }

private MainWindow _mainWindow;

private RegistryKey _startupKey;
private RegistryKey _settingsPersistenceKey;
private const string Minimize_To_Tray_Name = "MinimizeToTray";
private const string Close_To_Tray_Name = "CloseToTray";
private const string Start_Minimized_Name = "StartMinimized";
private const string Window_Pos_And_State_Name = "WindowsPosAndState";


public PreferencesController(MainWindow mainWindow)
{
_mainWindow = mainWindow;

_startupKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
LaunchOnStartup = _startupKey.GetValue(Application.ProductName) != null;

_settingsPersistenceKey = Registry.CurrentUser.CreateSubKey($"SOFTWARE\\{Application.CompanyName}");
MinimizeToTray = bool.Parse(_settingsPersistenceKey.GetValue(Minimize_To_Tray_Name) as string ?? bool.FalseString);
CloseToTray = bool.Parse(_settingsPersistenceKey.GetValue(Close_To_Tray_Name) as string ?? bool.FalseString);
StartMinimized = bool.Parse(_settingsPersistenceKey.GetValue(Start_Minimized_Name) as string ?? bool.FalseString);
WindowData = WindowData.Parse(_settingsPersistenceKey.GetValue(Window_Pos_And_State_Name) as string);

Version = Assembly.GetExecutingAssembly().GetName().Version;
}

public void SetLaunchOnStartup(bool enable)
{
if (enable)
_startupKey.DeleteValue(Application.ProductName, false);
else
_startupKey.SetValue(Application.ProductName, Application.ExecutablePath);

LaunchOnStartup = enable;
}

public void SetStartMinimized(bool enable)
{
_settingsPersistenceKey.SetValue(Start_Minimized_Name, enable);
StartMinimized = enable;
}

public void SetMinimizeToTray(bool enable)
{
_settingsPersistenceKey.SetValue(Minimize_To_Tray_Name, enable);
MinimizeToTray = enable;
}

public void SetCloseToTray(bool enable)
{
_settingsPersistenceKey.SetValue(Close_To_Tray_Name, enable);
CloseToTray = enable;
}

public void SetWindowPosAndState(WindowData windowData)
{
_settingsPersistenceKey.SetValue(Window_Pos_And_State_Name, windowData.ToString());
}
}
}
4 changes: 2 additions & 2 deletions skiffWindowsApp/Skiff Desktop/Skiff Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<AssemblyVersion>1.0.0.3</AssemblyVersion>
<FileVersion>1.0.0.3</FileVersion>
<AssemblyVersion>1.0.0.5</AssemblyVersion>
<FileVersion>1.0.0.5</FileVersion>

</PropertyGroup>

Expand Down
Loading

0 comments on commit cc170c7

Please sign in to comment.