ProgrartEditor now correctly checks modification.

TabButtons now support modification indicator.
This commit is contained in:
Creeper Lv
2026-01-25 03:22:11 +11:00
parent f1c556839f
commit f43ed5a585
7 changed files with 118 additions and 4 deletions

View File

@@ -13,7 +13,12 @@
<Border Grid.ColumnSpan="2" Background="#4888" CornerRadius="12" Name="HintBackground" IsVisible="False"> <Border Grid.ColumnSpan="2" Background="#4888" CornerRadius="12" Name="HintBackground" IsVisible="False">
<Border BorderThickness="1,1,1,0" CornerRadius="12" BorderBrush="#8888"></Border> <Border BorderThickness="1,1,1,0" CornerRadius="12" BorderBrush="#8888"></Border>
</Border> </Border>
<Button Name="MainButton" MinWidth="50" Padding="6,2" VerticalAlignment="Stretch">Blank Page</Button> <Button Name="MainButton" MinWidth="50" Padding="6,2" VerticalAlignment="Stretch">
<StackPanel Orientation="Horizontal">
<TextBlock Name="ModificationIndicator" IsVisible="False">*</TextBlock>
<TextBlock Name="TitleBlock">Blank Page</TextBlock>
</StackPanel>
</Button>
<Button Name="CloseButton" VerticalAlignment="Stretch" Grid.Column="1" Padding="6,2"> <Button Name="CloseButton" VerticalAlignment="Stretch" Grid.Column="1" Padding="6,2">
<Viewbox Height="10" Width="10"> <Viewbox Height="10" Width="10">
<PathIcon Data="F1 M 4.08 6 L 4.16 5.96 C 4.24 5.853333 4.34 5.799999 4.46 5.799999 C 4.58 5.799999 4.693333 5.826666 4.8 5.879999 L 10 11.08 L 15.16 5.96 C 15.239999 5.853333 15.339999 5.799999 15.46 5.799999 C 15.579999 5.799999 15.693332 5.826666 15.799999 5.879999 L 15.839999 5.96 C 15.946666 6.040001 16 6.14 16 6.259999 C 16 6.38 15.973333 6.493334 15.92 6.599999 L 10.719999 11.799999 L 15.839999 16.959999 C 15.946666 17.040001 16 17.139999 16 17.259998 C 16 17.379999 15.973333 17.493332 15.92 17.599998 L 15.839999 17.639999 C 15.759998 17.746666 15.659999 17.799999 15.54 17.799999 C 15.419999 17.799999 15.306665 17.773333 15.2 17.719999 L 10 12.52 L 4.84 17.639999 C 4.759999 17.746666 4.66 17.799999 4.54 17.799999 C 4.42 17.799999 4.306666 17.773333 4.2 17.719999 L 4.16 17.639999 C 4.053333 17.559999 4 17.459999 4 17.34 C 4 17.219999 4.026667 17.106667 4.08 17 L 9.28 11.799999 L 4.16 6.639999 C 4.053333 6.56 4 6.46 4 6.339999 C 4 6.22 4.026667 6.106668 4.08 6 L 4.16 5.96 Z " VerticalAlignment="Center" HorizontalAlignment="Center" /> <PathIcon Data="F1 M 4.08 6 L 4.16 5.96 C 4.24 5.853333 4.34 5.799999 4.46 5.799999 C 4.58 5.799999 4.693333 5.826666 4.8 5.879999 L 10 11.08 L 15.16 5.96 C 15.239999 5.853333 15.339999 5.799999 15.46 5.799999 C 15.579999 5.799999 15.693332 5.826666 15.799999 5.879999 L 15.839999 5.96 C 15.946666 6.040001 16 6.14 16 6.259999 C 16 6.38 15.973333 6.493334 15.92 6.599999 L 10.719999 11.799999 L 15.839999 16.959999 C 15.946666 17.040001 16 17.139999 16 17.259998 C 16 17.379999 15.973333 17.493332 15.92 17.599998 L 15.839999 17.639999 C 15.759998 17.746666 15.659999 17.799999 15.54 17.799999 C 15.419999 17.799999 15.306665 17.773333 15.2 17.719999 L 10 12.52 L 4.84 17.639999 C 4.759999 17.746666 4.66 17.799999 4.54 17.799999 C 4.42 17.799999 4.306666 17.773333 4.2 17.719999 L 4.16 17.639999 C 4.053333 17.559999 4 17.459999 4 17.34 C 4 17.219999 4.026667 17.106667 4.08 17 L 9.28 11.799999 L 4.16 6.639999 C 4.053333 6.56 4 6.46 4 6.339999 C 4 6.22 4.026667 6.106668 4.08 6 L 4.16 5.96 Z " VerticalAlignment="Center" HorizontalAlignment="Center" />

