Skip to content

Commit

Permalink
feat: working read view overlay logic
Browse files Browse the repository at this point in the history
  • Loading branch information
squidrye committed Oct 5, 2023
1 parent 3209453 commit 0efa181
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,31 @@ class PaneNode extends Equatable {
final Axis? axis;
final String paneId;
final TabsController tabs;
final List<int> encoding;

PaneNode({
required this.paneId,
required this.children,
this.encoding = const [],
this.parent,
this.axis,
TabsController? tabs,
}) : tabs = tabs ?? TabsController(encoding: "[]");
}) : tabs = tabs ?? TabsController();

PaneNode copyWith({
PaneNode? parent,
List<PaneNode>? children,
Axis? axis,
String? paneId,
TabsController? tabs,
List<int>? encoding,
}) {
return PaneNode(
parent: parent ?? this.parent,
axis: axis ?? this.axis,
children: children ?? this.children,
paneId: paneId ?? this.paneId,
tabs: tabs ?? this.tabs,
encoding: encoding ?? this.encoding,
);
}

@override
List<Object?> get props => [paneId, axis, children, parent, tabs];

@override
String toString() {
return '${(paneId, axis)} => post ${encoding} \n';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ class PanesCubit extends Cubit<PanesState> {
setActivePane(state.root.children.last);
}

void closePane({required String paneId}) {
void closePane({required String paneId, bool move = false}) {
emit(
state.copyWith(
root: panesService.closePaneHandler(
node: state.root,
targetPaneId: paneId,
move: move,
),
count: state.count - 1,
),
Expand Down Expand Up @@ -126,6 +127,6 @@ class PanesCubit extends Cubit<PanesState> {
count: state.count + 1,
),
);
closePane(paneId: from.paneId);
closePane(paneId: from.paneId, move: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PanesState extends Equatable {

factory PanesState.initial() {
final pane = PaneNode(
tabs: TabsController(encoding: '[]'),
tabs: TabsController(),
children: const [],
paneId: nanoid(),
axis: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:appflowy/startup/plugin/plugin.dart';
import 'package:appflowy/workspace/application/panes/panes.dart';
import 'package:appflowy/workspace/application/tabs/tabs_controller.dart';
import 'package:appflowy_backend/log.dart';
import 'package:flutter/material.dart';
import 'package:nanoid/nanoid.dart';

Expand All @@ -26,38 +25,28 @@ class PanesService {
children: const [],
axis: axis,
tabs: null,
encoding: node.encoding,
);
final oldChildEncoding = [direction == Direction.front ? 0 : 1];
final newChildEncoding = [direction == Direction.front ? 1 : 0];

final oldChildNode = node.copyWith(
paneId: nanoid(),
parent: newHolderNode,
axis: null,
tabs: TabsController(
pageManagers: node.tabs.pageManagers,
encoding: oldChildEncoding.toString(),
),
encoding: [direction == Direction.front ? 0 : 1],
tabs: TabsController(pageManagers: node.tabs.pageManagers),
);

final newChildNode = fromNode?.copyWith(
parent: newHolderNode,
paneId: nanoid(),
children: const [],
axis: null,
tabs: TabsController(
pageManagers: fromNode.tabs.pageManagers,
encoding: newChildEncoding.toString()),
encoding: newChildEncoding,
tabs: TabsController(pageManagers: fromNode.tabs.pageManagers),
) ??
PaneNode(
paneId: nanoid(),
children: const [],
parent: newHolderNode,
axis: null,
tabs: TabsController(encoding: newChildEncoding.toString())
..openPlugin(plugin: plugin!),
encoding: newChildEncoding,
tabs: TabsController()..openPlugin(plugin: plugin!, newPane: true),
);
final ret = newHolderNode.copyWith(
children: direction == Direction.front
Expand All @@ -78,40 +67,31 @@ class PanesService {
if (node.axis == axis) {
for (int i = 0; i < node.children.length; i++) {
if (node.children[i].paneId == targetPaneId) {
final encode = node.encoding.toString() +
(direction == Direction.front ? "${i + 1}" : "$i");
final newNode = fromNode?.copyWith(
paneId: nanoid(),
parent: node.parent,
tabs: TabsController(
pageManagers: fromNode.tabs.pageManagers,
encoding: encode,
),
tabs: TabsController(pageManagers: fromNode.tabs.pageManagers),
) ??
PaneNode(
paneId: nanoid(),
children: const [],
parent: node.parent,
tabs: TabsController(encoding: encode)
..openPlugin(plugin: plugin!),
tabs: TabsController()..openPlugin(plugin: plugin!),
);
if (direction == Direction.front) {
node = node.copyWith(
children: insertAndEncode(node.children, i + 1, newNode),
);
if (i == node.children.length) {
node.children.add(newNode);
} else {
node.children.insert(i + 1, newNode);
}
} else {
node = node.copyWith(
children: insertAndEncode(node.children, i, newNode),
);
node.children.insert(i, newNode);
}
final ret = node.copyWith(
paneId: nanoid(),
tabs: TabsController(encoding: node.tabs.encoding),
children: node.children
.map(
(e) => e.copyWith(
tabs: TabsController(
encoding: e.tabs.encoding,
pageManagers: e.tabs.pageManagers,
),
),
Expand Down Expand Up @@ -144,23 +124,24 @@ class PanesService {
PaneNode closePaneHandler({
required PaneNode node,
required String targetPaneId,
required bool move,
}) {
if (node.paneId == targetPaneId) {
return node;
}
for (var i = 0; i < node.children.length; i++) {
final element = node.children[i];
if (element.paneId == targetPaneId) {
element.tabs.closeAllViews();
node = node.copyWith(children: removeAndEncode(node.children, i));
if (!move) {
element.tabs.closeAllViews();
}
node.children.remove(element);
if (node.children.length == 1) {
final ret = node.children.first.copyWith(
paneId: nanoid(),
parent: node.parent,
encoding: node.parent == null ? [] : [...node.parent!.encoding, 0],
tabs: TabsController(
pageManagers: node.children.first.tabs.pageManagers,
encoding: "[]",
),
);
return ret;
Expand All @@ -171,7 +152,6 @@ class PanesService {
.map(
(e) => e.copyWith(
tabs: TabsController(
encoding: e.tabs.encoding,
pageManagers: e.tabs.pageManagers,
),
),
Expand All @@ -188,44 +168,10 @@ class PanesService {
return closePaneHandler(
node: childNode,
targetPaneId: targetPaneId,
move: move,
);
}).toList();

return node.copyWith(children: newChildren);
}
}

List<PaneNode> insertAndEncode(List<PaneNode> list, int index, PaneNode node) {
if (index <= 0) index = 0;
if (index >= list.length) index = list.length - 1;

List<PaneNode> ret = [...list, node];
for (int i = ret.length - 1; i > index; i--) {
ret[i] = ret[i - 1].copyWith(
encoding: node.parent == null ? [i] : [...node.parent!.encoding, i],
);
}
ret[index] = node.copyWith(
encoding: node.parent == null ? [index] : [...node.parent!.encoding, index],
);
return ret;
}

List<PaneNode> removeAndEncode(List<PaneNode> list, int index) {
List<PaneNode> ret = [];
for (int i = 0; i < list.length; i++) {
if (i < index) {
ret.add(list[i]);
}
if (i > index) {
ret.add(
list[i].copyWith(
encoding: list[i].parent == null
? [i - 1]
: [...list[i].parent!.encoding, i - 1],
),
);
}
}
return ret;
}
Loading

0 comments on commit 0efa181

Please sign in to comment.