Skip to content

Commit

Permalink
SolverVizTools: Fixed issues: dialogs now open properly centered to p…
Browse files Browse the repository at this point in the history
…arent windows, Linux should now show the version properly in the window title, latest used paths in file open dialogs are persisted and saved to a config file.
  • Loading branch information
Jusas committed Aug 23, 2022
1 parent d3b6875 commit 27945c0
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public interface ISolveSettingsManager
ObservableCollection<SolveProfile> GetProfiles(bool fromDisk, bool copy);
WatneyConfiguration GetWatneyConfiguration(bool fromDisk, bool copy);
void SaveWatneyConfiguration();

void LoadStoredGeneralSettings();
string GetStoredGeneralSetting(string settingName);
void SetStoredGeneralSetting(string settingName, string value);
void SaveStoredGeneralSettings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,21 @@ public WatneyConfiguration GetWatneyConfiguration(bool fromDisk, bool copy)
public void SaveWatneyConfiguration()
{
}

public void LoadStoredGeneralSettings()
{
}

public string GetStoredGeneralSetting(string settingName)
{
return null;
}

public void SetStoredGeneralSetting(string settingName, string value)
{
}

public void SaveStoredGeneralSettings()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using DynamicData;
using WatneyAstrometry.SolverVizTools.Abstractions;
using WatneyAstrometry.SolverVizTools.Exceptions;
Expand All @@ -25,21 +24,26 @@ public class Options
public string StorageFolder { get; set; }
public string ProfilesFileName { get; set; }
public string WatneyConfigFileName { get; set; }
public string GeneralSettingsFilename { get; set; }
}

private readonly Options _options;
public string ProfilesJsonFilename => Path.Combine(_options.StorageFolder, _options.ProfilesFileName);
public string WatneyConfigJsonFilename => Path.Combine(_options.StorageFolder, _options.WatneyConfigFileName);
public string GeneralSettingsJsonFilename => Path.Combine(_options.StorageFolder, _options.GeneralSettingsFilename);
public ObservableCollection<SolveProfile> Profiles { get; } = new ObservableCollection<SolveProfile>();

private WatneyConfiguration _watneyConfiguration;
public WatneyConfiguration WatneyConfiguration => _watneyConfiguration;

private Dictionary<string, string> _generalSettings = new Dictionary<string, string>();

public static Options DefaultOptions => new Options()
{
StorageFolder = ProgramEnvironment.ApplicationDataFolder,
ProfilesFileName = "solverprofiles.json",
WatneyConfigFileName = "watneyconfig.json"
WatneyConfigFileName = "watneyconfig.json",
GeneralSettingsFilename = "generalsettings.json"
};

public SolveSettingsManager()
Expand Down Expand Up @@ -162,6 +166,33 @@ public void SaveWatneyConfiguration()
File.WriteAllText(WatneyConfigJsonFilename, serialized);
}

public void LoadStoredGeneralSettings()
{
if (File.Exists(GeneralSettingsJsonFilename))
{
var json = File.ReadAllText(GeneralSettingsJsonFilename);
_generalSettings = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
}
}

public string GetStoredGeneralSetting(string settingName)
{
if (_generalSettings.ContainsKey(settingName))
return _generalSettings[settingName];
return null;
}

public void SetStoredGeneralSetting(string settingName, string value)
{
_generalSettings[settingName] = value;
}

