Skip to content

Commit 69ccf03

Browse files
committed
Allow opened project to be closed
1 parent 720c85d commit 69ccf03

4 files changed

Lines changed: 107 additions & 18 deletions

File tree

MagickCrop/Controls/WelcomeMessage.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void OpenProject(RecentProjectInfo? project)
6464
if (project == null) return;
6565

6666
// Close the welcome screen
67-
WelcomeBorder.Visibility = Visibility.Collapsed;
67+
Visibility = Visibility.Collapsed;
6868

6969
// Get the main window and open the project
7070
if (Window.GetWindow(this) is MainWindow mainWindow)
@@ -109,7 +109,7 @@ private void Hyperlink_Click(object sender, RoutedEventArgs e)
109109

110110
private void OpenFileButton_Click(object sender, RoutedEventArgs e)
111111
{
112-
WelcomeBorder.Visibility = Visibility.Collapsed;
112+
Visibility = Visibility.Collapsed;
113113
PrimaryButtonEvent?.Invoke(sender, e);
114114
}
115115

@@ -130,13 +130,13 @@ private void AboutHyperbuttons_Click(object sender, RoutedEventArgs e)
130130

131131
private async void OpenPackageButton_Click(object sender, RoutedEventArgs e)
132132
{
133-
WelcomeBorder.Visibility = Visibility.Collapsed;
133+
Visibility = Visibility.Collapsed;
134134

135135
bool opened = false;
136136
if (Window.GetWindow(this) is MainWindow mainWindow)
137137
opened = await mainWindow.LoadMeasurementsPackageFromFile();
138138

139139
if (!opened)
140-
WelcomeBorder.Visibility = Visibility.Visible;
140+
Visibility = Visibility.Visible;
141141
}
142142
}

MagickCrop/MainWindow.xaml

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,31 @@
156156
</Grid.RowDefinitions>
157157
<ScrollViewer>
158158
<StackPanel Width="200" Orientation="Vertical">
159-
<wpfui:Button
160-
x:Name="ReOpenFileButton"
161-
Width="180"
162-
Margin="0,0,0,8"
163-
HorizontalAlignment="Center"
164-
Appearance="Transparent"
165-
Click="OpenFileButton_Click"
166-
Content="Open image..."
167-
ToolTip="Open a new image file">
168-
<wpfui:Button.Icon>
169-
<wpfui:SymbolIcon Symbol="FolderOpen24" />
170-
</wpfui:Button.Icon>
171-
</wpfui:Button>
159+
<Grid Width="160">
160+
<Grid.ColumnDefinitions>
161+
<ColumnDefinition Width="Auto" />
162+
<ColumnDefinition Width="*" />
163+
<ColumnDefinition Width="Auto" />
164+
</Grid.ColumnDefinitions>
165+
<wpfui:SymbolIcon
166+
Grid.Column="0"
167+
Margin="0,0,5,0"
168+
Symbol="Image24" />
169+
<TextBlock
170+
x:Name="ReOpenFileText"
171+
Grid.Column="1"
172+
VerticalAlignment="Center"
173+
Text="Image File Name"
174+
TextTrimming="CharacterEllipsis" />
175+
<wpfui:SymbolIcon
176+
x:Name="CloseFileIcon"
177+
Grid.Column="2"
178+
Margin="5,0,0,0"
179+
MouseDown="CloseFileIcon_MouseDown"
180+
Symbol="Dismiss24"
181+
ToolTip="Click to close and show the welcome screen"
182+
Visibility="Collapsed" />
183+
</Grid>
172184
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
173185
<MenuItem
174186
x:Name="UndoMenuItem"

MagickCrop/MainWindow.xaml.cs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public partial class MainWindow : FluentWindow
4848
private Services.RecentProjectsManager? recentProjectsManager;
4949
private string? currentProjectId;
5050
private System.Timers.Timer? autoSaveTimer;
51-
private readonly int AutoSaveIntervalMs = (int)TimeSpan.FromSeconds(3).TotalMilliseconds;
51+
private readonly int AutoSaveIntervalMs = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
5252

