compactrs 2025.12.25

High-performance native Windows file compressor using WOF (Windows Overlay Filter)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
#![allow(unsafe_op_in_unsafe_fn)]
use crate::ui::state::AppTheme;
use crate::ui::builder::ControlBuilder;
use crate::utils::to_wstring;
use crate::w;
use crate::ui::framework::WindowHandler;
use crate::types::*;
use crate::ui::wrappers::{Button, Label, Trackbar, ComboBox};

const SETTINGS_TITLE: &str = "Settings";

// Control IDs
const IDC_COMBO_THEME: u16 = 2001;
// const IDC_RADIO_SYSTEM: u16 = 2002; // Removed
// const IDC_RADIO_DARK: u16 = 2003;   // Removed
// const IDC_RADIO_LIGHT: u16 = 2004;  // Removed

const IDC_BTN_CANCEL: u16 = 2006;
const IDC_CHK_FORCE_STOP: u16 = 2007;
const IDC_CHK_CONTEXT_MENU: u16 = 2008;
const IDC_CHK_SYSTEM_GUARD: u16 = 2009;
const IDC_CHK_LOW_POWER: u16 = 2013;
const IDC_SLIDER_THREADS: u16 = 2014;
const IDC_LBL_THREADS_VALUE: u16 = 2015;

const IDC_CHK_LOG_ENABLED: u16 = 2021;
const IDC_CHK_LOG_ERRORS: u16 = 2022;
const IDC_CHK_LOG_WARNS: u16 = 2023;
const IDC_CHK_LOG_INFO: u16 = 2024;
const IDC_CHK_LOG_TRACE: u16 = 2025;

const IDC_EDIT_CONCURRENT: u16 = 2031;

const IDC_CHK_SKIP_EXT: u16 = 2041;
const IDC_EDIT_EXTENSIONS: u16 = 2042;
const IDC_BTN_RESET_EXT: u16 = 2043;
const IDC_CHK_SET_ATTR: u16 = 2044;


// Labels for Titles/Subtitles (Dynamic IDs or just static 0xffff if no interaction needed? 
// We generally don't need to interact with labels unless updating text. 
// We'll let ControlBuilder auto-assign or use a base range if needed. 
// For now, let's keep strict constants usually, but for many labels we can just use -1 (static) if wrappers allow, 
// or simple distinct IDs. Let's assume ControlBuilder can handle simple unique IDs if we increment.)

struct SettingsState {
    theme: AppTheme,
    result: Option<AppTheme>,
    is_dark: bool,
    enable_force_stop: bool,
    enable_context_menu: bool,
    enable_system_guard: bool,
    low_power_mode: bool,
    max_threads: u32,
    max_concurrent_items: u32,
    log_enabled: bool,
    log_level_mask: u8,
    enable_skip_heuristics: bool,
    skip_extensions: String,
    set_compressed_attr: bool,

    update_status: UpdateStatus,
    h_font_bold: HFONT, // Store bold font handle
}

#[derive(Clone, Debug, PartialEq)]
enum UpdateStatus {
    Idle,
    Checking,
    Available(String, String), // Version, URL
    UpToDate,
    Error(String),
}

const WM_APP_UPDATE_CHECK_RESULT: u32 = 0x8000 + 10;
const IDC_BTN_CHECK_UPDATE: u16 = 2010;
const IDC_LBL_UPDATE_STATUS: u16 = 2011;
const IDC_BTN_RESTART_TI: u16 = 2012;
const IDC_BTN_RESET_ALL: u16 = 2016;


// Main settings modal function with proper data passing
pub unsafe fn show_settings_modal(
    parent: HWND, 
    current_theme: AppTheme, 
    is_dark: bool, 
    enable_force_stop: bool, 
    enable_context_menu: bool, 
    enable_system_guard: bool, 
    low_power_mode: bool, 
    max_threads: u32, 
    max_concurrent_items: u32, 
    log_enabled: bool, 
    log_level_mask: u8,
    enable_skip_heuristics: bool,
    skip_extensions_buf: [u16; 512],
    set_compressed_attr: bool
) -> (Option<AppTheme>, bool, bool, bool, bool, u32, u32, bool, u8, bool, [u16; 512], bool) {
    // Convert buf to String for state
    let skip_string = String::from_utf16_lossy(&skip_extensions_buf)
        .trim_matches(char::from(0))
        .to_string();

    // Use centralized helper
    let mut state = SettingsState {
        theme: current_theme,
        result: None,
        is_dark,
        enable_force_stop,
        enable_context_menu,
        enable_system_guard,
        low_power_mode,
        max_threads,
        max_concurrent_items,
        log_enabled,
        log_level_mask,
        enable_skip_heuristics,
        skip_extensions: skip_string,
        set_compressed_attr,
        update_status: UpdateStatus::Idle,
        h_font_bold: std::ptr::null_mut(),
    };
    
    let ran_modal = crate::ui::dialogs::base::show_modal_singleton(
        parent, 
        &mut state, 
        "CompactRS_Settings", 
        SETTINGS_TITLE, 
        550, // Increased width slightly 
        680, // Target height < 700 for 768p screens
        is_dark
    );
    
    let mut final_buf = [0u16; 512];
    if ran_modal {
        let mut i = 0;
        for c in state.skip_extensions.encode_utf16() {
            if i < 511 {
                final_buf[i] = c;
                i += 1;
            }
        }
        (state.result, state.enable_force_stop, state.enable_context_menu, state.enable_system_guard, state.low_power_mode, state.max_threads, state.max_concurrent_items, state.log_enabled, state.log_level_mask, state.enable_skip_heuristics, final_buf, state.set_compressed_attr)
    } else {
         // Return original values if cancelled/prevented
         (None, enable_force_stop, enable_context_menu, enable_system_guard, low_power_mode, max_threads, max_concurrent_items, log_enabled, log_level_mask, enable_skip_heuristics, skip_extensions_buf, set_compressed_attr)
    }
}

impl WindowHandler for SettingsState {
    fn is_dark_mode(&self) -> bool {
        self.is_dark
    }

