Skip to content

Commit c8b1fa5

Browse files
A slightly more robust system for crop margins
1 parent 920d154 commit c8b1fa5

File tree

6 files changed

+114
-73
lines changed

6 files changed

+114
-73
lines changed

CropMargins.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,67 @@ public struct CropMargins {
1313
new CropMargins("Standard", 8, 32, 8, 8),
1414
new CropMargins("Browser", 2, 39, 2, 2),
1515
};
16+
public static List<CropMargins> Special = new List<CropMargins>() {
17+
new CropMargins("Special: Auto-Crop\u2122", GetAutoCropMargins),
18+
};
19+
20+
/// <summary>Contributed by @Spitfire_x86</summary>
21+
private static CropMarginsResult GetAutoCropMargins(DeckWindow window) {
22+
var handle = window.Handle;
23+
var style = (uint)WinAPI.GetWindowLong(handle, WinAPI_GWL.GWL_STYLE);
24+
var exStyle = (uint)WinAPI.GetWindowLong(handle, WinAPI_GWL.GWL_EXSTYLE);
25+
26+
var rect = new WinAPI_Rect(0, 0, 0, 0); // we just want margins, so adjusted rect is 0
27+
WinAPI.AdjustWindowRectEx(ref rect, style, false, exStyle);
28+
29+
// AdjustWindowRectEx adjusts the rect outwards, so left and top are negative
30+
return new CropMarginsResult(-rect.Left, -rect.Top, rect.Right, rect.Bottom);
31+
}
1632

1733
public string Name;
1834
public int Top, Left, Right, Bottom;
35+
public Func<DeckWindow, CropMarginsResult> Func;
1936
public CropMargins(string name) {
2037
Name = name;
2138
Top = 0;
2239
Left = 0;
2340
Right = 0;
2441
Bottom = 0;
42+
Func = null;
2543
}
2644
public CropMargins(string name, int left, int top, int right, int bottom) {
2745
Name = name;
2846
Left = left;
2947
Top = top;
3048
Right = right;
3149
Bottom = bottom;
50+
Func = null;
51+
}
52+
public CropMargins(string name, Func<DeckWindow, CropMarginsResult> func) {
53+
Name = name;
54+
Left = 0;
55+
Top = 0;
56+
Right = 0;
57+
Bottom = 0;
58+
Func = func;
3259
}
33-
60+
3461
// just so we don't have a hardcoded string in 5 different places
3562
public static readonly string AutoCropName = "Auto";
3663
}
64+
public struct CropMarginsResult {
65+
public int Top, Left, Right, Bottom;
66+
public CropMarginsResult(int left, int top, int right, int bottom) {
67+
Left = left;
68+
Top = top;
69+
Right = right;
70+
Bottom = bottom;
71+
}
72+
public CropMarginsResult(CropMargins m) {
73+
Left = m.Left;
74+
Top = m.Top;
75+
Right = m.Right;
76+
Bottom = m.Bottom;
77+
}
78+
}
3779
}

DeckColumn.cs

