Skip to content
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
25 changes: 25 additions & 0 deletions src/shell/lib/shared/widget/sliding_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ class SlidingContainer extends HookConsumerWidget {
upperBound: double.infinity,
initialValue: index.toDouble(),
);

// The caller moved the leading tile itself, not to follow this container's
// report: the pages before the view changed (a tile before it was
// removed), so the shown pages now sit at other indices. The view and the
// indicator jump with them, and what is on screen does not move.
final firstVisibleIndex = this.firstVisibleIndex;
final seenFirstVisibleIndex = useRef(firstVisibleIndex);
final previousFirstVisibleIndex = seenFirstVisibleIndex.value;
seenFirstVisibleIndex.value = firstVisibleIndex;
if (firstVisibleIndex != null &&
previousFirstVisibleIndex != null &&
firstVisibleIndex != previousFirstVisibleIndex &&
firstVisibleIndex != reportedFirstVisibleIndex.value &&
pageController.hasClients &&
pageController.position.hasPixels &&
pageController.position.hasViewportDimension) {
final shift = previousFirstVisibleIndex - firstVisibleIndex;
final pageDimension = pageController.position.viewportDimension / visible;
pageController.position.correctPixels(
pageController.offset - shift * pageDimension,
);
selectionPosition.value -= shift;
reportedFirstVisibleIndex.value = firstVisibleIndex;
}

