Skip to content

Commit

Permalink
feat: added logic for pane drag-drop
Browse files Browse the repository at this point in the history
  • Loading branch information
squidrye committed Sep 14, 2023
1 parent ca81a1c commit 83447b0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ class PanesCubit extends Cubit<PanesState> {
direction,
axis,
setActivePane,
null,
);

emit(
state.copyWith(
root: root,
),
);
emit(
state.copyWith(
count: panesService.countNodeHandler(root),
),
);
Expand Down Expand Up @@ -110,4 +107,28 @@ class PanesCubit extends Cubit<PanesState> {
}
}
}

void movePane(
PaneNode from,
PaneNode to,
PaneDraggableHoverPosition position,
) {
emit(
state.copyWith(
root: panesService.splitHandler(
state.root,
to.paneId,
null,
Direction.front,
(position == PaneDraggableHoverPosition.left ||
position == PaneDraggableHoverPosition.right)
? Axis.vertical
: Axis.horizontal,
setActivePane,
from,
),
),
);
closePane(from.paneId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
part of 'panes_cubit.dart';

enum PaneDraggableHoverPosition {
none,
top,
left,
right,
bottom,
whole,
}

class PanesState extends Equatable {
final PaneNode root;
final int count;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:appflowy/workspace/application/panes/panes.dart';
import 'package:appflowy/workspace/application/panes/panes_cubit/panes_cubit.dart';
import 'package:appflowy/workspace/application/panes/size_controller.dart';
import 'package:appflowy/workspace/application/tabs/tabs.dart';
import 'package:appflowy/workspace/application/view/view_ext.dart';
Expand All @@ -14,10 +15,11 @@ class PanesService {
PaneNode splitHandler(
PaneNode node,
String targetPaneId,
ViewPB view,
ViewPB? view,
Direction direction,
Axis axis,
void Function(PaneNode) activePane,
PaneNode? fromNode,
) {
/// This is a recursive handler, following condition checks if passed node
/// is our target node
Expand All @@ -43,14 +45,16 @@ class PanesService {
pageManagers: node.tabs.pageManagers,
),
),
PaneNode(
sizeController: PaneSizeController.intial(),
paneId: nanoid(),
children: const [],
parent: newNode,
axis: null,
tabs: Tabs(pageManagers: [PageManager()..setPlugin(view.plugin())]),
)
fromNode ??
PaneNode(
sizeController: PaneSizeController.intial(),
paneId: nanoid(),
children: const [],
parent: newNode,
axis: null,
tabs: Tabs(
pageManagers: [PageManager()..setPlugin(view!.plugin())]),
)
],
);
activePane(ret.children[0]);
Expand All @@ -62,13 +66,15 @@ class PanesService {
if (node.axis == axis) {
for (int i = 0; i < node.children.length; i++) {
if (node.children[i].paneId == targetPaneId) {
final newNode = PaneNode(
sizeController: PaneSizeController.intial(),
paneId: nanoid(),
children: const [],
parent: node.parent,
tabs: Tabs(pageManagers: [PageManager()..setPlugin(view.plugin())]),
);
final newNode = fromNode ??
PaneNode(
sizeController: PaneSizeController.intial(),
paneId: nanoid(),
children: const [],
parent: node.parent,
tabs: Tabs(
pageManagers: [PageManager()..setPlugin(view!.plugin())]),
);
if (direction == Direction.front) {
if (i == node.children.length) {
node.children.add(newNode);
Expand Down Expand Up @@ -99,8 +105,17 @@ class PanesService {
///node isn't right holder we proceed recursively to dfs remaining
///children
final newChildren = node.children
.map((e) =>
splitHandler(e, targetPaneId, view, direction, axis, activePane))
.map(
(e) => splitHandler(
e,
targetPaneId,
view,
direction,
axis,
activePane,
fromNode,
),
)
.toList();
return node.copyWith(children: newChildren);
}
Expand All @@ -123,7 +138,19 @@ class PanesService {
parent: node.parent,
);
}
return node;
return node.copyWith(
paneId: nanoid(),
tabs: Tabs(
currentIndex: node.tabs.currentIndex,
pageManagers: node.tabs.pageManagers,
),
sizeController: PaneSizeController(
flex: List.generate(
node.children.length,
(_) => 1 / (node.children.length),
),
),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import 'package:appflowy/workspace/application/panes/panes.dart';
import 'package:appflowy/workspace/application/panes/panes_cubit/panes_cubit.dart';
import 'package:appflowy/workspace/presentation/widgets/draggable_item/draggable_item.dart';
import 'package:appflowy_backend/log.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:vector_math/vector_math.dart' as math;
import 'dart:math';
import 'package:flutter/material.dart';

enum PaneDraggableHoverPosition {
none,
top,
left,
right,
bottom,
whole,
}

class DraggablePaneItem extends StatefulWidget {
const DraggablePaneItem({
super.key,
Expand Down Expand Up @@ -166,5 +159,7 @@ class _DraggablePaneItemState extends State<DraggablePaneItem> {
return true;
}

void _move(PaneNode from, PaneNode to) {}
void _move(PaneNode from, PaneNode to) {
context.read<PanesCubit>().movePane(from, to, position);
}
}

0 comments on commit 83447b0

Please sign in to comment.