Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make CapacityIndicator work with other values of splits, not only splits:10 #305

Merged
merged 18 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Other changes
* Update `MacosScrollBar.thumbVisibility` with the latest change introduced in Flutter 3.7
* Update `README.md` to address issues [#325](https://github.com/GroovinChip/macos_ui/issues/325) & [#332](https://github.com/GroovinChip/macos_ui/issues/332)

* Fixed a bug where `CapacityIndicator` only worked correctly for splits = 10


## [1.7.6]
* Fixed a bug where `MacosPopupButton` would report that a `ScrollController` was not attached to any views
Expand Down
1 change: 1 addition & 0 deletions example/lib/pages/indicators_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class _IndicatorsPageState extends State<IndicatorsPage> {
CapacityIndicator(
value: sliderValue,
onChanged: (v) => setState(() => sliderValue = v),
splits: 20,
discrete: true,
),
const SizedBox(height: 20),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/indicators/capacity_indicators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CapacityIndicator extends StatelessWidget {
}

void _handleUpdate(Offset lp, double width) {
double value = (lp.dx / width) * splits;
double value = (lp.dx / width) * 100 / splits;
onChanged?.call(value.clamp(0.0, 100.0));
}

Expand All @@ -108,7 +108,7 @@ class CapacityIndicator extends StatelessWidget {
if (width.isInfinite) width = 100;
final splitWidth = width / splits;
if (discrete) {
final fillToIndex = value / splits - 1;
final fillToIndex = (value / 100) * splits - 1;
return SizedBox(
width: width,
child: GestureDetector(
Expand Down
164 changes: 164 additions & 0 deletions test/indicators/capacity_indicators_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_test/flutter_test.dart';

import 'package:macos_ui/macos_ui.dart';

import '../mock_canvas.dart';

void main() {
testWidgets('debugFillProperties', (tester) async {
final builder = DiagnosticPropertiesBuilder();
Expand All @@ -27,4 +32,163 @@ void main() {
],
);
});

testWidgets('debugFillProperties with discrete splits = 20', (tester) async {
final builder = DiagnosticPropertiesBuilder();
const CapacityIndicator(
value: 50,
splits: 20,
discrete: true,
).debugFillProperties(builder);

final description = builder.properties
.where((node) => !node.isFiltered(DiagnosticLevel.info))
.map((node) => node.toString())
.toList();

expect(
description,
[
'value: 50.0',
'splits: 20',
'color: systemGreen(*color = Color(0xff34c759)*, darkColor = Color(0xff30d158), highContrastColor = Color(0xff248a3d), darkHighContrastColor = Color(0xff30db5b), resolved by: UNRESOLVED)',
'backgroundColor: tertiarySystemGroupedBackground(*color = Color(0xfff2f2f7)*, darkColor = Color(0xff2c2c2e), highContrastColor = Color(0xffebebf0), darkHighContrastColor = Color(0xff363638), *elevatedColor = Color(0xfff2f2f7)*, darkElevatedColor = Color(0xff3a3a3c), highContrastElevatedColor = Color(0xffebebf0), darkHighContrastElevatedColor = Color(0xff444446), resolved by: UNRESOLVED)',
'borderColor: tertiaryLabel(*color = Color(0x4c3c3c43)*, darkColor = Color(0x4cebebf5), highContrastColor = Color(0x603c3c43), darkHighContrastColor = Color(0x60ebebf5), resolved by: UNRESOLVED)',
'semanticLabel: null',
],
);
});

testWidgets(
'CapacityIndicator paints the correct number of segments',
(WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox(
width: 200.0,
child: CapacityIndicator(
value: 50,
splits: 20,
discrete: true,
),
),
),
),
);

expect(
find.byType(CapacityIndicator),
// each discrete segment is drawn 3 times, two times with fill, last time with stroke
paintsExactlyCountTimes(#drawRRect, 20 * 3),
);
},
);

testWidgets(
'CapacityIndicator paints two filled segments for value=10 and 20 segments',
(WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox(
width: 200.0,
child: CapacityIndicator(
value: 10,
splits: 20,
discrete: true,
),
),
),
),
);

expect(
find.byType(CapacityIndicator),
// each discrete segment is drawn 3 times, background - fill - stroke
// a filled segment is drawn by fromLTRBR with LTRB=0,0,8,16
// an empty segment is drawnby fromLTRBAndCorners with LTRB=0,0,0,16
paints
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..translate(x: 10.0, y: 0.0)
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..translate(x: 20.0, y: 0.0)
..rrect(
rrect: RRect.fromLTRBR(
0.0,
0.0,
8.0,
16.0,
const Radius.circular(2.0),
),
)
..rrect(
rrect: RRect.fromLTRBAndCorners(
0.0,
0.0,
0.0,
16.0,
topLeft: const Radius.circular(2.0),
topRight: const Radius.circular(0.0),
bottomRight: const Radius.circular(0.0),
bottomLeft: const Radius.circular(2.0),
),
),
);
},
);
}
Loading