native-theme 0.7.1

Cross-platform native theme detection and loading for Rust GUI applications
Documentation
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
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
//! # native-theme
//!
//! Cross-platform native theme detection and loading for Rust GUI applications.
//!
//! Any Rust GUI app can look native on any platform by loading a single theme
//! file or reading live OS settings, without coupling to any specific toolkit.
//!
//! A [`Theme`] is sparse and TOML-shaped: every field is an `Option`, and it
//! holds a light and a dark variant. Resolving one variant fills every field
//! the model requires and checks its ranges, giving a [`ResolvedTheme`] that
//! UI code reads without unwrapping. Most applications use a toolkit
//! connector, which does this and converts the result:
//! `native-theme-iced`, `native-theme-gpui` or `native-theme-egui`.
//!
//! # Quick start
//!
//! Read the OS theme and pick its active variant (the OS readers need the
//! `native` feature, or `kde`, `portal`, `macos`, `windows`):
//!
//! ```no_run
//! use native_theme::SystemTheme;
//!
//! let sys = SystemTheme::from_system()?;
//! let active = sys.pick(sys.mode); // &ResolvedTheme
//! let _accent = active.defaults.accent_color;
//! # Ok::<(), native_theme::Error>(())
//! ```
//!
//! Load a bundled preset and resolve one mode (no features needed):
//!
//! ```
//! use native_theme::{ColorMode, Theme};
//!
//! let resolved = Theme::preset("nord")?.resolve(ColorMode::Dark)?;
//! assert_eq!(resolved.mode, ColorMode::Dark);
//! let _background = resolved.variant.defaults.background_color;
//! # Ok::<(), native_theme::Error>(())
//! ```
//!
//! Apply an overlay to the OS theme; the overlay states only what it changes:
//!
//! ```no_run
//! use native_theme::{SystemTheme, Theme};
//!
//! let sys = SystemTheme::from_system()?;
//! let overlay = Theme::from_toml(r##"
//!     [light.defaults]
//!     accent_color = "#ff6600"
//!
//!     [dark.defaults]
//!     accent_color = "#ff6600"
//! "##)?;
//! let customised = sys.with_overlay(&overlay)?;
//! # Ok::<(), native_theme::Error>(())
//! ```
//!
//! Hand the result to a connector: `native-theme-iced`, `native-theme-gpui`
//! and `native-theme-egui` each have `from_system` and `from_preset`, which do
//! the steps above, and `to_theme`, which converts a [`ResolvedTheme`] you
//! resolved yourself into the toolkit's theme.
//!
//! Watch for OS theme changes (the `watch` feature). The callback runs on a
//! background thread and carries no theme: clear the detection caches, which
//! hold the dark mode, reduced motion, accessibility preferences, icon theme
//! and, on Linux, desktop detected before (the OS read takes its icon theme
//! and desktop from them), and read the OS theme again. Keep the subscription
//! alive; dropping it stops watching:
//!
//! ```no_run
//! # #[cfg(feature = "watch")]
//! # {
//! let _subscription = native_theme::watch::on_theme_change(|_event| {
//!     // Tell the UI thread to call `detect::invalidate_caches()`, then
//!     // `SystemTheme::from_system()` again.
//! })?;
//! # }
//! # Ok::<(), native_theme::Error>(())
//! ```
//!
//! # Presets
//!
//! 17 presets are bundled, each a TOML file with a light and a dark variant,
//! loaded by key with [`Theme::preset`] and listed by
//! [`Theme::list_presets`] (or [`Theme::list_presets_for_platform`], the ones
//! offered on the running platform):
//!
//! - Platform: `kde-breeze`, `adwaita`, `windows-11`, `macos-monterey`
//!   (macOS 12 Monterey to 15 Sequoia), `macos-tahoe` (macOS 26 Tahoe),
//!   `material-3` and `ios-13` (the flat look of iOS 13 to 18).
//! - Community: `catppuccin-latte`, `catppuccin-frappe`,
//!   `catppuccin-macchiato`, `catppuccin-mocha`, `nord`, `dracula`,
//!   `gruvbox`, `solarized`, `tokyo-night` and `one-dark`.
//!
//! `material-3` and `ios-13` have no OS reader; an application chooses them
//! as a look. `ios-13`'s SF Symbols icons load only on macOS (with
//! `system-icons`); elsewhere its icons are `None`, never another set's.
//!
//! # Platform readers
//!
//! [`SystemTheme::from_system`] reads the OS theme with the running
//! platform's reader and merges what it reads over that platform's preset; a
//! field the reader does not supply keeps the preset's value. Both variants
//! are resolved, so [`SystemTheme::pick`] serves either mode.
//!
//! | Platform | Feature | Reads | Preset |
//! |---|---|---|---|
//! | KDE Plasma | `kde` | `kdeglobals`; with `portal`, the portal's accent colour | `kde-breeze` |
//! | GNOME, Budgie | `portal` | the XDG Desktop Portal settings and gsettings | `adwaita` |
//! | macOS 11 and later | `macos` | `NSColor` under both appearances, `NSFont`, the scroller style and the accessibility options | `macos-monterey` before 26 (it covers 12 to 15), `macos-tahoe` from 26 |
//! | Windows | `windows` | `UISettings`, the system metrics and fonts | `windows-11` |
//!
//! The KDE, GNOME and Windows readers read the active variant only, and the
//! other variant is the preset's; the macOS reader reads both. On another
//! Linux desktop, or without the desktop's reader feature, `from_system`
//! returns the desktop's preset (`kde-breeze` on KDE, `adwaita` elsewhere)
//! with no live reading, in the mode [`detect::detect_is_dark`] reads. On macOS and Windows without the reader feature it returns
//! [`Error::FeatureDisabled`], and on other platforms
//! [`Error::PlatformUnsupported`].
//!
//! On macOS and Windows the preset is the one of the running OS version's
//! visual generation. [`SystemTheme::version_match`] tells how the running
//! version relates to it ([`VersionMatch`]): macOS 27, which has no preset of
//! its own yet, gets `macos-tahoe` as [`VersionMatch::PresetOlder`], and
//! Windows 10 gets `windows-11` as [`VersionMatch::PresetNewer`].
//! [`SystemTheme::from_system_with_preset`] merges over another generation of
//! the running platform instead. [`pipeline::platform_preset_name`] names the
//! preset without reading the theme, and
//! [`pipeline::diagnose_platform_support`] reports what detection can see.
//!
//! # Feature flags
//!
//! No feature is needed to load, merge and resolve presets and TOML files.
//! Each OS-specific dependency is compiled only on its own target, so
//! `native` on macOS pulls in only the macOS dependencies.
//!
//! | Feature | Enables |
//! |---|---|
//! | `kde`, `portal`, `macos`, `windows` | The platform readers above |
//! | `linux-kde`, `linux-portal` | Aliases of `kde` and `portal` |
//! | `linux` | `linux-kde` and `linux-portal` |
//! | `native` | `linux`, `macos` and `windows`: every reader |
//! | `watch` | `watch::on_theme_change`, for the platforms whose reader feature is on |
//! | `material-icons`, `lucide-icons` | The bundled Material Symbols and Lucide icon sets |
//! | `system-icons` | The platform icon sets: freedesktop icon themes (Linux), SF Symbols (macOS), Segoe Fluent Icons and stock icons (Windows); also enables `material-icons` |
//! | `svg-rasterize` | `rasterize::rasterize_svg`, SVG icons to RGBA through resvg |
//! | `system-fonts` | `fonts::system_face`, the platform's own typeface for a family; on macOS the system UI font's file through Core Text |
//!
//! The minimum supported Rust version is 1.88.0.
//!
//! # Modules
//!
//! - The crate root: [`Theme`], [`ColorMode`], [`SystemTheme`],
//!   [`ResolvedTheme`], [`Resolved`], [`ResolutionContext`],
//!   [`AccessibilityPreferences`], [`Rgba`], [`Error`] and [`Result`], and
//!   from [`pipeline`] [`VersionMatch`], [`PlatformPreset`] and
//!   [`DiagnosticEntry`].
//! - [`prelude`]: the ten most-used items, for a glob import.
//! - [`theme`]: the data model, the sparse types and their resolved
//!   counterparts, widget by widget.
//! - [`presets`]: [`PresetInfo`](presets::PresetInfo), the description of a
//!   bundled preset that [`Theme::list_presets`] returns; [`Theme::preset`]
//!   loads one.
//! - [`resolve`]: the resolution-time inputs.
//! - [`detect`]: OS detection: dark mode, reduced motion, accessibility
//!   preferences, icon theme and set, desktop environment.
//! - [`icons`]: the per-set icon loaders, the icon data types and the
//!   icon-set choice.
//! - [`fonts`]: font face matching; the platform's own typeface with the
//!   `system-fonts` feature.
//! - [`pipeline`]: the running platform's preset and a diagnosis of its
//!   theme support.
//! - [`error`] and [`color`]: the error and colour types.
//! - `watch` (the `watch` feature): OS theme-change notifications.
//! - `rasterize` (the `svg-rasterize` feature): SVG icons to RGBA.

