Skip to content

Commit

Permalink
Add print as export option
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 14, 2023
1 parent 6015b7d commit bee3bef
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 350 deletions.
4 changes: 3 additions & 1 deletion app/lib/actions/pdf_export.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import '../dialogs/pdf_export.dart';

class PdfExportIntent extends Intent {
final BuildContext context;
final bool print;

const PdfExportIntent(this.context);
const PdfExportIntent(this.context, [this.print = false]);
}

class PdfExportAction extends Action<PdfExportIntent> {
Expand Down Expand Up @@ -36,6 +37,7 @@ class PdfExportAction extends Action<PdfExportIntent> {
value: bloc,
child: PdfExportDialog(
areas: areas,
print: intent.print,
),
),
context: context);
Expand Down
18 changes: 17 additions & 1 deletion app/lib/dialogs/area/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,23 @@ ContextMenuBuilder buildGeneralAreaContextMenu(DocumentBloc bloc, Area area,
context: context);
},
child: Text(AppLocalizations.of(context).pdf),
)
),
MenuItemButton(
leadingIcon: const PhosphorIcon(PhosphorIconsLight.printer),
onPressed: () {
Navigator.of(context).pop();
showDialog<void>(
builder: (context) => BlocProvider.value(
value: bloc,
child: PdfExportDialog(
areas: [AreaPreset(area: area, name: area.name)],
print: true,
),
),
context: context);
},
child: Text(AppLocalizations.of(context).print),
),
],
),
ContextMenuItem(
Expand Down
20 changes: 13 additions & 7 deletions app/lib/dialogs/pdf_export.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:butterfly/api/save_data.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -7,16 +6,19 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';
import 'package:printing/printing.dart';

import '../bloc/document_bloc.dart';
import 'name.dart';

class PdfExportDialog extends StatefulWidget {
final List<AreaPreset> areas;
final bool print;

const PdfExportDialog({
super.key,
this.areas = const [],
this.print = false,
});

@override
Expand Down Expand Up @@ -137,13 +139,17 @@ class _PdfExportDialogState extends State<PdfExportDialog> {
onPressed: () => Navigator.of(context).pop(),
),
ElevatedButton(
child: Text(AppLocalizations.of(context).export),
child: Text(widget.print
? AppLocalizations.of(context).print
: AppLocalizations.of(context).export),
onPressed: () async {
Navigator.of(context).pop();
final document = await currentIndex
.renderPDF(state.data, state.info, areas: areas);
final data = await document.save();
exportPdf(context, data);
Printing.layoutPdf(
onLayout: (_) async => (await currentIndex
.renderPDF(state.data, state.info,
areas: areas))
.save(),
);
},
),
],
Expand Down Expand Up @@ -191,7 +197,7 @@ class _AreaPreview extends StatelessWidget {
min: 1,
max: 10,
onChanged: onQualityChanged,
label: AppLocalizations.of(context).quality,
header: Text(AppLocalizations.of(context).quality),
),
OutlinedButton(
onPressed: onRemove,
Expand Down
3 changes: 2 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -550,5 +550,6 @@
"show": "Show",
"pause": "Pause",
"stop": "Stop",
"refresh": "Refresh"
"refresh": "Refresh",
"print": "Print"
}
15 changes: 9 additions & 6 deletions app/lib/settings/connections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ class ConnectionsSettingsPage extends StatelessWidget {
}),
],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => showDialog<void>(
context: context, builder: (context) => const _AddRemoteDialog()),
label: Text(AppLocalizations.of(context).addConnection),
icon: const PhosphorIcon(PhosphorIconsLight.plus),
),
floatingActionButton: kIsWeb
? null
: FloatingActionButton.extended(
onPressed: () => showDialog<void>(
context: context,
builder: (context) => const _AddRemoteDialog()),
label: Text(AppLocalizations.of(context).addConnection),
icon: const PhosphorIcon(PhosphorIconsLight.plus),
),
body: Builder(builder: (context) {
if (kIsWeb) {
return Center(
Expand Down
19 changes: 17 additions & 2 deletions app/lib/views/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,30 @@ class _MainPopupMenu extends StatelessWidget {
},
child: Text(AppLocalizations.of(context).pdf),
),
MenuItemButton(
leadingIcon: const PhosphorIcon(PhosphorIconsLight.printer),
shortcut: const SingleActivator(
LogicalKeyboardKey.keyP,
control: true,
),
onPressed: () {
Actions.maybeInvoke<PdfExportIntent>(
context, PdfExportIntent(context, true));
},
child: Text(AppLocalizations.of(context).print),
),
],
leadingIcon:
const PhosphorIcon(PhosphorIconsLight.paperPlaneRight),
child: Text(AppLocalizations.of(context).export),
),
MenuItemButton(
leadingIcon: const PhosphorIcon(PhosphorIconsLight.package),
shortcut:
const SingleActivator(LogicalKeyboardKey.keyP, control: true),
shortcut: const SingleActivator(
LogicalKeyboardKey.keyP,
control: true,
alt: true,
),
onPressed: () {
Actions.maybeInvoke<PacksIntent>(context, PacksIntent(context));
},
Expand Down
126 changes: 71 additions & 55 deletions app/lib/views/home.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:butterfly/actions/settings.dart';
import 'package:butterfly/api/file_system/file_system.dart';
import 'package:butterfly/api/open.dart';
Expand Down Expand Up @@ -408,7 +410,6 @@ class _QuickstartHomeViewState extends State<_QuickstartHomeView> {

@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
Expand Down Expand Up @@ -479,60 +480,15 @@ class _QuickstartHomeViewState extends State<_QuickstartHomeView> {
(e) {
final thumbnail = e.getThumbnail();
final metadata = e.getMetadata()!;
return ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 200),
child: AspectRatio(
aspectRatio: 16 / 9,
child: Card(
elevation: 5,
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: () async {
GoRouter.of(context).pushReplacementNamed(
'new',
queryParameters: {
'path': metadata.directory
},
extra: e.createDocument().save());
},
child: Stack(
children: [
if (thumbnail?.isNotEmpty ?? false)
Align(
child: Image.memory(
thumbnail!,
fit: BoxFit.cover,
width: 640,
alignment: Alignment.center,
),
),
Align(
alignment: Alignment.bottomLeft,
child: Container(
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(8),
color: colorScheme.primaryContainer
.withAlpha(200),
),
child: Text(
metadata.name,
style: Theme.of(context)
.textTheme
.bodyLarge
?.copyWith(
color: colorScheme.onSurface,
),
),
),
),
],
),
),
),
));
return AssetCard(
metadata: metadata,
thumbnail: thumbnail,
onTap: () async {
GoRouter.of(context).pushReplacementNamed('new',
queryParameters: {'path': metadata.directory},
extra: e.createDocument().save());
},
);
},
).toList(),
);
Expand All @@ -542,3 +498,63 @@ class _QuickstartHomeViewState extends State<_QuickstartHomeView> {
);
}
}