5353
private static readonly List<FormatItem> _formats =
5454
[
@@ -91,6 +91,7 @@ public MainWindow()
9191
AspectRatioTransformPreview.RatioItem = selectedAspectRatio;
9292

9393
InitializeProjectManager();
94+
UpdateOpenedFileNameText();
9495
}
9596

9697
private void DrawPolyLine()
@@ -576,6 +577,9 @@ await Task.Run(async () =>
576577

577578
// Create a new project ID for this image
578579
currentProjectId = Guid.NewGuid().ToString();
580+
581+
// Update the ReOpenFileButton to show the current file name
582+
UpdateOpenedFileNameText();
579583
}
580584

581585
private void OpenFolderButton_Click(object sender, RoutedEventArgs e)
@@ -1490,6 +1494,8 @@ private async Task LoadMeasurementPackageAsync(string fileName)
14901494
currentProjectId = package.Metadata.ProjectId;
14911495
else
14921496
currentProjectId = Guid.NewGuid().ToString();
1497+
1498+
UpdateOpenedFileNameText();
14931499
}
14941500

14951501
public async void LoadMeasurementsPackageFromFile(string filePath)
@@ -1591,4 +1597,69 @@ protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
15911597

15921598
base.OnClosing(e);
15931599
}
1600+
1601+
private void UpdateOpenedFileNameText()
1602+
{
1603+
if (string.IsNullOrEmpty(openedFileName))
1604+
{
1605+
ReOpenFileText.Text = "Image/Project Name";
1606+
CloseFileIcon.Visibility = Visibility.Collapsed;
1607+
}
1608+
else
1609+
{
1610+
ReOpenFileText.Text = openedFileName;
1611+
1612+
if (openedPackage is not null)
1613+
ReOpenFileText.Text = $" {openedPackage.Metadata.OriginalFilename}";
1614+
CloseFileIcon.Visibility = Visibility.Visible;
1615+
}
1616+
}
1617+
1618+
private void CloseFileIcon_MouseDown(object sender, MouseButtonEventArgs e)
1619+
{
1620+
e.Handled = true; // Prevent the click from bubbling to the button
1621+
ResetApplicationState();
1622+
}
1623+
1624+
private void ResetApplicationState()
1625+
{
1626+
// Stop the autosave timer
1627+
autoSaveTimer?.Stop();
1628+
AutosaveCurrentState();
1629+
1630+
// Clear the image
1631+
MainImage.Source = null;
1632+
imagePath = null;
1633+
openedFileName = string.Empty;
1634+
openedPackage = null;
1635+
savedPath = null;
1636+
1637+
// Reset the title
1638+
wpfuiTitleBar.Title = "Magick Crop & Measure by TheJoeFin";
1639+
1640+
// Reset UI elements
1641+
RemoveMeasurementControls();
1642+
HideTransformControls();
1643+
HideCroppingControls();
1644+
HideResizeControls();
1645+
BottomBorder.Visibility = Visibility.Collapsed;
1646+
WelcomeMessageModal.Visibility = Visibility.Visible;
1647+
OpenFolderButton.IsEnabled = false;
1648+
Save.IsEnabled = false;
1649+
1650+
// Reset the canvas transform
1651+
if (ShapeCanvas.RenderTransform is MatrixTransform matTrans)
1652+
{
1653+
matTrans.Matrix = new Matrix();
1654+
}
1655+
1656+
// Reset undo/redo
1657+
undoRedo.Clear();
1658+
1659+
// Create a new project ID
1660+
currentProjectId = Guid.NewGuid().ToString();
1661+
1662+
// Update the button state
1663+
UpdateOpenedFileNameText();
1664+
}
15941665
}

MagickCrop/Models/UndoRedo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public string Redo()
4242
_undoStack.Push(item);
4343
return newPath;
4444
}
45+
46+
internal void Clear()
47+
{
48+
_undoStack.Clear();
49+
_redoStack.Clear();
50+
}
4551
}
4652

4753
public abstract class UndoRedoItem

0 commit comments

Comments
 (0)