#![warn(missing_docs)]
#![deny(unsafe_code)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;

/// Generates `merge()` and `is_empty()` methods for theme structs.
///
/// Three field categories:
/// - `option { field1, field2, ... }` -- `Option<T>` leaf fields
/// - `nested { field1, field2, ... }` -- nested struct fields with their own `merge()`
/// - `optional_nested { field1, field2, ... }` -- `Option<T>` where T has its own `merge()`
///
/// For `option` fields, `Some` values in the overlay replace the
/// corresponding fields in self; `None` fields are left unchanged.
/// For `nested` fields, merge is called recursively.
/// For `optional_nested` fields: if both base and overlay are `Some`, the inner values
/// are merged recursively. If base is `None` and overlay is `Some`, overlay is cloned.
/// If overlay is `None`, the base field is preserved unchanged.
///
/// # Examples
///
/// ```ignore
/// impl_merge!(MyColors {
///     option { accent, background }
/// });
/// ```
macro_rules! impl_merge {
    (
        $struct_name:ident {
            $(option { $($opt_field:ident),* $(,)? })?
            $(nested { $($nest_field:ident),* $(,)? })?
            $(optional_nested { $($on_field:ident),* $(,)? })*
        }
    ) => {
        impl $struct_name {
            /// Merge an overlay into this value. `Some` fields in the overlay
            /// replace the corresponding fields in self; `None` fields are
            /// left unchanged. Nested structs are merged recursively.
            pub fn merge(&mut self, overlay: &Self) {
                $($(
                    if overlay.$opt_field.is_some() {
                        self.$opt_field = overlay.$opt_field.clone();
                    }
                )*)?
                $($(
                    self.$nest_field.merge(&overlay.$nest_field);
                )*)?
                $($(
                    match (&mut self.$on_field, &overlay.$on_field) {
                        (Some(base), Some(over)) => base.merge(over),
                        (None, Some(over)) => self.$on_field = Some(over.clone()),
                        _ => {}
                    }
                )*)*
            }

            /// Returns true if all fields are at their default (None/empty) state.
            pub fn is_empty(&self) -> bool {
                true
                $($(&& self.$opt_field.is_none())*)?
                $($(&& self.$nest_field.is_empty())*)?
                $($(&& self.$on_field.as_ref().map_or(true, |v| v.is_empty()))*)*
            }
        }
    };
}

/// Color types and sRGB utilities.
pub mod color;
pub mod detect;
/// Error types for theme operations.
pub mod error;
pub mod fonts;
/// GNOME portal theme reader.
///
/// Requires the `portal` feature, and Linux.
#[cfg(all(target_os = "linux", feature = "portal"))]
pub(crate) mod gnome;
/// Icon loading.
///
/// - The semantic roles, [`IconRole`], and the icon data, [`IconData`] and
///   [`AnimatedIcon`].
/// - A typed loader per icon set ([`IconSet`]): [`MaterialLoader`] and
///   [`LucideLoader`] for the bundled sets, [`FreedesktopLoader`] (Linux),
///   [`SfSymbolsLoader`] (macOS) and [`SegoeFluentLoader`] (Windows) for the
///   platform sets. Each needs its set's feature, `material-icons`,
///   `lucide-icons` or `system-icons`, and gives `None` without it.
/// - [`load_icon`] and [`load_icon_indicator`], which load from whichever set
///   they are given.
/// - The icon-set choice of a settings UI,
///   [`IconSetChoice`](icons::IconSetChoice).
pub mod icons;
#[cfg(all(target_os = "linux", feature = "kde"))]
pub mod kde;
/// Breeze widget sizing constants. Compiled on every target and feature set,
/// so the preset gate sees the values the KDE reader states.
#[path = "kde/metrics.rs"]
pub(crate) mod kde_metrics;
/// Theme data model types, public through [`theme`].
mod model;
pub mod pipeline;
pub mod presets;
/// Internal `ThemeReader` trait (see module docs for Option A rationale).
///
/// Consumed by `pipeline::select_reader` as `Box<dyn ThemeReader>`; the
/// trait and every impl are `pub(crate)` — not part of the public API.
mod reader;
/// Theme resolution engine (inheritance + validation).
///
/// Public surface: [`resolve::ResolutionContext`] — the resolution-time
/// inputs struct consumed by
/// [`ThemeMode::into_resolved`](crate::theme::ThemeMode::into_resolved).
/// Inheritance rules, validation machinery, and the range-check helpers
/// are all crate-internal (no stability guarantee).
pub mod resolve;
#[cfg(any(
    feature = "material-icons",
    feature = "lucide-icons",
    feature = "system-icons"
))]
mod spinners;
#[cfg(feature = "watch")]
pub mod watch;

pub mod prelude;

/// Theme data model: types, defaults, fonts, borders, widgets.
///
/// Core types: [`Theme`], [`ThemeMode`],
/// [`ResolvedTheme`], [`ResolvedDefaults`].
pub mod theme {
    pub use crate::model::widgets::*;
    pub use crate::model::{
        ArrowSide, ColorMode, DefaultsBorderSpec, DialogButtonOrder, DialogEntranceStyle, FontSize,
        FontSpec, FontStyle, IconSet, IconSizes, Resolved, ResolvedDefaults,
        ResolvedDefaultsBorder, ResolvedFontSpec, ResolvedIconSizes, ResolvedPadding,
        ResolvedTextScale, ResolvedTextScaleEntry, ResolvedTheme, ResolvedWidgetBorder,
        TabIndicatorSide, TextScale, TextScaleEntry, Theme, ThemeDefaults, ThemeMode,
        WidgetBorderSpec, intern_font_family,
    };
    pub use crate::presets::PresetInfo;
}

/// Freedesktop icon theme lookup (Linux).
///
/// Requires the `system-icons` feature, and Linux.
#[cfg(all(target_os = "linux", feature = "system-icons"))]
pub(crate) mod freedesktop;
/// macOS platform helpers.
pub(crate) mod macos;
/// SVG-to-RGBA rasterization utilities.
///
/// Requires the `svg-rasterize` feature.
#[cfg(feature = "svg-rasterize")]
pub mod rasterize;
/// SF Symbols icon loader (macOS).
///
/// Requires the `system-icons` feature, and macOS.
#[cfg(all(target_os = "macos", feature = "system-icons"))]
pub(crate) mod sficons;
/// Windows platform theme reader.
#[cfg(target_os = "windows")]
#[cfg_attr(not(feature = "windows"), allow(dead_code))]
pub(crate) mod windows;
#[cfg(not(target_os = "windows"))]
#[allow(dead_code, unused_variables)]
pub(crate) mod windows;
/// Windows Segoe Fluent / stock icon loader.
///
/// Requires the `system-icons` feature, and Windows.
#[cfg(all(target_os = "windows", feature = "system-icons"))]
pub(crate) mod winicons;
#[cfg(all(not(target_os = "windows"), feature = "system-icons"))]
#[allow(dead_code, unused_imports)]
pub(crate) mod winicons;