View File

@@ -6,7 +6,7 @@ namespace Progrart.Controls.TabSystem;
public partial class TabButton : UserControl public partial class TabButton : UserControl
{ {
public string? Title { get => MainButton.Content as string; set => MainButton.Content = value; } public string? Title { get => TitleBlock.Text; set => TitleBlock.Text = value; }
public string? TooltipText { get => ToolTip.GetTip(MainButton) as string; set => ToolTip.SetTip(MainButton, value); } public string? TooltipText { get => ToolTip.GetTip(MainButton) as string; set => ToolTip.SetTip(MainButton, value); }
public ITabPage? page = null; public ITabPage? page = null;
public TabHost? Host = null; public TabHost? Host = null;
@@ -48,4 +48,19 @@ public partial class TabButton : UserControl
pageControl.IsVisible = v; pageControl.IsVisible = v;
this.HintBackground.IsVisible = v; this.HintBackground.IsVisible = v;
} }
public void ModificationCheck()
{
if (page is ITabPage tabPage)
{
ModificationIndicator.IsVisible= tabPage.IsModified();
}
}
public bool IsModified()
{
if (page is ITabPage tabPage)
{
return tabPage.IsModified();
}
return false;
}
} }

View File

@@ -55,6 +55,26 @@ public partial class TabHost : UserControl
} }
} }
} }
public async Task ForeachButton(Func<TabButton, Task<bool>> handler)
{
foreach (var item in TabContainer.Children)
{
if (item is TabButton btn)
{
if (await handler(btn)) break;
}
}
}
public void ForeachButton(Func<TabButton, bool> handler)
{
foreach (var item in TabContainer.Children)
{
if (item is TabButton btn)
{
if (handler(btn)) break;
}
}
}
public void SelectButton(TabButton button) public void SelectButton(TabButton button)
{ {
foreach (var item in TabContainer.Children) foreach (var item in TabContainer.Children)

View File

@@ -0,0 +1,21 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Progrart.Dialogs.CloseConfirmationDialog">
<StackPanel>
<TextBlock FontSize="18" Name="TitleBlock" TextWrapping="Wrap"/>
<TextBlock Name="MessageBlock" TextWrapping="Wrap"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Classes="accent" Name="OKBtn" Content="Save All"/>
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Classes="accent" Grid.Column="1" Name="DiscardBtn" Content="Discard And Close"/>
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Name="CancelBtn" Grid.Column="2" Content="Cancel"/>
</Grid>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Progrart.Dialogs;
public partial class CloseConfirmationDialog : UserControl
{
public CloseConfirmationDialog()
{
InitializeComponent();
}
}

View File

@@ -90,7 +90,7 @@ public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
public bool IsModified() public bool IsModified()
{ {
return lastSave == CodeEditor.Text; return lastSave != CodeEditor.Text;
} }
public bool IsSameFile(IStorageFile file) public bool IsSameFile(IStorageFile file)

View File

@@ -2,6 +2,7 @@ using Acornima.Ast;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
using Avalonia.Threading; using Avalonia.Threading;
using DialogHostAvalonia;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newtonsoft.Json; using Newtonsoft.Json;
using Progrart.Commands; using Progrart.Commands;
@@ -11,6 +12,7 @@ using Progrart.Core.JSExecution;
using Progrart.Core.ProjectSystem; using Progrart.Core.ProjectSystem;
using Progrart.Core.Settings; using Progrart.Core.Settings;
using Progrart.Core.Storage; using Progrart.Core.Storage;
using Progrart.Dialogs;
using Progrart.Pages; using Progrart.Pages;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -147,6 +149,21 @@ public partial class MainView : UserControl
} }
ConfigBox.SelectedIndex = 0; ConfigBox.SelectedIndex = 0;
}; };
Task.Run(async () =>
{
while (true)
{
await Task.Delay(500);
await Dispatcher.UIThread.InvokeAsync(async () =>
{
await MainTabHost.ForeachButton(async (btn) =>
{
btn.ModificationCheck();
return false;
});
});
}
});
} }
private void Execute() private void Execute()
@@ -279,8 +296,31 @@ public partial class MainView : UserControl
{ {
MainTabHost.AddPage(new SettingsPage()); MainTabHost.AddPage(new SettingsPage());
} }
internal bool CloseCheck()
{
bool v = false;
MainTabHost.ForeachButton((btn) =>
{
if (btn.IsModified())
{
v = true;
return true;
}
return false;
});
return v;
}
private void ExitItem_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) private void ExitItem_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{ {
if (CloseCheck())
{
CloseConfirmationDialog dialog = new CloseConfirmationDialog();
DialogHost.Show(dialog);
}
else
{
Environment.Exit(0);
}
} }
} }