useEffect(() {
selectionPosition.animateTo(index.toDouble(), curve: Curves.easeInOut);
return null;
Expand Down
13 changes: 13 additions & 0 deletions src/shell/lib/tile_grab/widget/tile_drop_slot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TileDropSlot extends HookConsumerWidget {
this.windowId,
this.initialDisplayIndex,
this.leaving,
this.viewShift = 0,
super.key,
});

Expand All @@ -69,6 +70,12 @@ class TileDropSlot extends HookConsumerWidget {
/// It must be keyed: once a key has been covered, it is not drawn again.
final Widget? leaving;

/// How many slots the row's view jumped back with the latest change of the
/// window list: the tiles before it were removed, so every tile now sits
/// that many slots earlier while staying where it is on screen. The tile
/// jumps with the view instead of sliding to its new slot.
final int viewShift;

final Widget child;

@override
Expand Down Expand Up @@ -110,6 +117,11 @@ class TileDropSlot extends HookConsumerWidget {
(morphs) => windowList.any(morphs.containsKey),
),
);
// The view only jumps with the change of window list it came with, not on
// a later rebuild of this slot alone.
final shownWindowList = useRef(windowList);
final viewShift = shownWindowList.value == windowList ? 0 : this.viewShift;
shownWindowList.value = windowList;
final position = useAnimationController(
lowerBound: double.negativeInfinity,
upperBound: double.infinity,
Expand All @@ -120,6 +132,7 @@ class TileDropSlot extends HookConsumerWidget {
// The hidden held slot follows invisibly. Every other change of slot
// slides, even with a single tile shown: the next tile pulled into a
// vacated slot moves over it instead of jumping into place.
if (viewShift != 0) position.value -= viewShift;
if (isHeld) {
position.value = displayIndex;
} else if (makesWayForLaunch) {
Expand Down
9 changes: 9 additions & 0 deletions src/shell/lib/workspace/provider/workspace_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,16 @@ class WorkspaceState extends _$WorkspaceState {
final removedIsCurrentlyFocused =
state.selectedIndex < state.tileableWindowList.length &&
windowId == state.tileableWindowList[state.selectedIndex];
final removedIndex = state.tileableWindowList.indexOf(windowId);
final newWindowList = state.tileableWindowList.remove(windowId);

// A tile removed before the leading one shown pulls the shown tiles back
// one slot: the view follows them, so what is on screen stays put.
final firstVisibleIndex =
removedIndex >= 0 && removedIndex < state.firstVisibleIndex
? state.firstVisibleIndex - 1
: state.firstVisibleIndex;

var selectedIndex = state.selectedIndex;
if (!removedIsCurrentlyFocused) {
if (selectedIndex < state.tileableWindowList.length) {
Expand All @@ -219,6 +227,7 @@ class WorkspaceState extends _$WorkspaceState {
tileableWindowList: newWindowList,
category: state.forcedCategory ?? await _determineCategory(newWindowList),
selectedIndex: selectedIndex,
firstVisibleIndex: firstVisibleIndex,
);

final workspaceId = ref.read(windowWorkspaceMapProvider).get(windowId);
Expand Down
13 changes: 11 additions & 2 deletions src/shell/lib/workspace/widget/workspace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class WorkspaceWidget extends HookConsumerWidget {
previous.tileableWindowList != workspaceState.tileableWindowList
? previous.tileableWindowList
: null;
// Tiles removed before the leading one shown move the view back with the
// tiles after them (see `WorkspaceState.removeWindow`), so the tiles on
// screen stay put instead of sliding.
final viewShift = previous != null && previousList != null
? previous.firstVisibleIndex - workspaceState.firstVisibleIndex
: 0;
if (previous != null && previousList != null) {
// A held tile is already drawn flying to its destination.
leavingTiles.value = isGrabbing
Expand All @@ -159,7 +165,7 @@ class WorkspaceWidget extends HookConsumerWidget {
final index = tileable is PersistentWindowTileable
? previousList.indexOf(tileable.windowId)
: previousList.length;
return index < 0 ? null : index.toDouble();
return index < 0 ? null : (index - viewShift).toDouble();
}

final selectionIndicator =
Expand Down Expand Up @@ -327,6 +333,7 @@ class WorkspaceWidget extends HookConsumerWidget {
tileable,
),
leaving: leavingTiles.value[index],
viewShift: viewShift,
child: _FocusSaturation(
desaturated:
grayscaleUnfocused &&
Expand Down Expand Up @@ -395,7 +402,9 @@ Map<int, Widget> _leavingTiles(
for (final (index, windowId) in previous.tileableWindowList.indexed) {
if (windowList.contains(windowId)) continue;
final provider = persistentWindowStateProvider(windowId);
if (ref.exists(provider)) {
// One before the leading tile shown was off screen: the view moves back
// over its slot, and no tile slides over its face.
if (index >= previous.firstVisibleIndex && ref.exists(provider)) {
// The tiles after it moved back one slot per removal before them.
leaving[index - removedBefore] = TileRemovalGhost(
// A fresh key per removal: a covered face is never drawn again.
Expand Down
195 changes: 195 additions & 0 deletions src/shell/test/tile_removal_before_view_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import 'dart:async';

import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:material_ui/material_ui.dart';
import 'package:shell/platform/model/event/platform_event.serializable.dart';
import 'package:shell/platform/provider/platform_manager.dart';
import 'package:shell/shared/widget/sliding_container.dart';
import 'package:shell/tile_grab/widget/tile_drop_slot.dart';
import 'package:shell/window/model/window_id.serializable.dart';
import 'package:shell/workspace/model/workspace.serializable.dart';
import 'package:shell/workspace/provider/window_workspace_map.dart';
import 'package:shell/workspace/provider/workspace_state.dart';

const _rowWidth = 800.0;

/// A workspace scrolled past its first windows: [selected] and [leading]
/// are what it shows.
class _Workspace extends WorkspaceState {
_Workspace({
required this.windows,
required this.visible,
required this.selected,
required this.leading,
});

final List<String> windows;
final int visible;
final int selected;
final int leading;

@override
Workspace build(WorkspaceId workspaceId) {
ref.keepAlive();
return Workspace(
workspaceId: workspaceId,
tileableWindowList: [
for (final id in windows) PersistentWindowId(id),
].lock,
selectedIndex: selected,
visibleLength: visible,
firstVisibleIndex: leading,
// A fixed category: the removal does not read desktop entries.
forcedCategory: WorkspaceCategory.Utility,
);
}
}

/// The workspace's page row, wired like `WorkspaceWidget`: tiles start at the
/// slot they came from, and jump with the view when it moves.
class _PagedRow extends HookConsumerWidget {
const _PagedRow();

@override
Widget build(BuildContext context, WidgetRef ref) {
final workspace = ref.watch(workspaceStateProvider('ws'));
final previous = useRef<Workspace?>(null);
final last = previous.value;
previous.value = workspace;
final changed =
last != null && last.tileableWindowList != workspace.tileableWindowList;
final viewShift = changed
? last.firstVisibleIndex - workspace.firstVisibleIndex
: 0;
double? startsAt(PersistentWindowId windowId) {
if (!changed) return null;
final index = last.tileableWindowList.indexOf(windowId);
return index < 0 ? null : (index - viewShift).toDouble();
}

final windowList = workspace.tileableWindowList;
return Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: _rowWidth,
height: 300,
child: SlidingContainer(
index: workspace.selectedIndex,
visible: workspace.visibleLength,
firstVisibleIndex: workspace.firstVisibleIndex,
onFirstVisibleIndexChanged: ref
.read(workspaceStateProvider('ws').notifier)
.setFirstVisibleIndex,
children: [
for (final (index, windowId) in windowList.indexed)
TileDropSlot(
key: ValueKey(windowId),
workspaceId: 'ws',
index: index,
windowId: windowId,
initialDisplayIndex: startsAt(windowId),
viewShift: viewShift,
child: Text(windowId.uuid),
),
],
),
),
);
}
}

Future<ProviderContainer> _pump(
WidgetTester tester,
_Workspace Function() workspace,
) async {
final events = StreamController<PlatformEvent>.broadcast();
addTearDown(events.close);
final container = ProviderContainer(
overrides: [
workspaceStateProvider('ws').overrideWith(workspace),
platformManagerProvider.overrideWithValue(events.stream),
windowWorkspaceMapProvider.overrideWithValue(const IMapConst({})),
],
);
addTearDown(container.dispose);
await tester.pumpWidget(
UncontrolledProviderScope(
container: container,
child: const Directionality(
textDirection: TextDirection.ltr,
child: _PagedRow(),
),
),
);
await tester.pumpAndSettle();
return container;
}

/// Removes [id] and checks, frame by frame until everything settles, that
/// the tiles shown stay exactly where they were.
Future<void> _expectStill(
WidgetTester tester,
ProviderContainer container,
String id,
Map<String, double> shown,
) async {
for (final MapEntry(key: tile, value: dx) in shown.entries) {
expect(tester.getTopLeft(find.text(tile)).dx, dx, reason: tile);
}
await container
.read(workspaceStateProvider('ws').notifier)
.removeWindow(PersistentWindowId(id));
for (var frame = 0; frame < 20; frame++) {
await tester.pump(const Duration(milliseconds: 20));
for (final MapEntry(key: tile, value: dx) in shown.entries) {
expect(
tester.getTopLeft(find.text(tile)).dx,
dx,
reason: '$tile at frame $frame',
);
}
}
await tester.pumpAndSettle();
}

void main() {
testWidgets('removing a tile before the one shown does not move it', (
tester,
) async {
final container = await _pump(
tester,
() => _Workspace(
windows: ['a', 'b', 'c'],
visible: 1,
selected: 2,
leading: 2,
),
);
await _expectStill(tester, container, 'a', {'c': 0});
final workspace = container.read(workspaceStateProvider('ws'));
expect(workspace.selectedIndex, 1);
expect(workspace.firstVisibleIndex, 1);
expect(tester.getTopLeft(find.text('c')).dx, 0);
});

testWidgets('removing a tile before the ones shown does not move them', (
tester,
) async {
final container = await _pump(
tester,
() => _Workspace(
windows: ['a', 'b', 'c', 'd'],
visible: 2,
selected: 3,
leading: 2,
),
);
await _expectStill(tester, container, 'b', {'c': 0, 'd': _rowWidth / 2});
final workspace = container.read(workspaceStateProvider('ws'));
expect(workspace.selectedIndex, 2);
expect(workspace.firstVisibleIndex, 1);
});
}
Loading