use std::borrow::Cow;

/// Convenience Result type alias for this crate.
pub type Result<T> = std::result::Result<T, error::Error>;

// Internal re-exports: keep crate::Type paths working for internal modules
// without exposing them in the public API. External users access types via
// native_theme::theme::*, native_theme::icons::*, native_theme::detect::*, etc.
#[allow(unused_imports)]
pub(crate) use color::ParseColorError;
pub use color::Rgba;
#[cfg(target_os = "linux")]
#[allow(unused_imports)]
pub(crate) use detect::LinuxDesktop;
#[cfg(target_os = "linux")]
#[allow(unused_imports)]
pub(crate) use detect::detect_linux_desktop;
#[allow(unused_imports)]
pub(crate) use detect::{detect_icon_theme, system_icon_set, system_icon_theme};
#[allow(unused_imports)]
pub(crate) use detect::{
    detect_is_dark, detect_reduced_motion, invalidate_caches, prefers_reduced_motion,
    system_is_dark,
};
pub use error::Error;
#[allow(unused_imports)]
pub(crate) use error::{ErrorKind, RangeViolation};
#[allow(unused_imports)]
pub(crate) use icons::{
    FreedesktopLoader, IconId, LucideLoader, MaterialLoader, SegoeFluentLoader, SfSymbolsLoader,
    is_freedesktop_theme_available, load_icon, load_icon_indicator,
};
#[allow(unused_imports)]
pub(crate) use model::icons::icon_name;
#[allow(unused_imports)]
pub(crate) use model::{
    AnimatedIcon, ButtonTheme, CardTheme, CheckboxTheme, ComboBoxTheme, DefaultsBorderSpec,
    DialogButtonOrder, DialogTheme, ExpanderTheme, FontSize, FontSpec, FontStyle, IconData,
    IconProvider, IconRole, IconSet, IconSizes, InputTheme, LayoutTheme, LinkTheme, ListTheme,
    MenuTheme, PopoverTheme, ProgressBarTheme, ResolvedDefaults, ResolvedDefaultsBorder,
    ResolvedFontSpec, ResolvedIconSizes, ResolvedPadding, ResolvedTextScale,
    ResolvedTextScaleEntry, ResolvedWidgetBorder, ScrollbarTheme, SegmentedControlTheme,
    SeparatorTheme, SidebarTheme, SliderTheme, SpinnerTheme, SplitterTheme, StatusBarTheme,
    SwitchTheme, TabTheme, TextScale, TextScaleEntry, ThemeDefaults, ThemeMode, ToolbarTheme,
    TooltipTheme, TransformAnimation, WidgetBorderSpec, WindowTheme, bundled_icon_by_name,
    bundled_icon_svg,
};
pub use pipeline::{DiagnosticEntry, PlatformPreset, VersionMatch};
#[allow(unused_imports)]
pub(crate) use pipeline::{diagnose_platform_support, platform_preset_name};
pub use resolve::ResolutionContext;
pub use theme::{ColorMode, Resolved, ResolvedTheme, Theme};

/// OS-detected accessibility preferences.
///
/// A single copy lives on [`SystemTheme`], shared across light and dark
/// variants. These are runtime values detected from the OS -- not stored
/// in TOML presets. Outside this crate, start from
/// [`AccessibilityPreferences::DEFAULT`] (or `default()`) and set fields.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct AccessibilityPreferences {
    /// Text scaling factor (1.0 = no scaling): the factor of the user's
    /// preference for larger text that the toolkit has not applied. Multiply
    /// font sizes by it ([`scaled`](Self::scaled)) to honour the preference.
    /// Where the toolkit's own scale factor carries the preference, it is
    /// 1.0: on a GNOME or Budgie X11 session, where winit and gpui take their
    /// scale factor from the `Xft.dpi` gnome-settings-daemon writes as
    /// 96 × `text-scaling-factor` × the window scale (gnome-settings-daemon
    /// 51.0 `plugins/xsettings/gsd-xsettings-manager.c:666-668`, `:742`,
    /// `:797-798`; winit 0.30.13 `src/platform_impl/linux/x11/util/randr.rs:41-53`,
    /// `:145-146`; gpui-pre-linux 0.3.12 `src/linux/x11/client.rs:2674-2681`).
    /// They do so only while `WINIT_X11_SCALE_FACTOR` and
    /// `GPUI_X11_SCALE_FACTOR` are unset (`randr.rs:112-154`,
    /// `client.rs:2623-2669`); with an override the application owns the
    /// scale, and the factor is applied nowhere.
    pub text_scaling_factor: f32,
    /// Whether the user has requested reduced motion.
    pub reduce_motion: bool,
    /// Whether a high-contrast mode is active.
    pub high_contrast: bool,
    /// Whether the user has requested reduced transparency.
    pub reduce_transparency: bool,
}

impl Default for AccessibilityPreferences {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl AccessibilityPreferences {
    /// No text scaling and no flag set: what [`Default::default`] returns,
    /// usable in a `const` or `static`.
    pub const DEFAULT: Self = Self {
        text_scaling_factor: 1.0,
        reduce_motion: false,
        high_contrast: false,
        reduce_transparency: false,
    };

    /// Read the OS accessibility preferences without resolving a theme.
    ///
    /// Runs the same platform reader [`SystemTheme::from_system`] runs (KDE
    /// `kdeglobals`, GNOME portal + gsettings, macOS `NSWorkspace`, Windows
    /// `UISettings`) and takes its accessibility block, blocking on it with
    /// `pollster::block_on`. Fields no reader supplies keep their defaults.
    /// Never fails: with no reader or a failing reader the defaults are
    /// returned, with `reduce_motion` the platform's read
    /// ([`crate::detect::detect_reduced_motion`], the one the readers make).
    ///
    /// Use it on the preset path, where accessibility is orthogonal to the
    /// theme choice (a user with large text wants it under a preset too).
    #[must_use]
    pub fn from_system() -> Self {
        pollster::block_on(pipeline::reader_accessibility())
    }

    /// The text-scaling multiplier the toolkit has not applied:
    /// [`text_scaling_factor`](Self::text_scaling_factor) when it is finite
    /// and positive, else `1.0`, so a factor no platform could mean (NaN,
    /// zero, negative, infinite) leaves text at its size.
    #[must_use]
    pub fn text_scale_factor(&self) -> f32 {
        let s = self.text_scaling_factor;
        if s.is_finite() && s > 0.0 { s } else { 1.0 }
    }