    fn on_create(&mut self, hwnd: HWND) -> LRESULT {
        unsafe {
            // Apply DWM title bar color
            crate::ui::theme::set_window_frame_theme(hwnd, self.is_dark);

            // Create Bold Font
            // Get current message box font from a standard control or SystemParameters (simulation)
            // Simpler: use the default GUI font, get its LOGFONT, make it bold
            let h_default = GetStockObject(DEFAULT_GUI_FONT);
            let mut lf: LOGFONTW = std::mem::zeroed();
            GetObjectW(h_default, std::mem::size_of::<LOGFONTW>() as i32, &mut lf as *mut _ as *mut _);
            lf.lfWeight = FW_BOLD as i32;
            
            // Better approach for Fluent UI: Use Segoe UI explicitly if possible, or just trust the system font
            // We'll trust the modified default font for now.
            self.h_font_bold = CreateFontIndirectW(&lf);
            
            // Create Segoe MDL2 Assets font for flat icons
            let mut lf_icon: LOGFONTW = std::mem::zeroed();
            lf_icon.lfHeight = -16; // 16pt icon size
            lf_icon.lfWeight = 400; // Normal weight
            lf_icon.lfCharSet = 1; // DEFAULT_CHARSET
            let mdl2_name = "Segoe MDL2 Assets";
            for (i, c) in mdl2_name.encode_utf16().enumerate() {
                if i < 32 { lf_icon.lfFaceName[i] = c; }
            }
            let h_font_icon = CreateFontIndirectW(&lf_icon);

            let is_dark_mode = self.is_dark;
            
            // --- Layout state ---
            let mut current_y = 10;
            let row_height_base = 36;
            let client_width = 580;
            let right_padding = 20; // "Rata tengah beneran" -> aligned nicely with standard padding
            
            // --- Helper to create a Section Header (no icon, just bold text) ---
            let create_section_header = |y: i32, text: &str| -> i32 {
                let _h = ControlBuilder::new(hwnd, 0)
                    .label(false)
                    .text(text)
                    .pos(20, y)
                    .size(500, 25)
                    .dark_mode(is_dark_mode)
                    .font(self.h_font_bold)
                    .build();
                y + 25
            };
            
            // --- Helper to create a Row with MDL2 Icon ---
            let create_row = |y: i32, icon_glyph: &str, title: &[u16], subtitle: &[u16], _control_id: u16, ctl_fn: &dyn Fn(i32, i32) -> HWND| -> i32 {
                // Icon (using MDL2 Assets font)
                let _h_icon = ControlBuilder::new(hwnd, 0)
                    .label(false)
                    .text(icon_glyph)
                    .pos(30, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .font(h_font_icon)
                    .build();
                
                // Title (offset for icon)
                let _h_title = ControlBuilder::new(hwnd, 0)
                    .label(false)
                    .text_w(title)
                    .pos(55, y)
                    .size(280, 18) // Reduced height
                    .dark_mode(is_dark_mode)
                    .font(self.h_font_bold)
                    .build();
                
                // Subtitle (Moved up to y+15 to tighten gap and fit in 36px row)
                let _h_sub = ControlBuilder::new(hwnd, 0)
                    .label(false)
                    .text_w(subtitle)
                    .pos(55, y + 15) 
                    .size(330, 18) // Reduced height
                    .dark_mode(is_dark_mode)
                    .build();
                
                // Control (Right Aligned)
                ctl_fn(420, y);
                
                y + row_height_base
            };

            // --- Layout Content --- //

            // 1. Appearance Section
            current_y = create_section_header(current_y, "Appearance");
            
            // App Theme - MDL2 glyph E713 = Settings
            current_y = create_row(current_y, "\u{E713}", crate::w!("Application Theme"), crate::w!("Choose between Light, Dark, or System Default"), IDC_COMBO_THEME, &|x, y| {
                let h_combo = ControlBuilder::new(hwnd, IDC_COMBO_THEME)
                    .combobox()
                    .pos(x, y + 5) // Center vertically roughly
                    .size(140, 100)
                    .dark_mode(is_dark_mode)
                    .build();
                
                let cb = ComboBox::new(h_combo);
                cb.add_string("System Default");
                cb.add_string("Dark Mode");
                cb.add_string("Light Mode");
                
                let sel = match self.theme {
                    AppTheme::System => 0,
                    AppTheme::Dark => 1,
                    AppTheme::Light => 2,
                };
                cb.set_selected_index(sel);
                h_combo
            });
            
            // Compressed Attribute - MDL2 glyph E8C9 = Tag? or E7B3 = GroupList?
            // E8FB = Accept
            // E73E = CheckList
            // E8C9 = Tag. 
            // "Show Compressed Color" - "Mark folders/files with Compressed attribute (Blue Text/Icon)"
            current_y = create_row(current_y, "\u{F012}", crate::w!("Show Compressed Color"), crate::w!("Mark compressed items with blue system attribute"), IDC_CHK_SET_ATTR, &|x, y| {
                ControlBuilder::new(hwnd, IDC_CHK_SET_ATTR)
                    .checkbox()
                    .text("")
                    .pos(x + 120, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.set_compressed_attr)
                    .build()
            });

            // current_y += 5; // Extra spacing removed

            // 2. Behavior Section
            current_y = create_section_header(current_y, "General Behavior");

            // Force Stop - MDL2 glyph E74D = Stop
            current_y = create_row(current_y, "\u{E74D}", crate::w!("Force Kill Processes"), crate::w!("Automatically terminate locking processes"), IDC_CHK_FORCE_STOP, &|x, y| {
                 ControlBuilder::new(hwnd, IDC_CHK_FORCE_STOP)
                    .checkbox()
                    .text("") // No text, just the box
                    .pos(x + 120, y + 5) // Rightmost
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.enable_force_stop)
                    .build()
            });

            // Context Menu - MDL2 glyph E8DE = More
            current_y = create_row(current_y, "\u{E8DE}", crate::w!("Explorer Context Menu"), crate::w!("Add 'CompactRS' to right-click menu"), IDC_CHK_CONTEXT_MENU, &|x, y| {
                ControlBuilder::new(hwnd, IDC_CHK_CONTEXT_MENU)
                    .checkbox()
                    .text("")
                    .pos(x + 120, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.enable_context_menu)
                    .build()
            });
            
            // System Guard - MDL2 glyph EA18 = Shield
            current_y = create_row(current_y, "\u{EA18}", crate::w!("System Safety Guard"), crate::w!("Prevent compression of critical system files"), IDC_CHK_SYSTEM_GUARD, &|x, y| {
                ControlBuilder::new(hwnd, IDC_CHK_SYSTEM_GUARD)
                    .checkbox()
                    .text("")
                    .pos(x + 120, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.enable_system_guard)
                    .build()
            });
            
            // Low Power - MDL2 glyph EC48 = Battery Saver
            current_y = create_row(current_y, "\u{EC48}", crate::w!("Efficiency Mode"), crate::w!("Reduce background resource usage (Low Power)"), IDC_CHK_LOW_POWER, &|x, y| {
                ControlBuilder::new(hwnd, IDC_CHK_LOW_POWER)
                    .checkbox()
                    .text("")
                    .pos(x + 120, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.low_power_mode)
                    .build()
            });

 

            // 3. Performance Section
            current_y = create_section_header(current_y, "Performance");
            
            // Threads
            let cpu_count = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1) as u32;
            let current_threads = if self.max_threads == 0 { cpu_count } else { self.max_threads };
            
            // CPU Threads - MDL2 glyph E9D9 = Processing
            let subtitle = crate::utils::concat_wstrings(&[crate::w!("Maximum worker threads (Current: "), &crate::utils::fmt_u32(current_threads), crate::w!(")")]);
            current_y = create_row(current_y, "\u{E9D9}", crate::w!("CPU Thread Limit"), &subtitle, IDC_SLIDER_THREADS, &|x, y| {
                 // Label for value updates
                 let _lbl = ControlBuilder::new(hwnd, IDC_LBL_THREADS_VALUE)
                    .label(false)
                    .text("") // Handled by subtitle mostly, but we can put value here or just on slider tooltip? 
                              // Current subtitle has value. We'll update logic to update subtitle text if possible, or aux label.
                    .pos(0, 0).size(0,0).build(); // Invisible anchor if needed, or just update the one we passed?
                                                  // Simpler: Just put slider. We update subtitle logic later or repaint?
                    
                 let h_slider = ControlBuilder::new(hwnd, IDC_SLIDER_THREADS)
                    .trackbar()
                    .pos(x, y)
                    .size(140, 30)
                    .dark_mode(is_dark_mode)
                    .build();
                 Trackbar::new(h_slider).set_range(1, cpu_count);
                 Trackbar::new(h_slider).set_pos(current_threads);
                 h_slider
            });

            // Queue - MDL2 glyph E902 = List
            current_y = create_row(current_y, "\u{E902}", crate::w!("Concurrent File Queue"), crate::w!("Files compressed simultaneously (0 = Unlimited)"), IDC_EDIT_CONCURRENT, &|x, y| {
                 ControlBuilder::new(hwnd, IDC_EDIT_CONCURRENT)
                    .edit()
                    .text(&self.max_concurrent_items.to_string())
                    .pos(x + 90, y + 5)
                    .size(50, 20)
                    .style(ES_NUMBER | ES_CENTER)
                    .dark_mode(is_dark_mode)
                    .build()
            });
            

            
            // 4. Filtering Section
            current_y = create_section_header(current_y, "File Filtering");
            
            // Skip Heuristics - MDL2 glyph E71C = Filter
            current_y = create_row(current_y, "\u{E71C}", crate::w!("Smart Compression Skip"), crate::w!("Skip files that are unlikely to compress further"), IDC_CHK_SKIP_EXT, &|x, y| {
                ControlBuilder::new(hwnd, IDC_CHK_SKIP_EXT)
                    .checkbox()
                    .text("")
                    .pos(x + 120, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.enable_skip_heuristics)
                    .build()
            });
            
            // Extensions (Multi-line area below)
            let _h_lbl_ext = ControlBuilder::new(hwnd, 0)
                .label(false)
                .text("Excluded Extensions (comma separated):")
                .pos(30, current_y)
                .size(300, 20)
                .dark_mode(is_dark_mode)
                .font(self.h_font_bold) // Make this bold too? Or just subtitle style? Bold looks like header.
                // Let's keep it regular or use helper.
                .build();
            current_y += 25;
            
            let h_edit_ext = ControlBuilder::new(hwnd, IDC_EDIT_EXTENSIONS)
                .edit()
                .text(&self.skip_extensions)
                .pos(30, current_y)
                .size(480, 50)
                .style(ES_AUTOVSCROLL | ES_MULTILINE) 
                .dark_mode(is_dark_mode)
                .build();
                
            let h_btn_reset = ControlBuilder::new(hwnd, IDC_BTN_RESET_EXT)
                .button()
                .text_w(w!("Reset Defaults"))
                .pos(client_width - 120 - right_padding, current_y + 55) // below edit
                .size(120, 25)
                .dark_mode(is_dark_mode)
                .build();
            
            if !self.enable_skip_heuristics {
                Button::new(h_edit_ext).set_enabled(false);
                Button::new(h_btn_reset).set_enabled(false);
            }
            current_y += 90;

            // 5. Diagnostics
             current_y = create_section_header(current_y, "Diagnostics");
             
             // Logging Master - MDL2 glyph E9D9 = Bug
             current_y = create_row(current_y, "\u{EBE8}", crate::w!("Enable Diagnostic Logging"), crate::w!("Show real-time logs in a console window"), IDC_CHK_LOG_ENABLED, &|x, y| {
                 ControlBuilder::new(hwnd, IDC_CHK_LOG_ENABLED)
                    .checkbox()
                    .text("")
                    .pos(x + 120, y + 5)
                    .size(20, 20)
                    .dark_mode(is_dark_mode)
                    .checked(self.log_enabled)
                    .build()
             });
             
             // Log Levels (Horizontal or indented?)
             // Let's do a horizontal row of small checkboxes for levels if enabled
             let level_y = current_y; 
             let _lbl_lv = ControlBuilder::new(hwnd, 0).label(false).text("Levels:").pos(30, level_y).size(50, 20).dark_mode(is_dark_mode).build();
             
             let mk_chk = |id, txt, x, width, checked| {
                 ControlBuilder::new(hwnd, id)
                    .checkbox()
                    .text(txt)
                    .pos(x, level_y)
                    .size(width, 20)
                    .dark_mode(is_dark_mode)
                    .checked(checked)
                    .build()
             };
             
             // Adjusted widths and positions to prevent overlap (hitbox fix)
             mk_chk(IDC_CHK_LOG_ERRORS, "Errors", 80, 70, self.log_level_mask & crate::logger::LOG_LEVEL_ERROR != 0);
             mk_chk(IDC_CHK_LOG_WARNS, "Warnings", 160, 80, self.log_level_mask & crate::logger::LOG_LEVEL_WARN != 0);
             mk_chk(IDC_CHK_LOG_INFO, "Info", 250, 60, self.log_level_mask & crate::logger::LOG_LEVEL_INFO != 0);
             mk_chk(IDC_CHK_LOG_TRACE, "Trace", 320, 70, self.log_level_mask & crate::logger::LOG_LEVEL_TRACE != 0);
             
             current_y += 25;

            // Disable child checkboxes if logging is not enabled
            if !self.log_enabled {
                let ids = [IDC_CHK_LOG_ERRORS, IDC_CHK_LOG_WARNS, IDC_CHK_LOG_INFO, IDC_CHK_LOG_TRACE];
                for &id in &ids {
                    let h = GetDlgItem(hwnd, id as i32);
                    if h != std::ptr::null_mut() {
                        Button::new(h).set_enabled(false);
                    }
                }
            }

            // 6. About & Updates
            current_y = create_section_header(current_y, "About & Updates");

            // Reset Application Defaults (Requested Feature)
            let btn_reset_w = 180;
            ControlBuilder::new(hwnd, IDC_BTN_RESET_ALL)
                .button()
                .text_w(w!("Reset Application Defaults"))
                .pos(client_width - btn_reset_w - right_padding, current_y - 25) // Dynamic Right Align
                .size(btn_reset_w, 25)
                .dark_mode(is_dark_mode)
                .build();
            
            // MANUAL ROW for Updates (to bind IDC_LBL_UPDATE_STATUS to subtitle)
            // Title
            ControlBuilder::new(hwnd, 0)
                .label(false)
                .text("Application Version")
                .pos(30, current_y)
                .size(300, 20)
                .dark_mode(is_dark_mode)
                .font(self.h_font_bold)
                .build();
            
            // Subtitle (Dynamic Status)
            let version_str = crate::utils::concat_wstrings(&[
                &crate::utils::to_wstring(env!("APP_VERSION")),
                crate::w!(" - Check for updates")
            ]);
            ControlBuilder::new(hwnd, IDC_LBL_UPDATE_STATUS)
                .label(false)
                .text_w(&version_str)
                .pos(30, current_y + 20)
                .size(350, 20)
                .dark_mode(is_dark_mode)
                .build();
            
            // Check Update Button
            let btn_update_w = 140;
            ControlBuilder::new(hwnd, IDC_BTN_CHECK_UPDATE)
                .button()
                .text_w(w!("Check for Updates"))
                .pos(client_width - btn_update_w - right_padding, current_y) // Dynamic Right Align
                .size(btn_update_w, 30)
                .dark_mode(is_dark_mode)
                .build();
            
            current_y += 50;

            // Restart TI Row - MDL2 glyph E7EF = Admin
            current_y = create_row(current_y, "\u{E7EF}", crate::w!("Advanced Startup"), crate::w!("Restart with TrustedInstaller privileges"), IDC_BTN_RESTART_TI, &|_x_pos, y_pos| {
                 // Adjusted x_pos from helper is 350. Let's use 400 manually or stick to helper?
                 // Helper passes 350. 
                 ControlBuilder::new(hwnd, IDC_BTN_RESTART_TI)
                    .button()
                    .text_w(w!("Restart as TI"))
                    .pos(client_width - 140 - right_padding, y_pos) // Dynamic Right Align
                    .size(140, 30) // Match width
                    .dark_mode(is_dark_mode)
                    .build()
            });

            // Close Button Removed as per user request (Save space)
            
            // Resize Window to fit content
            // current_y is at the bottom of the last row (Restart TI)
            // Add small padding
            // Resize Window to fit content DYNAMICALLY
            // current_y is the bottom of the client content.
            // We need to calculate the actual window size required to hold this client area.
            
            let client_height = current_y + 20; // +Padding
            // let client_width = 580; // Already defined above
            
            let mut rect = RECT {
                left: 0,
                top: 0,
                right: client_width,
                bottom: client_height,
            };
            
            // Get current style to calculate correct borders
            let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
            AdjustWindowRect(&mut rect, style, 0); // 0 = false (no menu)
            
            let total_width = rect.right - rect.left;
            let total_height = rect.bottom - rect.top;

            SetWindowPos(hwnd, std::ptr::null_mut(), 0, 0, total_width, total_height, SWP_NOMOVE | SWP_NOZORDER);

            // Apply recursively to catch any stragglers
            crate::ui::theme::apply_theme_recursive(hwnd, self.is_dark);
        }
        0
    }

    fn on_message(&mut self, hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> Option<LRESULT> {
        unsafe {
            match msg {
                WM_DESTROY => {
                    if self.h_font_bold != std::ptr::null_mut() {
                        DeleteObject(self.h_font_bold);
                        self.h_font_bold = std::ptr::null_mut();
                    }
                    Some(0)
                },
                WM_HSCROLL => {
                     // Check if it's our slider
                     let h_ctl = lparam as HWND;
                     let h_slider = GetDlgItem(hwnd, IDC_SLIDER_THREADS as i32);
                     if h_ctl == h_slider {
                         // Get Position
                         let pos = Trackbar::new(h_slider).get_pos();
                         self.max_threads = pos;
                         
                         // Update Subtitle/Label? Use a dedicated label for now if it exists, or update the main subtitle if we stored it?
                         // We didn't store the subtitle handle. 
                         // But we have `IDC_LBL_THREADS_VALUE` (2015) in the code.
                         // Let's use that if we can.
                         // let _label_text = format!("(Current: {})", pos); // Simplified - REMOVED
                         // Wait, in on_create I didn't assign ID 2015 to the Subtitle, I assigned it to a hidden label?
                         // I should probably find the control.
                         // Actually, let's just ignore the real-time subtitle update for now unless we need it perfect. 
                         // Or, find IDC_LBL_THREADS_VALUE:
                         let _h_lbl = GetDlgItem(hwnd, IDC_LBL_THREADS_VALUE as i32);
                         // If I commented it out in on_create, this won't work.
                         // Let's rely on standard trackbar tooltip if available, 
                         // or user just sees it when they open it.
                         // For now, let's keep the backend logic.
                     }
                     Some(0)
                },
                WM_APP_UPDATE_CHECK_RESULT => {
                    let status_ptr = lparam as *mut UpdateStatus;
                    let status = Box::from_raw(status_ptr); // Take ownership
                    self.update_status = *status;
                    
                    // Update UI based on status
                    let h_btn = GetDlgItem(hwnd, IDC_BTN_CHECK_UPDATE as i32);
                    let h_lbl = GetDlgItem(hwnd, IDC_LBL_UPDATE_STATUS as i32);
                    
                    match &self.update_status {
                        UpdateStatus::Available(ver, _) => {
                             let txt = "Download and Restart";
                             Button::new(h_btn).set_text(txt);
                             
                             let status_txt = crate::utils::concat_wstrings(&[
                                 crate::w!("New version "),
                                 &crate::utils::to_wstring(ver),
                                 crate::w!(" available!")
                             ]);
                             Label::new(h_lbl).set_text_w(&status_txt);
                             
                             // Re-enable button so user can click it
                             Button::new(h_btn).set_enabled(true);
                        },
                        UpdateStatus::UpToDate => {
                             let txt = "Check for Updates";
                             Button::new(h_btn).set_text(txt);
                             
                             let status_txt = "You are up to date.";
                             Label::new(h_lbl).set_text(status_txt);
                             
                             // Re-enable button
                             Button::new(h_btn).set_enabled(true);
                        },
                        UpdateStatus::Error(e) => {
                             let txt = "Check for Updates";
                             Button::new(h_btn).set_text(txt);
                             
                             let status_txt = crate::utils::concat_wstrings(&[
                                 crate::w!("Error: "),
                                 &crate::utils::to_wstring(&e.to_string())
                             ]);
                             Label::new(h_lbl).set_text_w(&status_txt);
                             
                             Button::new(h_btn).set_enabled(true);
                        },
                        _ => {}
                    }
                    Some(0)
                },
                
                WM_COMMAND => {
                     let id = (wparam & 0xFFFF) as u16;
                     let code = ((wparam >> 16) & 0xFFFF) as u16;
                     
                     match id {
                         IDC_COMBO_THEME => {
                            if (code as u32) == CBN_SELCHANGE {
                                let h_combo = GetDlgItem(hwnd, IDC_COMBO_THEME as i32);
                                let idx = ComboBox::new(h_combo).get_selected_index();
                                let theme = match idx {
                                    0 => AppTheme::System,
                                    1 => AppTheme::Dark,
                                    2 => AppTheme::Light,
                                    _ => AppTheme::System,
                                };
                                
                                // Determine if new theme is dark
                                let new_is_dark = match theme {
                                    AppTheme::Dark => true,
                                    AppTheme::Light => false,
                                    AppTheme::System => {
                                        crate::ui::theme::is_system_dark_mode()
                                    }
                                };
                                
                                // Update local state including is_dark
                                self.theme = theme;
                                self.result = Some(theme);
                                self.is_dark = new_is_dark;
                                
                                // Update Settings window title bar
                                crate::ui::theme::set_window_frame_theme(hwnd, new_is_dark);
                                
                                // Update controls theme
                                crate::ui::theme::apply_theme_recursive(hwnd, new_is_dark);
                                
                                // Repaint
                                InvalidateRect(hwnd, std::ptr::null(), 1);
                                
                                // Notify Parent Immediately
                                use GetParent;
                                let parent = GetParent(hwnd);
                                if parent != std::ptr::null_mut() {
                                    let theme_val = match theme {
                                        AppTheme::System => 0,
                                        AppTheme::Dark => 1,
                                        AppTheme::Light => 2,
                                    };
                                    SendMessageW(parent, 0x8000 + 1, theme_val as WPARAM, 0);
                                }
                                
                                // Broadcast to About/Console (Same as before)
                                let compactrs_about = crate::utils::to_wstring("CompactRS_About");
                                let about_hwnd = FindWindowW(compactrs_about.as_ptr(), std::ptr::null());
                                if about_hwnd != std::ptr::null_mut() {
                                    let is_dark_val = if new_is_dark { 1 } else { 0 };
                                    SendMessageW(about_hwnd, 0x8000 + 2, is_dark_val as WPARAM, 0);
                                }
                                
                                let compactrs_console = crate::w!("CompactRS_Console");
                                let console_hwnd = FindWindowW(compactrs_console.as_ptr(), std::ptr::null());
                                if console_hwnd != std::ptr::null_mut() {
                                    let is_dark_val = if new_is_dark { 1 } else { 0 };
                                    SendMessageW(console_hwnd, 0x8000 + 2, is_dark_val as WPARAM, 0);
                                }
                            }
                         },
                         IDC_BTN_CANCEL => {
                             // Read concurrent items from edit box before closing
                             let h_edit = GetDlgItem(hwnd, IDC_EDIT_CONCURRENT as i32);
                             if h_edit != std::ptr::null_mut() {
                                 let len = GetWindowTextLengthW(h_edit);
                                 if len > 0 {
                                     let mut buf = vec![0u16; (len + 1) as usize];
                                     GetWindowTextW(h_edit, buf.as_mut_ptr(), len + 1);
                                     let s = String::from_utf16_lossy(&buf[..len as usize]);
                                     let clean: String = s.chars().take_while(|c| c.is_digit(10)).collect();
                                     if let Ok(val) = clean.parse::<u32>() {
                                         self.max_concurrent_items = val;
                                     }
                                 } else {
                                     self.max_concurrent_items = 0; // Treat empty as unlimited
                                 }
                             }
                             DestroyWindow(hwnd);
                         },
                          IDC_CHK_SKIP_EXT => {
                              if (code as u32) == BN_CLICKED {
                                  let h_chk = GetDlgItem(hwnd, IDC_CHK_SKIP_EXT as i32);
                                  let checked = Button::new(h_chk).is_checked();
                                  self.enable_skip_heuristics = checked;
                                  
                                  // Enable/Disable Edit and Reset Button
                                  let h_edit = GetDlgItem(hwnd, IDC_EDIT_EXTENSIONS as i32);
                                  let h_reset = GetDlgItem(hwnd, IDC_BTN_RESET_EXT as i32);
                                  
                                  if h_edit != std::ptr::null_mut() { Button::new(h_edit).set_enabled(checked); }
                                  if h_reset != std::ptr::null_mut() { Button::new(h_reset).set_enabled(checked); }
                              }
                          },
                          IDC_BTN_RESET_EXT => {
                              if (code as u32) == BN_CLICKED {
                                  // Reset text to default
                                   let default_skip = "zip,7z,rar,gz,bz2,xz,zst,lz4,jpg,jpeg,png,gif,webp,avif,heic,mp4,mkv,avi,webm,mov,wmv,mp3,flac,aac,ogg,opus,wma,pdf";
                                   let h_edit = GetDlgItem(hwnd, IDC_EDIT_EXTENSIONS as i32);
                                   if h_edit != std::ptr::null_mut() {
                                       SetWindowTextW(h_edit, to_wstring(default_skip).as_ptr());
                                   }
                              }
                          },
                          IDC_EDIT_EXTENSIONS => {
                               if (code as u32) == EN_CHANGE {
                                   let h_edit = GetDlgItem(hwnd, IDC_EDIT_EXTENSIONS as i32);
                                   if h_edit != std::ptr::null_mut() {
                                       let len = GetWindowTextLengthW(h_edit);
                                       let mut buf = vec![0u16; (len + 1) as usize];
                                       GetWindowTextW(h_edit, buf.as_mut_ptr(), len + 1);
                                       self.skip_extensions = String::from_utf16_lossy(&buf[..len as usize]);
                                   }
                               }
                          },
                          IDC_CHK_FORCE_STOP => {
                              if (code as u32) == BN_CLICKED {
                                   let mut checked = false;
                                   let h_ctl = GetDlgItem(hwnd, IDC_CHK_FORCE_STOP as i32);
                                   if h_ctl != std::ptr::null_mut() {
                                       checked = Button::new(h_ctl).is_checked();
                                       self.enable_force_stop = checked;
                                   }
                                   
                                   // Notify Parent immediately (WM_APP + 3)
                                   use GetParent;
                                   let parent = GetParent(hwnd);
                                   if parent != std::ptr::null_mut() {
                                       let val = if checked { 1 } else { 0 };
                                       SendMessageW(parent, 0x8000 + 3, val as WPARAM, 0);
                                   }
                              }
                          },
                          IDC_CHK_CONTEXT_MENU => {
                               if (code as u32) == BN_CLICKED {
                                    let mut checked = false;
                                    let h_ctl = GetDlgItem(hwnd, IDC_CHK_CONTEXT_MENU as i32);
                                    if h_ctl != std::ptr::null_mut() {
                                        checked = Button::new(h_ctl).is_checked();
                                        self.enable_context_menu = checked;
                                    }
                                    
                                    // Perform registry operation
                                    if checked {
                                        if let Err(_e) = crate::registry::register_context_menu() {
                                            // Show error, revert checkbox
                                            let msg = w!("Failed to register context menu. Run as Administrator.");
                                            let title = w!("Error");
                                            
                                            MessageBoxW(
                                                hwnd,
                                                msg.as_ptr(),
                                                title.as_ptr(),
                                                MB_ICONERROR | MB_OK
                                            );
                                            self.enable_context_menu = false;
                                            
                                            let h_ctl_revert = GetDlgItem(hwnd, IDC_CHK_CONTEXT_MENU as i32);
                                            if h_ctl_revert != std::ptr::null_mut() {
                                                Button::new(h_ctl_revert).set_checked(false);
                                            }
                                        }
                                    } else {
                                        let _ = crate::registry::unregister_context_menu();
                                    }
                                    
                                    // Notify Parent immediately (WM_APP + 5)
                                    use GetParent;
                                    let parent = GetParent(hwnd);
                                    if parent != std::ptr::null_mut() {
                                        let val = if self.enable_context_menu { 1 } else { 0 };
                                        SendMessageW(parent, 0x8000 + 5, val as WPARAM, 0);
                                    }
                               }
                          },
                          IDC_CHK_LOW_POWER => {
                               if (code as u32) == BN_CLICKED {
                                   let mut checked = false;
                                    let h_ctl = GetDlgItem(hwnd, IDC_CHK_LOW_POWER as i32);
                                    if h_ctl != std::ptr::null_mut() {
                                        checked = Button::new(h_ctl).is_checked();
                                        self.low_power_mode = checked;
                                    }
                                    
                                    // Notify Parent immediately (WM_APP + 7)
                                    use GetParent;
                                    let parent = GetParent(hwnd);
                                    if parent != std::ptr::null_mut() {
                                        let val = if checked { 1 } else { 0 };
                                        SendMessageW(parent, 0x8000 + 7, val as WPARAM, 0);
                                    }
                               }
                          },
                          IDC_CHK_SYSTEM_GUARD => {
                              if (code as u32) == BN_CLICKED {
                                   let mut checked = false;
                                   let h_ctl = GetDlgItem(hwnd, IDC_CHK_SYSTEM_GUARD as i32);
                                   if h_ctl != std::ptr::null_mut() {
                                       checked = Button::new(h_ctl).is_checked();
                                       self.enable_system_guard = checked;
                                   }
                                   
                                   // Notify Parent immediately (WM_APP + 6)
                                   use GetParent;
                                   let parent = GetParent(hwnd);
                                   if parent != std::ptr::null_mut() {
                                       let val = if checked { 1 } else { 0 };
                                       SendMessageW(parent, 0x8000 + 6, val as WPARAM, 0);
                                   }
                              }
                          },
                          IDC_BTN_CHECK_UPDATE => {
                              if (code as u32) == BN_CLICKED {
                                  let clone_hwnd_ptr = hwnd as usize;
                                  match &self.update_status {
                                      UpdateStatus::Available(_, url) => {
                                          let url = url.clone();
                                          // Start Update
                                          std::thread::spawn(move || {
                                               let clone_hwnd = clone_hwnd_ptr as HWND;
                                               if let Err(e) = crate::updater::download_and_start_update(&url) {
                                                    let status = Box::new(UpdateStatus::Error(e));
                                                    SendMessageW(clone_hwnd, WM_APP_UPDATE_CHECK_RESULT, 0, Box::into_raw(status) as LPARAM);
                                               } else {
                                                    // Restart Application
                                                    use ShellExecuteW;
                                                    use SW_SHOW;
                                                    
                                                    let exe = std::env::current_exe().unwrap_or_default();
                                                    let exe_path = crate::utils::to_wstring(exe.to_str().unwrap_or(""));
                                                    
                                                    ShellExecuteW(
                                                        std::ptr::null_mut(),
                                                        w!("open").as_ptr(),
                                                        exe_path.as_ptr(),
                                                        std::ptr::null(),
                                                        std::ptr::null(),
                                                        SW_SHOW
                                                    );
                                                    std::process::exit(0);
                                               }
                                          });
                                      },
                                      UpdateStatus::Checking => {}, // Ignore
                                      _ => {
                                          // Check for update
                                          self.update_status = UpdateStatus::Checking;
                                          
                                          let h_btn = GetDlgItem(hwnd, IDC_BTN_CHECK_UPDATE as i32);
                                          Button::new(h_btn).set_enabled(false); // Disable button
                                          let h_lbl = GetDlgItem(hwnd, IDC_LBL_UPDATE_STATUS as i32);
                                          Label::new(h_lbl).set_text("Checking for updates...");

                                          let clone_hwnd_ptr = hwnd as usize;
                                          std::thread::spawn(move || {
                                              let clone_hwnd = clone_hwnd_ptr as HWND;
                                              let res = match crate::updater::check_for_updates() {
                                                  Ok(Some(info)) => UpdateStatus::Available(info.version, info.download_url),
                                                  Ok(None) => UpdateStatus::UpToDate,
                                                  Err(e) => UpdateStatus::Error(e),
                                              };
                                              let boxed = Box::new(res);
                                              SendMessageW(clone_hwnd, WM_APP_UPDATE_CHECK_RESULT, 0, Box::into_raw(boxed) as LPARAM);
                                          });
                                      }
                                  }
                              }
                          },
                          IDC_CHK_SET_ATTR => {
                               if (code as u32) == BN_CLICKED {
                                   let mut checked = false;
                                   let h_ctl = GetDlgItem(hwnd, IDC_CHK_SET_ATTR as i32);
                                   if h_ctl != std::ptr::null_mut() {
                                       checked = Button::new(h_ctl).is_checked();
                                       self.set_compressed_attr = checked;
                                   }
                                   
                                   // Notify Parent immediately (WM_APP + 9) - New message for this
                                   use GetParent;
                                   let parent = GetParent(hwnd);
                                   if parent != std::ptr::null_mut() {
                                       let val = if checked { 1 } else { 0 };
                                       SendMessageW(parent, 0x8000 + 9, val as WPARAM, 0);
                                   }
                              }
                          },
                          IDC_CHK_LOG_ENABLED | IDC_CHK_LOG_ERRORS | IDC_CHK_LOG_WARNS | IDC_CHK_LOG_INFO | IDC_CHK_LOG_TRACE => {
                              if (code as u32) == BN_CLICKED {
                                   let h_ctl = GetDlgItem(hwnd, id as i32);
                                   let checked = Button::new(h_ctl).is_checked();
                                   
                                   match id {
                                       IDC_CHK_LOG_ENABLED => {
                                           self.log_enabled = checked;
                                           // Enable/Disable child checkboxes
                                           let ids = [IDC_CHK_LOG_ERRORS, IDC_CHK_LOG_WARNS, IDC_CHK_LOG_INFO, IDC_CHK_LOG_TRACE];
                                           for &child_id in &ids {
                                               let h = GetDlgItem(hwnd, child_id as i32);
                                               if h != std::ptr::null_mut() {
                                                   Button::new(h).set_enabled(checked);
                                               }
                                           }
                                       },
                                       IDC_CHK_LOG_ERRORS => if checked { self.log_level_mask |= crate::logger::LOG_LEVEL_ERROR; } else { self.log_level_mask &= !crate::logger::LOG_LEVEL_ERROR; },
                                       IDC_CHK_LOG_WARNS => if checked { self.log_level_mask |= crate::logger::LOG_LEVEL_WARN; } else { self.log_level_mask &= !crate::logger::LOG_LEVEL_WARN; },
                                       IDC_CHK_LOG_INFO => if checked { self.log_level_mask |= crate::logger::LOG_LEVEL_INFO; } else { self.log_level_mask &= !crate::logger::LOG_LEVEL_INFO; },
                                       IDC_CHK_LOG_TRACE => if checked { self.log_level_mask |= crate::logger::LOG_LEVEL_TRACE; } else { self.log_level_mask &= !crate::logger::LOG_LEVEL_TRACE; },
                                       _ => {}
                                   }
                                   
                                   // Notify Parent immediately (WM_APP + 8)
                                   // Send (Enabled: bool) in WPARAM, (Mask: u8) in LPARAM
                                   use GetParent;
                                   let parent = GetParent(hwnd);
                                   if parent != std::ptr::null_mut() {
                                       let w = if self.log_enabled { 1 } else { 0 };
                                       let l = self.log_level_mask as isize;
                                       SendMessageW(parent, 0x8000 + 8, w as WPARAM, l as LPARAM);
                                   }
                              }
                          },
                          IDC_BTN_RESTART_TI => {
                              if (code as u32) == BN_CLICKED {
                                  let msg = w!("This will restart CompactRS as System/TrustedInstaller.\n\nUse this ONLY if you need to compress protected system folders (e.g. WinSxS).\n\nAre you sure?");
                                  let title = w!("Privilege Elevation");
                                  let res = MessageBoxW(hwnd, msg.as_ptr(), title.as_ptr(), MB_YESNO | MB_ICONWARNING);
                                  
                                  if res == IDYES {
                                      if let Err(e) = crate::engine::elevation::restart_as_trusted_installer() {
                                          let err_msg = to_wstring(&("Failed to elevate: ".to_string() + &e.to_string()));
                                          let err_title = w!("Error");
                                          MessageBoxW(hwnd, err_msg.as_ptr(), err_title.as_ptr(), MB_ICONERROR | MB_OK);
                                      }
                                  }
                              }
                          },
                          IDC_BTN_RESET_ALL => {
                            if (code as u32) == BN_CLICKED {
                                let msg = w!("Are you sure you want to reset ALL settings to their default values?\n\nThis will revert theme, logging, threads, and extension filters.");
                                let title = w!("Reset Settings");
                                if MessageBoxW(hwnd, msg.as_ptr(), title.as_ptr(), MB_YESNO | MB_ICONWARNING) == IDYES {
                                    // Reset State
                                    self.theme = AppTheme::System;
                                    self.enable_force_stop = false;
                                    self.enable_context_menu = false;
                                    self.enable_system_guard = true;
                                    self.low_power_mode = false;
                                    self.max_threads = 0;
                                    self.max_concurrent_items = 0;
                                    self.log_enabled = false;
                                    self.log_level_mask = crate::logger::LOG_LEVEL_ERROR | crate::logger::LOG_LEVEL_WARN;
                                    self.enable_skip_heuristics = true;
                                    self.skip_extensions = "zip,7z,rar,gz,bz2,xz,zst,lz4,jpg,jpeg,png,gif,webp,avif,heic,mp4,mkv,avi,webm,mov,wmv,mp3,flac,aac,ogg,wma,wav,iso,img,vhd,vhdx,pdf,doc,docx,xls,xlsx,ppt,pptx,exe,dll,sys,msi,cab".to_string();
                                    
                                    // Update UI Controls
                                    
                                    // Helper to set check
                                    let set_chk = |id, val| {
                                        let h = GetDlgItem(hwnd, id as i32);
                                        if h != std::ptr::null_mut() { Button::new(h).set_checked(val); }
                                    };
                                    
                                    // Theme Combo
                                    let h_combo = GetDlgItem(hwnd, IDC_COMBO_THEME as i32);
                                    if h_combo != std::ptr::null_mut() { ComboBox::new(h_combo).set_selected_index(0); }
                                    
                                    set_chk(IDC_CHK_FORCE_STOP, false);
                                    set_chk(IDC_CHK_CONTEXT_MENU, false);
                                    set_chk(IDC_CHK_SYSTEM_GUARD, true);
                                    set_chk(IDC_CHK_LOW_POWER, false);
                                    set_chk(IDC_CHK_SET_ATTR, false);
                                    self.set_compressed_attr = false;
                                    
                                    // Performance
                                    let h_slider = GetDlgItem(hwnd, IDC_SLIDER_THREADS as i32);
                                    let cpu_count = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1) as u32;
                                    if h_slider != std::ptr::null_mut() { Trackbar::new(h_slider).set_pos(cpu_count); }
                                    
                                    let h_edit_conc = GetDlgItem(hwnd, IDC_EDIT_CONCURRENT as i32);
                                    if h_edit_conc != std::ptr::null_mut() { 
                                        use SetWindowTextW;
                                        SetWindowTextW(h_edit_conc, w!("0").as_ptr());
                                    }
                                    
                                    // Filtering
                                    set_chk(IDC_CHK_SKIP_EXT, true);
                                     let h_edit_ext = GetDlgItem(hwnd, IDC_EDIT_EXTENSIONS as i32);
                                    if h_edit_ext != std::ptr::null_mut() { 
                                        Button::new(h_edit_ext).set_enabled(true);
                                        use SetWindowTextW;
                                        let txt = to_wstring(&self.skip_extensions);
                                        SetWindowTextW(h_edit_ext, txt.as_ptr());
                                    }
                                    let h_btn_reset = GetDlgItem(hwnd, IDC_BTN_RESET_EXT as i32);
                                    if h_btn_reset != std::ptr::null_mut() { Button::new(h_btn_reset).set_enabled(true); }

                                    // Diagnostics
                                    set_chk(IDC_CHK_LOG_ENABLED, false);
                                    set_chk(IDC_CHK_LOG_ERRORS, true);
                                    set_chk(IDC_CHK_LOG_WARNS, true);
                                    set_chk(IDC_CHK_LOG_INFO, false);
                                    set_chk(IDC_CHK_LOG_TRACE, false);
                                    
                                     // Disable child checkboxes for logs
                                     let ids = [IDC_CHK_LOG_ERRORS, IDC_CHK_LOG_WARNS, IDC_CHK_LOG_INFO, IDC_CHK_LOG_TRACE];
                                     for &child_id in &ids {
                                         let h = GetDlgItem(hwnd, child_id as i32);
                                         if h != std::ptr::null_mut() {
                                             Button::new(h).set_enabled(false);
                                         }
                                     }
                                    
                                    // Notify Parent of immediate changes
                                    use GetParent;
                                    let parent = GetParent(hwnd);
                                    if parent != std::ptr::null_mut() {
                                        // Theme
                                        SendMessageW(parent, 0x8000 + 4, 0, 0); // 0 = System
                                        // Logging
                                        let w_log = 0; // Disabled
                                        let l_log = self.log_level_mask as isize;
                                        SendMessageW(parent, 0x8000 + 8, w_log as WPARAM, l_log as LPARAM);
                                        // System Guard
                                        SendMessageW(parent, 0x8000 + 6, 1, 0);
                                    }
                                }
                            }
                         },
                          _ => {}
                     }
                     Some(0)
                },
                _ => None,
            }
        }
    }
}