-
-
Notifications
You must be signed in to change notification settings - Fork 438
feat: Automatic print start dialog on upload #2223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ammmze
wants to merge
2
commits into
mainsail-crew:develop
Choose a base branch
from
ammmze:launch-print-dialog
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<template> | ||
<start-print-dialog | ||
v-if="showPrintDialog" | ||
:bool="showPrintDialog" | ||
:file="file" | ||
current-path="" | ||
@closeDialog="onCloseDialog" /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Mixins, Watch } from 'vue-property-decorator' | ||
import BaseMixin from '@/components/mixins/base' | ||
import FilesMixin from '@/components/mixins/files' | ||
import Component from 'vue-class-component' | ||
import StartPrintDialog from '@/components/dialogs/StartPrintDialog.vue' | ||
import { FileStateGcodefile, FileStateSimpleFile } from '@/store/files/types' | ||
import { launchCount } from '@/plugins/pwaLaunchCount' | ||
|
||
@Component({ | ||
components: { | ||
StartPrintDialog, | ||
}, | ||
}) | ||
export default class TheLaunchFileHandler extends Mixins(BaseMixin, FilesMixin) { | ||
item: FileStateGcodefile | null = null | ||
private uploadedFile: null | { path: string; filename: string } = null | ||
|
||
get fileReady() { | ||
return this.file && this.file.metadataPulled | ||
} | ||
|
||
get file() { | ||
if (!this.uploadedFile) return null | ||
const { path, filename } = this.uploadedFile | ||
const files = this.$store.getters['files/getGcodeFiles'](path ? `/${path}` : path, false, true) | ||
return files.find((f: FileStateGcodefile) => f.filename === filename) | ||
} | ||
|
||
get canPrint() { | ||
return !this.isUpdating && !['error', 'printing', 'paused'].includes(this.printer_state) | ||
} | ||
|
||
get isUpdating() { | ||
return Boolean(this.$store.state.server.updateManager.updateResponse.application ?? '') | ||
} | ||
|
||
get showPrintDialog() { | ||
return this.fileReady && this.canPrint | ||
} | ||
|
||
@Watch('file') | ||
onFileChange(file: FileStateGcodefile) { | ||
if (file && !file.metadataPulled && !file.metadataRequested) { | ||
const filename = ['gcodes', file.path, file.filename].filter(Boolean).join('/') | ||
this.$store.dispatch('files/requestMetadata', [{ filename }]) | ||
} | ||
} | ||
|
||
get latestGcodeFile(): FileStateSimpleFile | null { | ||
return this.$store.state.files.latestGcodeFile as FileStateSimpleFile | null | ||
} | ||
|
||
get showPrintOnUpload(): boolean { | ||
return this.$store.state.gui.uiSettings.showPrintOnUpload | ||
} | ||
|
||
@Watch('latestGcodeFile') | ||
onLatestGcodeFileChange(file: FileStateSimpleFile | null) { | ||
const latestKnownGcodeFileTime = Number(localStorage.getItem('latestKnownGcodeFileTime') ?? 0) | ||
if (file && file.modified > latestKnownGcodeFileTime) { | ||
localStorage.setItem('latestKnownGcodeFileTime', String(file.modified)) | ||
if (latestKnownGcodeFileTime > 0 && this.showPrintOnUpload) | ||
this.uploadedFile = { path: file.path, filename: file.filename } | ||
} | ||
} | ||
|
||
mounted() { | ||
window.launchQueue?.setConsumer(this.onLaunch) | ||
if (this.latestGcodeFile) this.onLatestGcodeFileChange(this.latestGcodeFile) | ||
} | ||
|
||
beforeDestroy() { | ||
window.launchQueue?.setConsumer(() => undefined) | ||
} | ||
|
||
/** | ||
* Determine if the launch is a new file launch. This is somewhat quirky because the launch queue is is re-sent | ||
* when the page is reloaded, so to prevent re-processing the same launch when the page is reloaded, we calculate a | ||
* launch key, save it to session storage, and compare it to the last launch key. And we also check the launch count | ||
* to see if this is the first launch or a subsequent launch. This is needed because the launch queue is re-sent | ||
* when the page is reloaded, so we need to differentiate between the first launch and subsequent launches. | ||
* | ||
* @param files The files to check. | ||
*/ | ||
isNewFileLaunch(files: File[]) { | ||
const lastLaunchKey = sessionStorage.getItem('launchKey') | ||
const launchKey = files | ||
.map(({ name, lastModified, size }: File) => [name, lastModified, size].join('::')) | ||
.sort() | ||
.join(',') | ||
sessionStorage.setItem('launchKey', launchKey) | ||
|
||
return launchCount.value > 1 || launchKey !== lastLaunchKey | ||
} | ||
|
||
isGcodeFileHandle(file: FileSystemHandle): file is FileSystemFileHandle { | ||
if (file.kind !== 'file') return false | ||
return this.isGcodeFilename(file.name) // | ||
} | ||
|
||
onCloseDialog() { | ||
this.uploadedFile = null | ||
} | ||
|
||
async onLaunch(launchParams: LaunchParams) { | ||
// increment the launch count | ||
launchCount.value++ | ||
|
||
// Nothing to do when the queue is empty. | ||
if (!launchParams.files || !launchParams.files.length) { | ||
return | ||
} | ||
|
||
// Filter to only gcode files and get the files from the file handles | ||
const files = await Promise.all( | ||
launchParams.files.filter(this.isGcodeFileHandle).map((file: FileSystemFileHandle) => file.getFile()) | ||
) | ||
|
||
if (!this.isNewFileLaunch(files)) { | ||
console.log('Repeated first launch key, ignoring because the page was probably just reloaded.') | ||
return | ||
} | ||
this.uploadedFile = null | ||
|
||
const root = 'gcodes' | ||
const path = '' | ||
const uploadedFiles = await this.uploadFiles(files, { root, path }) | ||
|
||
if (uploadedFiles.length > 0) this.uploadedFile = { path, filename: uploadedFiles[0].name } | ||
} | ||
} | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { validGcodeExtensions } from '@/store/variables' | ||
import Vue from 'vue' | ||
import Component from 'vue-class-component' | ||
|
||
type FileUploadRoot = 'gcodes' | 'config' | ||
type FileUploadDestination = { root: FileUploadRoot; path?: string } | ||
type FileUploadDestinationGetter = (file: File) => FileUploadDestination | ||
|
||
@Component | ||
export default class FilesMixins extends Vue { | ||
loadingKeyForRoot(root?: FileUploadRoot) { | ||
if (root === 'config') { | ||
return 'configFileUpload' | ||
} | ||
return 'gcodeUpload' | ||
} | ||
|
||
isGcodeFilename(filename: string) { | ||
const extensionPos = filename.lastIndexOf('.') | ||
const extension = filename.slice(extensionPos) | ||
return validGcodeExtensions.includes(extension) | ||
} | ||
|
||
async uploadFiles(files: File[], destination: Partial<FileUploadDestination> | FileUploadDestinationGetter = {}) { | ||
const loadingKey = this.loadingKeyForRoot(typeof destination === 'object' ? destination.root : 'gcodes') | ||
await this.$store.dispatch('socket/addLoading', { name: loadingKey }) | ||
await this.$store.dispatch('files/uploadSetCurrentNumber', 0) | ||
await this.$store.dispatch('files/uploadSetMaxNumber', files.length) | ||
|
||
const uploadedFiles = [] | ||
for (const file of files) { | ||
await this.$store.dispatch('files/uploadIncrementCurrentNumber') | ||
const { path = '', root } = typeof destination === 'function' ? destination(file) : destination | ||
const pathWithoutPrefix = path.slice(0, 1) === '/' ? path.slice(1) : path | ||
const result = await this.$store.dispatch('files/uploadFile', { file, path: pathWithoutPrefix, root }) | ||
|
||
if (result !== false) { | ||
this.$toast.success(this.$t('Files.SuccessfullyUploaded', { filename: result }).toString()) | ||
uploadedFiles.push(file) | ||
} | ||
} | ||
|
||
await this.$store.dispatch('socket/removeLoading', { name: loadingKey }) | ||
return uploadedFiles | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ref } from 'vue' | ||
|
||
// This file is used to track the number of times the PWA has been launched | ||
// via launchQueue. This has been extracted from the launch handler because | ||
// if it is present in the launch handler, it will be reset during hot reload. | ||
// This throws off the functionality that determines if it is a new launch, | ||
// causing new launches to be treated as a relaunch and get ignored. | ||
export const launchCount = ref(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing i've kind of been on the fence with here is perhaps maybe we should add an additional condition that the modified timestamp is within the last 5 minutes or so? The reason being, if I've got multiple instances of mainsail that I use with the "showPrintOnUpload" setting enabled, then lets say i send a print and go to mainsail, the dialog is presented, and i go ahead an hit print (or cancel). Then later I open another mainsail instance with the setting turned on, it would see there is a new gcode file with a timestamp newer than what it had saved in its
localStorage
, so it would present the dialog for the other instance.Or alternatively, perhaps we save the
latestKnownGcodeFileTime
in the moonraker db, so we can track the latest one any of the mainsail instances have seen? The main caveat there is AFAICT the moonraker db stuff isn't actively updated (nor do I see a way to subscribe to changes to the db), so that means if there are multiple instances already open, they could have stalelatestKnownGcodeFileTime
attributes.