Skip to content

Commit

Permalink
Add render resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 13, 2024
1 parent 9142010 commit 64bdb79
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 81 deletions.
15 changes: 11 additions & 4 deletions app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
double? pixelRatio,
bool reset = false,
bool resetAllLayers = false}) async {
final cameraViewport = state.cameraViewport;
var cameraViewport = state.cameraViewport;
final resolution = state.settingsCubit.state.renderResolution;
var size = viewportSize ?? cameraViewport.toSize();
final ratio = pixelRatio ?? cameraViewport.pixelRatio;
if (size.height <= 0 || size.width <= 0) {
Expand Down Expand Up @@ -516,6 +517,8 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
size = Size(bottomRight.dx - topLeft.dx, bottomRight.dy - topLeft.dy) *
transform.size;
}
rect = resolution.getRect(rect);
final renderTransform = transform.improve(resolution, size);
final document = blocState.data;
final page = blocState.page;
final info = blocState.info;
Expand All @@ -539,6 +542,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
..addAll(renderers);
}
canvas.scale(ratio);
size *= resolution.multiplier;

// Wait one frame
await Future.delayed(const Duration(milliseconds: 1));
Expand All @@ -547,7 +551,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
document,
page,
info,
transform: transform,
transform: renderTransform,
states: state.allRendererStates,
cameraViewport: reset
? cameraViewport.unbake(
Expand All @@ -557,6 +561,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
visibleElements: visibleElements,
)
: cameraViewport,
resolution: resolution,
renderBackground: false,
renderBaked: !reset,
renderBakedLayers: false,
Expand Down Expand Up @@ -610,6 +615,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
info,
transform: transform,
states: state.allRendererStates,
resolution: resolution,
cameraViewport: cameraViewport.unbake(
unbakedElements: visibleElements
.where((e) => belowLayers.contains(e.layer))
Expand All @@ -623,6 +629,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
info,
transform: transform,
states: state.allRendererStates,
resolution: resolution,
cameraViewport: cameraViewport.unbake(
unbakedElements: visibleElements
.where((e) => aboveLayers.contains(e.layer))
Expand All @@ -645,8 +652,8 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
width: size.width.ceil(),
pixelRatio: ratio,
scale: transform.size,
x: transform.position.dx,
y: transform.position.dy,
x: rect.left,
y: rect.top,
image: newImage,
bakedElements: renderers,
unbakedElements: currentRenderers,
Expand Down
31 changes: 31 additions & 0 deletions app/lib/cubits/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ enum ToolbarSize {
};
}

enum RenderResolution {
performance(1),
normal(1.5),
high(2);

final double multiplier;

const RenderResolution(this.multiplier);

Rect getRect(Rect rect) {
final width = rect.width * multiplier;
final height = rect.height * multiplier;
return Rect.fromCenter(
center: rect.center,
width: width,
height: height,
);
}
}

enum PlatformTheme {
system,
mobile,
Expand Down Expand Up @@ -220,6 +240,7 @@ class ButterflySettings with _$ButterflySettings, LeapSettings {
@Default(true) bool colorToolbarEnabled,
@Default(OptionsPanelPosition.top)
OptionsPanelPosition optionsPanelPosition,
@Default(RenderResolution.normal) RenderResolution renderResolution,
}) = _ButterflySettings;

factory ButterflySettings.fromPrefs(SharedPreferences prefs) {
Expand Down Expand Up @@ -319,6 +340,7 @@ class ButterflySettings with _$ButterflySettings, LeapSettings {
? OptionsPanelPosition.values
.byName(prefs.getString('options_panel_position')!)
: OptionsPanelPosition.top,
renderResolution: prefs.containsKey('render_resolution') ? RenderResolution.values.byName(prefs.getString('render_resolution')!) : RenderResolution.normal,
);
}

Expand Down Expand Up @@ -389,6 +411,7 @@ class ButterflySettings with _$ButterflySettings, LeapSettings {
await prefs.setBool('color_toolbar_enabled', colorToolbarEnabled);
await prefs.setBool('show_save_button', showSaveButton);
await prefs.setString('options_panel_position', optionsPanelPosition.name);
await prefs.setString('render_resolution', renderResolution.name);
}

ExternalStorage? getRemote(String? identifier) {
Expand Down Expand Up @@ -920,4 +943,12 @@ class SettingsCubit extends Cubit<ButterflySettings>
}

Future<void> resetColorToolbarEnabled() => changeColorToolbarEnabled(true);

Future<void> changeRenderResolution(RenderResolution value) {
emit(state.copyWith(renderResolution: value));
return save();
}

Future<void> resetRenderResolution() =>
changeRenderResolution(RenderResolution.normal);
}
135 changes: 79 additions & 56 deletions app/lib/cubits/settings.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ mixin _$ButterflySettings {
bool get colorToolbarEnabled => throw _privateConstructorUsedError;
OptionsPanelPosition get optionsPanelPosition =>
throw _privateConstructorUsedError;
RenderResolution get renderResolution => throw _privateConstructorUsedError;

/// Create a copy of ButterflySettings
/// with the given fields replaced by the non-null parameter values.
Expand Down Expand Up @@ -429,7 +430,8 @@ abstract class $ButterflySettingsCopyWith<$Res> {
UtilitiesState utilities,
StartupBehavior onStartup,
bool colorToolbarEnabled,
OptionsPanelPosition optionsPanelPosition});
OptionsPanelPosition optionsPanelPosition,
RenderResolution renderResolution});