    /// A text size times [`text_scale_factor`](Self::text_scale_factor).
    ///
    /// For every text size alike: a widget's font, a `text_scale` role's
    /// size, the default fonts. No other length is scaled. Where the product
    /// is not a normal positive number (a finite factor large enough to
    /// overflow `f32`, or one small enough to make it subnormal or zero),
    /// `size` is returned unscaled, as for a factor that is not finite and
    /// positive.
    #[must_use]
    pub fn scaled(&self, size: f32) -> f32 {
        let product = size * self.text_scale_factor();
        if product.is_normal() && product > 0.0 {
            product
        } else {
            size
        }
    }
}

/// Complete reader result for the pipeline.
///
/// Bundles the type-safe [`ReaderOutput`] with reader metadata
/// (name, icon_set, layout, font_dpi, accessibility) so that
/// `run_pipeline` accepts a single struct instead of many arguments.
#[derive(Clone, Debug)]
pub(crate) struct ReaderResult {
    /// The reader's variant data.
    pub(crate) output: ReaderOutput,
    /// Theme name from reader (e.g. "BreezeDark", "GNOME", "macOS").
    pub(crate) name: Cow<'static, str>,
    /// Shared icon_set from reader.
    pub(crate) icon_set: Option<IconSet>,
    /// Shared layout from reader.
    pub(crate) layout: LayoutTheme,
    /// The reader's font DPI (None = `ResolutionContext::from_system()`'s).
    pub(crate) font_dpi: Option<f32>,
    /// OS-detected accessibility preferences.
    pub(crate) accessibility: AccessibilityPreferences,
}

/// Output contract for platform readers.
///
/// Expresses single-vs-dual variant semantics explicitly:
/// - `Single`: KDE, GNOME, and Windows readers report only the OS-active mode.
///   The pipeline fills the inactive variant from the platform preset.
/// - `Dual`: macOS reads both light and dark appearances in a single call.
///   The pipeline uses both reader-provided variants directly.
///
/// Both carry `is_dark`, the OS-active mode, from which the pipeline sets
/// [`SystemTheme::mode`].
#[derive(Clone, Debug)]
pub(crate) enum ReaderOutput {
    /// Reader provides only the OS-active variant. The pipeline fills the
    /// inactive variant from the platform preset.
    #[cfg_attr(all(target_os = "windows", not(feature = "windows")), allow(dead_code))]
    Single {
        /// The reader-provided variant (OS-active).
        mode: Box<ThemeMode>,
        /// Which color mode this variant represents.
        is_dark: bool,
    },
    /// Reader provides both light and dark variants (macOS).
    #[allow(dead_code)]
    Dual {
        /// The light variant from the reader.
        light: Box<ThemeMode>,
        /// The dark variant from the reader.
        dark: Box<ThemeMode>,
        /// Whether the OS-active appearance is the dark one.
        is_dark: bool,
    },
}

impl ReaderOutput {
    /// The OS-active color mode, as `is_dark` reports it.
    pub(crate) fn mode(&self) -> ColorMode {
        let (ReaderOutput::Single { is_dark, .. } | ReaderOutput::Dual { is_dark, .. }) = self;
        if *is_dark {
            ColorMode::Dark
        } else {
            ColorMode::Light
        }
    }

    /// Reconstruct a [`Theme`] from this reader output (for overlay replay
    /// and merge compatibility).
    pub(crate) fn to_theme(
        &self,
        name: &str,
        icon_set: Option<IconSet>,
        layout: &LayoutTheme,
    ) -> Theme {
        let (light, dark) = match self {
            ReaderOutput::Single { mode, is_dark } => {
                if *is_dark {
                    (None, Some(ThemeMode::clone(mode)))
                } else {
                    (Some(ThemeMode::clone(mode)), None)
                }
            }
            ReaderOutput::Dual { light, dark, .. } => {
                (Some(ThemeMode::clone(light)), Some(ThemeMode::clone(dark)))
            }
        };
        Theme {
            name: std::borrow::Cow::Owned(name.to_string()),
            light,
            dark,
            layout: layout.clone(),
            icon_set,
            // Readers keep Theme-level icon_theme = None and rely on either
            // the preset's value (tier 2) or system detect (tier 3). Readers
            // that need to override per color mode use ThemeDefaults::icon_theme
            // on the variant (tier 1, e.g. the KDE reader for breeze/breeze-dark).
            icon_theme: None,
        }
    }
}

/// Data needed to replay the merge+resolve pipeline for overlay support.
///
/// Stores the reader's output, the preset name and every overlay applied so
/// far (merged into one [`Theme`]), so that [`SystemTheme::with_overlay()`]
/// rebuilds the pre-resolve variants (preset, then reader, then overlays)
/// and resolves them again.
#[derive(Clone, Debug)]
pub(crate) struct OverlaySource {
    /// The reader's variant data for replay.
    pub(crate) reader_output: ReaderOutput,
    /// Theme name from reader.
    pub(crate) name: Cow<'static, str>,
    /// Shared icon_set from reader.
    pub(crate) icon_set: Option<IconSet>,
    /// Shared layout from reader.
    pub(crate) layout: LayoutTheme,
    /// The platform preset name (e.g. "kde-breeze").
    pub(crate) preset_name: String,
    /// Resolution-time inputs captured at detection time. Replaces the
    /// old `font_dpi: Option<f32>` field; the context bundles
    /// `font_dpi` + `button_order` + detected `icon_theme` and is cloned
    /// into `with_overlay` replays so resolution is deterministic across
    /// overlay applications.
    pub(crate) context: crate::resolve::ResolutionContext,
    /// Every overlay applied so far, merged in order; `None` before the
    /// first [`SystemTheme::with_overlay()`].
    pub(crate) overlay: Option<Theme>,
}

/// Result of the OS-first pipeline. Holds both resolved variants.
///
/// Produced by [`SystemTheme::from_system()`] and [`SystemTheme::from_system_async()`].
/// Both light and dark are always populated: the OS-active variant
/// comes from the reader + preset + resolve, the inactive variant
/// comes from the preset + resolve.
#[derive(Clone, Debug)]
pub struct SystemTheme {
    /// Theme name: the reader's (the KDE colour scheme's, such as
    /// `"BreezeDark"`, or `"GNOME"`, `"macOS"`, `"Windows"`), or the preset's
    /// where no reader ran.
    //
    // # Ownership type
    //
    // This field uses `Cow<'static, str>`, not `Arc<str>`. The v0.5.7 API
    // review recommended uniform `Arc<str>` across `name`, `icon_theme`,
    // `ReaderOutput::name`, and `ResolvedFontSpec::family`. The audit in
    // `docs/archive/v0.5.7_gaps.md` §G9 concluded that the
    // uniform recommendation should be adopted ONLY for
    // [`ResolvedFontSpec::family`](crate::model::font::ResolvedFontSpec)
    // (where 26 widgets × connectors genuinely share font families), and
    // should be REVERSED for `name` / `icon_theme` because:
    //
    // - Each resolved theme carries exactly ONE `name` — no dedup benefit.
    // - Bundled preset names are `&'static str` literals; `Cow::Borrowed(static_lit)`
    //   is zero allocation, zero refcount. `Arc<str>` would require at least one
    //   allocation per unique string at construction time, paying allocation cost
    //   for a dedup benefit that is structurally absent.
    //
    // The same reasoning applies symmetrically to
    // [`SystemTheme::icon_theme`](Self::icon_theme),
    // [`Theme::name`](crate::theme::Theme), and
    // [`ThemeDefaults::icon_theme`](crate::model::defaults::ThemeDefaults).
    //
    // See `docs/archive/v0.5.7_gaps.md` §G9 for the full audit.
    pub name: Cow<'static, str>,
    /// The OS color mode preference (light or dark).
    pub mode: ColorMode,
    /// Resolved light variant (always populated).
    pub light: ResolvedTheme,
    /// Resolved dark variant (always populated).
    pub dark: ResolvedTheme,
    /// Data for replaying the pipeline on overlay (replaces light_variant/dark_variant).
    pub(crate) overlay_source: OverlaySource,
    /// The platform preset used (e.g., "kde-breeze", "adwaita", "macos-tahoe").
    pub preset: String,
    /// How the running OS version relates to the generation of
    /// [`preset`](Self::preset): `None` where the platform has no generation
    /// table (Linux, whose readers read the installed style) or no reader ran.
    /// On macOS 27 under `macos-tahoe`, which no preset of its own covers yet,
    /// it is `Some(VersionMatch::PresetOlder)`; on Windows 10 under
    /// `windows-11`, `Some(VersionMatch::PresetNewer)`. Early macOS 26 betas
    /// reported version 16 to a binary built against the macOS 15 SDK; a
    /// major version 16 gets `macos-monterey` with
    /// `Some(VersionMatch::PresetOlder)` (sources in the macOS generation
    /// table, `pipeline.rs`).
    pub version_match: Option<VersionMatch>,
    /// Which icon loading mechanism to use for this theme.
    pub icon_set: IconSet,
    /// The name of the visual icon theme (e.g. `"breeze"`, `"Adwaita"`):
    /// the active variant's, else the theme's, else the detected system
    /// icon theme. `None` where the theme states none and detection fails;
    /// [`system_icon_theme()`](crate::detect::system_icon_theme) gives the
    /// reason.
    //
    // # Ownership type
    //
    // `Cow<'static, str>` is used here per the same principled deviation
    // documented on `SystemTheme::name` — see `docs/archive/v0.5.7_gaps.md`
    // §G9. Each resolved theme carries a single icon-theme name (KDE has
    // exactly two across light/dark variants — `"breeze"` / `"breeze-dark"`;
    // other platforms have one), so the `Arc<str>` dedup benefit does not apply.
    pub icon_theme: Option<Cow<'static, str>>,
    /// Each variant's icon theme, resolved by the same three tiers as
    /// [`icon_theme`](Self::icon_theme), kept by variant rather than relative
    /// to [`mode`](Self::mode), a public field a caller may change. Read
    /// through [`icon_theme_for`](Self::icon_theme_for).
    pub(crate) light_icon_theme: Option<Cow<'static, str>>,
    /// The dark variant's counterpart of [`light_icon_theme`](Self::light_icon_theme).
    pub(crate) dark_icon_theme: Option<Cow<'static, str>>,
    /// Layout spacing shared by both variants: the platform reader's values
    /// merged field-wise over the preset's, the same precedence the pipeline
    /// uses for colours. `None` in a field means neither the platform nor the
    /// preset specifies it (platform-facts §2.20); nothing is invented.
    pub layout: LayoutTheme,
    /// OS-detected accessibility preferences (shared across variants).
    pub accessibility: AccessibilityPreferences,
}

impl SystemTheme {
    /// Pick a resolved variant by color mode.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use native_theme::theme::ColorMode;
    ///
    /// let sys = native_theme::SystemTheme::from_system()?;
    /// let dark = sys.pick(ColorMode::Dark);
    /// let active = sys.pick(sys.mode);
    /// # Ok::<(), native_theme::error::Error>(())
    /// ```
    #[must_use]
    pub fn pick(&self, mode: ColorMode) -> &ResolvedTheme {
        match mode {
            ColorMode::Light => &self.light,
            ColorMode::Dark => &self.dark,
        }
    }

