-
Notifications
You must be signed in to change notification settings - Fork 4
/
HomePanel.cs
172 lines (162 loc) · 4.72 KB
/
HomePanel.cs
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
using CropperDeck.Properties;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
namespace CropperDeck {
public class HomePanel : Panel {
public DeckPicker DeckPicker;
public Button BtLoad;
public Button BtMenu;
public MainForm MainForm = null;
public ContextMenuStrip CmActions;
public string DeckName {
get => BtLoad.Text;
set => BtLoad.Text = value;
}
public HomePanel(DeckPicker deckPicker, string name) : base() {
DeckPicker = deckPicker;
Margin = new Padding(12, 12, 12, 12);
Dock = DockStyle.Top;
Height = 40;
Action<Control> vcenter = (ctl) => {
ctl.Height = 30;
ctl.Top = (Height - ctl.Height) / 2;
};
CmActions = new ContextMenuStrip();
#region Change Name
var tfName = new ToolStripTextBox();
tfName.Text = name;
tfName.AutoSize = false;
tfName.Width = 150;
tfName.KeyDown += (sender, args) => {
if (args.KeyCode == Keys.Enter) {
var newName = tfName.Text;
if (!DeckPicker.IsValidDeckName(newName)) {
MessageBox.Show(
$"Can't change deck name to \"{newName}\" - not a valid filename",
DeckPicker.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
);
return;
}
try {
File.Move(DeckState.GetPath(DeckName), DeckState.GetPath(newName));
} catch (Exception ex) {
MessageBox.Show(
$"Can't change deck name to \"{newName}\":\n" + ex,
DeckPicker.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
);
return;
}
if (MainForm != null) {
MainForm.DeckName = newName;
}
DeckName = newName;
args.Handled = true;
args.SuppressKeyPress = true;
CmActions.Close();
}
};
CmActions.Items.Add(tfName);
#endregion
#region Copy colors
var tbCopyColors = new ToolStripButton("Copy custom colors");
tbCopyColors.Click += (sender, args) => {
DeckColors cc;
if (MainForm != null) {
cc = MainForm.CustomColors;
} else cc = DeckState.Load(DeckName).CustomColors;
cc.Enabled = true;
DeckPicker.CustomColors = cc;
DeckPicker.SyncCustomColors();
DeckPicker.CbCustomColors.Checked = true;
};
CmActions.Items.Add(tbCopyColors);
#endregion
#region Delete
var tbDelete = new ToolStripButton("Delete");
tbDelete.Image = Resources.delete;
tbDelete.Click += (sender, args) => {
if (MessageBox.Show(
$"Are you sure you want to delete deck \"{DeckName}\"? This cannot be undone!",
DeckPicker.Text,
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning
) != DialogResult.Yes) return;
MainForm?.Close();
try {
File.Delete(DeckState.GetPath(DeckName));
} catch (Exception ex) {
MessageBox.Show(
"Couldn't delete the deck file:\n" + ex,
DeckPicker.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
);
return;
}
DeckPicker.PanList.Controls.Remove(this);
};
CmActions.Items.Add(tbDelete);
#endregion
CmActions.Opening += (sender, args) => {
tfName.Text = DeckName;
};
ContextMenuStrip = CmActions;
BtMenu = new Button();
BtMenu.Image = Resources.cog;
BtMenu.Width = 30;
BtMenu.Anchor = AnchorStyles.Right;
BtMenu.Left = Width - BtMenu.Width - 6;
BtMenu.Click += (sender, args) => {
CmActions.Show(this, PointToClient(MousePosition));
};
var w = BtMenu.Width;
vcenter(BtMenu);
BtMenu.AutoSize = false;
BtMenu.Width = w;
Controls.Add(BtMenu);
BtLoad = new Button();
var loadPad = BtLoad.Padding;
loadPad.Left += 12;
BtLoad.Padding = loadPad;
BtLoad.TextAlign = ContentAlignment.MiddleLeft;
BtLoad.Text = name;
BtLoad.Anchor = AnchorStyles.Left | AnchorStyles.Right;
vcenter(BtLoad);
BtLoad.Left = 6;
BtLoad.Width = BtMenu.Left - 12;
BtLoad.Click += (sender, args) => { Open(); };
Controls.Add(BtLoad);
ApplyColors(DeckPicker.CustomColors);
}
public void Open() {
if (MainForm != null) {
MainForm.Focus();
if (DeckPicker.CbAutoHide.Checked) { DeckPicker.Hide(); }
return;
}
if (DeckPicker.OverlayServer == null) DeckPicker.StartOverlayServer();
MainForm = new MainForm(this);
MainForm.Show();
if (DeckPicker.CbAutoHide.Checked) { DeckPicker.Hide(); }
}
public void AddTo(Panel ctr) {
var ctrCount = ctr.Controls.Count;
var top = (ctrCount > 0 ? ctr.Controls[ctrCount - 1].Bottom : 0) + Margin.Top;
Top = top;
Left = Margin.Left;
Width = ctr.Width - Left - Margin.Right;
ctr.Controls.Add(this);
ctr.Controls.SetChildIndex(this, 0);
}
public void ApplyColors(DeckColors cc) {
cc.ApplyToButton(BtLoad);
cc.ApplyToButton(BtMenu);
cc.ApplyToToolStrip(CmActions);
}
}
}