Skip to content
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

Make positive input prompt drag resizable. #1236

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ai_diffusion/ui/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ def keyPressEvent(self, a0: QKeyEvent | None):
super().keyPressEvent(a0)


class TextPromptResizeWidget(QWidget):
"""Handles drag-based resizing for TextPromptWidget."""

def __init__(self, text_prompt: TextPromptWidget):
super().__init__(text_prompt)
self.setCursor(Qt.CursorShape.SizeVerCursor)
self.setFixedHeight(5)
self._dragging = False
self._text_prompt = text_prompt

def mousePressEvent(self, a0: QMouseEvent | None) -> None:
if ensure(a0).button() == Qt.MouseButton.LeftButton:
self._dragging = True

def mouseReleaseEvent(self, a0: QMouseEvent | None) -> None:
self._dragging = False

def mouseMoveEvent(self, a0: QMouseEvent | None) -> None:
if not self._dragging:
return
new_height = self.mapToParent(ensure(a0).pos()).y() - self._text_prompt.contentsRect().top()
fm = QFontMetrics(ensure(self._text_prompt._multi.document()).defaultFont())
new_line_count = round((new_height - 13) / fm.lineSpacing())
if 1 <= new_line_count <= 10:
settings.prompt_line_count = new_line_count
self._text_prompt.line_count = new_line_count


class TextPromptWidget(QFrame):
"""Wraps a single or multi-line text widget, with ability to switch between them.
Using QPlainTextEdit set to a single line doesn't work properly because it still
Expand Down Expand Up @@ -471,6 +499,9 @@ def __init__(self, line_count=2, is_negative=False, parent=None):
self._layout.addWidget(self._multi)
self._layout.addWidget(self._single)

if not is_negative:
self._layout.addWidget(TextPromptResizeWidget(self))

palette: QPalette = self._multi.palette()
self._base_color = palette.color(QPalette.ColorRole.Base)
self.is_negative = self._is_negative
Expand Down
Loading