    /// The icon-theme name for one colour mode, resolved for that mode's
    /// variant by the same three tiers as [`icon_theme`](Self::icon_theme)
    /// (its `defaults.icon_theme`, then the theme's, then the detected system
    /// theme); for the mode the theme was built in it equals `icon_theme` as
    /// built. KDE names one theme per variant (`breeze`, `breeze-dark`); a
    /// toolkit that keeps a style per colour scheme needs both. `None` where
    /// that variant states none and detection failed; nothing is invented.
    #[must_use]
    pub fn icon_theme_for(&self, mode: ColorMode) -> Option<&str> {
        match mode {
            ColorMode::Light => self.light_icon_theme.as_deref(),
            ColorMode::Dark => self.dark_icon_theme.as_deref(),
        }
    }

    /// Load an icon from this theme's [`icon_set`](Self::icon_set), in
    /// `color`, for one colour mode's variant: a freedesktop icon comes from
    /// that variant's theme, [`icon_theme_for(mode)`](Self::icon_theme_for)
    /// (`breeze` or `breeze-dark` under `kde-breeze`), so an application can
    /// draw the variant that is not the OS's.
    ///
    /// `color` is honoured as in
    /// [`load_custom_icon`](crate::icons::load_custom_icon): an SF Symbol or
    /// a Segoe glyph loads in it, and a Material or Lucide SVG (or a
    /// provider's own SVG) is coloured through
    /// [`colorize_monochrome_svg`](crate::icons::colorize_monochrome_svg). A
    /// freedesktop icon takes it where it has a foreground left to the
    /// caller: a symbolic icon's placeholders
    /// ([`FreedesktopLoader::color`](crate::icons::FreedesktopLoader::color))
    /// and a `currentColor` the document sets no `color` for
    /// ([`fill_current_color`](crate::icons::fill_current_color)). An icon
    /// whose own stylesheet sets the `color` its `currentColor` takes
    /// (Breeze's `current-color-scheme`) keeps that colour, as it is not yet
    /// rewritten from the theme as KDE's own icon loader does, and a
    /// full-colour icon keeps its palette. `None` leaves each icon in the
    /// colour it loads in. A system set's icon loads at its loader's default
    /// size (a freedesktop icon looked up at 24
    /// pixels, an SF Symbol at 24 points, a Segoe glyph at 32 pixels);
    /// [`load_custom_icon`](crate::icons::load_custom_icon) takes a size.
    ///
    /// `None` where the set has no such icon, or without the set's feature
    /// (see the loaders in [`icons`]).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use native_theme::icons::IconRole;
    ///
    /// let sys = native_theme::SystemTheme::from_system()?;
    /// let fg = sys.pick(sys.mode).defaults.text_color;
    /// let _icon = sys.icon(IconRole::ActionCopy, sys.mode, Some(fg));
    /// # Ok::<(), native_theme::Error>(())
    /// ```
    #[must_use]
    pub fn icon<'a>(
        &self,
        id: impl Into<IconId<'a>>,
        mode: ColorMode,
        color: Option<Rgba>,
    ) -> Option<IconData> {
        self.with_icon_source(mode, |set, theme| {
            icons::load_icon_in(id.into(), set, theme, color, None)
        })
    }

    /// Load this theme's [`icon_set`](Self::icon_set) animated loading
    /// indicator for one colour mode's variant: a freedesktop spinner comes
    /// from [`icon_theme_for(mode)`](Self::icon_theme_for), as in
    /// [`icon`](Self::icon). See
    /// [`load_icon_indicator`].
    #[must_use]
    pub fn icon_indicator(&self, mode: ColorMode) -> Option<AnimatedIcon> {
        self.with_icon_source(mode, icons::load_icon_indicator)
    }

    /// Run `load` on the set and the freedesktop theme that [`icon`](Self::icon)
    /// and [`icon_indicator`](Self::icon_indicator) load from for `mode`.
    fn with_icon_source<'s, R>(
        &'s self,
        mode: ColorMode,
        load: impl FnOnce(IconSet, Option<&'s str>) -> R,
    ) -> R {
        load(self.icon_set, self.icon_theme_for(mode))
    }

    /// Apply an app-level TOML overlay and re-resolve.
    ///
    /// Merges the overlay onto the pre-resolve [`ThemeMode`] (not the
    /// already-resolved [`ResolvedTheme`]) so that changed source fields
    /// propagate correctly through `resolve()`. For example, changing
    /// `defaults.accent_color` in the overlay will cause `button.primary_background`,
    /// `checkbox.checked_background`, `slider.fill`, etc. to be re-derived from
    /// the new accent color where the theme does not state them: windows-11
    /// states its accent fills, `button.primary_background` among them, and
    /// those keep their values.
    ///
    /// Overlays stack: each call applies its overlay over the ones applied
    /// before it, so `sys.with_overlay(&a)?.with_overlay(&b)?` keeps what `a`
    /// states unless `b` states it too.
    ///
    /// The overlay's `icon_set`, `icon_theme` and `[layout]` apply too, by
    /// the precedence [`from_system()`](Self::from_system) uses: a variant's
    /// `defaults.icon_theme`, then the theme's `icon_theme`, then the
    /// detected one. So an overlay's top-level `icon_theme` loses to a
    /// variant's own, such as the KDE reader's `breeze` and `breeze-dark`;
    /// to replace those, state `[light.defaults] icon_theme` and
    /// `[dark.defaults] icon_theme`.
    ///
    /// The result's `light`, `dark`, `layout`, `icon_set` and `icon_theme`
    /// (with [`icon_theme_for`](Self::icon_theme_for)'s per-variant names)
    /// are rebuilt from the theme's sources and the overlays, not copied
    /// from `self`, so a change the caller made to one of those public fields
    /// is lost. `name`, `mode`, `preset`, `version_match` and `accessibility`
    /// are kept from `self`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ResolutionInvalid`] when a value the overlay states,
    /// or one that inherits it, is out of range. Each path is the TOML key,
    /// as [`Error::ResolutionIncomplete`] describes: a variant's value under
    /// its variant table (`light.button.min_height_px`), a `[layout]` value
    /// with no variant (`layout.widget_gap_px`).
    ///
    /// # Examples
    ///
    /// An overlay is a theme file that states only what it changes; it needs
    /// no `name`:
    ///
    /// ```
    /// let overlay = native_theme::Theme::from_toml(r##"
    ///     [light.defaults]
    ///     accent_color = "#ff6600"
    ///     [dark.defaults]
    ///     accent_color = "#ff6600"
    /// "##)?;
    /// # Ok::<(), native_theme::Error>(())
    /// ```
    ///
    /// Then apply it to the live OS theme:
    ///
    /// ```no_run
    /// # let overlay = native_theme::Theme::default();
    /// let system = native_theme::SystemTheme::from_system()?;
    /// let customized = system.with_overlay(&overlay)?;
    /// // customized.pick(customized.mode).defaults.accent_color is now #ff6600
    /// // and all accent-derived fields are updated
    /// # Ok::<(), native_theme::Error>(())
    /// ```
    pub fn with_overlay(&self, overlay: &Theme) -> crate::Result<Self> {
        let mut source = self.overlay_source.clone();
        // The earlier overlays go under this one
        source.overlay = Some(match source.overlay.take() {
            Some(mut stacked) => {
                stacked.merge(overlay);
                stacked
            }
            None => overlay.clone(),
        });
        Ok(SystemTheme {
            name: self.name.clone(),
            preset: self.preset.clone(),
            version_match: self.version_match,
            ..pipeline::replay(source, self.mode, self.accessibility.clone())?
        })
    }