class AssetCard extends StatelessWidget {
const AssetCard({
super.key,
required this.metadata,
required this.thumbnail,
required this.onTap,
});

final FileMetadata metadata;
final Uint8List? thumbnail;
final VoidCallback onTap;

@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
return ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 200),
child: AspectRatio(
aspectRatio: 16 / 9,
child: Card(
elevation: 5,
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: onTap,
child: Stack(
children: [
if (thumbnail?.isNotEmpty ?? false)
Align(
child: Image.memory(
thumbnail!,
fit: BoxFit.cover,
width: 640,
alignment: Alignment.center,
),
),
Align(
alignment: Alignment.bottomLeft,
child: Container(
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: colorScheme.primaryContainer.withAlpha(200),
),
child: Text(
metadata.name,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: colorScheme.onSurface,
),
),
),
),
],
),
),
),
));
}
}
6 changes: 3 additions & 3 deletions app/lib/views/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ class _ProjectPageState extends State<ProjectPage> {
LogicalKeyboardKey.shift,
LogicalKeyboardKey.keyN):
NewIntent(context, fromTemplate: true),
LogicalKeySet(LogicalKeyboardKey.control,
LogicalKeyboardKey.keyP):
ColorPaletteIntent(context),
LogicalKeySet(LogicalKeyboardKey.control,
LogicalKeyboardKey.keyB):
BackgroundIntent(context),
Expand Down Expand Up @@ -355,6 +352,9 @@ class _ProjectPageState extends State<ProjectPage> {
LogicalKeyboardKey.shift,
LogicalKeyboardKey.keyE):
PdfExportIntent(context),
LogicalKeySet(LogicalKeyboardKey.control,
LogicalKeyboardKey.keyP):
PdfExportIntent(context, true),
LogicalKeySet(
LogicalKeyboardKey.control,
LogicalKeyboardKey.alt,
Expand Down
8 changes: 4 additions & 4 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ packages:
dependency: "direct main"
description:
name: file_picker
sha256: be325344c1f3070354a1d84a231a1ba75ea85d413774ec4bdf444c023342e030
sha256: "903dd4ba13eae7cef64acc480e91bf54c3ddd23b5b90b639c170f3911e489620"
url: "https://pub.dev"
source: hosted
version: "5.5.0"
version: "6.0.0"
fixnum:
dependency: transitive
description:
Expand Down Expand Up @@ -611,10 +611,10 @@ packages:
dependency: "direct main"
description:
name: idb_shim
sha256: e22cbec15c3732ce53ad79927f106e7be7d4a66f836eb6270b51450c15bd7b84
sha256: cf1b3870ad63a6f5393edb351a9c9364e739731f2176c27f44d9645327c2cdd5
url: "https://pub.dev"
source: hosted
version: "2.3.1"
version: "2.3.2"
image:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ dependencies:
path_provider: ^2.1.1
shared_preferences: ^2.2.2
url_launcher: ^6.1.14
file_picker: ^5.5.0
file_picker: ^6.0.0
phosphor_flutter:
git:
url: https://github.com/CodeDoctorDE/phosphor-flutter
ref: f370dd2c25d3ea51ffb8f1c7c183bc9f33889c82
replay_bloc: ^0.2.4
share_plus: ^7.1.0
package_info_plus: ^4.1.0
idb_shim: ^2.3.1
idb_shim: ^2.3.2
go_router: ^11.1.4
xml: ^6.4.2
collection: ^1.18.0
Expand Down
3 changes: 3 additions & 0 deletions docs/docs/shortcuts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ Some of them are written below the buttons.
* `Ctrl` + `N`: New file
* `Ctrl` + `Shift` + `N`: New file from template
* `Ctrl` + `E`: Export file
* `Ctrl` + `P`: Print file
* `Ctrl` + `Shift` + `E`: Export file as image
* `Ctrl` + `Alt` + `E`: Export file as svg
* `Ctrl` + `Alt` + `Shift` + `E`: Export file as svg
* `Ctrl` + `Alt` + `P`: Open packs
* `Ctrl` + `Alt` + `S`: Open settings

### Project
Expand Down
Loading

0 comments on commit bee3bef

Please sign in to comment.