From 77fb0fb70861dbec74103566331d67f9fdd99609 Mon Sep 17 00:00:00 2001 From: Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> Date: Fri, 20 Sep 2024 03:33:28 -0700 Subject: [PATCH] =?UTF-8?q?Possible=20to=20show=20=E2=80=9Clast=E2=80=9D?= =?UTF-8?q?=20subdeck=20name=20in=20Browser=3F=20(#3387)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * elide middle of deck names * Update CONTRIBUTORS * made elide mode enum * add elide mode field * fix enum number * remove dataclass decorator * Update CONTRIBUTORS * format rust code * Update CONTRIBUTORS * formatting * Update CONTRIBUTORS * fix type hint * Update CONTRIBUTORS --- CONTRIBUTORS | 2 +- proto/anki/search.proto | 8 ++++++++ pylib/anki/collection.py | 11 +++++++---- qt/aqt/browser/table/__init__.py | 27 ++++++++++++++++++++++----- qt/aqt/browser/table/table.py | 1 + rslib/src/browser_table.rs | 12 ++++++++++++ 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index ffb1a1f84..ab77c8e95 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -187,7 +187,7 @@ Christian Donat Asuka Minato Dillon Baldwin Voczi -Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> +Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> Themis Demetriades Gregory Abrasaldo Taylor Obyen diff --git a/proto/anki/search.proto b/proto/anki/search.proto index 85ac01c84..bb417294c 100644 --- a/proto/anki/search.proto +++ b/proto/anki/search.proto @@ -173,8 +173,16 @@ message BrowserColumns { message BrowserRow { message Cell { + enum TextElideMode { + ElideLeft = 0; + ElideRight = 1; + ElideMiddle = 2; + ElideNone = 3; + } + string text = 1; bool is_rtl = 2; + TextElideMode elide_mode = 3; } enum Color { COLOR_DEFAULT = 0; diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index ce1c7135a..6ae37befe 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -861,12 +861,15 @@ class Collection(DeprecatedNamesMixin): return column return None - def browser_row_for_id( - self, id_: int - ) -> tuple[Generator[tuple[str, bool], None, None], BrowserRow.Color.V, str, int]: + def browser_row_for_id(self, id_: int) -> tuple[ + Generator[tuple[str, bool, BrowserRow.Cell.TextElideMode.V], None, None], + BrowserRow.Color.V, + str, + int, + ]: row = self._backend.browser_row_for_id(id_) return ( - ((cell.text, cell.is_rtl) for cell in row.cells), + ((cell.text, cell.is_rtl, cell.elide_mode) for cell in row.cells), row.color, row.font_name, row.font_size, diff --git a/qt/aqt/browser/table/__init__.py b/qt/aqt/browser/table/__init__.py index dc3ed7d5d..a5a885311 100644 --- a/qt/aqt/browser/table/__init__.py +++ b/qt/aqt/browser/table/__init__.py @@ -33,10 +33,15 @@ class SearchContext: ids: Sequence[ItemId] | None = None -@dataclass class Cell: - text: str - is_rtl: bool + def __init__( + self, text: str, is_rtl: bool, elide_mode: BrowserRow.Cell.TextElideMode.V + ) -> None: + self.text: str = text + self.is_rtl: bool = is_rtl + self.elide_mode: aqt.Qt.TextElideMode = backend_elide_mode_to_aqt_elide_mode( + elide_mode + ) class CellRow: @@ -44,7 +49,7 @@ class CellRow: def __init__( self, - cells: Generator[tuple[str, bool], None, None], + cells: Generator[tuple[str, bool, BrowserRow.Cell.TextElideMode.V], None, None], color: BrowserRow.Color.V, font_name: str, font_size: int, @@ -61,7 +66,7 @@ class CellRow: @staticmethod def generic(length: int, cell_text: str) -> CellRow: return CellRow( - ((cell_text, False) for cell in range(length)), + ((cell_text, False, BrowserRow.Cell.ElideRight) for cell in range(length)), BrowserRow.COLOR_DEFAULT, "arial", 12, @@ -78,6 +83,18 @@ class CellRow: return row +def backend_elide_mode_to_aqt_elide_mode( + elide_mode: BrowserRow.Cell.TextElideMode.V, +) -> aqt.Qt.TextElideMode: + if elide_mode == BrowserRow.Cell.ElideLeft: + return aqt.Qt.TextElideMode.ElideLeft + if elide_mode == BrowserRow.Cell.ElideMiddle: + return aqt.Qt.TextElideMode.ElideMiddle + if elide_mode == BrowserRow.Cell.ElideNone: + return aqt.Qt.TextElideMode.ElideNone + return aqt.Qt.TextElideMode.ElideRight + + def backend_color_to_aqt_color(color: BrowserRow.Color.V) -> dict[str, str] | None: temp_color = None diff --git a/qt/aqt/browser/table/table.py b/qt/aqt/browser/table/table.py index 54631ebfa..d7ba18baa 100644 --- a/qt/aqt/browser/table/table.py +++ b/qt/aqt/browser/table/table.py @@ -643,6 +643,7 @@ class StatusDelegate(QItemDelegate): def paint( self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex ) -> None: + option.textElideMode = self._model.get_cell(index).elide_mode if self._model.get_cell(index).is_rtl: option.direction = Qt.LayoutDirection.RightToLeft if row_color := self._model.get_row(index).color: diff --git a/rslib/src/browser_table.rs b/rslib/src/browser_table.rs index be4e6393e..0a03e5452 100644 --- a/rslib/src/browser_table.rs +++ b/rslib/src/browser_table.rs @@ -413,6 +413,7 @@ impl RowContext { Ok(anki_proto::search::browser_row::Cell { text: self.get_cell_text(column)?, is_rtl: self.get_is_rtl(column), + elide_mode: self.get_elide_mode(column) as i32, }) } @@ -461,6 +462,17 @@ impl RowContext { } } + fn get_elide_mode( + &self, + column: Column, + ) -> anki_proto::search::browser_row::cell::TextElideMode { + use anki_proto::search::browser_row::cell::TextElideMode; + match column { + Column::Deck => TextElideMode::ElideMiddle, + _ => TextElideMode::ElideRight, + } + } + fn template(&self) -> Result<&CardTemplate> { self.notetype.get_template(self.cards[0].template_idx) }