Skip to content

Commit cb89ecd

Browse files
committed
pref: 优化布局载入、保存布局和重命名逻辑;双击恢复布局
1 parent dbdb084 commit cb89ecd

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

MainForm.Designer.cs

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MainForm.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ await Task.Run(() =>
6767
foreach (var file in files)
6868
{
6969
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
70+
// Layout_2560x1600_UserName_2025_0310_200118.json 示例
7071
var parts = fileNameWithoutExtension.Split('_');
71-
string displayName = parts.Length >= 3 ? $"{LayoutPrefix}[{parts[1]}]" : fileNameWithoutExtension;
72+
string displayName = $"{parts[0]}[{parts[1]}]";
73+
if (parts[0] == "Layout")
74+
{
75+
displayName = parts.Length >= 3 ? $"{LayoutPrefix}[{parts[1]}]" : fileNameWithoutExtension;
76+
}
77+
7278
string userName = parts.Length >= 3 ? parts[2] : "";
73-
DateTime lastWrite = File.GetLastWriteTime(file);
7479

80+
string dateString = $"{parts[3]}{parts[4]}{parts[5]}";
81+
DateTime lastWrite = DateTime.ParseExact(dateString, "yyyyMMddHHmmss", null);
7582
var item = new ListViewItem(displayName) { Tag = file };
7683
item.SubItems.Add(userName);
7784
item.SubItems.Add(lastWrite.ToString("yyyy-MM-dd HH:mm"));
@@ -90,6 +97,7 @@ await Task.Run(() =>
9097
}, "加载布局时出错");
9198
}
9299

100+
// 保存桌面布局
93101
private async void SaveButton_Click(object sender, EventArgs e)
94102
{
95103
// 禁用保存按钮防止重复点击
@@ -141,7 +149,7 @@ await ExecuteWithErrorHandlingAsync(async () =>
141149
}
142150
}
143151

144-
152+
// 恢复桌面布局
145153
private async void RestoreButton_Click(object sender, EventArgs e)
146154
{
147155
string? filePath = GetSelectedLayoutFilePath();
@@ -157,28 +165,45 @@ await ExecuteWithErrorHandlingAsync(async () =>
157165
}, "恢复布局时出错");
158166
}
159167

168+
// 重命名布局
160169
private void RenameButton_Click(object sender, EventArgs e)
161170
{
162171
string? oldFilePath = GetSelectedLayoutFilePath();
163172
if (oldFilePath == null)
164173
return;
174+
string? directoryName = Path.GetDirectoryName(oldFilePath);
175+
if (directoryName == null)
176+
{
177+
throw new Exception("无法获取文件目录路径");
178+
}
179+
180+
// 获取文件名的其他部分
181+
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(oldFilePath);
182+
var parts = fileNameWithoutExtension.Split('_');
183+
if (parts.Length < 4)
184+
{
185+
throw new Exception("文件名格式不正确");
186+
}
165187

166188
string oldName = Path.GetFileNameWithoutExtension(oldFilePath);
167-
using var renameDialog = new InputDialog(oldName);
189+
// 显示在重命名窗口中的旧名称
190+
string oldDisplayName = $"{parts[0]}";
191+
using var renameDialog = new InputDialog(oldDisplayName);
192+
168193
if (renameDialog.ShowDialog() == DialogResult.OK)
169194
{
170195
string newName = renameDialog.InputTextBox.Text.Trim();
171-
string? directoryName = Path.GetDirectoryName(oldFilePath);
172-
if (directoryName == null)
173-
{
174-
throw new Exception("无法获取文件目录路径");
175-
}
176-
string newFilePath = Path.Combine(directoryName, newName + ".json");
196+
197+
// 仅替换前缀
198+
string newFileName = $"{newName}_{parts[1]}_{parts[2]}_{parts[3]}_{parts[4]}_{parts[5]}";
199+
string newDisplayName = $"{newName}[{parts[1]}]";
200+
string newFilePath = Path.Combine(directoryName, newFileName + ".json");
201+
177202
ExecuteWithErrorHandling(() =>
178203
{
179204
File.Move(oldFilePath, newFilePath);
180205
var selectedItem = LayoutsListView.SelectedItems[0];
181-
selectedItem.Text = newName;
206+
selectedItem.Text = newDisplayName;
182207
selectedItem.Tag = newFilePath;
183208
UpdateRunningStatus("重命名成功");
184209
}, "重命名布局时出错");

0 commit comments

Comments
 (0)