Skip to content

Commit

Permalink
Add option to hide renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 26, 2023
1 parent 2a61030 commit 6b75dbe
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
17 changes: 17 additions & 0 deletions app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum SaveState { saved, saving, unsaved }

enum HideState { visible, keyboard, touch }

enum RendererState { visible, temporary, hidden }

@Freezed(equal: false)
class CurrentIndex with _$CurrentIndex {
const CurrentIndex._();
Expand All @@ -53,6 +55,9 @@ class CurrentIndex with _$CurrentIndex {
@Default(SaveState.unsaved) SaveState saved,
PreferredSizeWidget? toolbar,
PreferredSizeWidget? temporaryToolbar,
@Default(<Renderer, RendererState>{})
Map<Renderer, RendererState> rendererStates,
Map<Renderer, RendererState>? temporaryRendererStates,
@Default(ViewOption()) ViewOption viewOption,
@Default(HideState.visible) HideState hideUi,
}) = _CurrentIndex;
Expand All @@ -63,6 +68,11 @@ class CurrentIndex with _$CurrentIndex {
MouseCursor get currentCursor => temporaryCursor ?? cursor;

UtilitiesState get utilitiesState => cameraViewport.utilities.element;

Map<Renderer, RendererState> get allRendererStates => {
...rendererStates,
...?temporaryRendererStates,
};
}

class CurrentIndexCubit extends Cubit<CurrentIndex> {
Expand Down Expand Up @@ -116,10 +126,12 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
foregrounds: handler.createForegrounds(
this, document, blocState.page, info, blocState.currentArea),
toolbar: handler.getToolbar(bloc),
rendererStates: handler.getRendererStates(bloc),
temporaryForegrounds: null,
temporaryHandler: null,
temporaryToolbar: null,
temporaryCursor: null,
temporaryRendererStates: null,
));
}
return handler;
Expand All @@ -142,6 +154,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
handler: handler,
foregrounds: foregrounds,
toolbar: handler.getToolbar(bloc),
rendererStates: handler.getRendererStates(bloc),
cursor: handler.cursor ?? MouseCursor.defer,
));
}
Expand All @@ -160,6 +173,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
temporaryHandler: handler,
temporaryForegrounds: temporaryForegrounds,
temporaryToolbar: handler.getToolbar(bloc),
temporaryRendererStates: handler.getRendererStates(bloc),
temporaryCursor: handler.cursor,
));
}
Expand Down Expand Up @@ -252,6 +266,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
temporaryHandler: null,
temporaryForegrounds: null,
temporaryCursor: null,
temporaryRendererStates: null,
cameraViewport: CameraViewport.unbaked(UtilitiesRenderer()),
));
}
Expand Down Expand Up @@ -298,6 +313,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
temporaryForegrounds: temporaryForegrounds,
temporaryToolbar: handler.getToolbar(bloc),
temporaryCursor: handler.cursor,
temporaryRendererStates: handler.getRendererStates(bloc),
));
}
return handler;
Expand All @@ -317,6 +333,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
temporaryForegrounds: null,
temporaryToolbar: null,
temporaryCursor: null,
temporaryRendererStates: null,
));
}

Expand Down
61 changes: 60 additions & 1 deletion app/lib/cubits/current_index.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ mixin _$CurrentIndex {
PreferredSizeWidget? get toolbar => throw _privateConstructorUsedError;
PreferredSizeWidget? get temporaryToolbar =>
throw _privateConstructorUsedError;
Map<Renderer, RendererState> get rendererStates =>
throw _privateConstructorUsedError;
Map<Renderer, RendererState>? get temporaryRendererStates =>
throw _privateConstructorUsedError;
ViewOption get viewOption => throw _privateConstructorUsedError;
HideState get hideUi => throw _privateConstructorUsedError;

Expand Down Expand Up @@ -71,6 +75,8 @@ abstract class $CurrentIndexCopyWith<$Res> {
SaveState saved,
PreferredSizeWidget? toolbar,
PreferredSizeWidget? temporaryToolbar,
Map<Renderer, RendererState> rendererStates,
Map<Renderer, RendererState>? temporaryRendererStates,
ViewOption viewOption,
HideState hideUi});