public void SaveStoredGeneralSettings()
{
var json = JsonSerializer.Serialize(_generalSettings, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(GeneralSettingsJsonFilename, json);
}

private void EnsureMinimalValidWatneyConfiguration()
{
if(_watneyConfiguration == null)
Expand Down
50 changes: 50 additions & 0 deletions src/WatneyAstrometry.SolverVizTools/Utils/WindowWorkarounds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Avalonia;
using Avalonia.Controls;

namespace WatneyAstrometry.SolverVizTools.Utils;

public static class WindowWorkarounds
{
/// <summary>
/// From: https://github.com/AvaloniaUI/Avalonia/issues/6433#issuecomment-1001766862
/// Essentially the window centering bugs out in Linux. This fixes it.
/// </summary>
/// <param name="window"></param>
public static void ApplyWindowCenteringWorkaround(Window window)
{
if (OperatingSystem.IsWindows())
{
// Not needed for Windows
return;
}

var scale = window.PlatformImpl?.DesktopScaling ?? 1.0;
var pOwner = window.Owner?.PlatformImpl;
if (pOwner != null)
{
scale = pOwner.DesktopScaling;
}
var rect = new PixelRect(PixelPoint.Origin,
PixelSize.FromSize(window.ClientSize, scale));
if (window.WindowStartupLocation == WindowStartupLocation.CenterScreen)
{
var screen = window.Screens.ScreenFromPoint(pOwner?.Position ?? window.Position);
if (screen == null)
{
return;
}
window.Position = screen.WorkingArea.CenterRect(rect).Position;
}
else
{
if (pOwner == null ||
window.WindowStartupLocation != WindowStartupLocation.CenterOwner)
{
return;
}
window.Position = new PixelRect(pOwner.Position,
PixelSize.FromSize(pOwner.ClientSize, scale)).CenterRect(rect).Position;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using WatneyAstrometry.SolverVizTools.Abstractions;
using WatneyAstrometry.SolverVizTools.Models.Profile;
Expand Down Expand Up @@ -57,9 +59,21 @@ public MainWindowViewModel(IServiceProvider serviceProvider)

private void PopulateInitialData()
{
//var v = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location).ProductVersion;
var v = FileVersionInfo.GetVersionInfo(Process.GetCurrentProcess().MainModule.FileName)?.ProductVersion ?? "v?";
string v = null;
try
{
v = GetType().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;
}
catch (Exception)
{
v = "v?";
}

WindowTitle = $"Watney Astrometry Desktop ({v})";

var settingsManager = _serviceProvider.GetService<ISolveSettingsManager>();
settingsManager.LoadStoredGeneralSettings();
}

protected override void OnViewCreated()
Expand All @@ -78,7 +92,7 @@ public void OnClosing()
{
var settingsManager = _serviceProvider.GetService<ISolveSettingsManager>();
settingsManager.SaveWatneyConfiguration();
//settingsManager.SaveProfiles();
settingsManager.SaveStoredGeneralSettings();
}

public void SetSolveSettingsPaneVisible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,20 @@ public async Task StretchLevels()

public async Task OpenImageViaDialog()
{

var pathSetting = "lastImageDialogPath";
var initialDirectory = _settingsManager.GetStoredGeneralSetting(pathSetting) ?? "";
var fileNames = await _dialogProvider.ShowOpenFileDialog(OwnerWindow, "Select image file",
new (string description, string[] extension)[]
{
("FITS files", new[] { "fits", "fit" }),
("Common image formats", new[] { "jpg", "jpeg", "png" })
}, "", false);
}, initialDirectory, false);

var filename = fileNames?.FirstOrDefault();

if (filename != null)
{
_settingsManager.SetStoredGeneralSetting(pathSetting, Path.GetDirectoryName(filename));
await OpenImage(filename);
}

Expand Down Expand Up @@ -569,10 +571,16 @@ await _dialogProvider.ShowMessageBox(OwnerWindow, "Error",

public async Task SaveLogToDiskViaDialog()
{
var pathSetting = "lastSaveLogDialogPath";
var initialDirectory = _settingsManager.GetStoredGeneralSetting(pathSetting) ?? "";
var filename = await _dialogProvider.ShowSaveFileDialog(OwnerWindow, "Save log file as...",
null, "watney_log.txt", ".txt");
if(filename != null)
initialDirectory, "watney_log.txt", ".txt");

if (filename != null)
{
_settingsManager.SetStoredGeneralSetting(pathSetting, Path.GetDirectoryName(filename));
SaveLogToDisk(filename);
}
}

public void SaveLogToDisk(string filename)
Expand All @@ -582,10 +590,16 @@ public void SaveLogToDisk(string filename)

public async Task SaveSolutionWcsToDiskViaDialog()
{
var pathSetting = "lastSaveSolutionDialogPath";
var initialDirectory = _settingsManager.GetStoredGeneralSetting(pathSetting) ?? "";
var filename = await _dialogProvider.ShowSaveFileDialog(OwnerWindow, "Save WCS file as...",
null, "watney_solution.wcs", ".wcs");
initialDirectory, "watney_solution.wcs", ".wcs");

if (filename != null)
{
_settingsManager.SetStoredGeneralSetting(pathSetting, Path.GetDirectoryName(filename));
SaveSolutionWcsToDisk(filename);
}
}

public void SaveSolutionWcsToDisk(string filename)
Expand All @@ -597,10 +611,16 @@ public void SaveSolutionWcsToDisk(string filename)

public async Task SaveSolutionJsonToDiskViaDialog()
{
var pathSetting = "lastSaveSolutionDialogPath";
var initialDirectory = _settingsManager.GetStoredGeneralSetting(pathSetting) ?? "";
var filename = await _dialogProvider.ShowSaveFileDialog(OwnerWindow, "Save JSON file as...",
null, "watney_solution.json", ".json");
initialDirectory, "watney_solution.json", ".json");

if (filename != null)
{
_settingsManager.SetStoredGeneralSetting(pathSetting, Path.GetDirectoryName(filename));
SaveSolutionJsonToDisk(filename);
}
}

public void SaveSolutionJsonToDisk(string filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="250"
Width="400" Height="250"
x:Class="WatneyAstrometry.SolverVizTools.Views.AboutWindow"
Title="AboutWindow">
Title="About Watney Desktop">
<StackPanel>
<TextBlock Name="Title" Padding="16" Text="Watney Astrometry Desktop" FontSize="24" FontWeight="Bold"></TextBlock>
<Border BorderBrush="White" BorderThickness="1" Margin="0,0,0,0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using WatneyAstrometry.SolverVizTools.Utils;

namespace WatneyAstrometry.SolverVizTools.Views
{
Expand All @@ -24,5 +26,12 @@ private void Dismiss_OnClick(object sender, RoutedEventArgs e)
{
this.Close();
}

protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
WindowWorkarounds.ApplyWindowCenteringWorkaround(this);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using WatneyAstrometry.SolverVizTools.Utils;

namespace WatneyAstrometry.SolverVizTools.Views
{
Expand All @@ -18,5 +20,11 @@ private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
WindowWorkarounds.ApplyWindowCenteringWorkaround(this);
}
}
}
1 change: 1 addition & 0 deletions src/WatneyAstrometry.SolverVizTools/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Width="1024"
Height="768"
x:Class="WatneyAstrometry.SolverVizTools.Views.MainWindow"
WindowStartupLocation="CenterScreen"
Icon="/Assets/wicon.ico"
Title="{Binding WindowTitle}">

Expand Down
10 changes: 9 additions & 1 deletion src/WatneyAstrometry.SolverVizTools/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Avalonia.Controls;
Expand All @@ -6,6 +7,7 @@
using Avalonia.Interactivity;
using Splat;
using WatneyAstrometry.SolverVizTools.Models.Profile;
using WatneyAstrometry.SolverVizTools.Utils;
using WatneyAstrometry.SolverVizTools.ViewModels;

namespace WatneyAstrometry.SolverVizTools.Views
Expand Down Expand Up @@ -50,8 +52,14 @@ public void ImageSelectionArea_PointerEnter(object sender, PointerEventArgs even
private void AboutButton_OnClick(object sender, RoutedEventArgs e)
{
var aboutWin = new AboutWindow();
aboutWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
aboutWin.WindowStartupLocation = WindowStartupLocation.CenterOwner;
aboutWin.ShowDialog(this);
}

protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
WindowWorkarounds.ApplyWindowCenteringWorkaround(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using WatneyAstrometry.SolverVizTools.Utils;
using WatneyAstrometry.SolverVizTools.ViewModels;

namespace WatneyAstrometry.SolverVizTools.Views
Expand All @@ -23,6 +25,11 @@ private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}


protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
WindowWorkarounds.ApplyWindowCenteringWorkaround(this);
}
}
}
Loading

0 comments on commit 27945c0

Please sign in to comment.