-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtab_strip.dart
150 lines (140 loc) · 4.19 KB
/
tab_strip.dart
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
import 'package:files/backend/utils.dart';
import 'package:files/backend/workspace.dart';
import 'package:files/widgets/context_menu.dart';
import 'package:flutter/material.dart';
class TabStrip extends StatelessWidget {
final List<WorkspaceController> tabs;
final int selectedTab;
final bool allowClosing;
final ValueChanged<int>? onTabChanged;
final ValueChanged<int>? onTabClosed;
final VoidCallback? onNewTab;
const TabStrip({
required this.tabs,
required this.selectedTab,
this.allowClosing = true,
this.onTabChanged,
this.onTabClosed,
this.onNewTab,
super.key,
});
@override
Widget build(BuildContext context) {
return SizedBox.expand(
child: Row(
children: [
Expanded(
child: ListView.separated(
itemBuilder: (context, index) => ContextMenu(
openOnLongPress: false,
entries: [
ContextMenuItem(
child: const Text("Create new tab"),
onTap: () => onNewTab?.call(),
),
ContextMenuItem(
child: const Text("Close tab"),
onTap: () => onTabClosed?.call(index),
enabled: allowClosing,
),
],
child: _Tab(
tab: tabs[index],
selected: selectedTab == index,
onTap: () => onTabChanged?.call(index),
onClosed: () => onTabClosed?.call(index),
allowClosing: allowClosing,
),
),
scrollDirection: Axis.horizontal,
separatorBuilder: (context, index) => const SizedBox(width: 8),
itemCount: tabs.length,
padding: const EdgeInsets.all(8),
),
),
IconButton(
onPressed: onNewTab,
icon: const Icon(Icons.add),
),
],
),
);
}
}
class _Tab extends StatefulWidget {
final WorkspaceController tab;
final bool selected;
final VoidCallback? onTap;
final VoidCallback? onClosed;
final bool allowClosing;
const _Tab({
required this.tab,
required this.selected,
this.onTap,
this.onClosed,
this.allowClosing = true,
});
@override
State<_Tab> createState() => _TabState();
}
class _TabState extends State<_Tab> {
@override
void initState() {
super.initState();
widget.tab.addListener(updateOnDirChange);
}
@override
void dispose() {
widget.tab.removeListener(updateOnDirChange);
super.dispose();
}
void updateOnDirChange() {
if (mounted) setState(() {});
}
@override
Widget build(BuildContext context) {
return SizedBox(
width: 240,
height: double.infinity,
child: Material(
color: widget.selected
? Theme.of(context).colorScheme.surface
: Theme.of(context).colorScheme.background,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: widget.onTap,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
children: [
Icon(Utils.iconForFolder(widget.tab.currentDir), size: 16),
const SizedBox(width: 8),
Expanded(
child: Text(
Utils.getEntityName(widget.tab.currentDir),
overflow: TextOverflow.ellipsis,
),
),
AnimatedOpacity(
opacity: widget.allowClosing ? 1 : 0,
duration: const Duration(milliseconds: 200),
child: IconButton(
onPressed: widget.allowClosing ? widget.onClosed : null,
icon: const Icon(Icons.close),
iconSize: 16,
splashRadius: 16,
padding: EdgeInsets.zero,
constraints: BoxConstraints.tight(const Size.square(16)),
),
),
],
),
),
),
),
);
}
}