Expand Down Expand Up @@ -110,6 +116,8 @@ class _$CurrentIndexCopyWithImpl<$Res, $Val extends CurrentIndex>
Object? saved = null,
Object? toolbar = freezed,
Object? temporaryToolbar = freezed,
Object? rendererStates = null,
Object? temporaryRendererStates = freezed,
Object? viewOption = null,
Object? hideUi = null,
}) {
Expand Down Expand Up @@ -190,6 +198,14 @@ class _$CurrentIndexCopyWithImpl<$Res, $Val extends CurrentIndex>
? _value.temporaryToolbar
: temporaryToolbar // ignore: cast_nullable_to_non_nullable
as PreferredSizeWidget?,
rendererStates: null == rendererStates
? _value.rendererStates
: rendererStates // ignore: cast_nullable_to_non_nullable
as Map<Renderer, RendererState>,
temporaryRendererStates: freezed == temporaryRendererStates
? _value.temporaryRendererStates
: temporaryRendererStates // ignore: cast_nullable_to_non_nullable
as Map<Renderer, RendererState>?,
viewOption: null == viewOption
? _value.viewOption
: viewOption // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -246,6 +262,8 @@ abstract class _$$_CurrentIndexCopyWith<$Res>
SaveState saved,
PreferredSizeWidget? toolbar,
PreferredSizeWidget? temporaryToolbar,
Map<Renderer, RendererState> rendererStates,
Map<Renderer, RendererState>? temporaryRendererStates,
ViewOption viewOption,
HideState hideUi});

Expand Down Expand Up @@ -285,6 +303,8 @@ class __$$_CurrentIndexCopyWithImpl<$Res>
Object? saved = null,
Object? toolbar = freezed,
Object? temporaryToolbar = freezed,
Object? rendererStates = null,
Object? temporaryRendererStates = freezed,
Object? viewOption = null,
Object? hideUi = null,
}) {
Expand Down Expand Up @@ -365,6 +385,14 @@ class __$$_CurrentIndexCopyWithImpl<$Res>
? _value.temporaryToolbar
: temporaryToolbar // ignore: cast_nullable_to_non_nullable
as PreferredSizeWidget?,
rendererStates: null == rendererStates
? _value._rendererStates
: rendererStates // ignore: cast_nullable_to_non_nullable
as Map<Renderer, RendererState>,
temporaryRendererStates: freezed == temporaryRendererStates
? _value._temporaryRendererStates
: temporaryRendererStates // ignore: cast_nullable_to_non_nullable
as Map<Renderer, RendererState>?,
viewOption: null == viewOption
? _value.viewOption
: viewOption // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -396,11 +424,16 @@ class _$_CurrentIndex extends _CurrentIndex {
this.saved = SaveState.unsaved,
this.toolbar,
this.temporaryToolbar,
final Map<Renderer, RendererState> rendererStates =
const <Renderer, RendererState>{},
final Map<Renderer, RendererState>? temporaryRendererStates,
this.viewOption = const ViewOption(),
this.hideUi = HideState.visible})
: _foregrounds = foregrounds,
_temporaryForegrounds = temporaryForegrounds,
_pointers = pointers,
_rendererStates = rendererStates,
_temporaryRendererStates = temporaryRendererStates,
super._();