    /// Load the OS theme synchronously.
    ///
    /// Detects the platform and desktop environment, reads the current theme
    /// settings, merges with a platform preset, and returns a fully resolved
    /// [`SystemTheme`] with both light and dark variants.
    ///
    /// The return value goes through the full pipeline: reader output ->
    /// resolve -> validate -> [`SystemTheme`] with both light and dark
    /// [`ResolvedTheme`] variants.
    ///
    /// On every platform `pollster::block_on` drives the async implementation
    /// that [`from_system_async`](Self::from_system_async) awaits.
    ///
    /// # Platform Behavior
    ///
    /// - **macOS:** with the `macos` feature, reads both light and dark
    ///   variants via NSAppearance and merges them with the preset of the
    ///   running version's generation, `macos-monterey` on 12 to 15 and
    ///   `macos-tahoe` on 26 and later ([`version_match`](Self::version_match)
    ///   reports macOS 27 as `PresetOlder`); the active mode comes from
    ///   [`detect::detect_is_dark`].
    /// - **Linux:** the async implementation handles portal D-Bus calls when
    ///   the `portal` feature is enabled. Without a reader for the detected
    ///   desktop (feature disabled, or a desktop with no reader), returns the
    ///   desktop's platform preset with no live reading (`kde-breeze` on KDE,
    ///   `adwaita` elsewhere, as [`pipeline::platform_preset_name`] names
    ///   it), resolved in the light/dark mode [`detect::detect_is_dark`]
    ///   reads.
    /// - **Windows:** with the `windows` feature, reads the active variant and
    ///   merges it with the `windows-11` preset, the one Windows generation
    ///   ([`version_match`](Self::version_match) reports Windows 10 as
    ///   `PresetNewer`).
    /// - **Other platforms:** Returns `Error::PlatformUnsupported`.
    ///
    /// # Errors
    ///
    /// - `Error::FeatureDisabled` on macOS or Windows when the `macos` /
    ///   `windows` feature is not enabled.
    /// - `Error::PlatformUnsupported` if the platform has no reader at all.
    /// - `Error::ReaderFailed` if the platform reader cannot access theme data.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let sys = native_theme::SystemTheme::from_system()?;
    /// let theme = sys.pick(sys.mode);
    /// // Icon set and theme are on SystemTheme, shared across variants
    /// let _icon_set = sys.icon_set;
    /// let _icon_theme = &sys.icon_theme;
    /// # Ok::<(), native_theme::error::Error>(())
    /// ```
    pub fn from_system() -> crate::Result<Self> {
        pollster::block_on(pipeline::from_system_inner())
    }

    /// Async version of [`from_system()`](Self::from_system).
    ///
    /// On Linux, this enables portal D-Bus calls (e.g. GNOME settings portal,
    /// KDE portal backend detection) via `.await`. On macOS and Windows, the
    /// future completes immediately -- no actual async operations occur.
    ///
    /// Returns a [`SystemTheme`] with both resolved light and dark variants,
    /// same as [`from_system()`](Self::from_system).
    pub async fn from_system_async() -> crate::Result<Self> {
        pipeline::from_system_inner().await
    }

    /// Load the OS theme over a chosen generation's preset: the platform
    /// reader runs as in [`from_system()`](Self::from_system), and its values
    /// merge over `preset` instead of the running version's generation.
    ///
    /// `preset` must be a generation of the running platform: `macos-monterey`
    /// or `macos-tahoe` on macOS, `windows-11` on Windows. Any other name, and
    /// any name on a platform without a generation table (Linux, whose readers
    /// read the installed style, BSD and other platforms), is
    /// `Error::UnknownPreset`, where [`from_system()`](Self::from_system) may
    /// return `Error::PlatformUnsupported` (BSD and other platforms).
    ///
    /// The result is a hybrid, not the older OS's look: the chosen
    /// generation's geometry and design colours under the running OS's
    /// colours, fonts and accessibility settings, which the reader reads live
    /// (on macOS 26 with `macos-monterey`, `NSColor` gives macOS 26's
    /// colours). [`version_match`](Self::version_match) reports the chosen
    /// generation against the running OS: `PresetOlder` for an older one,
    /// `PresetNewer` for a newer one.
    ///
    /// # Errors
    ///
    /// - `Error::UnknownPreset` for a name that is not a generation of the
    ///   running platform, and for any name on a platform without a
    ///   generation table.
    /// - `Error::FeatureDisabled` on macOS or Windows when the `macos` /
    ///   `windows` feature is not enabled.
    /// - `Error::ReaderFailed` if the platform reader cannot access theme data.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// // Keep the Monterey-to-Sequoia look on a Mac running macOS 26.
    /// let sys = native_theme::SystemTheme::from_system_with_preset("macos-monterey")?;
    /// assert_eq!(sys.preset, "macos-monterey");
    /// # Ok::<(), native_theme::error::Error>(())
    /// ```
    pub fn from_system_with_preset(preset: &str) -> crate::Result<Self> {
        pollster::block_on(pipeline::from_system_with_preset_inner(preset))
    }

    /// Async version of
    /// [`from_system_with_preset()`](Self::from_system_with_preset).
    pub async fn from_system_with_preset_async(preset: &str) -> crate::Result<Self> {
        pipeline::from_system_with_preset_inner(preset).await
    }
}

// =============================================================================
// Tests -- SystemTheme public API (active, pick, platform_preset_name)
// =============================================================================

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::field_reassign_with_default
)]
mod system_theme_tests {
    use super::*;

    // --- SystemTheme::active() / pick() tests ---

