File size: 7,868 Bytes
71174bc | 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | <template>
<div class="progress-bar-container bg-light hide-on-app-closed">
<div class="progress" style="height: 20px; border-radius: 0">
<div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar"
:style="{ width: progressPercent + '%' }" :aria-valuenow="progressPercent" aria-valuemin="0"
aria-valuemax="100">
<!-- Conditionally render text only when progress > 0 -->
<span v-if="displayProgress > 0">
{{ displayMessage }} ({{ progressPercent }}%)
</span>
</div>
</div>
</div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { Prop, Watch } from "vue-property-decorator";
const INACTIVITY_TIMEOUT_MS = 2 * 60 * 1000; // 2 minutes
const COMPLETION_PAUSE_MS = 250; // 0.25 seconds
const MIN_DISPLAY_PROGRESS = 0.05; // 5%
/**
* A simple progress bar component that manages its own display logic,
* including minimum display, completion pause, and inactivity timeout.
* The gray track is always visible, the colored bar shows progress.
*/
@Options({})
export default class ProgressBar extends Vue {
/** The actual highest progress value (0-1) from the parent. Defaults to 0. */
@Prop({ type: Number, default: 0 })
progress!: number;
/** Indicates if any job is actively running in the parent. Defaults to false. */
@Prop({ type: Boolean, default: false })
active!: boolean;
displayProgress = 0; // The progress value shown (0-1), controls the inner bar width
displayMessage = "";
completionPauseActive = false;
completionPauseTimeout: number | null = null;
inactivityTimeout: number | null = null;
/**
* Calculates the display percentage (0-100).
*
* @returns {number} The display progress as an integer percentage.
*/
get progressPercent(): number {
const percent = Math.round(this.displayProgress * 100);
return Math.max(0, Math.min(100, percent)); // Clamp
}
/** Resets the internal state (progress/message/timers). */
private resetInternalState() {
// Don't change visibility, container is always visible
this.displayProgress = 0;
this.displayMessage = "";
this.completionPauseActive = false;
this.clearCompletionPauseTimeout();
this.clearInactivityTimeout();
}
/** Clears the inactivity timeout. */
private clearInactivityTimeout() {
if (this.inactivityTimeout) {
clearTimeout(this.inactivityTimeout);
this.inactivityTimeout = null;
}
}
/** Starts or restarts the inactivity timeout. */
private startInactivityTimeout() {
this.clearInactivityTimeout();
this.inactivityTimeout = window.setTimeout(() => {
console.warn("Progress bar timed out due to inactivity.");
this.resetInternalState(); // Reset if inactive
this.inactivityTimeout = null;
}, INACTIVITY_TIMEOUT_MS);
}
/** Clears the completion pause timeout. */
private clearCompletionPauseTimeout() {
if (this.completionPauseTimeout) {
clearTimeout(this.completionPauseTimeout);
this.completionPauseTimeout = null;
}
}
/** Initiates the 100% completion pause. */
private startCompletionPause() {
this.clearInactivityTimeout();
this.cancelCompletionPause(); // Clear any previous pause
this.displayProgress = 1; // Force 100% width
this.displayMessage = "Finishing...";
// displayVisible is always true now
this.completionPauseActive = true;
this.completionPauseTimeout = window.setTimeout(() => {
this.completionPauseActive = false; // End pause state
this.completionPauseTimeout = null;
// Reset progress and message, bar stays visible but empty
if (!this.active) { // Only reset if no new job started
this.displayProgress = 0;
this.displayMessage = "";
} else {
// If a job started during the pause, re-evaluate state
this.evaluateState();
}
}, COMPLETION_PAUSE_MS);
}
/** Cancels an active completion pause. */
private cancelCompletionPause() {
this.clearCompletionPauseTimeout();
this.completionPauseActive = false;
}
/** Evaluates the current state based on props and updates internal display state. */
private evaluateState() {
// If pausing, do nothing here, let the timeout handle it.
if (this.completionPauseActive) {
return;
}
if (this.active) {
// --- Jobs are active ---
const potentialNewProgress = Math.max(MIN_DISPLAY_PROGRESS, this.progress);
// Only update if progress increases or if bar was previously empty
if (potentialNewProgress > this.displayProgress || this.displayProgress === 0) {
this.displayProgress = potentialNewProgress;
}
this.displayMessage = (this.progressPercent > 15) ? "Job running" : "";
// displayVisible is always true now
this.startInactivityTimeout(); // Keep resetting inactivity timer
this.cancelCompletionPause(); // Ensure no pause is active
} else {
// --- No jobs are active ---
this.clearInactivityTimeout(); // Stop inactivity timer
// Check if the internal bar was showing progress before this update
if (this.displayProgress > 0) {
// Jobs just finished (internal bar was > 0, now inactive)
this.startCompletionPause(); // Start the 100% pause
} else {
// Was already inactive and bar was empty, ensure reset state
this.resetInternalState();
}
}
}
/** Watches the 'active' prop. Calls evaluateState on change. */
@Watch("active")
onActiveChanged() {
this.evaluateState();
}
/** Watches the 'progress' prop. Calls evaluateState on change. */
@Watch("progress")
onProgressChanged() {
this.evaluateState();
}
/** Lifecycle hook: Clear timeouts when component is destroyed. */
beforeUnmount() {
this.clearInactivityTimeout();
this.clearCompletionPauseTimeout();
}
/** Lifecycle hook: Initialize state when component is created/mounted. */
mounted() {
this.evaluateState(); // Set initial state based on props
}
}
</script>
<style scoped lang="scss">
.progress-bar-container {
width: 100%;
/* Optional: Add styling like padding, borders etc. */
// padding: 5px 0;
border-top: 1px solid #dee2e6;
/* Subtle top border */
z-index: 50;
/* Ensure it's above some elements but below menus/modals */
flex-shrink: 0;
/* Prevent shrinking when in a flex container */
}
.progress {
background-color: #e9ecef;
/* Track color */
overflow: hidden;
/* Important for the inner bar display */
height: 20px;
/* Match inner bar height */
border-radius: 0;
/* Remove default rounding if desired */
}
.progress-bar {
/* Customize bar appearance */
background-color: #0d6efd;
/* Bootstrap primary blue */
color: white;
text-align: center;
white-space: nowrap;
font-size: 0.85rem;
line-height: 20px;
/* Match height for vertical centering */
transition: width 0.3s ease-in-out;
/* Ensure bar starts from the left */
display: flex;
justify-content: center;
align-items: center;
}
/* Ensure text is only visible when the bar has width */
.progress-bar span {
display: inline-block;
/* Or block */
}
</style> |