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

Copy & paste feature #62

Merged
merged 9 commits into from
Jul 12, 2024
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
78 changes: 58 additions & 20 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -71,7 +72,6 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
@override
void initState() {
super.initState();

_preferences = widget.preferences;
_updateChecker = UpdateChecker(currentVersion: widget.version);

Expand Down Expand Up @@ -1317,8 +1317,13 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
(!Settings.layoutLocked) ? () => _importLayout() : null,
shortcut:
const SingleActivator(LogicalKeyboardKey.keyO, control: true),
child: const Text(
'Open Layout',
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.folder_open_outlined),
SizedBox(width: 8),
Text('Open Layout'),
],
),
),
// Save
Expand All @@ -1329,22 +1334,32 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
},
shortcut:
const SingleActivator(LogicalKeyboardKey.keyS, control: true),
child: const Text(
'Save',
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.save_outlined),
SizedBox(width: 8),
Text('Save'),
],
),
),

// Export layout
MenuItemButton(
style: menuButtonStyle,
onPressed: () {
_exportLayout();
},
shortcut: const SingleActivator(LogicalKeyboardKey.keyS,
shift: true, control: true),
child: const Text(
'Save As',
),
),
style: menuButtonStyle,
onPressed: () {
_exportLayout();
},
shortcut: const SingleActivator(LogicalKeyboardKey.keyS,
shift: true, control: true),
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.save_as_outlined),
SizedBox(width: 8),
Text('Save As'),
],
)),
],
child: const Text(
'File',
Expand Down Expand Up @@ -1399,8 +1414,13 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
onPressed: () {
_displayAboutDialog(context);
},
child: const Text(
'About',
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.info_outline),
SizedBox(width: 8),
Text('About'),
],
),
),
// Check for Updates
Expand All @@ -1409,8 +1429,13 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
onPressed: () {
_checkForUpdates();
},
child: const Text(
'Check for Updates',
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.update_outlined),
SizedBox(width: 8),
Text('Check for updates'),
],
),
),
],
Expand Down Expand Up @@ -1522,6 +1547,20 @@ class _DashboardPageState extends State<DashboardPage> with WindowListener {
onTabChanged: (index) {
setState(() => _currentTabIndex = index);
},
onTabDuplicate: (index, tab) {
setState(() {
_tabData.insert(index + 1, tab);
Map<String, dynamic> tabJson = _grids[index].toJson();
_grids.insert(
index + 1,
TabGrid.fromJson(
key: GlobalKey(),
jsonData: tabJson,
onAddWidgetPressed: _displayAddWidgetDialog,
onJsonLoadingWarning: _showJsonLoadingWarning,
));
});
},
tabData: _tabData,
tabViews: _grids,
),
Expand Down Expand Up @@ -1641,7 +1680,6 @@ class _AddWidgetDialog extends StatefulWidget {

class _AddWidgetDialogState extends State<_AddWidgetDialog> {
bool _hideMetadata = true;

@override
Widget build(BuildContext context) {
return Visibility(
Expand Down
14 changes: 14 additions & 0 deletions lib/widgets/editable_tab_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EditableTabBar extends StatelessWidget {
final Function() onTabMoveRight;
final Function(int index, TabData newData) onTabRename;
final Function(int index) onTabChanged;
final Function(int index, TabData newData) onTabDuplicate;

final int currentIndex;

Expand All @@ -39,6 +40,7 @@ class EditableTabBar extends StatelessWidget {
required this.onTabMoveRight,
required this.onTabRename,
required this.onTabChanged,
required this.onTabDuplicate,
});

void renameTab(BuildContext context, int index) {
Expand Down Expand Up @@ -74,6 +76,13 @@ class EditableTabBar extends StatelessWidget {
);
}

void duplicateTab(BuildContext context, int index) {
String tabName = '${tabData[index].name} (Copy)';
TabData data = TabData(name: tabName);

onTabDuplicate.call(index, data);
}

void createTab() {
String tabName = 'Tab ${tabData.length + 1}';
TabData data = TabData(name: tabName);
Expand Down Expand Up @@ -142,6 +151,11 @@ class EditableTabBar extends StatelessWidget {
icon: Icons.drive_file_rename_outline_outlined,
onSelected: () => renameTab(context, index),
),
MenuItem(
label: 'Duplicate',
icon: Icons.control_point_duplicate_sharp,
onSelected: () => duplicateTab(context, index),
),
MenuItem(
label: 'Close',
icon: Icons.close,
Expand Down
95 changes: 81 additions & 14 deletions lib/widgets/tab_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TabGridModel extends ChangeNotifier {
}

class TabGrid extends StatelessWidget {
static Map<String, dynamic>? _copyJsonData;
final List<WidgetContainerModel> _widgetModels = [];

MapEntry<WidgetContainerModel, Offset>? _containerDraggingIn;
Expand Down Expand Up @@ -460,6 +461,7 @@ class TabGrid extends StatelessWidget {
widget.draggingRect.height,
),
);

_containerDraggingIn = MapEntry(widget, globalPosition);
refresh();
}
Expand Down Expand Up @@ -516,7 +518,6 @@ class TabGrid extends StatelessWidget {
_containerDraggingIn = null;

widget.disposeModel();

refresh();
}

Expand Down Expand Up @@ -648,6 +649,10 @@ class TabGrid extends StatelessWidget {
);
}

void copyWidget(WidgetContainerModel widget) {
_copyJsonData = widget.toJson();
}

void lockLayout() {
for (WidgetContainerModel container in _widgetModels) {
container.setDraggable(false);
Expand Down Expand Up @@ -782,7 +787,6 @@ class TabGrid extends StatelessWidget {

dashboardWidgets.add(
GestureDetector(
onTap: () {},
onSecondaryTapUp: (details) {
if (Settings.layoutLocked) {
return;
Expand All @@ -801,6 +805,12 @@ class TabGrid extends StatelessWidget {
},
),
...container.getContextMenuItems(),
MenuItem(
label: 'Copy',
icon: Icons.copy_outlined,
onSelected: () {
copyWidget(container);
}),
MenuItem(
label: 'Remove',
icon: Icons.delete_outlined,
Expand Down Expand Up @@ -898,22 +908,37 @@ class TabGrid extends StatelessWidget {
if (Settings.layoutLocked) {
return;
}

List<MenuItem> contextMenuEntries = [
MenuItem(
label: 'Add Widget',
icon: Icons.add,
onSelected: () => onAddWidgetPressed.call(),
),
MenuItem(
label: 'Clear Layout',
icon: Icons.clear,
onSelected: () => clearWidgets(context),
),
];

if (_copyJsonData != null) {
contextMenuEntries.add(
MenuItem(
label: 'Paste',
icon: Icons.paste_outlined,
onSelected: () {
pasteWidget(_copyJsonData, details.localPosition);
},
),
);
}

ContextMenu contextMenu = ContextMenu(
position: details.globalPosition,
borderRadius: BorderRadius.circular(5.0),
padding: const EdgeInsets.all(4.0),
entries: [
MenuItem(
label: 'Add Widget',
icon: Icons.add,
onSelected: () => onAddWidgetPressed.call(),
),
MenuItem(
label: 'Clear Layout',
icon: Icons.clear,
onSelected: () => clearWidgets(context),
),
],
entries: contextMenuEntries,
);

showContextMenu(
Expand All @@ -939,4 +964,46 @@ class TabGrid extends StatelessWidget {
),
);
}

void pasteWidget(Map<String, dynamic>? widgetJson, Offset localPosition) {
if (widgetJson == null) return;

// Put the top left corner of the widget in the square the user pastes it in
double snappedX =
(localPosition.dx ~/ Settings.gridSize) * Settings.gridSize.toDouble();
double snappedY =
(localPosition.dy ~/ Settings.gridSize) * Settings.gridSize.toDouble();

widgetJson['x'] = snappedX;
widgetJson['y'] = snappedY;

Rect pasteLocation = Rect.fromLTWH(
snappedX,
snappedY,
widgetJson['width'],
widgetJson['height'],
);

if (isValidLocation(pasteLocation)) {
WidgetContainerModel copiedWidget = createWidgetFromJson(widgetJson);

_widgetModels.add(copiedWidget);
refresh();
}
}

WidgetContainerModel createWidgetFromJson(Map<String, dynamic> json) {
if (json['type'] == 'List Layout') {
return ListLayoutModel.fromJson(
jsonData: json,
tabGrid: this,
onDragCancel: _layoutContainerOnDragCancel,
);
} else {
return NTWidgetContainerModel.fromJson(
enabled: ntConnection.isNT4Connected,
jsonData: json,
);
}
}
}
32 changes: 32 additions & 0 deletions test/pages/dashboard_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,38 @@ void main() {
findsOneWidget);
});

testWidgets('Duplicating tab', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
preferences: preferences,
version: '0.0.0.0',
),
),
);

await widgetTester.pumpAndSettle();

final teleopTab = find.widgetWithText(AnimatedContainer, 'Teleoperated');

expect(teleopTab, findsOneWidget);

await widgetTester.tap(teleopTab, buttons: kSecondaryButton);
await widgetTester.pumpAndSettle();

final duplicateButton = find.text('Duplicate');

expect(duplicateButton, findsOneWidget);

await widgetTester.tap(duplicateButton);
await widgetTester.pumpAndSettle();

expect(find.text('Teleoperated (Copy)'), findsOneWidget);
});

testWidgets('Minimizing window', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();
Expand Down
Loading
Loading