    #[test]
    fn test_system_theme_pick_dark_mode() {
        let preset = Theme::preset("catppuccin-mocha").unwrap();
        let mut light_v = preset.light.clone().unwrap();
        let mut dark_v = preset.dark.clone().unwrap();
        // Give them distinct accents so we can tell them apart
        // (test fixture values -- not production hardcoded colors)
        light_v.defaults.accent_color = Some(Rgba::rgb(0, 0, 255));
        dark_v.defaults.accent_color = Some(Rgba::rgb(255, 0, 0));
        light_v.resolve_all();
        dark_v.resolve_all();
        let light_resolved = light_v.validate().unwrap();
        let dark_resolved = dark_v.validate().unwrap();

        let st = SystemTheme {
            name: "test".into(),
            mode: ColorMode::Dark,
            light: light_resolved.clone(),
            dark: dark_resolved.clone(),
            overlay_source: OverlaySource {
                reader_output: ReaderOutput::Dual {
                    light: Box::new(ThemeMode::default()),
                    dark: Box::new(ThemeMode::default()),
                    is_dark: false,
                },
                name: Cow::Borrowed(""),
                icon_set: None,
                layout: LayoutTheme::default(),
                preset_name: "catppuccin-mocha".into(),
                context: crate::resolve::ResolutionContext::for_tests(),
                overlay: None,
            },
            preset: "catppuccin-mocha".into(),
            version_match: None,
            icon_set: IconSet::Lucide,
            icon_theme: Some("lucide".into()),
            light_icon_theme: None,
            dark_icon_theme: Some("lucide".into()),
            layout: LayoutTheme::default(),
            accessibility: AccessibilityPreferences::default(),
        };
        assert_eq!(
            st.pick(st.mode).defaults.accent_color,
            dark_resolved.defaults.accent_color
        );
    }

    #[test]
    fn test_system_theme_pick_light_mode() {
        let preset = Theme::preset("catppuccin-mocha").unwrap();
        let mut light_v = preset.light.clone().unwrap();
        let mut dark_v = preset.dark.clone().unwrap();
        light_v.defaults.accent_color = Some(Rgba::rgb(0, 0, 255));
        dark_v.defaults.accent_color = Some(Rgba::rgb(255, 0, 0));
        light_v.resolve_all();
        dark_v.resolve_all();
        let light_resolved = light_v.validate().unwrap();
        let dark_resolved = dark_v.validate().unwrap();

        let st = SystemTheme {
            name: "test".into(),
            mode: ColorMode::Light,
            light: light_resolved.clone(),
            dark: dark_resolved.clone(),
            overlay_source: OverlaySource {
                reader_output: ReaderOutput::Dual {
                    light: Box::new(ThemeMode::default()),
                    dark: Box::new(ThemeMode::default()),
                    is_dark: false,
                },
                name: Cow::Borrowed(""),
                icon_set: None,
                layout: LayoutTheme::default(),
                preset_name: "catppuccin-mocha".into(),
                context: crate::resolve::ResolutionContext::for_tests(),
                overlay: None,
            },
            preset: "catppuccin-mocha".into(),
            version_match: None,
            icon_set: IconSet::Lucide,
            icon_theme: Some("lucide".into()),
            light_icon_theme: Some("lucide".into()),
            dark_icon_theme: None,
            layout: LayoutTheme::default(),
            accessibility: AccessibilityPreferences::default(),
        };
        assert_eq!(
            st.pick(st.mode).defaults.accent_color,
            light_resolved.defaults.accent_color
        );
    }

    #[test]
    fn test_system_theme_pick_explicit() {
        let preset = Theme::preset("catppuccin-mocha").unwrap();
        let mut light_v = preset.light.clone().unwrap();
        let mut dark_v = preset.dark.clone().unwrap();
        light_v.defaults.accent_color = Some(Rgba::rgb(0, 0, 255));
        dark_v.defaults.accent_color = Some(Rgba::rgb(255, 0, 0));
        light_v.resolve_all();
        dark_v.resolve_all();
        let light_resolved = light_v.validate().unwrap();
        let dark_resolved = dark_v.validate().unwrap();

        let st = SystemTheme {
            name: "test".into(),
            mode: ColorMode::Light,
            light: light_resolved.clone(),
            dark: dark_resolved.clone(),
            overlay_source: OverlaySource {
                reader_output: ReaderOutput::Dual {
                    light: Box::new(ThemeMode::default()),
                    dark: Box::new(ThemeMode::default()),
                    is_dark: false,
                },
                name: Cow::Borrowed(""),
                icon_set: None,
                layout: LayoutTheme::default(),
                preset_name: "catppuccin-mocha".into(),
                context: crate::resolve::ResolutionContext::for_tests(),
                overlay: None,
            },
            preset: "catppuccin-mocha".into(),
            version_match: None,
            icon_set: IconSet::Lucide,
            icon_theme: Some("lucide".into()),
            light_icon_theme: Some("lucide".into()),
            dark_icon_theme: None,
            layout: LayoutTheme::default(),
            accessibility: AccessibilityPreferences::default(),
        };
        assert_eq!(
            st.pick(ColorMode::Dark).defaults.accent_color,
            dark_resolved.defaults.accent_color
        );
        assert_eq!(
            st.pick(ColorMode::Light).defaults.accent_color,
            light_resolved.defaults.accent_color
        );
    }

    // --- platform_preset_name() pure tests ---
    // Tests the same logic path (parse_linux_desktop -> linux_preset_for_de) without env var mocking.

