Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec4a63b

Browse files
Fix incorrect sidebar and sidebar item color (#484)
1 parent b3a8e53 commit ec4a63b

File tree

8 files changed

+251
-135
lines changed

8 files changed

+251
-135
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
## [2.0.3]
2+
### 🛠️ Fixed 🛠️
3+
* Fixed a bug that caused the sidebar to appear darker than intended.
4+
5+
### 🔄 Updated 🔄
6+
* `SidebarItems` has now respects the user’s selected accent color and mimics the look of macOS’ sidebar items more closely.
7+
18
## [2.0.2]
29
### 🛠️ Fixed 🛠️
310
* Fixed images in generated documentation.
411

512
## [2.0.1]
613
### 🔄 Updated 🔄
714
* `PushButton` has received a facelift. It now mimics the look and feel of native macOS buttons more closely.
8-
* **Note:** As a result, its `pressedOpacity` property and the `PushButtonTheme` class have been deprecated.
15+
* **Note:** As a result, its `pressedOpacity` property and the `PushButtonTheme` class have been deprecated.
916

1017
## [2.0.0]
1118
### 🚨 Breaking Changes 🚨

example/macos/Runner.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
isa = PBXProject;
203203
attributes = {
204204
LastSwiftUpdateCheck = 0920;
205-
LastUpgradeCheck = 1300;
205+
LastUpgradeCheck = 1430;
206206
ORGANIZATIONNAME = "";
207207
TargetAttributes = {
208208
33CC10EC2044A3C60003C045 = {

example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1300"
3+
LastUpgradeVersion = "1430"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

lib/src/layout/sidebar/sidebar_item.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SidebarItem with Diagnosticable {
4343
final Color? unselectedColor;
4444

4545
/// The [shape] property specifies the outline (border) of the
46-
/// decoration. The shape must not be null. It's used alonside
46+
/// decoration. The shape must not be null. It's used alongside
4747
/// [selectedColor].
4848
final ShapeBorder? shape;
4949

lib/src/layout/sidebar/sidebar_items.dart

+131-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:macos_ui/macos_ui.dart';
2+
import 'package:macos_ui/src/enums/accent_color.dart';
23
import 'package:macos_ui/src/library.dart';
34

45
const Duration _kExpand = Duration(milliseconds: 200);
@@ -79,7 +80,8 @@ class SidebarItems extends StatelessWidget {
7980

8081
/// The color to paint the item when it's selected.
8182
///
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.
8385
final Color? selectedColor;
8486

8587
/// The color to paint the item when it's unselected.
@@ -97,6 +99,21 @@ class SidebarItems extends StatelessWidget {
9799
/// Defaults to [SystemMouseCursors.basic].
98100
final MouseCursor? cursor;
99101

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+
100117
List<SidebarItem> get _allItems {
101118
List<SidebarItem> result = [];
102119
for (var element in items) {
@@ -117,39 +134,50 @@ class SidebarItems extends StatelessWidget {
117134
final theme = MacosTheme.of(context);
118135
return MacosIconTheme.merge(
119136
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+
}),
140176
),
141177
);
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+
},
153181
),
154182
);
155183
}
@@ -497,3 +525,74 @@ class __DisclosureSidebarItemState extends State<_DisclosureSidebarItem>
497525
);
498526
}
499527
}
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

Comments
 (0)