$InputConfigurationCopyWith<$Res> get inputConfiguration;
$UtilitiesStateCopyWith<$Res> get utilities;
Expand Down Expand Up @@ -498,6 +500,7 @@ class _$ButterflySettingsCopyWithImpl<$Res, $Val extends ButterflySettings>
Object? onStartup = null,
Object? colorToolbarEnabled = null,
Object? optionsPanelPosition = null,
Object? renderResolution = null,
}) {
return _then(_value.copyWith(
theme: null == theme
Expand Down Expand Up @@ -692,6 +695,10 @@ class _$ButterflySettingsCopyWithImpl<$Res, $Val extends ButterflySettings>
? _value.optionsPanelPosition
: optionsPanelPosition // ignore: cast_nullable_to_non_nullable
as OptionsPanelPosition,
renderResolution: null == renderResolution
? _value.renderResolution
: renderResolution // ignore: cast_nullable_to_non_nullable
as RenderResolution,
) as $Val);
}

Expand Down Expand Up @@ -773,7 +780,8 @@ abstract class _$$ButterflySettingsImplCopyWith<$Res>
UtilitiesState utilities,
StartupBehavior onStartup,
bool colorToolbarEnabled,
OptionsPanelPosition optionsPanelPosition});
OptionsPanelPosition optionsPanelPosition,
RenderResolution renderResolution});

