1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
import 'dart:async';
import 'dart:math';
import 'package:aves/model/entry/entry.dart';
import 'package:aves/services/common/services.dart';
import 'package:aves/theme/durations.dart';
import 'package:aves/widgets/viewer/controls/cast.dart';
import 'package:aves/widgets/viewer/controls/events.dart';
import 'package:aves_magnifier/aves_magnifier.dart';
import 'package:aves_model/aves_model.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:leak_tracker/leak_tracker.dart';
class ViewerController with CastMixin {
final ValueNotifier<AvesEntry?> entryNotifier = ValueNotifier(null);
final ViewerTransition transition;
final Duration? autopilotInterval;
final bool autopilotAnimatedZoom;
final bool repeat;
late final ScaleLevel _initialScale;
late final ValueNotifier<bool> _autopilotNotifier;
Timer? _playTimer;
final StreamController _streamController = StreamController.broadcast();
final Map<TickerProvider, _AutopilotAnimators> _autopilotAnimators = {};
ScaleLevel? _autopilotInitialScale;
Stream<dynamic> get _events => _streamController.stream;
Stream<ViewerShowNextEvent> get showNextCommands => _events.where((event) => event is ViewerShowNextEvent).cast<ViewerShowNextEvent>();
Stream<ViewerOverlayToggleEvent> get overlayCommands => _events.where((event) => event is ViewerOverlayToggleEvent).cast<ViewerOverlayToggleEvent>();
bool get autopilot => _autopilotNotifier.value;
set autopilot(bool enabled) => _autopilotNotifier.value = enabled;
ScaleLevel get initialScale => _autopilotInitialScale ?? _initialScale;
static final _autopilotScaleTweens = [
Tween<double>(begin: 1, end: 1.2),
Tween<double>(begin: 1.2, end: 1),
];
ViewerController({
ScaleLevel initialScale = const ScaleLevel(ref: ScaleReference.contained),
this.transition = ViewerTransition.parallax,
this.repeat = false,
bool autopilot = false,
this.autopilotInterval,
this.autopilotAnimatedZoom = false,
}) {
if (kFlutterMemoryAllocationsEnabled) {
LeakTracking.dispatchObjectCreated(
library: 'aves',
className: '$ViewerController',
object: this,
);
}
_initialScale = initialScale;
entryNotifier.addListener(_onEntryChanged);
_autopilotNotifier = ValueNotifier(autopilot);
_autopilotNotifier.addListener(_onAutopilotChanged);
_onAutopilotChanged();
}
void dispose() {
if (kFlutterMemoryAllocationsEnabled) {
LeakTracking.dispatchObjectDisposed(object: this);
}
entryNotifier.removeListener(_onEntryChanged);
windowService.setColorMode(wideColorGamut: false, hdr: false);
_autopilotNotifier.dispose();
_clearAutopilotAnimations();
_stopPlayTimer();
_streamController.close();
}
Future<void> _onEntryChanged() async {
if (await windowService.supportsHdr()) {
await windowService.setColorMode(
wideColorGamut: false,
hdr: entryNotifier.value?.isHdr ?? false,
);
}
}
void _onAutopilotChanged() {
_clearAutopilotAnimations();
_stopPlayTimer();
if (autopilot && autopilotInterval != null) {
_playTimer = Timer.periodic(autopilotInterval!, (_) => _streamController.add(ViewerShowNextEvent()));
_streamController.add(const ViewerOverlayToggleEvent(visible: false));
}
}
void _stopPlayTimer() => _playTimer?.cancel();
void _clearAutopilotAnimations() => _autopilotAnimators.keys.toSet().forEach((v) => stopAutopilotAnimation(vsync: v));
void stopAutopilotAnimation({required TickerProvider vsync}) {
final animationController = _autopilotAnimators.remove(vsync);
return animationController?.dispose();
}
void startAutopilotAnimation({
required TickerProvider vsync,
required void Function({required ScaleLevel scaleLevel}) onUpdate,
}) {
stopAutopilotAnimation(vsync: vsync);
if (!autopilot || !autopilotAnimatedZoom) return;
final scaleLevelRef = _initialScale.ref;
final scaleFactorTween = _autopilotScaleTweens[Random().nextInt(_autopilotScaleTweens.length)];
_autopilotInitialScale = ScaleLevel(ref: scaleLevelRef, factor: scaleFactorTween.begin!);
final animationController = AnimationController(
duration: autopilotInterval,
vsync: vsync,
);
final animation = CurvedAnimation(
parent: animationController,
curve: Curves.linear,
);
animationController.addListener(() {
return onUpdate.call(
scaleLevel: ScaleLevel(
ref: scaleLevelRef,
factor: scaleFactorTween.evaluate(animation),
),
);
});
_autopilotAnimators[vsync] = _AutopilotAnimators(
controller: animationController,
animation: animation,
);
Future.delayed(ADurations.viewerHorizontalPageAnimation).then((_) => _autopilotAnimators[vsync]?.controller.forward());
}
}
class _AutopilotAnimators {
final AnimationController controller;
final CurvedAnimation animation;
_AutopilotAnimators({
required this.controller,
required this.animation,
});
void dispose() {
animation.dispose();
controller.dispose();
}
}
|