Allow changing output directory

Closes #5
This commit is contained in:
2026-03-03 09:28:03 +09:00
parent 193f57df02
commit 03da58277e
5 changed files with 124 additions and 19 deletions
+26
View File
@@ -13,11 +13,23 @@ class Settings : ChangeNotifier
{
private string _lastUsedPath;
private bool _useDocnetPDFImageRendering;
private bool _saveOutputPdfInWorkingDir;
private string _outputPdfDir;
public Settings()
{
_lastUsedPath = "";
_useDocnetPDFImageRendering = true;
_saveOutputPdfInWorkingDir = true;
_outputPdfDir = "";
}
public Settings(Settings other)
{
_lastUsedPath = other.LastUsedPath;
_useDocnetPDFImageRendering = other.UseDocnetPDFImageRendering;
_saveOutputPdfInWorkingDir = other.SaveOutputPdfInWorkingDir;
_outputPdfDir = other.OutputPdfDir;
}
[JsonInclude]
@@ -35,6 +47,20 @@ class Settings : ChangeNotifier
set { _useDocnetPDFImageRendering = value; NotifyPropertyChanged(); }
}
[JsonInclude]
public bool SaveOutputPdfInWorkingDir
{
get => _saveOutputPdfInWorkingDir;
set { _saveOutputPdfInWorkingDir = value; NotifyPropertyChanged(); }
}
[JsonInclude]
public string OutputPdfDir
{
get => _outputPdfDir;
set { _outputPdfDir = value; NotifyPropertyChanged(); }
}
public static string GetSettingsFileName()
{
return "settings.json";
+13 -5
View File
@@ -256,7 +256,7 @@ class MainViewModel : BaseViewModel, IFontResolver, ICanCheckShutdown
public async Task ShowSettings()
{
var updatedSettings = await DialogHost.Show(new SettingsViewModel(_settings));
var updatedSettings = await DialogHost.Show(new SettingsViewModel(_settings, TopLevelGrabber));
if (updatedSettings != null)
{
_settings = (Settings)updatedSettings;
@@ -575,6 +575,14 @@ class MainViewModel : BaseViewModel, IFontResolver, ICanCheckShutdown
{
// TODO: calculate needed width for images based on page width and margins and all that?
// TODO: resize (non-HEIC) images for smaller size...?
// safety checks
var outputDir = _settings.SaveOutputPdfInWorkingDir ? folderPath : _settings.OutputPdfDir;
if (!Directory.Exists(outputDir))
{
await DialogHost.Show(new WarningViewModel("Output directory not found! Please adjust your application Settings before continuing. Output directory: " + outputDir));
return;
}
// start making PDF!
IsCreatingPDF = true;
var pdfDoc = new Document();
var outputFileName = ReportTitle + ".pdf";
@@ -828,15 +836,15 @@ class MainViewModel : BaseViewModel, IFontResolver, ICanCheckShutdown
};
LogInfo("Rendering document to PDF file...");
pdfRenderer.RenderDocument();
string outputPDFFileName = Path.Join(folderPath, outputFileName);
string outputPDFFilePath = Path.Join(outputDir, outputFileName);
LogInfo("Saving PDF document to disk...");
pdfRenderer.PdfDocument.Save(outputPDFFileName);
LogInfo("Finished saving PDF output to: " + outputPDFFileName);
pdfRenderer.PdfDocument.Save(outputPDFFilePath);
LogInfo("Finished saving PDF output to: " + outputPDFFilePath);
await CreateAndSaveReportObjectAfterReportCreation();
// clean up data dir
Directory.Delete(convertedDir, true);
// show output folder to user
OpenFolderForFileInFileViewer(outputPDFFileName);
OpenFolderForFileInFileViewer(outputPDFFilePath);
IsCreatingPDF = false;
}
+65 -6
View File
@@ -25,15 +25,15 @@ class SettingsViewModel: ChangeNotifier
{
private Settings _previousSettings;
private Settings _settings;
private string _errorMessage;
private ITopLevelGrabber? _topLevelGrabber;
public SettingsViewModel(Settings settingsToEdit)
public SettingsViewModel(Settings settingsToEdit, ITopLevelGrabber? topLevelGrabber)
{
_previousSettings = settingsToEdit;
_settings = new Settings
{
LastUsedPath = _previousSettings.LastUsedPath,
UseDocnetPDFImageRendering = _previousSettings.UseDocnetPDFImageRendering
};
_settings = new Settings(settingsToEdit); // clone it
_errorMessage = "";
_topLevelGrabber = topLevelGrabber;
}
public bool UseDocnetPDFImageRendering
@@ -46,6 +46,65 @@ class SettingsViewModel: ChangeNotifier
}
}
public bool SaveOutputPdfInWorkingDir
{
get => _settings.SaveOutputPdfInWorkingDir;
set
{
_settings.SaveOutputPdfInWorkingDir = value;
NotifyPropertyChanged();
}
}
public string OutputPdfDirPath
{
get => _settings.OutputPdfDir;
set
{
_settings.OutputPdfDir = value;
NotifyPropertyChanged();
}
}
public bool IsOutputPdfDirValid
{
get => SaveOutputPdfInWorkingDir || (!SaveOutputPdfInWorkingDir && Directory.Exists(OutputPdfDirPath));
}
public bool HasErrorMessage
{
get => !string.IsNullOrWhiteSpace(_errorMessage);
}
public string ErrorMessage
{
get => _errorMessage;
set
{
_errorMessage = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(HasErrorMessage));
}
}
public async void ChooseOutputFolder()
{
var topLevel = _topLevelGrabber?.GetTopLevel();
if (topLevel != null)
{
var folders = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions()
{
Title = "Choose where to save your report file...",
AllowMultiple = false,
});
if (folders.Count == 1)
{
var folder = folders[0];
OutputPdfDirPath = folder.Path.LocalPath;
}
}
}
public void Cancel()
{
DialogHost.Close("DialogHost", null);
+15 -2
View File
@@ -12,13 +12,26 @@
MaxWidth="350">
<ScrollViewer AllowAutoHide="False">
<StackPanel Orientation="Vertical"
Spacing="4"
Margin="12,4,12,0">
Spacing="8"
Margin="12,4,12,4">
<Label Content="Settings"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Bold" />
<CheckBox IsChecked="{Binding !UseDocnetPDFImageRendering}">Use legacy PDF handling (does not work with macOS annotations)</CheckBox>
<CheckBox IsChecked="{Binding SaveOutputPdfInWorkingDir}">Always save report PDF in working directory</CheckBox>
<Grid ColumnDefinitions="*, *"
IsVisible="{Binding !SaveOutputPdfInWorkingDir}">
<Button Content="Choose output folder..."
Grid.Column="0"
Command="{Binding ChooseOutputFolder}"
VerticalAlignment="Top"/>
<TextBlock Text="{Binding OutputPdfDirPath}"
Margin="6,0,0,0"
TextWrapping="Wrap"
Grid.Column="1"
VerticalAlignment="Top"/>
</Grid>
<StackPanel Orientation="Horizontal"
Spacing="12"
Margin="0,4,0,0"
+5 -6
View File
@@ -3,13 +3,12 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace MayShow.Views
namespace MayShow.Views;
public partial class SettingsView : UserControl
{
public partial class SettingsView : UserControl
public SettingsView()
{
public SettingsView()
{
this.InitializeComponent();
}
this.InitializeComponent();
}
}