+37-25
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class DeckColumn : Panel {
2424
public ToolStripTextBox TfName;
2525
public DeckWindow Window = null;
2626
public CropMargins CropMargins = CropMargins.Zero;
27-
public bool ShouldAutoCrop = false;
2827
public Panel WindowCtr;
2928
public ToolStripSpringLabel TlName;
3029
public Point OverlaySize = new Point(0, 0);
@@ -80,6 +79,7 @@ public void ChangeName(string newName) {
8079
MainForm.FlushConfig();
8180
}
8281
public static Image LoadImage(string path) {
82+
// doing Image.FromFile locks the file and then we can't delete it along with the column
8383
var bytes = File.ReadAllBytes(path);
8484
var stream = new MemoryStream(bytes);
8585
return Image.FromStream(stream);
@@ -119,32 +119,43 @@ public void HideOverlay() {
119119
private void TmPadding_Init() {
120120
TbPadding = new ToolStripDropDownButton("Crop...", Resources.shape_handles);
121121
TbPadding.DisplayStyle = ToolStripItemDisplayStyle.Image;
122+
TbPadding.DropDownOpening += TmPadding_DropDownOpening;
122123
TmPadding_Build();
123124
}
124-
125-
public void TmPadding_Build() {
126-
TbPadding.DropDownItems.Clear();
127-
foreach (var m in MainForm.CropMargins) {
128-
var tb = new ToolStripButton(m.Name);
129-
tb.TextAlign = ContentAlignment.MiddleLeft;
130-
//tb.AutoSize = true;
131-
tb.Width = 80;
132-
tb.Click += (object sender, EventArgs e) => {
133-
CropMargins = m;
134-
ShouldAutoCrop = false; // window should not auto-crop
135-
Window?.Update();
136-
MainForm.FlushConfig();
137-
};
138-
TbPadding.DropDownItems.Add(tb);
125+
private void TmPadding_DropDownOpening(object sender, EventArgs e) {
126+
var cmName = CropMargins.Name;
127+
foreach (var item in TbPadding.DropDownItems) {
128+
if (!(item is DeckColumnMarginButton bt)) continue;
129+
bt.Checked = bt.Text == cmName;// ? CheckState.Checked : CheckState.Unchecked;
139130
}
140-
141-
var tbAuto = new ToolStripButton("Auto-Crop\u2122");
142-
tbAuto.Click += (sender, args) => {
143-
ShouldAutoCrop = true; // window should auto-crop, see DeckWindow.GetAutoCropMargins()
131+
}
132+
private void TmPadding_Build_Margin(CropMargins m) {
133+
var tb = new DeckColumnMarginButton();
134+
tb.TextAlign = ContentAlignment.MiddleLeft;
135+
//tb.AutoSize = true; // doesn't work..?
136+
tb.Width = 120;
137+
tb.Text = m.Name;
138+
tb.Click += (sender, e) => {
139+
CropMargins = m;
144140
Window?.Update();
145141
MainForm.FlushConfig();
146142
};
147-
TbPadding.DropDownItems.Add(tbAuto);
143+
TbPadding.DropDownItems.Add(tb);
144+
}
145+
public void TmPadding_Build() {
146+
TbPadding.DropDownItems.Clear();
147+
foreach (var m in MainForm.CropMargins) TmPadding_Build_Margin(m);
148+
149+
TbPadding.DropDownItems.Add(new ToolStripSeparator());
150+
foreach (var m in CropMargins.Special) TmPadding_Build_Margin(m);
151+
152+
TbPadding.DropDownItems.Add(new ToolStripSeparator());
153+
var tbEdit = new ToolStripButton("Add/edit margins");
154+
tbEdit.Name = "Edit";
155+
tbEdit.Click += (sender, args) => {
156+
MainForm.TbConfig_Click(sender, args);
157+
};
158+
TbPadding.DropDownItems.Add(tbEdit);
148159
}
149160

150161
private void TbConfig_Init() {
@@ -162,7 +173,7 @@ private void TbConfig_Init() {
162173
TbConfig.DropDownItems.Add(TfName);
163174

164175
var tbIcon = new ToolStripButton("Change icon", Resources.pencil);
165-
tbIcon.Click += (object sender, EventArgs e) => {
176+
tbIcon.Click += (sender, e) => {
166177
MainForm.OnIconPick = (string path) => {
167178
if (IconName != "") {
168179
if (File.Exists(IconPath)) File.Delete(IconPath);
@@ -181,7 +192,7 @@ private void TbConfig_Init() {
181192
TbConfig.DropDownItems.Add(tbIcon);
182193

183194
var tbLeft = new ToolStripButton("Move left", Resources.arrow_left);
184-
tbLeft.Click += (object sender, EventArgs e) => {
195+
tbLeft.Click += (sender, e) => {
185196
var ind = MainForm.PanCtr.Controls.GetChildIndex(this);
186197
if (ind < MainForm.PanCtr.Controls.Count - 1) {
187198
MainForm.PanCtr.Controls.SetChildIndex(this, ind + 1);
@@ -192,7 +203,7 @@ private void TbConfig_Init() {
192203
TbConfig.DropDownItems.Add(tbLeft);
193204

194205
var tbRight = new ToolStripButton("Move Right", Resources.arrow_right);
195-
tbRight.Click += (object sender, EventArgs e) => {
206+
tbRight.Click += (sender, e) => {
196207
var ind = MainForm.PanCtr.Controls.GetChildIndex(this);
197208
if (ind > 0) {
198209
MainForm.PanCtr.Controls.SetChildIndex(this, ind - 1);
@@ -203,7 +214,7 @@ private void TbConfig_Init() {
203214
TbConfig.DropDownItems.Add(tbRight);
204215

205216
var tbDelete = new ToolStripButton("Remove", Resources.delete);
206-
tbDelete.Click += (object sender, EventArgs e) => {
217+
tbDelete.Click += (sender, e) => {
207218
if (MessageBox.Show(
208219
"Are you sure you want to delete this column? This cannot be undone!",
209220
MainForm.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning
@@ -269,4 +280,5 @@ public void UpdateCrop() {
269280
Window?.Update();
270281
}
271282
}
283+
public class DeckColumnMarginButton : ToolStripButton { }
272284
}

DeckState.cs

+20-23
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ public void Acquire(MainForm form) {
3434
foreach (var m in form.CropMargins) Margins.Add(m);
3535
foreach (var pan in form.GetDeckColumns()) {
3636
var cm = pan.CropMargins;
37-
string cmName;
38-
if (pan.ShouldAutoCrop) {
39-
cmName = CropMargins.AutoCropName;
40-
}
41-
else {
42-
cmName = cm.Name;
43-
if (!MarginsContainName(Margins, cmName) && !MarginsContainName(HiddenMargins, cmName)) {
44-
HiddenMargins.Add(cm);
45-
}
37+
var cmName = cm.Name;
38+
if (!MarginsContainName(Margins, cmName)
39+
&& !MarginsContainName(CropMargins.Special, cmName)
40+
&& !MarginsContainName(HiddenMargins, cmName)
41+
) {
42+
HiddenMargins.Add(cm);
4643
}
4744
var cd = new ColumnData();
4845
cd.Name = pan.ColumnName;
@@ -62,19 +59,19 @@ public void Apply(MainForm form) {
6259
form.PanCtr.SuspendLayout();
6360
foreach (var panState in Columns) {
6461
var pan = new DeckColumn(form);
65-
if (panState.CropStyle == CropMargins.AutoCropName) {
66-
pan.ShouldAutoCrop = true;
67-
}
68-
else {
69-
var cm = Margins.Where(m => m.Name == panState.CropStyle).FirstOrDefault();
70-
if (cm.Name == null)
71-
{
72-
cm = HiddenMargins.Where(m => m.Name == panState.CropStyle).FirstOrDefault();
73-
if (cm.Name == null) cm = CropMargins.Zero;
74-
}
75-
pan.CropMargins = cm;
76-
pan.ShouldAutoCrop = false;
77-
}
62+
63+
CropMargins cm;
64+
do {
65+
cm = Margins.Where(m => m.Name == panState.CropStyle).FirstOrDefault();
66+
if (cm.Name != null) break;
67+
cm = CropMargins.Special.Where(m => m.Name == panState.CropStyle).FirstOrDefault();
68+
if (cm.Name != null) break;
69+
cm = HiddenMargins.Where(m => m.Name == panState.CropStyle).FirstOrDefault();
70+
if (cm.Name != null) break;
71+
cm = CropMargins.Zero;
72+
} while (false);
73+
pan.CropMargins = cm;
74+
7875
pan.TfName.Text = pan.TlName.Text = pan.ColumnName = panState.Name;
7976
if (panState.Icon != "") {
8077
pan.IconName = panState.Icon;
@@ -98,7 +95,7 @@ public void Apply(MainForm form) {
9895
public void Save() {
9996
if (!Directory.Exists("config")) Directory.CreateDirectory("config");
10097
var text = JsonConvert.SerializeObject(this, Formatting.Indented);
101-
File.WriteAllText($"config/{Name}.json", text);
98+
File.WriteAllText($"config/{Name}.json", text, Encoding.UTF8);
10299
}
103100

104101
static bool MarginsContainName(List<CropMargins> list, string cmName) {

DeckWindow.cs

+12-22
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ public class DeckWindow {
1010
public DeckColumn Column;
1111
public IntPtr Handle;
1212
public WinAPI_Rect OrigRect = new WinAPI_Rect();
13-
public CropMargins CropMargins {
14-
get => (Column.ShouldAutoCrop)
15-
? GetAutoCropMargins()
16-
: Column.CropMargins;
17-
}
13+
public CropMarginsResult GetCropMargins() {
14+
var m = Column.CropMargins;
15+
if (m.Func != null) {
16+
return m.Func(this);
17+
} else return new CropMarginsResult(m);
18+
}
1819

1920
public DeckWindow(IntPtr hwnd, DeckColumn col) {
2021
Handle = hwnd;
2122
Column = col;
2223
WinAPI.GetWindowRect(Handle, out OrigRect);
2324
}
2425
public int GetWidth() {
25-
return OrigRect.Width - CropMargins.Left - CropMargins.Right;
26+
var m = GetCropMargins();
27+
return OrigRect.Width - m.Left - m.Right;
2628
}
2729
public void Insert() {
2830
WinAPI.SetParent(Handle, Column.WindowCtr.Handle);
@@ -36,23 +38,11 @@ public void Update() {
3638
var stripHeight = 0;//Panel.ToolStrip.Height;
3739
var height = Column.WindowCtr.Height - stripHeight;
3840
var width = Column.WindowCtr.Width;
41+
var m = GetCropMargins();
3942
WinAPI.SetWindowRect(Handle,
40-
-CropMargins.Left, -CropMargins.Top + stripHeight,
41-
width + CropMargins.Left + CropMargins.Right,
42-
height + CropMargins.Top + CropMargins.Bottom);
43+
-m.Left, -m.Top + stripHeight,
44+
width + m.Left + m.Right,
45+
height + m.Top + m.Bottom);
4346
}
44-
45-
// get margins from winapi based on the current window styles (should crop just the client area)
46-
private CropMargins GetAutoCropMargins()
47-
{
48-
uint style = (uint) WinAPI.GetWindowLong(Handle, WinAPI_GWL.GWL_STYLE);
49-
uint exStyle = (uint) WinAPI.GetWindowLong(Handle, WinAPI_GWL.GWL_EXSTYLE);
50-
51-
WinAPI_Rect rect = new WinAPI_Rect(0, 0, 0, 0); // we just want margins, so adjusted rect is 0
52-
WinAPI.AdjustWindowRectEx(ref rect, style, false, exStyle);
53-
54-
// AdjustWindowRectEx adjusts the rect outwards, so left and top are negative
55-
return new CropMargins(CropMargins.AutoCropName, -rect.Left, -rect.Top, rect.Right, rect.Bottom);
56-
}
5747
}
5848
}

Forms/MainForm.Designer.cs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Forms/MainForm.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void MainForm_ResizeEnd(object sender, EventArgs e) {
183183
}
184184
}
185185

186-
private void TbConfig_Click(object sender, EventArgs e) {
186+
public void TbConfig_Click(object sender, EventArgs e) {
187187
if (ConfigForm == null) {
188188
ConfigForm = new ConfigForm(this);
189189
ConfigForm.Show();

0 commit comments

Comments
 (0)