1
1
import 'package:macos_ui/macos_ui.dart' ;
2
+ import 'package:macos_ui/src/enums/accent_color.dart' ;
2
3
import 'package:macos_ui/src/library.dart' ;
3
4
4
5
const Duration _kExpand = Duration (milliseconds: 200 );
@@ -79,7 +80,8 @@ class SidebarItems extends StatelessWidget {
79
80
80
81
/// The color to paint the item when it's selected.
81
82
///
82
- /// If null, [MacosThemeData.primaryColor] is used.
83
+ /// If null, the color is chosen automatically based on the user’s selected
84
+ /// system accent color and whether the sidebar is in the main window.
83
85
final Color ? selectedColor;
84
86
85
87
/// The color to paint the item when it's unselected.
@@ -97,6 +99,21 @@ class SidebarItems extends StatelessWidget {
97
99
/// Defaults to [SystemMouseCursors.basic] .
98
100
final MouseCursor ? cursor;
99
101
102
+ /// The user’s selected system accent color.
103
+ AccentColor get _accentColor =>
104
+ AccentColorListener .instance.currentAccentColor ?? AccentColor .blue;
105
+
106
+ /// Returns the sidebar item’s selected color.
107
+ Color _getColor (BuildContext context) {
108
+ final isMainWindow = WindowMainStateListener .instance.isMainWindow;
109
+
110
+ return _ColorProvider .getSelectedColor (
111
+ accentColor: _accentColor,
112
+ isDarkModeEnabled: MacosTheme .of (context).brightness.isDark,
113
+ isWindowMain: isMainWindow,
114
+ );
115
+ }
116
+
100
117
List <SidebarItem > get _allItems {
101
118
List <SidebarItem > result = [];
102
119
for (var element in items) {
@@ -117,39 +134,50 @@ class SidebarItems extends StatelessWidget {
117
134
final theme = MacosTheme .of (context);
118
135
return MacosIconTheme .merge (
119
136
data: const MacosIconThemeData (size: 20 ),
120
- child: _SidebarItemsConfiguration (
121
- selectedColor: selectedColor ?? theme.primaryColor,
122
- unselectedColor: unselectedColor ?? MacosColors .transparent,
123
- shape: shape ?? _defaultShape,
124
- itemSize: itemSize,
125
- child: ListView (
126
- controller: scrollController,
127
- physics: const ClampingScrollPhysics (),
128
- padding: EdgeInsets .all (10.0 - theme.visualDensity.horizontal),
129
- children: List .generate (items.length, (index) {
130
- final item = items[index];
131
- if (item.disclosureItems != null ) {
132
- return MouseRegion (
133
- cursor: cursor! ,
134
- child: _DisclosureSidebarItem (
135
- item: item,
136
- selectedItem: _allItems[currentIndex],
137
- onChanged: (item) {
138
- onChanged (_allItems.indexOf (item));
139
- },
137
+ child: StreamBuilder (
138
+ stream: AccentColorListener .instance.onChanged,
139
+ builder: (context, _) {
140
+ return StreamBuilder <bool >(
141
+ stream: WindowMainStateListener .instance.onChanged,
142
+ builder: (context, _) {
143
+ return _SidebarItemsConfiguration (
144
+ selectedColor: selectedColor ?? _getColor (context),
145
+ unselectedColor: unselectedColor ?? MacosColors .transparent,
146
+ shape: shape ?? _defaultShape,
147
+ itemSize: itemSize,
148
+ child: ListView (
149
+ controller: scrollController,
150
+ physics: const ClampingScrollPhysics (),
151
+ padding:
152
+ EdgeInsets .all (10.0 - theme.visualDensity.horizontal),
153
+ children: List .generate (items.length, (index) {
154
+ final item = items[index];
155
+ if (item.disclosureItems != null ) {
156
+ return MouseRegion (
157
+ cursor: cursor! ,
158
+ child: _DisclosureSidebarItem (
159
+ item: item,
160
+ selectedItem: _allItems[currentIndex],
161
+ onChanged: (item) {
162
+ onChanged (_allItems.indexOf (item));
163
+ },
164
+ ),
165
+ );
166
+ }
167
+ return MouseRegion (
168
+ cursor: cursor! ,
169
+ child: _SidebarItem (
170
+ item: item,
171
+ selected: _allItems[currentIndex] == item,
172
+ onClick: () => onChanged (_allItems.indexOf (item)),
173
+ ),
174
+ );
175
+ }),
140
176
),
141
177
);
142
- }
143
- return MouseRegion (
144
- cursor: cursor! ,
145
- child: _SidebarItem (
146
- item: item,
147
- selected: _allItems[currentIndex] == item,
148
- onClick: () => onChanged (_allItems.indexOf (item)),
149
- ),
150
- );
151
- }),
152
- ),
178
+ },
179
+ );
180
+ },
153
181
),
154
182
);
155
183
}
@@ -497,3 +525,74 @@ class __DisclosureSidebarItemState extends State<_DisclosureSidebarItem>
497
525
);
498
526
}
499
527
}
528
+
529
+ class _ColorProvider {
530
+ /// Returns the selected color based on the provided parameters.
531
+ static Color getSelectedColor ({
532
+ required AccentColor accentColor,
533
+ required bool isDarkModeEnabled,
534
+ required bool isWindowMain,
535
+ }) {
536
+ if (isDarkModeEnabled) {
537
+ if (! isWindowMain) {
538
+ return const MacosColor .fromRGBO (76 , 78 , 65 , 1.0 );
539
+ }
540
+
541
+ switch (accentColor) {
542
+ case AccentColor .blue:
543
+ return const MacosColor .fromRGBO (22 , 105 , 229 , 0.749 );
544
+
545
+ case AccentColor .purple:
546
+ return const MacosColor .fromRGBO (204 , 45 , 202 , 0.749 );
547
+
548
+ case AccentColor .pink:
549
+ return const MacosColor .fromRGBO (229 , 74 , 145 , 0.749 );
550
+
551
+ case AccentColor .red:
552
+ return const MacosColor .fromRGBO (238 , 64 , 68 , 0.749 );
553
+
554
+ case AccentColor .orange:
555
+ return const MacosColor .fromRGBO (244 , 114 , 0 , 0.749 );
556
+
557
+ case AccentColor .yellow:
558
+ return const MacosColor .fromRGBO (233 , 176 , 0 , 0.749 );
559
+
560
+ case AccentColor .green:
561
+ return const MacosColor .fromRGBO (76 , 177 , 45 , 0.749 );
562
+
563
+ case AccentColor .graphite:
564
+ return const MacosColor .fromRGBO (129 , 129 , 122 , 0.824 );
565
+ }
566
+ }
567
+
568
+ if (! isWindowMain) {
569
+ return const MacosColor .fromRGBO (213 , 213 , 208 , 1.0 );
570
+ }
571
+
572
+ switch (accentColor) {
573
+ case AccentColor .blue:
574
+ return const MacosColor .fromRGBO (9 , 129 , 255 , 0.749 );
575
+
576
+ case AccentColor .purple:
577
+ return const MacosColor .fromRGBO (162 , 28 , 165 , 0.749 );
578
+
579
+ case AccentColor .pink:
580
+ return const MacosColor .fromRGBO (234 , 81 , 152 , 0.749 );
581
+
582
+ case AccentColor .red:
583
+ return const MacosColor .fromRGBO (220 , 32 , 40 , 0.749 );
584
+
585
+ case AccentColor .orange:
586
+ return const MacosColor .fromRGBO (245 , 113 , 0 , 0.749 );
587
+
588
+ case AccentColor .yellow:
589
+ return const MacosColor .fromRGBO (240 , 180 , 2 , 0.749 );
590
+
591
+ case AccentColor .green:
592
+ return const MacosColor .fromRGBO (66 , 174 , 33 , 0.749 );
593
+
594
+ case AccentColor .graphite:
595
+ return const MacosColor .fromRGBO (174 , 174 , 167 , 0.847 );
596
+ }
597
+ }
598
+ }
0 commit comments