1+ using System ;
2+ using System . Drawing ;
3+ using System . Windows . Forms ;
4+ using Serilog ;
5+
6+ namespace ModShardLauncher . Core . UI
7+ {
8+ public class ErrorMessageDialog : Form
9+ {
10+ private readonly RichTextBox messageTextBox ;
11+ public DialogResult Result { get ; private set ; }
12+ private readonly TableLayoutPanel panel ;
13+ private readonly Button okButton ;
14+ private readonly Button cpyButton ;
15+ private readonly Button logButton ;
16+ public ErrorMessageDialog ( string title , string message , string ? logPath = null )
17+ {
18+ panel = new ( ) ;
19+ messageTextBox = new RichTextBox ( ) ;
20+ okButton = new Button ( ) ;
21+ cpyButton = new Button ( ) ;
22+ logButton = new Button ( ) ;
23+
24+ InitializeComponent ( ) ;
25+ SetupDialog ( title , message , logPath ) ;
26+ }
27+ private void InitializeComponent ( )
28+ {
29+ SuspendLayout ( ) ;
30+
31+ Text = string . Empty ;
32+ Size = new Size ( 450 , 200 ) ;
33+ StartPosition = FormStartPosition . CenterParent ;
34+ FormBorderStyle = FormBorderStyle . FixedDialog ;
35+ MaximizeBox = false ;
36+ MinimizeBox = false ;
37+ ShowIcon = false ;
38+ ShowInTaskbar = false ;
39+ Dock = DockStyle . Fill ;
40+
41+ // panel
42+ panel . Anchor = AnchorStyles . Top ;
43+ panel . Size = new Size ( 300 , 150 ) ;
44+ panel . BorderStyle = BorderStyle . None ;
45+ panel . ColumnCount = 3 ;
46+ panel . RowCount = 2 ;
47+ panel . RowStyles . Add ( new RowStyle ( SizeType . AutoSize ) ) ;
48+ panel . RowStyles . Add ( new RowStyle ( SizeType . Absolute , 44 ) ) ;
49+ panel . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 100 ) ) ;
50+ panel . ColumnStyles . Add ( new ColumnStyle ( SizeType . AutoSize ) ) ;
51+ panel . ColumnStyles . Add ( new ColumnStyle ( SizeType . AutoSize ) ) ;
52+ panel . Dock = DockStyle . Fill ;
53+
54+ // Message TextBox
55+ messageTextBox . ReadOnly = true ;
56+ messageTextBox . BorderStyle = BorderStyle . None ;
57+ messageTextBox . BackColor = BackColor ;
58+ messageTextBox . ScrollBars = RichTextBoxScrollBars . Vertical ;
59+ messageTextBox . TabStop = false ;
60+
61+ // ok button
62+ okButton . Text = "OK" ;
63+ okButton . Size = new Size ( 75 , 23 ) ;
64+ okButton . DialogResult = DialogResult . OK ;
65+ okButton . Click += ( s , e ) => { Result = DialogResult . OK ; Close ( ) ; } ;
66+
67+ // copy button
68+ cpyButton . Text = "Copy error" ;
69+ cpyButton . Size = new Size ( 100 , 23 ) ;
70+ cpyButton . Click += CopyButton_Click ;
71+
72+ // log button
73+ logButton . Text = "Open Log Folder" ;
74+ logButton . Size = new Size ( 120 , 23 ) ;
75+ logButton . Click += LogButton_Click ;
76+
77+ panel . Controls . Add ( messageTextBox , 0 , 0 ) ;
78+ panel . SetColumnSpan ( messageTextBox , 3 ) ;
79+ panel . Controls . Add ( okButton , 0 , 1 ) ;
80+ panel . Controls . Add ( cpyButton , 1 , 1 ) ;
81+ panel . Controls . Add ( logButton , 2 , 1 ) ;
82+
83+ Controls . Add ( panel ) ;
84+
85+ // Set default button and cancel button
86+ AcceptButton = okButton ;
87+ CancelButton = okButton ;
88+
89+ ResumeLayout ( ) ;
90+ }
91+ private void SetupDialog ( string title , string message , string ? logPath = null )
92+ {
93+ Text = title ;
94+ messageTextBox . Rtf = message ;
95+ messageTextBox . AutoSize = true ;
96+
97+ Size size = messageTextBox . GetPreferredSize ( new Size ( 800 , 0 ) ) + new Size ( 0 , 50 ) ;
98+ int newHeight = size . Height + 100 ;
99+ int newWidth = Math . Max ( 450 , size . Width + 50 ) ;
100+
101+ messageTextBox . Size = new Size ( size . Width , size . Height ) ;
102+ Size = new Size ( newWidth , newHeight ) ;
103+
104+ // Hide log button if no path provided
105+ if ( string . IsNullOrEmpty ( logPath ) )
106+ {
107+ logButton . Visible = false ;
108+ okButton . Location = new Point ( 340 , 125 ) ; // Center the OK button
109+ }
110+ else
111+ {
112+ logButton . Tag = logPath ; // Store the log path
113+ }
114+ }
115+ private void LogButton_Click ( object ? sender , EventArgs e )
116+ {
117+ try
118+ {
119+ string ? logPath = logButton . Tag ? . ToString ( ) ;
120+ if ( ! string . IsNullOrEmpty ( logPath ) )
121+ {
122+ System . Diagnostics . Process . Start ( "explorer.exe" , logPath ) ;
123+ }
124+ }
125+ catch ( Exception ex )
126+ {
127+ MessageBox . Show ( $ "Could not open log folder: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
128+ }
129+
130+ // nifty trick: use Retry to indicate log button was clicked
131+ Result = DialogResult . Retry ;
132+ Close ( ) ;
133+ }
134+ private void CopyButton_Click ( object ? sender , EventArgs e )
135+ {
136+ Clipboard . SetText ( messageTextBox . Text ) ;
137+ }
138+ public static DialogResult Show ( string title , string message , string ? logPath = null )
139+ {
140+ using var dialog = new ErrorMessageDialog ( title , message , logPath ) ;
141+ dialog . ShowDialog ( ) ;
142+ return dialog . Result ;
143+ }
144+ }
145+ }
0 commit comments