@override
$InputConfigurationCopyWith<$Res> get inputConfiguration;
Expand Down Expand Up @@ -842,6 +850,7 @@ class __$$ButterflySettingsImplCopyWithImpl<$Res>
Object? onStartup = null,
Object? colorToolbarEnabled = null,
Object? optionsPanelPosition = null,
Object? renderResolution = null,
}) {
return _then(_$ButterflySettingsImpl(
theme: null == theme
Expand Down Expand Up @@ -1036,6 +1045,10 @@ class __$$ButterflySettingsImplCopyWithImpl<$Res>
? _value.optionsPanelPosition
: optionsPanelPosition // ignore: cast_nullable_to_non_nullable
as OptionsPanelPosition,
renderResolution: null == renderResolution
? _value.renderResolution
: renderResolution // ignore: cast_nullable_to_non_nullable
as RenderResolution,
));
}
}
Expand Down Expand Up @@ -1092,7 +1105,8 @@ class _$ButterflySettingsImpl extends _ButterflySettings
this.utilities = const UtilitiesState(),
this.onStartup = StartupBehavior.openHomeScreen,
this.colorToolbarEnabled = true,
this.optionsPanelPosition = OptionsPanelPosition.top})
this.optionsPanelPosition = OptionsPanelPosition.top,
this.renderResolution = RenderResolution.normal})
: _history = history,
_connections = connections,
_starred = starred,
Expand Down Expand Up @@ -1273,10 +1287,13 @@ class _$ButterflySettingsImpl extends _ButterflySettings
@override
@JsonKey()
final OptionsPanelPosition optionsPanelPosition;
@override
@JsonKey()
final RenderResolution renderResolution;

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return 'ButterflySettings(theme: $theme, density: $density, localeTag: $localeTag, documentPath: $documentPath, gestureSensitivity: $gestureSensitivity, touchSensitivity: $touchSensitivity, selectSensitivity: $selectSensitivity, scrollSensitivity: $scrollSensitivity, penOnlyInput: $penOnlyInput, inputGestures: $inputGestures, design: $design, bannerVisibility: $bannerVisibility, history: $history, navigatorEnabled: $navigatorEnabled, zoomEnabled: $zoomEnabled, lastVersion: $lastVersion, connections: $connections, defaultRemote: $defaultRemote, nativeTitleBar: $nativeTitleBar, startInFullScreen: $startInFullScreen, navigationRail: $navigationRail, syncMode: $syncMode, inputConfiguration: $inputConfiguration, fallbackPack: $fallbackPack, starred: $starred, defaultTemplate: $defaultTemplate, navigatorPage: $navigatorPage, navigatorPosition: $navigatorPosition, toolbarPosition: $toolbarPosition, toolbarSize: $toolbarSize, sortBy: $sortBy, sortOrder: $sortOrder, imageScale: $imageScale, pdfQuality: $pdfQuality, platformTheme: $platformTheme, recentColors: $recentColors, flags: $flags, spreadPages: $spreadPages, highContrast: $highContrast, gridView: $gridView, autosave: $autosave, showSaveButton: $showSaveButton, toolbarRows: $toolbarRows, hideCursorWhileDrawing: $hideCursorWhileDrawing, utilities: $utilities, onStartup: $onStartup, colorToolbarEnabled: $colorToolbarEnabled, optionsPanelPosition: $optionsPanelPosition)';
return 'ButterflySettings(theme: $theme, density: $density, localeTag: $localeTag, documentPath: $documentPath, gestureSensitivity: $gestureSensitivity, touchSensitivity: $touchSensitivity, selectSensitivity: $selectSensitivity, scrollSensitivity: $scrollSensitivity, penOnlyInput: $penOnlyInput, inputGestures: $inputGestures, design: $design, bannerVisibility: $bannerVisibility, history: $history, navigatorEnabled: $navigatorEnabled, zoomEnabled: $zoomEnabled, lastVersion: $lastVersion, connections: $connections, defaultRemote: $defaultRemote, nativeTitleBar: $nativeTitleBar, startInFullScreen: $startInFullScreen, navigationRail: $navigationRail, syncMode: $syncMode, inputConfiguration: $inputConfiguration, fallbackPack: $fallbackPack, starred: $starred, defaultTemplate: $defaultTemplate, navigatorPage: $navigatorPage, navigatorPosition: $navigatorPosition, toolbarPosition: $toolbarPosition, toolbarSize: $toolbarSize, sortBy: $sortBy, sortOrder: $sortOrder, imageScale: $imageScale, pdfQuality: $pdfQuality, platformTheme: $platformTheme, recentColors: $recentColors, flags: $flags, spreadPages: $spreadPages, highContrast: $highContrast, gridView: $gridView, autosave: $autosave, showSaveButton: $showSaveButton, toolbarRows: $toolbarRows, hideCursorWhileDrawing: $hideCursorWhileDrawing, utilities: $utilities, onStartup: $onStartup, colorToolbarEnabled: $colorToolbarEnabled, optionsPanelPosition: $optionsPanelPosition, renderResolution: $renderResolution)';
}

@override
Expand Down Expand Up @@ -1332,7 +1349,8 @@ class _$ButterflySettingsImpl extends _ButterflySettings
..add(DiagnosticsProperty('utilities', utilities))
..add(DiagnosticsProperty('onStartup', onStartup))
..add(DiagnosticsProperty('colorToolbarEnabled', colorToolbarEnabled))
..add(DiagnosticsProperty('optionsPanelPosition', optionsPanelPosition));
..add(DiagnosticsProperty('optionsPanelPosition', optionsPanelPosition))
..add(DiagnosticsProperty('renderResolution', renderResolution));
}

@override
Expand Down Expand Up @@ -1428,7 +1446,9 @@ class _$ButterflySettingsImpl extends _ButterflySettings
(identical(other.colorToolbarEnabled, colorToolbarEnabled) ||
other.colorToolbarEnabled == colorToolbarEnabled) &&
(identical(other.optionsPanelPosition, optionsPanelPosition) ||
other.optionsPanelPosition == optionsPanelPosition));
other.optionsPanelPosition == optionsPanelPosition) &&
(identical(other.renderResolution, renderResolution) ||
other.renderResolution == renderResolution));
}

@override
Expand Down Expand Up @@ -1481,7 +1501,8 @@ class _$ButterflySettingsImpl extends _ButterflySettings
utilities,
onStartup,
colorToolbarEnabled,
optionsPanelPosition
optionsPanelPosition,
renderResolution
]);

