-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cs
More file actions
9533 lines (8289 loc) · 364 KB
/
Copy pathMain.cs
File metadata and controls
9533 lines (8289 loc) · 364 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using BeanModManager.Dialogs;
using BeanModManager.Helpers;
using BeanModManager.Models;
using BeanModManager.Services;
using BeanModManager.Themes;
using BeanModManager.Wizard;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BeanModManager
{
public partial class Main : Form
{
private Config _config;
private ModStore _modStore;
private ModDownloader _modDownloader;
private ModInstaller _modInstaller;
private ModImporter _modImporter;
private BepInExInstaller _bepInExInstaller;
private SteamDepotService _steamDepotService;
private UpdateChecker _updateChecker;
private List<Mod> _availableMods;
private Dictionary<string, ModCard> _modCards;
private bool _isInstalling = false;
private readonly object _installLock = new object();
private readonly HashSet<string> _selectedModIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private readonly HashSet<string> _bulkSelectedModIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private readonly HashSet<string> _bulkSelectedStoreModIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private string _installedSearchText = string.Empty;
private string _storeSearchText = string.Empty;
private string _installedCategoryFilter = "All";
private string _storeCategoryFilter = "All";
private bool _isUpdatingCategoryFilters = false;
private Timer _installedSearchDebounceTimer;
private Timer _storeSearchDebounceTimer;
private Timer _refreshDebounceTimer;
private bool _isRefreshing = false;
private bool _isApplyingThemeSelection;
private List<InstalledModInfo> _cachedDetectedMods;
private HashSet<string> _cachedDetectedModIds;
private HashSet<string> _cachedExistingModFolders;
private string _cachedModsFolder;
private Dictionary<string, bool> _cachedInstallationStatus;
private DateTime _cacheLastUpdated = DateTime.MinValue;
private readonly TimeSpan _cacheMaxAge = TimeSpan.FromSeconds(5);
private bool? _cachedIsEpicOrMsStore;
private int? _cachedPendingUpdatesCount;
private readonly HashSet<string> _explicitlySetMods = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private bool _suppressSkeletonOnRefresh = false;
private volatile bool _isInitialLoadInProgress = false;
private bool _suppressStorePanelUpdates = false;
private TabPage _tabModpacks;
private Button _btnSidebarModpacks;
private ListView _lvModpacks;
private Label _lblModpackTitle;
private Label _lblModpackModCount;
private Button _btnModpackPlay;
private ContextMenuStrip _ctxModpackList;
private ContextMenuStrip _ctxInPackList;
private ContextMenuStrip _ctxInstalledList;
private Button _btnModpackNew;
private ListView _lvModpackMods;
private bool _isApplyingModpackSelection;
private ListView _lvInstalledMods;
private Button _btnAddToModpack;
private Button _btnRemoveFromModpack;
private Panel _modpacksLeftOuter;
private Panel _modpacksRightOuter;
private Control _modpacksEditor;
private Panel _modpacksListOuter;
private Panel _modpacksListInner;
private Panel _inPackListOuter;
private Panel _inPackListInner;
private Panel _installedListOuter;
private Panel _installedListInner;
private void RefreshModpacksUiAfterModsLoaded()
{
if (_lvModpacks == null || _tabModpacks == null)
return;
RefreshModpacksList();
RefreshModpackDetails();
}
private sealed class ThemedToolStripRenderer : ToolStripProfessionalRenderer
{
public ThemedToolStripRenderer(ProfessionalColorTable table) : base(table)
{
RoundedEdges = false;
}
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
if (e?.ToolStrip == null)
return;
var rect = new Rectangle(Point.Empty, e.ToolStrip.Size);
rect.Width -= 1;
rect.Height -= 1;
using (var pen = new Pen(ThemeManager.Current.CardBorderColor))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
{
if (e?.Item == null)
return;
var bounds = new Rectangle(6, e.Item.ContentRectangle.Top + (e.Item.ContentRectangle.Height / 2), e.Item.ContentRectangle.Width - 12, 1);
using (var pen = new Pen(ThemeManager.Current.CardBorderColor))
{
e.Graphics.DrawLine(pen, bounds.Left, bounds.Top, bounds.Right, bounds.Top);
}
}
}
private sealed class ThemedMenuColorTable : ProfessionalColorTable
{
private readonly Color _bg;
private readonly Color _border;
private readonly Color _itemSelected;
private readonly Color _itemPressed;
public ThemedMenuColorTable(Color bg, Color border, Color itemSelected, Color itemPressed)
{
_bg = bg;
_border = border;
_itemSelected = itemSelected;
_itemPressed = itemPressed;
UseSystemColors = false;
}
public override Color ToolStripDropDownBackground => _bg;
public override Color MenuBorder => _border;
public override Color MenuItemBorder => _border;
public override Color MenuItemSelected => _itemSelected;
public override Color MenuItemPressedGradientBegin => _itemPressed;
public override Color MenuItemPressedGradientMiddle => _itemPressed;
public override Color MenuItemPressedGradientEnd => _itemPressed;
public override Color ImageMarginGradientBegin => _bg;
public override Color ImageMarginGradientMiddle => _bg;
public override Color ImageMarginGradientEnd => _bg;
}
private void ApplyThemeToContextMenu(ContextMenuStrip ctx)
{
if (ctx == null)
return;
var palette = ThemeManager.Current;
var isDark = ThemeManager.CurrentVariant == ThemeVariant.Dark;
var bg = palette.SurfaceColor;
var border = palette.CardBorderColor;
var hover = isDark ? Color.FromArgb(50, 55, 65) : Color.FromArgb(235, 238, 244);
var pressed = isDark ? Color.FromArgb(60, 66, 78) : Color.FromArgb(225, 229, 238);
ctx.ShowImageMargin = false; ctx.ShowCheckMargin = false;
ctx.RenderMode = ToolStripRenderMode.Professional;
ctx.Renderer = new ThemedToolStripRenderer(new ThemedMenuColorTable(bg, border, hover, pressed));
ctx.BackColor = bg;
ctx.ForeColor = palette.PrimaryTextColor;
ctx.Font = new Font("Segoe UI", 9F);
foreach (ToolStripItem item in ctx.Items)
{
item.BackColor = bg;
item.ForeColor = palette.PrimaryTextColor;
if (item is ToolStripMenuItem mi)
{
mi.Font = ctx.Font;
}
}
}
public Main()
{
InitializeComponent();
var version = Assembly.GetExecutingAssembly().GetName().Version;
this.Text = $"Bean Mod Manager v{version.Major}.{version.Minor}.{version.Build}";
InitializeUiPerformanceTweaks();
_config = Config.Load();
EnsureModpacksInitialized();
if (!_config.FirstLaunchWizardCompleted)
{
this.ShowInTaskbar = false;
this.Visible = false;
}
DarkModeHelper.InitializeDarkMode();
this.HandleCreated += Main_HandleCreated;
this.Load += Main_Load;
InitializeThemeSystem();
if (tabControl != null)
{
tabControl.SelectedIndexChanged += TabControl_SelectedIndexChanged;
}
this.KeyPreview = true;
this.KeyDown += Main_KeyDown;
InitializeModpacksUi();
UpdateSidebarSelection();
UpdateStats();
UpdateHeaderInfo();
if (sidebarBorder != null && leftSidebar != null && leftSidebar.Controls.Contains(sidebarBorder))
{
leftSidebar.Controls.SetChildIndex(sidebarBorder, leftSidebar.Controls.Count - 1);
}
_modStore = new ModStore();
_modDownloader = new ModDownloader();
_modInstaller = new ModInstaller();
_modImporter = new ModImporter();
_bepInExInstaller = new BepInExInstaller();
_updateChecker = new UpdateChecker();
_modCards = new Dictionary<string, ModCard>();
LoadSavedSelection();
_modDownloader.ProgressChanged += (s, msg) => UpdateStatus(msg);
_modInstaller.ProgressChanged += (s, msg) => UpdateStatus(msg);
_modImporter.ProgressChanged += (s, msg) => UpdateStatus(msg);
_bepInExInstaller.ProgressChanged += (s, msg) => UpdateStatus(msg);
_steamDepotService = new SteamDepotService(_modStore);
_steamDepotService.ProgressChanged += (s, msg) => UpdateStatus(msg);
_updateChecker.ProgressChanged += (s, msg) => HandleUpdateCheckerProgress(msg);
_updateChecker.UpdateAvailable += UpdateChecker_UpdateAvailable;
LoadSettings();
_installedSearchDebounceTimer = new Timer { Interval = 300 };
_installedSearchDebounceTimer.Tick += (s, e) =>
{
_installedSearchDebounceTimer.Stop();
RefreshModCardsDebounced();
};
_storeSearchDebounceTimer = new Timer { Interval = 300 };
_storeSearchDebounceTimer.Tick += (s, e) =>
{
_storeSearchDebounceTimer.Stop();
RefreshModCardsDebounced();
};
_refreshDebounceTimer = new Timer { Interval = 150 };
_refreshDebounceTimer.Tick += (s, e) =>
{
_refreshDebounceTimer.Stop();
if (!_isRefreshing)
{
RefreshModCards();
}
};
}
private void EnsureModpacksInitialized()
{
if (_config == null)
return;
if (_config.Modpacks == null)
_config.Modpacks = new List<ModPack>();
}
private void InitializeModpacksUi()
{
if (tabControl == null || sidebarButtons == null)
return;
_tabModpacks = new TabPage
{
Text = "Modpacks",
Padding = new Padding(10)
};
var storeTab = tabControl.TabPages[1]; var settingsTab = tabControl.TabPages[2];
tabControl.TabPages.Remove(storeTab);
tabControl.TabPages.Remove(settingsTab);
tabControl.TabPages.Add(_tabModpacks);
tabControl.TabPages.Add(storeTab);
tabControl.TabPages.Add(settingsTab);
_btnSidebarModpacks = new Button
{
BackColor = Color.Transparent,
Dock = DockStyle.Top,
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI", 9.5F, FontStyle.Regular, GraphicsUnit.Point, 0),
ForeColor = Color.FromArgb(90, 90, 110),
Margin = new Padding(0),
Padding = new Padding(16, 0, 0, 0),
Size = new Size(203, 40),
Text = "Modpacks",
TextAlign = ContentAlignment.MiddleLeft,
UseVisualStyleBackColor = false
};
_btnSidebarModpacks.FlatAppearance.BorderSize = 0;
_btnSidebarModpacks.FlatAppearance.MouseOverBackColor = Color.FromArgb(240, 242, 247);
_btnSidebarModpacks.Click += btnSidebarModpacks_Click;
sidebarButtons.Controls.Add(_btnSidebarModpacks);
if (btnSidebarLaunchVanilla != null) sidebarButtons.Controls.SetChildIndex(btnSidebarLaunchVanilla, 0);
if (btnSidebarSettings != null) sidebarButtons.Controls.SetChildIndex(btnSidebarSettings, 1);
if (btnSidebarStore != null) sidebarButtons.Controls.SetChildIndex(btnSidebarStore, 2);
sidebarButtons.Controls.SetChildIndex(_btnSidebarModpacks, 3);
if (btnSidebarInstalled != null) sidebarButtons.Controls.SetChildIndex(btnSidebarInstalled, 4);
sidebarButtons.Height += 40;
BuildModpacksTabContent();
}
private void BuildModpacksTabContent()
{
if (_tabModpacks == null)
return;
var palette = ThemeManager.Current;
Button MakeActionButton(string text, bool primary = false, bool danger = false, bool success = false)
{
var btn = new Button
{
Text = text,
AutoSize = false,
Width = 120,
Height = 36,
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI Semibold", 9.5F, FontStyle.Bold),
Margin = new Padding(0, 0, 10, 0),
Padding = new Padding(6, 0, 6, 0),
TextAlign = ContentAlignment.MiddleCenter,
UseVisualStyleBackColor = false
};
btn.FlatAppearance.BorderSize = 0;
if (success)
{
btn.BackColor = palette.SuccessButtonColor;
btn.ForeColor = palette.SuccessButtonTextColor;
}
else if (danger)
{
btn.BackColor = palette.DangerButtonColor;
btn.ForeColor = palette.DangerButtonTextColor;
}
else if (primary)
{
btn.BackColor = palette.PrimaryButtonColor;
btn.ForeColor = palette.PrimaryButtonTextColor;
}
else
{
btn.BackColor = palette.NeutralButtonColor;
btn.ForeColor = palette.NeutralButtonTextColor;
}
return btn;
}
var root = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 1,
RowCount = 1,
Padding = new Padding(16)
};
root.BackColor = palette.WindowBackColor;
root.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
root.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
var split = new SplitContainer
{
Dock = DockStyle.Fill,
Orientation = Orientation.Vertical,
SplitterWidth = 8
};
ConfigureModpacksSplitContainerSizing(split);
_modpacksLeftOuter = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(0),
BackColor = palette.SurfaceColor
};
var leftPanel = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(8),
BackColor = palette.SurfaceColor
};
var leftLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 1,
RowCount = 2
};
leftLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
leftLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); leftLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
var headerBar = new Panel
{
Dock = DockStyle.Top,
Height = 56,
Padding = new Padding(12),
BackColor = palette.SurfaceAltColor,
Margin = new Padding(0, 0, 0, 8)
};
var headerRow = new TableLayoutPanel
{
Dock = DockStyle.Fill,
AutoSize = true,
ColumnCount = 2,
RowCount = 1
};
headerRow.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
headerRow.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
var headerLabel = new Label
{
AutoSize = true,
Text = "Modpacks",
Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold),
ForeColor = palette.HeadingTextColor,
Margin = new Padding(0, 6, 0, 0)
};
_btnModpackNew = MakeActionButton("New modpack", primary: true);
_btnModpackNew.Click += (s, e) => CreateNewModpack(empty: true);
headerRow.Controls.Add(headerLabel, 0, 0);
headerRow.Controls.Add(_btnModpackNew, 1, 0);
headerBar.Controls.Add(headerRow);
_lvModpacks = new ListView
{
Dock = DockStyle.Fill,
View = View.Details,
FullRowSelect = true,
HideSelection = false,
MultiSelect = false,
BorderStyle = BorderStyle.None,
GridLines = false
};
_lvModpacks.Columns.Add("Name", 200, HorizontalAlignment.Left);
_lvModpacks.SelectedIndexChanged += (s, e) => RefreshModpackDetails();
_lvModpacks.DoubleClick += (s, e) => PlaySelectedModpack();
InitializeModpacksListView(_lvModpacks, isPackList: true);
_modpacksListOuter = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(1),
BackColor = palette.CardBorderColor,
Margin = new Padding(0)
};
_modpacksListInner = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(0),
BackColor = palette.CardBackground,
Margin = new Padding(0)
};
_modpacksListInner.Controls.Add(_lvModpacks);
_modpacksListOuter.Controls.Add(_modpacksListInner);
leftLayout.Controls.Add(headerBar, 0, 0);
leftLayout.Controls.Add(_modpacksListOuter, 0, 1);
leftPanel.Controls.Add(leftLayout);
_modpacksLeftOuter.Controls.Add(leftPanel);
_modpacksRightOuter = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(0),
BackColor = palette.SurfaceColor
};
var rightPanel = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(8, 8, 0, 8),
BackColor = palette.SurfaceColor
};
var rightLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 1,
RowCount = 2
};
rightLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
rightLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); rightLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
var titleBar = new Panel
{
Dock = DockStyle.Top,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Padding = new Padding(12, 12, 12, 10),
BackColor = palette.SurfaceAltColor,
Margin = new Padding(0, 0, 0, 8)
};
var titleRow = new TableLayoutPanel
{
Dock = DockStyle.Fill,
AutoSize = true,
ColumnCount = 2,
RowCount = 1
};
titleRow.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
titleRow.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
_lblModpackTitle = new Label
{
AutoSize = true,
Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold),
ForeColor = palette.HeadingTextColor,
Text = "Select a modpack",
Margin = new Padding(0, 4, 0, 0)
};
_btnModpackPlay = MakeActionButton("Play", success: true);
_btnModpackPlay.Click += (s, e) => PlaySelectedModpack();
titleRow.Controls.Add(_lblModpackTitle, 0, 0);
titleRow.Controls.Add(_btnModpackPlay, 1, 0);
_lblModpackModCount = new Label
{
AutoSize = true,
Font = new Font("Segoe UI", 9F),
ForeColor = palette.SecondaryTextColor,
Text = "",
Margin = new Padding(0, 2, 0, 0)
};
var titleBarLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
AutoSize = true,
ColumnCount = 1,
RowCount = 2
};
titleBarLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
titleBarLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
titleBarLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
titleBarLayout.Controls.Add(titleRow, 0, 0);
titleBarLayout.Controls.Add(_lblModpackModCount, 0, 1);
titleBar.Controls.Add(titleBarLayout);
_lvModpackMods = new ListView
{
Dock = DockStyle.Fill,
View = View.Details,
FullRowSelect = true,
HideSelection = false,
MultiSelect = true,
BorderStyle = BorderStyle.None,
GridLines = false
};
_lvModpackMods.Columns.Add("Mod Name", 280, HorizontalAlignment.Left);
InitializeModpacksListView(_lvModpackMods, isPackList: false);
_lvModpackMods.KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Delete && _lvModpackMods.SelectedItems.Count > 0)
{
RemoveSelectedModsFromPack();
}
};
var editor = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 3,
RowCount = 2,
Margin = new Padding(0)
};
_modpacksEditor = editor;
editor.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
editor.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 160));
editor.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
editor.RowStyles.Add(new RowStyle(SizeType.AutoSize));
editor.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
var lblInPack = new Label
{
AutoSize = true,
Text = "In modpack",
Font = new Font("Segoe UI", 9F, FontStyle.Bold),
ForeColor = palette.SecondaryTextColor,
Padding = new Padding(0, 0, 0, 6)
};
var lblInstalled = new Label
{
AutoSize = true,
Text = "Installed mods",
Font = new Font("Segoe UI", 9F, FontStyle.Bold),
ForeColor = palette.SecondaryTextColor,
Padding = new Padding(0, 0, 0, 6)
};
_lvInstalledMods = new ListView
{
Dock = DockStyle.Fill,
View = View.Details,
FullRowSelect = true,
HideSelection = false,
MultiSelect = true,
BorderStyle = BorderStyle.None,
GridLines = false
};
_lvInstalledMods.Columns.Add("Mod Name", 280, HorizontalAlignment.Left);
InitializeModpacksListView(_lvInstalledMods, isPackList: false);
_inPackListOuter = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(1),
BackColor = palette.CardBorderColor,
Margin = new Padding(0)
};
_inPackListInner = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(0),
BackColor = palette.CardBackgroundInstalled,
Margin = new Padding(0)
};
_inPackListInner.Controls.Add(_lvModpackMods);
_inPackListOuter.Controls.Add(_inPackListInner);
_installedListOuter = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(1),
BackColor = palette.CardBorderColor,
Margin = new Padding(0)
};
_installedListInner = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(0),
BackColor = palette.CardBackgroundInstalled,
Margin = new Padding(0)
};
_installedListInner.Controls.Add(_lvInstalledMods);
_installedListOuter.Controls.Add(_installedListInner);
AttachModpackContextMenus();
var buttonsMid = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 1,
RowCount = 3
};
buttonsMid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
buttonsMid.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
buttonsMid.RowStyles.Add(new RowStyle(SizeType.AutoSize));
buttonsMid.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
var buttonsMidInner = new FlowLayoutPanel
{
AutoSize = true,
FlowDirection = FlowDirection.TopDown,
WrapContents = false,
Anchor = AnchorStyles.None
};
_btnAddToModpack = MakeActionButton("← Add");
_btnAddToModpack.Margin = new Padding(0, 0, 0, 10);
_btnAddToModpack.Click += (s, e) => AddSelectedInstalledModsToModpack();
_btnRemoveFromModpack = MakeActionButton("Remove →");
_btnRemoveFromModpack.Click += (s, e) => RemoveSelectedModsFromPack();
buttonsMidInner.Controls.Add(_btnAddToModpack);
buttonsMidInner.Controls.Add(_btnRemoveFromModpack);
buttonsMid.Controls.Add(new Panel(), 0, 0);
buttonsMid.Controls.Add(buttonsMidInner, 0, 1);
buttonsMid.Controls.Add(new Panel(), 0, 2);
editor.Controls.Add(lblInPack, 0, 0);
editor.Controls.Add(new Label { AutoSize = true, Text = "", Padding = new Padding(0, 0, 0, 6) }, 1, 0);
editor.Controls.Add(lblInstalled, 2, 0);
editor.Controls.Add(_inPackListOuter, 0, 1);
editor.Controls.Add(buttonsMid, 1, 1);
editor.Controls.Add(_installedListOuter, 2, 1);
rightLayout.Controls.Add(titleBar, 0, 0);
rightLayout.Controls.Add(editor, 0, 1);
rightPanel.Controls.Add(rightLayout);
_modpacksRightOuter.Controls.Add(rightPanel);
split.Panel1.Controls.Add(_modpacksLeftOuter);
split.Panel2.Controls.Add(_modpacksRightOuter);
root.Controls.Add(split);
_tabModpacks.Controls.Add(root);
RefreshModpacksList();
RefreshModpackDetails();
}
private void InitializeModpacksListView(ListView lv, bool isPackList)
{
if (lv == null)
return;
lv.OwnerDraw = true;
lv.HeaderStyle = ColumnHeaderStyle.None;
lv.BorderStyle = BorderStyle.None;
lv.GridLines = false;
lv.FullRowSelect = true;
var rowHeight = isPackList ? 36 : 32;
var il = new ImageList();
il.ImageSize = new Size(1, rowHeight);
il.Images.Add(new Bitmap(1, rowHeight));
lv.SmallImageList = il;
if (lv.View == View.Details)
{
void ResizeColumns()
{
if (lv.IsDisposed || lv.Columns.Count < 1)
return;
var total = lv.ClientSize.Width;
if (total <= 0)
return;
lv.Columns[0].Width = Math.Max(80, total);
}
lv.SizeChanged += (s, e) => ResizeColumns();
ResizeColumns();
}
lv.DrawColumnHeader += (s, e) =>
{
e.DrawDefault = false;
};
lv.DrawItem += (s, e) =>
{
var palette = ThemeManager.Current;
var selectedBg = ThemeManager.CurrentVariant == ThemeVariant.Dark
? Color.FromArgb(45, 50, 60)
: Color.FromArgb(240, 242, 247);
var rowBg = isPackList ? palette.CardBackground : palette.CardBackgroundInstalled;
var bg = e.Item.Selected ? selectedBg : rowBg;
var fullRowBounds = new Rectangle(0, e.Bounds.Top, lv.ClientSize.Width, e.Bounds.Height);
using (var brush = new SolidBrush(bg))
{
e.Graphics.FillRectangle(brush, fullRowBounds);
}
var leftText = e.Item.Text ?? "";
var rightText = (e.Item.SubItems.Count > 1 ? e.Item.SubItems[1].Text : "") ?? "";
var leftBounds = new Rectangle(fullRowBounds.Left + 12, fullRowBounds.Top, fullRowBounds.Width - 24, fullRowBounds.Height);
var rightBounds = new Rectangle(fullRowBounds.Left + 12, fullRowBounds.Top, fullRowBounds.Width - 24, fullRowBounds.Height);
var leftColor = palette.PrimaryTextColor;
var rightColor = palette.SecondaryTextColor;
if (!isPackList && string.Equals(rightText, "Installed", StringComparison.OrdinalIgnoreCase))
{
rightColor = palette.MutedTextColor;
}
TextRenderer.DrawText(
e.Graphics,
leftText,
lv.Font,
leftBounds,
leftColor,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
if (!string.IsNullOrEmpty(rightText))
{
TextRenderer.DrawText(
e.Graphics,
rightText,
lv.Font,
rightBounds,
rightColor,
TextFormatFlags.Right | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
}
};
lv.DrawSubItem += (s, e) =>
{
e.DrawDefault = false;
};
}
private Button CreatePrimaryButton(string text)
{
var btn = new Button
{
Text = text,
AutoSize = true,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 8, 0),
Padding = new Padding(10, 4, 10, 4)
};
btn.FlatAppearance.BorderSize = 0;
return btn;
}
private Button CreateSecondaryButton(string text)
{
var btn = new Button
{
Text = text,
AutoSize = true,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 8, 0),
Padding = new Padding(10, 4, 10, 4)
};
btn.FlatAppearance.BorderSize = 0;
return btn;
}
private Button CreateDangerButton(string text)
{
var btn = new Button
{
Text = text,
AutoSize = true,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 8, 0),
Padding = new Padding(10, 4, 10, 4)
};
btn.FlatAppearance.BorderSize = 0;
return btn;
}
private void ConfigureModpacksSplitContainerSizing(SplitContainer split)
{
if (split == null)
return;
split.Panel1MinSize = 80;
split.Panel2MinSize = 80;
void Clamp()
{
if (split.IsDisposed)
return;
const int desiredLeftMin = 260;
const int desiredRightMin = 380;
int total = split.ClientSize.Width;
if (total <= 0)
return;
int leftMin = desiredLeftMin;
int rightMin = desiredRightMin;
if (leftMin + rightMin > total)
{
int relaxed = Math.Max(120, (total - split.SplitterWidth) / 3);
leftMin = Math.Min(desiredLeftMin, Math.Max(80, relaxed));
rightMin = Math.Min(desiredRightMin, Math.Max(80, relaxed));
if (leftMin + rightMin > total)
{
leftMin = Math.Max(60, (total - split.SplitterWidth) / 2);
rightMin = Math.Max(60, (total - split.SplitterWidth) / 2);
}
}
try
{
split.Panel1MinSize = leftMin;
split.Panel2MinSize = rightMin;
}
catch
{
return;
}
int min = split.Panel1MinSize;
int max = total - split.Panel2MinSize;
if (max < min)
return;
int desiredDistance = (int)(total * 0.35);
int clamped = Math.Max(min, Math.Min(max, desiredDistance));
try
{
split.SplitterDistance = clamped;
}
catch
{
}
}
split.HandleCreated += (s, e) =>
{
if (!split.IsDisposed)
{
split.BeginInvoke(new Action(Clamp));
}
};
split.SizeChanged += (s, e) => Clamp();
}
private void btnSidebarModpacks_Click(object sender, EventArgs e)
{
if (tabControl != null && tabControl.SelectedIndex != 1)
{
tabControl.SelectedIndex = 1;
}
}
private ModPack GetSelectedModpack()
{
if (_config?.Modpacks == null || _lvModpacks == null)
return null;
if (_lvModpacks.SelectedItems.Count == 0)
return null;
var selectedItem = _lvModpacks.SelectedItems[0];
var packId = selectedItem?.Tag as string;
if (string.IsNullOrWhiteSpace(packId))
return null;
return _config.Modpacks.FirstOrDefault(p => string.Equals(p.Id, packId, StringComparison.OrdinalIgnoreCase));
}
private void RefreshModpacksList()
{
if (_lvModpacks == null || _config?.Modpacks == null)
return;
var packs = _config.Modpacks
.Where(p => p != null && !string.IsNullOrWhiteSpace(p.Id))
.OrderBy(p => p.Name ?? "", StringComparer.OrdinalIgnoreCase)
.ToList();
var previouslySelectedId = (GetSelectedModpack()?.Id ?? "").Trim();
_lvModpacks.BeginUpdate();
try
{
_lvModpacks.Items.Clear();
foreach (var p in packs)
{
var count = p.ModIds?.Distinct(StringComparer.OrdinalIgnoreCase).Count() ?? 0;
var item = new ListViewItem(p.Name ?? "Unnamed");
item.Tag = p.Id;
item.ImageIndex = 0;
item.SubItems.Add($"{count} mods");
_lvModpacks.Items.Add(item);
}
}
finally
{
_lvModpacks.EndUpdate();
}
if (!string.IsNullOrEmpty(previouslySelectedId))
{
foreach (ListViewItem item in _lvModpacks.Items)
{
if (item.Tag is string id && string.Equals(id, previouslySelectedId, StringComparison.OrdinalIgnoreCase))
{
item.Selected = true;
item.EnsureVisible();
break;
}
}