-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtensionWindow.xaml.cs
More file actions
152 lines (136 loc) · 5.76 KB
/
ExtensionWindow.xaml.cs
File metadata and controls
152 lines (136 loc) · 5.76 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
using System.Collections.ObjectModel;
using System.Windows;
using XColumn.Models;
namespace XColumn
{
/// <summary>
/// 拡張機能を管理(追加・削除)するためのウィンドウ。
/// </summary>
public partial class ExtensionWindow : Window
{
public ObservableCollection<ExtensionItem> Extensions { get; set; }
public ExtensionWindow(List<ExtensionItem> currentExtensions)
{
InitializeComponent();
Extensions = new ObservableCollection<ExtensionItem>(currentExtensions);
this.DataContext = this;
}
/// <summary>
/// 「フォルダから追加」ボタンの処理。
/// フォルダ選択ダイアログを開き、manifest.json の存在確認を行った上でリストに追加します。
/// </summary>
private void AddExtension_Click(object sender, RoutedEventArgs e)
{
// Formsの名前空間をusingせずに、ここで「完全修飾名」を使って指定します
// これにより、他の場所での Button や MessageBox の競合を防ぎます
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
dialog.Description = "Chrome拡張機能のフォルダ(manifest.jsonが含まれるフォルダ)を選択してください";
dialog.UseDescriptionForTitle = true;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.SelectedPath;
string name = System.IO.Path.GetFileName(path);
foreach (var ext in Extensions)
{
if (ext.Path == path)
{
MessageWindow.Show("この拡張機能は既に追加されています。", "確認");
return;
}
}
Extensions.Add(new ExtensionItem
{
Name = name,
Path = path,
IsEnabled = true
});
}
}
}
/// <summary>
/// 「削除」ボタンの処理。選択された拡張機能をリストから除外します。
/// </summary>
private void RemoveExtension_Click(object sender, RoutedEventArgs e)
{
// Button は System.Windows.Controls.Button として認識されます
if (sender is System.Windows.Controls.Button btn && btn.DataContext is ExtensionItem item)
{
// MessageBox は System.Windows.MessageBox として認識されます
if (MessageWindow.Show($"拡張機能 '{item.Name}' を削除しますか?", "確認",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
Extensions.Remove(item);
}
}
}
/// <summary>
/// 各アイテムの「設定」ボタンクリック時の処理。
/// 親ウィンドウ(MainWindow)経由で拡張機能のオプションページを開きます。
/// </summary>
private void OpenOptions_Click(object sender, RoutedEventArgs e)
{
if (sender is System.Windows.Controls.Button btn && btn.DataContext is ExtensionItem item)
{
if (this.Owner is MainWindow mw)
{
mw.OpenExtensionOptions(item);
// 設定画面がユーザーに見えるよう、管理ウィンドウはいったん閉じる
this.Close();
}
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
/// <summary>
/// 「閉じる」ボタンの処理。DialogResultをtrueにしてウィンドウを閉じ、変更を確定させます。
/// </summary>
private void Close_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
/// <summary>
/// Chromeから拡張機能をインポートするボタンの処理。
/// </summary>
private void ImportFromChrome_Click(object sender, RoutedEventArgs e)
{
var importWin = new ChromeImportWindow();
importWin.Owner = this;
if (importWin.ShowDialog() == true)
{
int count = 0;
foreach (var newItem in importWin.ImportedExtensions)
{
// 重複チェック(パスで判断)
bool exists = false;
foreach (var existing in Extensions)
{
// 名前またはパスが完全に一致する場合
if (existing.Path == newItem.Path || existing.Name == newItem.Name)
{
exists = true;
break;
}
}
if (!exists)
{
Extensions.Add(newItem);
count++;
}
}
if (count > 0)
{
MessageWindow.Show(this, $"{count} 個の拡張機能をインポートしました。", "完了");
}
else
{
MessageWindow.Show(this, "選択された拡張機能は既に追加されています。", "情報");
}
}
}
}
}