    /// Prove that the sync `from_system()` API works without any async runtime.
    /// On Linux with KDE feature: exercises pollster::block_on(from_system_inner()).
    /// This acts as a compile-time and runtime gate that the sync path works.
    #[test]
    #[cfg(target_os = "linux")]
    #[cfg(feature = "kde")]
    fn sync_consumer_no_async_runtime() {
        // Call the actual from_system() entry point.
        // This exercises the pollster::block_on(pipeline::from_system_inner()) path.
        // We don't assert Ok because the test environment may lack KDE config files,
        // but the call must not panic and must return a Result (not hang or deadlock).
        let _result = SystemTheme::from_system();
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_platform_preset_name_kde() {
        let preset = pipeline::linux_preset_for_de(detect::parse_linux_desktop("KDE"));
        assert_eq!(preset.name, "kde-breeze");
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_platform_preset_name_gnome() {
        let preset = pipeline::linux_preset_for_de(detect::parse_linux_desktop("GNOME"));
        assert_eq!(preset.name, "adwaita");
    }

    /// §11.2: `from_system()` is an extraction of the reader path, so it must
    /// agree with `SystemTheme::from_system()` wherever both succeed, and it
    /// must never return a non-finite or non-positive text scale.
    #[test]
    fn accessibility_preferences_from_system_is_consistent_with_system_theme() {
        let prefs = AccessibilityPreferences::from_system();
        assert!(prefs.text_scaling_factor.is_finite());
        assert!(prefs.text_scaling_factor > 0.0);

        // CI has no desktop; only compare when the full pipeline also works.
        if let Ok(sys) = SystemTheme::from_system() {
            assert_eq!(
                prefs.text_scaling_factor,
                sys.accessibility.text_scaling_factor
            );
            assert_eq!(prefs.high_contrast, sys.accessibility.high_contrast);
            assert_eq!(
                prefs.reduce_transparency,
                sys.accessibility.reduce_transparency
            );
            assert_eq!(prefs.reduce_motion, sys.accessibility.reduce_motion);
        }
    }
}

// =============================================================================
// Tests -- with_overlay
// =============================================================================

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod overlay_tests {
    use super::*;

    /// Helper: build a SystemTheme from a preset via pipeline::run_pipeline.
    /// Uses test-only Result handling (module has #[allow(clippy::unwrap_used)]).
    fn default_system_theme() -> crate::Result<SystemTheme> {
        let preset = Theme::preset("catppuccin-mocha")?;
        let reader = ReaderResult {
            output: ReaderOutput::Dual {
                light: Box::new(preset.light.clone().unwrap_or_default()),
                dark: Box::new(preset.dark.clone().unwrap_or_default()),
                is_dark: false,
            },
            name: preset.name,
            icon_set: preset.icon_set,
            layout: preset.layout,
            font_dpi: None,
            accessibility: AccessibilityPreferences::default(),
        };
        pipeline::run_pipeline(reader, "catppuccin-mocha")
    }

    /// The range-violation paths of `result`'s error.
    fn violation_paths(result: crate::Result<SystemTheme>) -> Vec<String> {
        let Err(crate::Error::ResolutionInvalid { errors }) = result else {
            panic!("expected ResolutionInvalid, got {:?}", result.err());
        };
        errors.into_iter().map(|rv| rv.path).collect()
    }

    #[test]
    fn test_overlay_error_names_the_variant() -> crate::Result<()> {
        let st = default_system_theme()?;
        let overlay = Theme::from_toml("name = \"overlay\"\n[dark.button]\nmin_height_px = -1.0")?;
        assert_eq!(
            violation_paths(st.with_overlay(&overlay)),
            ["dark.button.min_height_px"]
        );
        Ok(())
    }

    #[test]
    fn test_pipeline_error_names_the_variant() -> crate::Result<()> {
        let preset = Theme::preset("catppuccin-mocha")?;
        let mut light = preset.light.clone().unwrap_or_default();
        light.button.min_height = Some(-1.0);
        let reader = ReaderResult {
            output: ReaderOutput::Dual {
                light: Box::new(light),
                dark: Box::new(preset.dark.clone().unwrap_or_default()),
                is_dark: true,
            },
            name: preset.name,
            icon_set: preset.icon_set,
            layout: preset.layout,
            font_dpi: None,
            accessibility: AccessibilityPreferences::default(),
        };
        assert_eq!(
            violation_paths(pipeline::run_pipeline(reader, "catppuccin-mocha",)),
            ["light.button.min_height_px"]
        );
        Ok(())
    }

    #[test]
    fn test_overlay_accent_propagates() -> crate::Result<()> {
        let st = default_system_theme()?;
        let new_accent = Rgba::rgb(255, 0, 0);

        // Build overlay with accent on both light and dark
        let mut overlay = Theme::default();
        let mut light_v = ThemeMode::default();
        light_v.defaults.accent_color = Some(new_accent);
        let mut dark_v = ThemeMode::default();
        dark_v.defaults.accent_color = Some(new_accent);
        overlay.light = Some(light_v);
        overlay.dark = Some(dark_v);

        let result = st.with_overlay(&overlay)?;

        // Accent itself
        assert_eq!(result.light.defaults.accent_color, new_accent);
        // Accent-derived widget fields
        assert_eq!(result.light.button.primary_background, new_accent);
        assert_eq!(result.light.checkbox.checked_background, new_accent);
        assert_eq!(result.light.slider.fill_color, new_accent);
        assert_eq!(result.light.progress_bar.fill_color, new_accent);
        assert_eq!(result.light.switch.checked_background, new_accent);
        // Additional accent-derived fields re-resolved via safety nets
        assert_eq!(
            result.light.spinner.fill_color, new_accent,
            "spinner.fill should re-derive from new accent"
        );
        Ok(())
    }

    #[test]
    fn test_overlay_preserves_unrelated_fields() -> crate::Result<()> {
        let st = default_system_theme()?;
        let original_bg = st.light.defaults.background_color;

        // Apply overlay changing only accent
        let mut overlay = Theme::default();
        let mut light_v = ThemeMode::default();
        light_v.defaults.accent_color = Some(Rgba::rgb(255, 0, 0));
        overlay.light = Some(light_v);

        let result = st.with_overlay(&overlay)?;
        assert_eq!(
            result.light.defaults.background_color, original_bg,
            "background should be unchanged"
        );
        Ok(())
    }

    #[test]
    fn test_overlay_empty_noop() -> crate::Result<()> {
        let st = default_system_theme()?;
        let original_light_accent = st.light.defaults.accent_color;
        let original_dark_accent = st.dark.defaults.accent_color;
        let original_light_bg = st.light.defaults.background_color;

        // Empty overlay
        let overlay = Theme::default();
        let result = st.with_overlay(&overlay)?;

        assert_eq!(result.light.defaults.accent_color, original_light_accent);
        assert_eq!(result.dark.defaults.accent_color, original_dark_accent);
        assert_eq!(result.light.defaults.background_color, original_light_bg);
        Ok(())
    }

    #[test]
    fn test_overlay_both_variants() -> crate::Result<()> {
        let st = default_system_theme()?;
        let red = Rgba::rgb(255, 0, 0);
        let green = Rgba::rgb(0, 255, 0);

        let mut overlay = Theme::default();
        let mut light_v = ThemeMode::default();
        light_v.defaults.accent_color = Some(red);
        let mut dark_v = ThemeMode::default();
        dark_v.defaults.accent_color = Some(green);
        overlay.light = Some(light_v);
        overlay.dark = Some(dark_v);

        let result = st.with_overlay(&overlay)?;
        assert_eq!(
            result.light.defaults.accent_color, red,
            "light accent = red"
        );
        assert_eq!(
            result.dark.defaults.accent_color, green,
            "dark accent = green"
        );
        Ok(())
    }

    #[test]
    fn test_overlay_font_family() -> crate::Result<()> {
        let st = default_system_theme()?;

        let mut overlay = Theme::default();
        let mut light_v = ThemeMode::default();
        light_v.defaults.font.family = Some("Comic Sans".into());
        overlay.light = Some(light_v);

        let result = st.with_overlay(&overlay)?;
        assert_eq!(result.light.defaults.font.family.as_ref(), "Comic Sans");
        Ok(())
    }

    #[test]
    fn test_overlay_roundtrip_via_overlay_source() -> crate::Result<()> {
        let st = default_system_theme()?;
        // Apply overlay and verify accent propagates
        let new_accent = Rgba::rgb(255, 0, 0);
        let mut overlay = Theme::default();
        let mut light_v = ThemeMode::default();
        light_v.defaults.accent_color = Some(new_accent);
        overlay.light = Some(light_v);

        let result = st.with_overlay(&overlay)?;
        assert_eq!(result.light.defaults.accent_color, new_accent);
        // The dark variant should be unchanged from original
        assert_eq!(
            result.dark.defaults.accent_color,
            st.dark.defaults.accent_color
        );
        Ok(())
    }

    #[test]
    fn test_overlay_source_no_variant_fields() -> crate::Result<()> {
        // Verify overlay_source exists on SystemTheme (compile-time structural check).
        // If light_variant or dark_variant fields still existed, this test would
        // need updating -- documenting the structural change.
        let st = default_system_theme()?;
        let _ = &st.overlay_source; // overlay_source exists
        Ok(())
    }
}

// =============================================================================
// Tests -- AccessibilityPreferences text scaling
// =============================================================================

#[cfg(test)]
mod accessibility_tests {
    use super::*;

    fn prefs(text_scaling_factor: f32) -> AccessibilityPreferences {
        AccessibilityPreferences {
            text_scaling_factor,
            ..AccessibilityPreferences::default()
        }
    }

    #[test]
    fn text_scale_factor_keeps_a_finite_positive_factor() {
        assert_eq!(prefs(2.0).text_scale_factor(), 2.0);
        assert_eq!(prefs(2.0).scaled(10.0), 20.0);
        assert_eq!(AccessibilityPreferences::default().text_scale_factor(), 1.0);
    }

    #[test]
    fn text_scale_factor_ignores_a_factor_that_is_not_finite_and_positive() {
        for f in [f32::NAN, 0.0, -0.0, -1.0, f32::INFINITY, f32::NEG_INFINITY] {
            assert_eq!(prefs(f).text_scale_factor(), 1.0, "{f}");
            assert_eq!(prefs(f).scaled(10.0), 10.0, "{f}");
        }
    }

    #[test]
    fn scaled_keeps_a_size_whose_product_is_not_a_normal_positive_number() {
        // A finite factor whose product overflows f32, one whose product is
        // subnormal, and one whose product underflows to zero.
        assert_eq!(prefs(f32::MAX).scaled(13.0), 13.0);
        assert_eq!(prefs(3e37).scaled(13.0), 13.0);
        assert!((13.0 * 1e-45_f32).is_subnormal());
        assert_eq!(prefs(1e-45).scaled(13.0), 13.0);
        assert_eq!(
            prefs(f32::MIN_POSITIVE).scaled(f32::MIN_POSITIVE),
            f32::MIN_POSITIVE
        );
        // A product that stays finite and positive is scaled, however large.
        assert_eq!(prefs(2e37).scaled(13.0), 13.0 * 2e37);
    }
}