Skip to content

Commit

Permalink
Modify live grace period to count seconds since the latest change.
Browse files Browse the repository at this point in the history
Start to generate after no further changes have been made for N seconds.
  • Loading branch information
Acly committed Nov 29, 2024
1 parent 81f0195 commit e12d241
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions ai_diffusion/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from enum import Enum
import time
from typing import NamedTuple
from typing import Any, NamedTuple
from PyQt5.QtCore import QObject, QUuid, pyqtSignal, Qt
from PyQt5.QtGui import QPainter, QColor, QBrush
import uuid
Expand Down Expand Up @@ -858,6 +858,7 @@ class LiveWorkspace(QObject, ObservableProperties):
_model: Model
_last_input: WorkflowInput | None = None
_last_change: float = 0
_has_changes: bool = True
_result: Image | None = None
_result_composition: Image | None = None
_result_params: JobParams | None = None
Expand Down Expand Up @@ -907,14 +908,16 @@ async def _continue_generating(self):
while self.is_active:
if self._model.document.is_active:
new_input, job_params = self._model._prepare_live_workflow()
now = time.monotonic()
if self._last_input != new_input:
now = time.monotonic()
if self._last_change + settings.live_redraw_grace_period <= now:
await self._model._generate_live(new_input, job_params)
self._last_input = new_input
return
else:
self._last_change = time.monotonic()
self._last_input = new_input
self._last_change = now
self._has_changes = True
elapsed = now - self._last_change
if self._has_changes and elapsed >= settings.live_redraw_grace_period:
await self._model._generate_live(new_input, job_params)
self._has_changes = False
return
await asyncio.sleep(self._poll_rate)

def apply_result(self, layer_only=False):
Expand Down

0 comments on commit e12d241

Please sign in to comment.