fix: fix UI bug and agent roles
This commit is contained in:
@@ -17,12 +17,19 @@ from __future__ import annotations
|
||||
|
||||
from PySide6.QtCore import Qt, Signal
|
||||
from PySide6.QtWidgets import (
|
||||
QGridLayout, QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget,
|
||||
QGridLayout, QHBoxLayout, QLabel, QPushButton, QSizePolicy, QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from ...i18n import on_language_changed, tr
|
||||
from ...ui.icons import icon
|
||||
|
||||
#: Width of the four-card block. A FLOOR for the cap, not a fixed number: the
|
||||
#: block never gets narrower than this, but the cap grows when the text needs
|
||||
#: more room. One number measured against English at 100% scale is exactly how
|
||||
#: the titles end up clipped in Vietnamese and Japanese (``qt_pitfalls.md`` P02).
|
||||
_GRID_WIDTH_FLOOR = 460
|
||||
|
||||
#: (khoá tiêu đề, khoá mô tả, khoá câu gợi ý, tên icon) cho từng thẻ.
|
||||
_CARDS = (
|
||||
("welcome.card_docs", "welcome.card_docs_sub", "welcome.prompt_docs", "file"),
|
||||
@@ -43,6 +50,11 @@ class _Card(QPushButton):
|
||||
self._sub_key = sub_key
|
||||
self.setObjectName("welcomeCard")
|
||||
self.setCursor(Qt.PointingHandCursor)
|
||||
# Vertically it must be able to GROW: QPushButton defaults to Fixed, so
|
||||
# a card whose description fits on one line was centred inside a row as
|
||||
# tall as its two-line neighbour — two cards side by side, staggered and
|
||||
# of different heights.
|
||||
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.MinimumExpanding)
|
||||
|
||||
row = QHBoxLayout(self)
|
||||
row.setContentsMargins(12, 10, 12, 10)
|
||||
@@ -67,6 +79,23 @@ class _Card(QPushButton):
|
||||
|
||||
self.retranslate()
|
||||
|
||||
# ---- size: taken from the child layout, not from the button's own text -- #
|
||||
# QPushButton computes sizeHint/minimumSizeHint from ITS OWN text and icon
|
||||
# and ignores the child layout. This card leaves both of those empty on
|
||||
# purpose (the two QLabels below draw the text; a non-empty text() prints
|
||||
# on top of them), so the button reported 54x15 while its layout asked for
|
||||
# 258x48 — the two QLabels and the icon cell were handed 0px of height, and
|
||||
# what the user saw was four empty frames with no text and no icon. The two
|
||||
# overrides below report the size the content actually needs.
|
||||
|
||||
def sizeHint(self): # noqa: N802 - Qt override
|
||||
"""Size the card's own content needs, not the (empty) button label."""
|
||||
return self.layout().sizeHint()
|
||||
|
||||
def minimumSizeHint(self): # noqa: N802 - Qt override
|
||||
"""Floor comes from the child layout, for the same reason."""
|
||||
return self.layout().minimumSize()
|
||||
|
||||
def retranslate(self) -> None:
|
||||
"""Áp lại chữ theo ngôn ngữ đang chọn."""
|
||||
self.title_label.setText(tr(self._title_key))
|
||||
@@ -74,6 +103,9 @@ class _Card(QPushButton):
|
||||
# Nhãn của chính QPushButton để rỗng — chữ do hai QLabel bên trong vẽ,
|
||||
# đặt cả hai chỗ sẽ in đè lên nhau.
|
||||
self.setAccessibleName(tr(self._title_key))
|
||||
# New text means a new content size — Japanese and Vietnamese are not
|
||||
# the same length, and sizeHint is computed from those two QLabels.
|
||||
self.updateGeometry()
|
||||
|
||||
|
||||
class ChatWelcome(QWidget):
|
||||
@@ -118,19 +150,19 @@ class ChatWelcome(QWidget):
|
||||
|
||||
grid_row = QHBoxLayout()
|
||||
grid_row.addStretch(1)
|
||||
grid_host = QWidget()
|
||||
grid_host.setMaximumWidth(460)
|
||||
grid = QGridLayout(grid_host)
|
||||
grid.setContentsMargins(0, 0, 0, 0)
|
||||
grid.setSpacing(10)
|
||||
self._grid_host = QWidget()
|
||||
self._grid = QGridLayout(self._grid_host)
|
||||
self._grid.setContentsMargins(0, 0, 0, 0)
|
||||
self._grid.setSpacing(10)
|
||||
self.cards: list = []
|
||||
for i, (title_key, sub_key, prompt_key, icon_name) in enumerate(_CARDS):
|
||||
card = _Card(title_key, sub_key, icon_name)
|
||||
card.clicked.connect(
|
||||
lambda _checked=False, key=prompt_key: self.suggestion_picked.emit(tr(key)))
|
||||
grid.addWidget(card, i // 2, i % 2)
|
||||
self._grid.addWidget(card, i // 2, i % 2)
|
||||
self.cards.append(card)
|
||||
grid_row.addWidget(grid_host)
|
||||
self._apply_grid_width()
|
||||
grid_row.addWidget(self._grid_host)
|
||||
grid_row.addStretch(1)
|
||||
root.addLayout(grid_row)
|
||||
|
||||
@@ -138,15 +170,28 @@ class ChatWelcome(QWidget):
|
||||
|
||||
on_language_changed(self._retranslate)
|
||||
|
||||
def _apply_grid_width(self) -> None:
|
||||
"""Cap the card block at the wider of the design width and what text needs.
|
||||
|
||||
Recomputed on every language change: ``vi`` and ``ja`` labels are not
|
||||
the same length as ``en``, and a cap fixed at build time clips whichever
|
||||
language happens to be longer.
|
||||
"""
|
||||
self._grid_host.setMaximumWidth(
|
||||
max(_GRID_WIDTH_FLOOR, self._grid.sizeHint().width()))
|
||||
|
||||
# ---- nội dung ----------------------------------------------------------
|
||||
|
||||
def refresh(self, user_name: str = "", project: str = "",
|
||||
files: int = -1, skills: int = -1) -> None:
|
||||
files: int = -1) -> None:
|
||||
"""Cập nhật lời chào và dòng bối cảnh.
|
||||
|
||||
``files`` / ``skills`` bằng ``-1`` nghĩa là KHÔNG BIẾT, và phần đó bị bỏ
|
||||
khỏi dòng meta — thà thiếu một mảnh còn hơn hiện số 0 mà người dùng vừa
|
||||
thấy có tệp trong thư mục.
|
||||
``files`` bằng ``-1`` nghĩa là KHÔNG BIẾT, và phần đó bị bỏ khỏi dòng
|
||||
meta — thà thiếu một mảnh còn hơn hiện số 0 mà người dùng vừa thấy có
|
||||
tệp trong thư mục.
|
||||
|
||||
Không hiện số skill đang bật: nó không giúp người dùng quyết định gõ gì
|
||||
vào ô nhập, mà lại chiếm một phần ba của dòng bối cảnh.
|
||||
"""
|
||||
self._user_name = (user_name or "").strip()
|
||||
parts = []
|
||||
@@ -154,8 +199,6 @@ class ChatWelcome(QWidget):
|
||||
parts.append(tr("welcome.meta_project", name=project.strip()))
|
||||
if files >= 0:
|
||||
parts.append(tr("welcome.meta_files", n=files))
|
||||
if skills >= 0:
|
||||
parts.append(tr("welcome.meta_skills", n=skills))
|
||||
self._meta_parts = parts
|
||||
self._retranslate()
|
||||
|
||||
@@ -169,3 +212,4 @@ class ChatWelcome(QWidget):
|
||||
self.meta_label.setVisible(bool(self._meta_parts))
|
||||
for card in self.cards:
|
||||
card.retranslate()
|
||||
self._apply_grid_width()
|
||||
|
||||
Reference in New Issue
Block a user