Skip to content

Commit 6338a04

Browse files
committed
feat(JSON formatting): Add performance optimizations and loading states for handling large JSON
- Add a loading status prompt on the JSON formatting page - Optimize the performance of large JSON processing by adding limits on the number of elements and nesting depth - Improve the Clipboard Assistant to support asynchronous processing of large text copying - Fix exception handling when selecting JSON values
1 parent 7643a7b commit 6338a04

3 files changed

Lines changed: 280 additions & 99 deletions

File tree

Helpers/ClipboardHelper.cs

Lines changed: 102 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading;
3+
using System.Threading.Tasks;
34
using System.Windows;
45
using System.Windows.Controls;
56
using System.Windows.Controls.Primitives;
@@ -9,6 +10,7 @@
910
using Clipboard = System.Windows.Clipboard;
1011
using Color = System.Windows.Media.Color;
1112
using Brushes = System.Windows.Media.Brushes;
13+
using WpfApplication = System.Windows.Application;
1214

1315
namespace DevTools.Helpers
1416
{
@@ -97,35 +99,121 @@ public static void CopyWithFeedback(string text, Button? button)
9799
_lastCopyTime = now;
98100
}
99101

100-
bool success = false;
101-
for (int i = 0; i < 3; i++)
102+
var isLargeData = text.Length > 10000;
103+
104+
if (isLargeData)
102105
{
106+
Task.Run(() =>
107+
{
108+
bool success = false;
109+
try
110+
{
111+
WpfApplication.Current.Dispatcher.Invoke(() =>
112+
{
113+
try
114+
{
115+
Clipboard.Clear();
116+
Clipboard.SetText(text);
117+
Clipboard.Flush();
118+
success = true;
119+
}
120+
catch
121+
{
122+
}
123+
});
124+
}
125+
catch
126+
{
127+
for (int i = 0; i < 2; i++)
128+
{
129+
try
130+
{
131+
Thread.Sleep(50);
132+
WpfApplication.Current.Dispatcher.Invoke(() =>
133+
{
134+
try
135+
{
136+
Clipboard.Clear();
137+
Clipboard.SetText(text);
138+
Clipboard.Flush();
139+
success = true;
140+
}
141+
catch
142+
{
143+
}
144+
});
145+
if (success) break;
146+
}
147+
catch
148+
{
149+
}
150+
}
151+
}
152+
153+
if (success)
154+
{
155+
if (button != null)
156+
{
157+
WpfApplication.Current.Dispatcher.Invoke(() =>
158+
{
159+
ShowToast(Resources.Strings.CopySuccess, button, false);
160+
});
161+
}
162+
}
163+
else
164+
{
165+
if (button != null)
166+
{
167+
WpfApplication.Current.Dispatcher.Invoke(() =>
168+
{
169+
ShowToast("复制失败,请重试", button, true);
170+
});
171+
}
172+
}
173+
});
174+
}
175+
else
176+
{
177+
bool success = false;
103178
try
104179
{
105180
Clipboard.Clear();
106181
Clipboard.SetText(text);
107182
Clipboard.Flush();
108183
success = true;
109-
break;
110184
}
111185
catch
112186
{
113-
Thread.Sleep(50);
187+
for (int i = 0; i < 2; i++)
188+
{
189+
try
190+
{
191+
Thread.Sleep(50);
192+
Clipboard.Clear();
193+
Clipboard.SetText(text);
194+
Clipboard.Flush();
195+
success = true;
196+
break;
197+
}
198+
catch
199+
{
200+
}
201+
}
114202
}
115-
}
116203

117-
if (success)
118-
{
119-
if (button != null)
204+
if (success)
120205
{
121-
ShowToast(Resources.Strings.CopySuccess, button, false);
206+
if (button != null)
207+
{
208+
ShowToast(Resources.Strings.CopySuccess, button, false);
209+
}
122210
}
123-
}
124-
else
125-
{
126-
if (button != null)
211+
else
127212
{
128-
ShowToast("复制失败,请重试", button, true);
213+
if (button != null)
214+
{
215+
ShowToast("复制失败,请重试", button, true);
216+
}
129217
}
130218
}
131219
}

Pages/JsonFormatPage.xaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@
4040

4141
<!-- Collapsible formatted JSON display -->
4242
<Border Grid.Row="4" BorderBrush="LightGray" BorderThickness="1" Padding="4">
43-
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
43+
<Grid>
4444
<StackPanel x:Name="JsonOutputPanel" />
45-
</ScrollViewer>
45+
<Border x:Name="LoadingOverlay" Background="#80000000" Visibility="Collapsed">
46+
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
47+
<TextBlock Text="处理中..." FontSize="16" Foreground="White" HorizontalAlignment="Center" />
48+
</StackPanel>
49+
</Border>
50+
</Grid>
4651
</Border>
4752
</Grid>
4853
</Page>

0 commit comments

Comments
 (0)