/// Create a copy of ButterflySettings
Expand All @@ -1496,55 +1517,55 @@ class _$ButterflySettingsImpl extends _ButterflySettings

abstract class _ButterflySettings extends ButterflySettings {
const factory _ButterflySettings(
{final ThemeMode theme,
final ThemeDensity density,
final String localeTag,
final String documentPath,
final double gestureSensitivity,
final double touchSensitivity,
final double selectSensitivity,
final double scrollSensitivity,
final bool penOnlyInput,
final bool inputGestures,
final String design,
final BannerVisibility bannerVisibility,
final List<AssetLocation> history,
final bool navigatorEnabled,
final bool zoomEnabled,
final String? lastVersion,
final List<ExternalStorage> connections,
final String defaultRemote,
final bool nativeTitleBar,
final bool startInFullScreen,
final bool navigationRail,
final SyncMode syncMode,
final InputConfiguration inputConfiguration,
final String fallbackPack,
final List<String> starred,
final String defaultTemplate,
final NavigatorPage navigatorPage,
final NavigatorPosition navigatorPosition,
final ToolbarPosition toolbarPosition,
final ToolbarSize toolbarSize,
final SortBy sortBy,
final SortOrder sortOrder,
final double imageScale,
final double pdfQuality,
final PlatformTheme platformTheme,
final List<int> recentColors,
final List<String> flags,
final bool spreadPages,
final bool highContrast,
final bool gridView,
final bool autosave,
final bool showSaveButton,
final int toolbarRows,
final bool hideCursorWhileDrawing,
final UtilitiesState utilities,
final StartupBehavior onStartup,
final bool colorToolbarEnabled,
final OptionsPanelPosition optionsPanelPosition}) =
_$ButterflySettingsImpl;
{final ThemeMode theme,
final ThemeDensity density,
final String localeTag,
final String documentPath,
final double gestureSensitivity,
final double touchSensitivity,
final double selectSensitivity,
final double scrollSensitivity,
final bool penOnlyInput,
final bool inputGestures,
final String design,
final BannerVisibility bannerVisibility,
final List<AssetLocation> history,
final bool navigatorEnabled,
final bool zoomEnabled,
final String? lastVersion,
final List<ExternalStorage> connections,
final String defaultRemote,
final bool nativeTitleBar,
final bool startInFullScreen,
final bool navigationRail,
final SyncMode syncMode,
final InputConfiguration inputConfiguration,
final String fallbackPack,
final List<String> starred,
final String defaultTemplate,
final NavigatorPage navigatorPage,
final NavigatorPosition navigatorPosition,
final ToolbarPosition toolbarPosition,
final ToolbarSize toolbarSize,
final SortBy sortBy,
final SortOrder sortOrder,
final double imageScale,
final double pdfQuality,
final PlatformTheme platformTheme,
final List<int> recentColors,
final List<String> flags,
final bool spreadPages,
final bool highContrast,
final bool gridView,
final bool autosave,
final bool showSaveButton,
final int toolbarRows,
final bool hideCursorWhileDrawing,
final UtilitiesState utilities,
final StartupBehavior onStartup,
final bool colorToolbarEnabled,
final OptionsPanelPosition optionsPanelPosition,
final RenderResolution renderResolution}) = _$ButterflySettingsImpl;
const _ButterflySettings._() : super._();

@override
Expand Down Expand Up @@ -1643,6 +1664,8 @@ abstract class _ButterflySettings extends ButterflySettings {
bool get colorToolbarEnabled;
@override
OptionsPanelPosition get optionsPanelPosition;
@override
RenderResolution get renderResolution;

/// Create a copy of ButterflySettings
/// with the given fields replaced by the non-null parameter values.
Expand Down
6 changes: 6 additions & 0 deletions app/lib/cubits/transform.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math';

import 'package:butterfly/cubits/settings.dart';
import 'package:butterfly/helpers/point.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:equatable/equatable.dart';
Expand Down Expand Up @@ -106,6 +107,11 @@ class CameraTransform extends Equatable {
CameraTransform withFrictionless(Offset position, double size) {
return CameraTransform(this.position - position, this.size - size, null);
}

CameraTransform improve(RenderResolution resolution, Size size) {
final rect = resolution.getRect(position & size);
return CameraTransform(rect.topLeft, this.size, friction);
}
}

class TransformCubit extends Cubit<CameraTransform> {
Expand Down
Loading

0 comments on commit 64bdb79

Please sign in to comment.