Skip to content

Commit

Permalink
Added shortcuts to create and close tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Oct 17, 2023
1 parent 8754f35 commit c4b7e6d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 66 deletions.
107 changes: 90 additions & 17 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
!const DeepCollectionEquality().equals(savedJson, currentJson);

if (showConfirmation) {
showCloseConfirmation(context);
showWindowCloseConfirmation(context);
await windowManager.focus();
} else {
await windowManager.destroy();
Expand Down Expand Up @@ -582,7 +582,7 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
setState(() {});
}

void showCloseConfirmation(BuildContext context) {
void showWindowCloseConfirmation(BuildContext context) {
showDialog(
barrierDismissible: false,
context: context,
Expand Down Expand Up @@ -617,6 +617,32 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
);
}

void showTabCloseConfirmation(
BuildContext context, String tabName, Function() onClose) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
onClose.call();
},
child: const Text('OK')),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Cancel')),
],
content: Text('Do you want to close the tab "$tabName"?'),
title: const Text('Confirm Tab Close'),
);
},
);
}

@override
Widget build(BuildContext context) {
TextStyle? menuTextStyle = Theme.of(context).textTheme.bodySmall;
Expand Down Expand Up @@ -776,6 +802,42 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
setState(() => currentTabIndex = i - 1);
}
},
const SingleActivator(LogicalKeyboardKey.keyT, control: true): () {
String newTabName = 'Tab ${tabData.length + 1}';
int newTabIndex = tabData.length;

tabData.add(TabData(name: newTabName));
grids.add(
DashboardGrid(
key: GlobalKey(),
onAddWidgetPressed: displayAddWidgetDialog,
),
);

setState(() => currentTabIndex = newTabIndex);
},
const SingleActivator(LogicalKeyboardKey.keyW, control: true): () {
if (tabData.length <= 1) {
return;
}

TabData currentTab = tabData[currentTabIndex];

showTabCloseConfirmation(context, currentTab.name, () {
int oldTabIndex = currentTabIndex;

if (currentTabIndex == tabData.length - 1) {
currentTabIndex--;
}

grids[oldTabIndex].onDestroy();

setState(() {
tabData.removeAt(oldTabIndex);
grids.removeAt(oldTabIndex);
});
});
},
},
child: Focus(
autofocus: true,
Expand All @@ -786,35 +848,46 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
Expanded(
child: Stack(
children: [
// Image.asset(
// "assets/first-background.png",
// width: MediaQuery.of(context).size.width,
// height: MediaQuery.of(context).size.height,
// fit: BoxFit.cover,
// ),
EditableTabBar(
currentIndex: currentTabIndex,
newDashboardGridBuilder: () {
return DashboardGrid(
key: GlobalKey(),
onAddWidgetPressed: displayAddWidgetDialog,
);
},
onTabRename: (index, newData) {
setState(() {
tabData[index] = newData;
});
},
onTabCreate: (tab, grid) {
onTabCreate: (tab) {
setState(() {
tabData.add(tab);
grids.add(grid);
grids.add(DashboardGrid(
key: GlobalKey(),
onAddWidgetPressed: displayAddWidgetDialog,
));
});
},
onTabDestroy: (tab, grid) {
if (currentTabIndex == tabData.length) {
currentTabIndex--;
onTabDestroy: (index) {
if (tabData.length <= 1) {
return;
}

grid.onDestroy();
TabData currentTab = tabData[index];

setState(() {
tabData.remove(tab);
grids.remove(grid);
showTabCloseConfirmation(context, currentTab.name, () {
if (currentTabIndex == tabData.length - 1) {
currentTabIndex--;
}

grids[index].onDestroy();

setState(() {
tabData.removeAt(index);
grids.removeAt(index);
});
});
},
onTabChanged: (index) {
Expand Down
58 changes: 9 additions & 49 deletions lib/widgets/editable_tab_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class EditableTabBar extends StatelessWidget {
final List<DashboardGrid> tabViews;
final List<TabData> tabData;

final Function(TabData tab, DashboardGrid grid) onTabCreate;
final Function(TabData tab, DashboardGrid grid) onTabDestroy;
final Function(TabData tab) onTabCreate;
final Function(int index) onTabDestroy;
final Function(int index, TabData newData) onTabRename;
final Function(int index) onTabChanged;

final DashboardGrid Function() newDashboardGridBuilder;

final int currentIndex;

const EditableTabBar({
Expand All @@ -32,6 +34,7 @@ class EditableTabBar extends StatelessWidget {
required this.onTabDestroy,
required this.onTabRename,
required this.onTabChanged,
required this.newDashboardGridBuilder,
});

void renameTab(BuildContext context, int index) {
Expand Down Expand Up @@ -64,53 +67,16 @@ class EditableTabBar extends StatelessWidget {
void createTab() {
String tabName = 'Tab ${tabData.length + 1}';
TabData data = TabData(name: tabName);
DashboardGrid grid = DashboardGrid(key: GlobalKey());

onTabCreate.call(data, grid);
onTabCreate.call(data);
}

void closeTab(int index) {
if (tabData.length == 1) {
return;
}

TabData data = tabData[index];
DashboardGrid grid = tabViews[index];

tabData.removeAt(index);
tabViews.removeAt(index);

// if (currentIndex > 0) {
// currentIndex--;
// }

onTabDestroy.call(data, grid);
}

void showTabCloseConfirmation(
BuildContext context, String tabName, Function() onClose) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
onClose.call();
},
child: const Text('OK')),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Cancel')),
],
content: Text('Do you want to close the tab "$tabName"?'),
title: const Text('Confirm Tab Close'),
);
},
);
onTabDestroy.call(index);
}

@override
Expand Down Expand Up @@ -166,10 +132,7 @@ class EditableTabBar extends StatelessWidget {
title: const Text('Close'),
onTap: () {
Navigator.of(context).pop();
showTabCloseConfirmation(
context, tabData[index].name, () {
closeTab(index);
});
closeTab(index);
},
),
],
Expand Down Expand Up @@ -210,10 +173,7 @@ class EditableTabBar extends StatelessWidget {
const SizedBox(width: 10),
IconButton(
onPressed: () {
showTabCloseConfirmation(
context, tabData[index].name, () {
closeTab(index);
});
closeTab(index);
},
padding: const EdgeInsets.all(0.0),
alignment: Alignment.center,
Expand Down

0 comments on commit c4b7e6d

Please sign in to comment.