@override
Expand Down Expand Up @@ -468,6 +501,26 @@ class _$_CurrentIndex extends _CurrentIndex {
final PreferredSizeWidget? toolbar;
@override
final PreferredSizeWidget? temporaryToolbar;
final Map<Renderer, RendererState> _rendererStates;
@override
@JsonKey()
Map<Renderer, RendererState> get rendererStates {
if (_rendererStates is EqualUnmodifiableMapView) return _rendererStates;
// ignore: implicit_dynamic_type
return EqualUnmodifiableMapView(_rendererStates);
}

final Map<Renderer, RendererState>? _temporaryRendererStates;
@override
Map<Renderer, RendererState>? get temporaryRendererStates {
final value = _temporaryRendererStates;
if (value == null) return null;
if (_temporaryRendererStates is EqualUnmodifiableMapView)
return _temporaryRendererStates;
// ignore: implicit_dynamic_type
return EqualUnmodifiableMapView(value);
}

@override
@JsonKey()
final ViewOption viewOption;
Expand All @@ -477,7 +530,7 @@ class _$_CurrentIndex extends _CurrentIndex {

@override
String toString() {
return 'CurrentIndex(index: $index, handler: $handler, cameraViewport: $cameraViewport, settingsCubit: $settingsCubit, transformCubit: $transformCubit, temporaryHandler: $temporaryHandler, foregrounds: $foregrounds, selection: $selection, pinned: $pinned, temporaryForegrounds: $temporaryForegrounds, cursor: $cursor, temporaryCursor: $temporaryCursor, pointers: $pointers, buttons: $buttons, location: $location, embedding: $embedding, saved: $saved, toolbar: $toolbar, temporaryToolbar: $temporaryToolbar, viewOption: $viewOption, hideUi: $hideUi)';
return 'CurrentIndex(index: $index, handler: $handler, cameraViewport: $cameraViewport, settingsCubit: $settingsCubit, transformCubit: $transformCubit, temporaryHandler: $temporaryHandler, foregrounds: $foregrounds, selection: $selection, pinned: $pinned, temporaryForegrounds: $temporaryForegrounds, cursor: $cursor, temporaryCursor: $temporaryCursor, pointers: $pointers, buttons: $buttons, location: $location, embedding: $embedding, saved: $saved, toolbar: $toolbar, temporaryToolbar: $temporaryToolbar, rendererStates: $rendererStates, temporaryRendererStates: $temporaryRendererStates, viewOption: $viewOption, hideUi: $hideUi)';
}

@JsonKey(ignore: true)
Expand Down Expand Up @@ -508,6 +561,8 @@ abstract class _CurrentIndex extends CurrentIndex {
final SaveState saved,
final PreferredSizeWidget? toolbar,
final PreferredSizeWidget? temporaryToolbar,
final Map<Renderer, RendererState> rendererStates,
final Map<Renderer, RendererState>? temporaryRendererStates,
final ViewOption viewOption,
final HideState hideUi}) = _$_CurrentIndex;
const _CurrentIndex._() : super._();
Expand Down Expand Up @@ -551,6 +606,10 @@ abstract class _CurrentIndex extends CurrentIndex {
@override
PreferredSizeWidget? get temporaryToolbar;
@override
Map<Renderer, RendererState> get rendererStates;
@override
Map<Renderer, RendererState>? get temporaryRendererStates;
@override
ViewOption get viewOption;
@override
HideState get hideUi;
Expand Down
2 changes: 2 additions & 0 deletions app/lib/handlers/handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ abstract class Handler<T> {

PreferredSizeWidget? getToolbar(DocumentBloc bloc) => null;

Map<Renderer, RendererState> getRendererStates(DocumentBloc bloc) => const {};

void dispose(DocumentBloc bloc) {}

@mustCallSuper
Expand Down
7 changes: 6 additions & 1 deletion app/lib/view_painter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math';

import 'package:butterfly/cubits/current_index.dart';
import 'package:butterfly/helpers/rect_helper.dart';
import 'package:butterfly/models/viewport.dart';
import 'package:butterfly/renderers/renderer.dart';
Expand Down Expand Up @@ -95,13 +96,15 @@ class ViewPainter extends CustomPainter {
final CameraTransform transform;
final ColorScheme? colorScheme;
final List<String> invisibleLayers;
final Map<Renderer, RendererState> states;

const ViewPainter(
this.document,
this.page,
this.info, {
this.currentArea,
this.invisibleLayers = const [],
this.states = const {},
this.renderBackground = true,
this.renderBaked = true,
required this.cameraViewport,
Expand Down Expand Up @@ -152,7 +155,9 @@ class ViewPainter extends CustomPainter {
canvas.scale(transform.size, transform.size);
canvas.translate(transform.position.dx, transform.position.dy);
for (var renderer in cameraViewport.unbakedElements) {
if (!invisibleLayers.contains(renderer.element.layer)) {
final state = states[renderer];
if (!invisibleLayers.contains(renderer.element.layer) &&
state != RendererState.hidden) {
final center = renderer.rect?.center;
final radian = renderer.rotation * (pi / 180);
if (center != null) {
Expand Down

0 comments on commit 6b75dbe

Please sign in to comment.