@@ -160,6 +160,10 @@ class _BranchSidebarState extends State<BranchSidebar> {
160160
161161 bool _localExpanded = true ;
162162 bool _remoteExpanded = true ;
163+
164+ /// Folder and remote nodes the user collapsed, keyed by
165+ /// "local:feature", "remote:origin" or "remote:origin/feature".
166+ final Set <String > _collapsed = {};
163167 bool _isSearching = false ;
164168 final TextEditingController _searchController = TextEditingController ();
165169 String _searchQuery = '' ;
@@ -670,6 +674,48 @@ class _BranchSidebarState extends State<BranchSidebar> {
670674 return null ;
671675 }
672676
677+ /// Folders stay open while searching, so every match is visible.
678+ bool _isExpanded (String key) =>
679+ _searchQuery.trim ().isNotEmpty || ! _collapsed.contains (key);
680+
681+ void _toggleFolder (String key) => setState (() {
682+ if (! _collapsed.remove (key)) _collapsed.add (key);
683+ });
684+
685+ /// Rows for [folder] : its sub-folders first, then its branches, each
686+ /// indented by [depth] . [key] prefixes the folder keys in [_collapsed] .
687+ List <Widget > _treeRows (
688+ _BranchFolder folder, {
689+ required String key,
690+ required int depth,
691+ }) {
692+ final names = folder.folders.keys.toList ()
693+ ..sort ((a, b) => a.toLowerCase ().compareTo (b.toLowerCase ()));
694+ return [
695+ for (final name in names) ...[
696+ _FolderRow (
697+ name: name,
698+ icon: Icons .folder_outlined,
699+ depth: depth,
700+ count: folder.folders[name]! .branchCount,
701+ expanded: _isExpanded ('$key $name ' ),
702+ onToggle: () => _toggleFolder ('$key $name ' ),
703+ ),
704+ if (_isExpanded ('$key $name ' ))
705+ ..._treeRows (
706+ folder.folders[name]! ,
707+ key: '$key $name /' ,
708+ depth: depth + 1 ,
709+ ),
710+ ],
711+ for (final branch in folder.branches)
712+ Padding (
713+ padding: EdgeInsetsDirectional .only (start: depth * _treeIndent),
714+ child: _buildBranchRow (branch, label: branch.name.split ('/' ).last),
715+ ),
716+ ];
717+ }
718+
673719 Widget _buildBranchRow (BranchEntry branch, {String ? label}) {
674720 return _BranchTile (
675721 branch: branch,
@@ -703,23 +749,16 @@ class _BranchSidebarState extends State<BranchSidebar> {
703749 _sort,
704750 );
705751
706- // Remote branches that a local branch already stands for are shown on
707- // that local row (cloud + ahead/behind), so only the rest are listed,
708- // grouped by remote ("origin/main" -> "origin").
709- final represented = {
710- for (final branch in widget.branches)
711- if (! branch.isRemote && branch.remoteCounterpart != null )
712- branch.remoteCounterpart! ,
713- };
752+ // Every remote branch, grouped by remote ("origin/main" -> "origin").
714753 final remotesByName = < String , List <BranchEntry >> {};
715754 for (final branch in remote) {
716- if (represented.contains (branch.name) || branch.name.endsWith ('/HEAD' )) {
717- continue ;
718- }
755+ if (branch.name.endsWith ('/HEAD' )) continue ;
719756 final slash = branch.name.indexOf ('/' );
720757 final remoteName = slash == - 1 ? '' : branch.name.substring (0 , slash);
721758 remotesByName.putIfAbsent (remoteName, () => []).add (branch);
722759 }
760+ final remoteCount =
761+ remotesByName.values.fold <int >(0 , (sum, list) => sum + list.length);
723762
724763 final watching = widget.watchIntervalMinutes != null ;
725764
@@ -817,35 +856,44 @@ class _BranchSidebarState extends State<BranchSidebar> {
817856 ),
818857 ),
819858 if (_localExpanded)
820- for (final branch in local) _buildBranchRow (branch),
859+ ..._treeRows (
860+ _BranchFolder .of (local, (branch) => branch.name),
861+ key: 'local:' ,
862+ depth: 0 ,
863+ ),
821864 if (remotesByName.isNotEmpty) ...[
822865 Padding (
823866 padding: const EdgeInsets .fromLTRB (8 , 12 , 8 , 12 ),
824867 child: Divider (height: 1 , thickness: 1 , color: b.line),
825868 ),
826- for (final entry in remotesByName.entries) ...[
827- _SectionHeader (
828- title: translate ('branches_only_on_remote' )
829- .replaceAll ('{remote}' , entry.key),
830- icon: Icons .cloud_outlined,
831- count: entry.value.length,
832- expanded: _remoteExpanded,
833- onToggle: () =>
834- setState (() => _remoteExpanded = ! _remoteExpanded),
835- ),
836- if (_remoteExpanded)
837- _RemoteOnlyBox (
838- children: [
839- for (final branch in entry.value)
840- _buildBranchRow (
841- branch,
842- label: _shortRemoteName (branch.name),
843- ),
844- ],
869+ _SectionHeader (
870+ title: translate ('branches_remote' ),
871+ icon: Icons .cloud_outlined,
872+ count: remoteCount,
873+ expanded: _remoteExpanded,
874+ onToggle: () =>
875+ setState (() => _remoteExpanded = ! _remoteExpanded),
876+ ),
877+ if (_remoteExpanded)
878+ for (final entry in remotesByName.entries) ...[
879+ _FolderRow (
880+ name: entry.key,
881+ icon: Icons .dns_outlined,
882+ depth: 0 ,
883+ count: entry.value.length,
884+ expanded: _isExpanded ('remote:${entry .key }' ),
885+ onToggle: () => _toggleFolder ('remote:${entry .key }' ),
845886 ),
846- if (entry.key != remotesByName.keys.last)
847- const SizedBox (height: 16 ),
848- ],
887+ if (_isExpanded ('remote:${entry .key }' ))
888+ ..._treeRows (
889+ _BranchFolder .of (
890+ entry.value,
891+ (branch) => _shortRemoteName (branch.name),
892+ ),
893+ key: 'remote:${entry .key }/' ,
894+ depth: 1 ,
895+ ),
896+ ],
849897 ],
850898 ],
851899 ),
@@ -1061,16 +1109,8 @@ class _BranchTile extends StatelessWidget {
10611109 final b = context.branchi;
10621110 final current = branch.isHead;
10631111 final base = branch.isRemote ? null : baseLabel;
1064- final name = Text .rich (
1065- TextSpan (children: [
1066- // Remote rows always say where they live.
1067- if (branch.isRemote && label != branch.name)
1068- TextSpan (
1069- text: branch.name.substring (0 , branch.name.length - label.length),
1070- style: TextStyle (color: b.off),
1071- ),
1072- TextSpan (text: label),
1073- ]),
1112+ final name = Text (
1113+ label,
10741114 maxLines: 1 ,
10751115 overflow: TextOverflow .ellipsis,
10761116 style: TextStyle (
@@ -1289,63 +1329,102 @@ class _SyncState extends StatelessWidget {
12891329 }
12901330}
12911331
1292- /// Dashed box around branches that exist only on a remote, so they can't
1293- /// be mistaken for local ones.
1294- class _RemoteOnlyBox extends StatelessWidget {
1295- const _RemoteOnlyBox ({required this .children});
1332+ /// How far each tree level is indented.
1333+ const double _treeIndent = 14 ;
1334+
1335+ /// Branches grouped by their "/"-separated name parts: "feature/login"
1336+ /// lands in folder "feature".
1337+ class _BranchFolder {
1338+ final Map <String , _BranchFolder > folders = {};
1339+ final List <BranchEntry > branches = [];
1340+
1341+ /// Builds the tree from [branches] (kept in their order), placed by the
1342+ /// path [pathOf] gives each one.
1343+ static _BranchFolder of (
1344+ List <BranchEntry > branches,
1345+ String Function (BranchEntry ) pathOf,
1346+ ) {
1347+ final root = _BranchFolder ();
1348+ for (final branch in branches) {
1349+ final parts = pathOf (branch).split ('/' );
1350+ var node = root;
1351+ for (final part in parts.take (parts.length - 1 )) {
1352+ if (part.isEmpty) continue ;
1353+ node = node.folders.putIfAbsent (part, _BranchFolder .new );
1354+ }
1355+ node.branches.add (branch);
1356+ }
1357+ return root;
1358+ }
1359+
1360+ int get branchCount => folders.values
1361+ .fold (branches.length, (sum, folder) => sum + folder.branchCount);
1362+ }
1363+
1364+ /// A collapsible folder (or remote) row in the branch tree.
1365+ class _FolderRow extends StatelessWidget {
1366+ const _FolderRow ({
1367+ required this .name,
1368+ required this .icon,
1369+ required this .depth,
1370+ required this .count,
1371+ required this .expanded,
1372+ required this .onToggle,
1373+ });
12961374
1297- final List <Widget > children;
1375+ final String name;
1376+ final IconData icon;
1377+ final int depth;
1378+ final int count;
1379+ final bool expanded;
1380+ final VoidCallback onToggle;
12981381
12991382 @override
13001383 Widget build (BuildContext context) {
13011384 final b = context.branchi;
1302- return CustomPaint (
1303- painter: _DashedBorderPainter (color: b.off),
1304- child: Padding (
1305- padding: const EdgeInsets .fromLTRB (2 , 6 , 2 , 6 ),
1306- child: Column (
1307- crossAxisAlignment: CrossAxisAlignment .stretch,
1308- children: [
1309- ...children,
1310- Padding (
1311- padding: const EdgeInsets .fromLTRB (8 , 2 , 8 , 2 ),
1312- child: Text (
1313- translate ('branches_remote_hint' ),
1314- style: TextStyle (fontSize: 11 , color: b.muted),
1315- ),
1385+ return Padding (
1386+ padding: EdgeInsetsDirectional .only (
1387+ start: depth * _treeIndent,
1388+ bottom: 2 ,
1389+ ),
1390+ child: Material (
1391+ color: Colors .transparent,
1392+ borderRadius: BorderRadius .circular (6 ),
1393+ child: InkWell (
1394+ onTap: onToggle,
1395+ borderRadius: BorderRadius .circular (6 ),
1396+ hoverColor: b.hover,
1397+ splashFactory: NoSplash .splashFactory,
1398+ child: Padding (
1399+ padding: const EdgeInsetsDirectional .fromSTEB (2 , 5 , 8 , 5 ),
1400+ child: Row (
1401+ children: [
1402+ Icon (
1403+ expanded
1404+ ? Icons .keyboard_arrow_down
1405+ : (Directionality .of (context) == TextDirection .rtl
1406+ ? Icons .keyboard_arrow_left
1407+ : Icons .keyboard_arrow_right),
1408+ size: 14 ,
1409+ color: b.muted,
1410+ ),
1411+ const SizedBox (width: 2 ),
1412+ Icon (icon, size: 13 , color: b.muted),
1413+ const SizedBox (width: 6 ),
1414+ Expanded (
1415+ child: Text (
1416+ name,
1417+ maxLines: 1 ,
1418+ overflow: TextOverflow .ellipsis,
1419+ style: TextStyle (fontSize: 13 , color: b.muted),
1420+ ),
1421+ ),
1422+ Text ('$count ' , style: TextStyle (fontSize: 11 , color: b.off)),
1423+ ],
13161424 ),
1317- ] ,
1425+ ) ,
13181426 ),
13191427 ),
13201428 );
13211429 }
13221430}
1323-
1324- class _DashedBorderPainter extends CustomPainter {
1325- _DashedBorderPainter ({required this .color});
1326-
1327- final Color color;
1328-
1329- @override
1330- void paint (Canvas canvas, Size size) {
1331- final paint = Paint ()
1332- ..color = color
1333- ..style = PaintingStyle .stroke
1334- ..strokeWidth = 1 ;
1335- final path = Path ()
1336- ..addRRect (
1337- RRect .fromRectAndRadius (
1338- Offset .zero & size,
1339- const Radius .circular (8 ),
1340- ).deflate (0.5 ),
1341- );
1342- for (final metric in path.computeMetrics ()) {
1343- for (var d = 0.0 ; d < metric.length; d += 7 ) {
1344- canvas.drawPath (metric.extractPath (d, d + 4 ), paint);
1345- }
1346- }
1347- }
1348-
1349- @override
1350- bool shouldRepaint (_DashedBorderPainter old) => old.color != color;
1351- }
0 commit comments