-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathViewerDialog.cs
More file actions
235 lines (205 loc) · 7.2 KB
/
Copy pathViewerDialog.cs
File metadata and controls
235 lines (205 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Eto.Drawing;
using Eto.Forms;
using Lin.Helper.Core.Pak;
using PakViewer.Controls;
using PakViewer.Localization;
using PakViewer.Viewers;
namespace PakViewer
{
/// <summary>
/// 詳細檢視視窗 - 獨立視窗顯示完整預覽器
/// </summary>
public class ViewerDialog : Form
{
private PakFile _pak;
private List<GalleryItem> _items;
private int _currentIndex;
private Panel _viewerContainer;
private Label _titleLabel;
private Label _indexLabel;
private IFileViewer _currentViewer;
public ViewerDialog(PakFile pak, List<GalleryItem> items, int startIndex)
{
_pak = pak;
_items = items;
_currentIndex = FindItemIndex(startIndex);
Title = I18n.T("ViewerDialog.Title");
Size = new Size(800, 600);
MinimumSize = new Size(400, 300);
Resizable = true;
BuildUI();
LoadCurrentFile();
}
private int FindItemIndex(int pakIndex)
{
for (int i = 0; i < _items.Count; i++)
{
if (_items[i].Index == pakIndex)
return i;
}
return 0;
}
private void BuildUI()
{
// 標題列
_titleLabel = new Label
{
Font = new Font(SystemFont.Bold, 12),
VerticalAlignment = VerticalAlignment.Center
};
_indexLabel = new Label
{
TextColor = Colors.Gray,
VerticalAlignment = VerticalAlignment.Center
};
var headerLayout = new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 10,
Padding = new Padding(10, 5),
Items = { _titleLabel, _indexLabel }
};
// 導航按鈕
var prevBtn = new Button { Text = I18n.T("Button.Prev"), Width = 80 };
var nextBtn = new Button { Text = I18n.T("Button.Next"), Width = 80 };
var exportBtn = new Button { Text = I18n.T("Button.Export"), Width = 80 };
prevBtn.Click += (s, e) => Navigate(-1);
nextBtn.Click += (s, e) => Navigate(1);
exportBtn.Click += OnExport;
var navLayout = new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 10,
Padding = new Padding(10, 5),
Items = { prevBtn, nextBtn, new StackLayoutItem(null, true), exportBtn }
};
// 預覽區容器
_viewerContainer = new Panel { BackgroundColor = Colors.DarkGray };
// 整體佈局
Content = new TableLayout
{
Rows =
{
new TableRow(headerLayout),
new TableRow(_viewerContainer) { ScaleHeight = true },
new TableRow(navLayout)
}
};
// 鍵盤快捷鍵
KeyDown += OnKeyDown;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Keys.Left:
case Keys.Up:
Navigate(-1);
e.Handled = true;
break;
case Keys.Right:
case Keys.Down:
Navigate(1);
e.Handled = true;
break;
case Keys.Escape:
Close();
e.Handled = true;
break;
}
}
private void Navigate(int delta)
{
if (_items.Count == 0) return;
_currentIndex = (_currentIndex + delta + _items.Count) % _items.Count;
LoadCurrentFile();
}
private void LoadCurrentFile()
{
if (_currentIndex < 0 || _currentIndex >= _items.Count) return;
var item = _items[_currentIndex];
// 更新標題
_titleLabel.Text = item.FileName;
_indexLabel.Text = $"[{_currentIndex + 1}/{_items.Count}]";
Title = $"{I18n.T("ViewerDialog.Title")} - {item.FileName}";
// 載入檔案
try
{
var data = _pak.Extract(item.Index);
var ext = Path.GetExtension(item.FileName).ToLowerInvariant();
// 釋放舊 viewer
_currentViewer?.Dispose();
_currentViewer = null;
// 建立新 viewer
_currentViewer = ViewerFactory.CreateViewerSmart(ext, data, item.FileName);
_currentViewer.EncryptionType = _pak?.EncryptionType;
_currentViewer.LoadData(data, item.FileName);
// 顯示 viewer
var viewerControl = _currentViewer.GetControl();
var editToolbar = _currentViewer.CanEdit ? _currentViewer.GetEditToolbar() : null;
if (editToolbar != null)
{
_viewerContainer.Content = new TableLayout
{
Spacing = new Size(0, 5),
Rows =
{
new TableRow(editToolbar),
new TableRow(viewerControl) { ScaleHeight = true }
}
};
}
else
{
_viewerContainer.Content = viewerControl;
}
}
catch (Exception ex)
{
_viewerContainer.Content = new Label
{
Text = $"Error: {ex.Message}",
TextColor = Colors.Red,
VerticalAlignment = VerticalAlignment.Center,
TextAlignment = TextAlignment.Center
};
}
}
private void OnExport(object sender, EventArgs e)
{
if (_currentIndex < 0 || _currentIndex >= _items.Count) return;
var item = _items[_currentIndex];
var dialog = new SaveFileDialog
{
Title = I18n.T("Dialog.SaveFile"),
FileName = item.FileName
};
if (dialog.ShowDialog(this) == DialogResult.Ok)
{
try
{
var data = _pak.Extract(item.Index);
File.WriteAllBytes(dialog.FileName, data);
MessageBox.Show(this, I18n.T("Status.Exported", 1), I18n.T("Dialog.Info"), MessageBoxType.Information);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, I18n.T("Dialog.Error"), MessageBoxType.Error);
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_currentViewer?.Dispose();
_currentViewer = null;
}
base.Dispose(disposing);
}
}
}