Skip to content

Commit

Permalink
Remove selectorwin previews feature, it's not actually used
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Jul 5, 2024
1 parent 5897225 commit 9235ee0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 119 deletions.
101 changes: 15 additions & 86 deletions src/app/selector_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import tkinter as tk

from contextlib import aclosing
from collections.abc import Callable, Container, Iterable, Sequence
from collections.abc import Callable, Container, Iterable
from collections import defaultdict
from enum import Enum, auto as enum_auto
import functools
Expand All @@ -31,13 +31,10 @@
from app import sound, img, DEV_MODE
from ui_tk.tooltip import add_tooltip, set_tooltip
from ui_tk.img import TK_IMG
from ui_tk.rich_textbox import RichText
from ui_tk.wid_transtoken import set_menu_text, set_text, set_win_title, set_stringvar
from ui_tk.wid_transtoken import set_menu_text, set_text, set_stringvar
from ui_tk import TK_ROOT, tk_tools
from packages import SelitemData, AttrTypes, AttrDef as AttrDef, AttrMap
from consts import (
SEL_ICON_SIZE as ICON_SIZE,
)
from consts import SEL_ICON_SIZE as ICON_SIZE
from transtoken import TransToken
from config.last_sel import LastSelected
from config.windows import SelectorState
Expand Down Expand Up @@ -176,73 +173,6 @@ def hover_end(self, _: tk.Event[tk.Misc] | None = None) -> None:
)


class PreviewWindow:
"""Displays images previewing the selected item."""
img: Sequence[img.Handle]
def __init__(self) -> None:
self.win = tk.Toplevel(TK_ROOT, name='selectorPreview')
self.win.withdraw()
self.win.resizable(False, False)

# Don't destroy the window when closed.
self.win.protocol("WM_DELETE_WINDOW", self.hide)
self.win.bind("<Escape>", self.hide)

self.display = ttk.Label(self.win)
self.display.grid(row=0, column=1, sticky='nsew')
self.win.columnconfigure(1, weight=1)
self.win.rowconfigure(0, weight=1)

self.parent: SelectorWinBase | None = None

self.prev_btn = ttk.Button(
self.win, text=BTN_PREV, command=functools.partial(self.cycle, -1))
self.next_btn = ttk.Button(
self.win, text=BTN_NEXT, command=functools.partial(self.cycle, +1))

self.img = ()
self.index = 0

def show(self, parent: SelectorWinBase, data: SelitemData) -> None:
"""Show the window."""
self.win.transient(parent.win)
set_win_title(self.win, TRANS_PREVIEW_TITLE.format(item=data.name))

self.parent = parent
self.index = 0
self.img = data.previews
TK_IMG.apply(self.display, self.img[0])

if len(self.img) > 1:
self.prev_btn.grid(row=0, column=0, sticky='ns')
self.next_btn.grid(row=0, column=2, sticky='ns')
else:
self.prev_btn.grid_remove()
self.next_btn.grid_remove()

self.win.deiconify()
self.win.lift()
tk_tools.center_win(self.win, parent.win)
if parent.modal:
parent.win.grab_release()
self.win.grab_set()

def hide(self, _: tk.Event[tk.Misc] | None = None) -> None:
"""Swap grabs if the parent is modal."""
if self.parent is not None and self.parent.modal:
self.win.grab_release()
self.parent.win.grab_set()
self.win.withdraw()

def cycle(self, off: int) -> None:
"""Switch to a new image."""
self.index = (self.index + off) % len(self.img)
TK_IMG.apply(self.display, self.img[self.index])


_PREVIEW = PreviewWindow()


@attrs.frozen(kw_only=True)
class Options:
"""Creation options for selector windows.
Expand Down Expand Up @@ -380,8 +310,6 @@ class SelectorWinBase[ButtonT, SuggLblT]:
samp_button: ttk.Button | None
sampler: sound.SamplePlayer | None

prop_desc_frm: ttk.Frame
prop_desc: RichText
context_menu: tk.Menu
norm_font: tk_font.Font
# A font for showing suggested items in the context menu
Expand Down Expand Up @@ -699,7 +627,7 @@ def save(self, _: object = None) -> None:
self._ui_win_hide()
self._visible = False
self.choose_item(self.selected)
self.prop_desc.set_text('') # Free resources used.
self._ui_props_set_desc(MarkdownData.BLANK) # Free resources used.

def set_disp(self) -> None:
"""Update the display textbox."""
Expand Down Expand Up @@ -775,13 +703,9 @@ async def _load_selected_task(self) -> None:
self.save()

def _icon_clicked(self, _: tk.Event[tk.Misc]) -> None:
"""When the large image is clicked, either show the previews or play sounds."""
if self.sampler:
"""When the large image is clicked, play sounds if available."""
if self.sampler is not None:
self.sampler.play_sample()
return
data = self._get_data(self.selected)
if data.previews:
_PREVIEW.show(self, data)

def open_win(self, _: object = None) -> object:
"""Display the window."""
Expand Down Expand Up @@ -881,16 +805,16 @@ def sel_item(self, item_id: utils.SpecialID, _: object = None) -> None:
n=len(data.auth),
))

self._ui_props_set_icon(data.large_icon, can_preview=len(data.previews) > 0 and not self.sampler)
self._ui_props_set_icon(data.large_icon)

if DEV_MODE.value:
# Show the ID of the item in the description
text = MarkdownData(TRANS_DEV_ITEM_ID.format(
item=f'`{', '.join(data.packages)}`:`{item_id}`\n' if data.packages else f'`{item_id}`\n',
), None)
self.prop_desc.set_text(text + data.desc)
self._ui_props_set_desc(text + data.desc)
else:
self.prop_desc.set_text(data.desc)
self._ui_props_set_desc(data.desc)

try:
button = self._id_to_button[self.selected]
Expand Down Expand Up @@ -1331,7 +1255,12 @@ def _ui_props_set_name(self, name: TransToken, /) -> None:
raise NotImplementedError

@abstractmethod
def _ui_props_set_icon(self, image: img.Handle, /, *, can_preview: bool) -> None:
def _ui_props_set_desc(self, desc: MarkdownData, /) -> None:
"""Set the description for the selected item."""
raise NotImplementedError

@abstractmethod
def _ui_props_set_icon(self, image: img.Handle) -> None:
"""Set the large icon's image, and whether to show a zoom-in cursor."""
raise NotImplementedError

Expand Down
26 changes: 1 addition & 25 deletions src/packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, NoReturn, ClassVar, Self, cast

from collections.abc import Callable, Collection, Iterable, Iterator, Mapping, Sequence
from collections.abc import Callable, Collection, Iterable, Iterator, Mapping
from collections import defaultdict
from pathlib import Path
import os
Expand Down Expand Up @@ -224,7 +224,6 @@ class SelitemData:
auth: frozenset[str]
icon: img.Handle
large_icon: img.Handle
previews: Sequence[img.Handle]
desc: MarkdownData
group: TransToken
group_id: str
Expand All @@ -248,7 +247,6 @@ def build(
authors: Iterable[str] = (),
small_icon: img.Handle | None = None,
large_icon: img.Handle | None = None,
previews: Sequence[img.Handle] = (),
desc: TransToken | MarkdownData = TransToken.BLANK,
group: TransToken = TransToken.BLANK,
sort_key: str = '',
Expand Down Expand Up @@ -277,7 +275,6 @@ def build(
auth=frozenset(authors),
icon=small_icon,
large_icon=large_icon,
previews=previews,
desc=desc,
group=group,
group_id=group.token.casefold(),
Expand Down Expand Up @@ -320,33 +317,13 @@ def parse(cls, info: Keyvalues, pack_id: utils.ObjectID) -> SelitemData:
pack_id,
*consts.SEL_ICON_SIZE_LRG,
)
previews: Sequence[img.Handle]
try:
preview_block = info.find_block('previews')
except LookupError:
# Use the large icon, if present.
if large_key is not None:
previews = [img.Handle.parse(
large_key,
pack_id,
0, 0,
)]
else:
previews = ()
else:
previews = [img.Handle.parse(
kv,
pack_id,
0, 0,
) for kv in preview_block]

return cls.build(
long_name=name,
short_name=short_name,
authors=auth,
small_icon=icon,
large_icon=large_icon,
previews=previews,
desc=desc,
group=group,
sort_key=sort_key,
Expand All @@ -365,7 +342,6 @@ def __add__(self, other: SelitemData) -> SelitemData:
return attrs.evolve(
self,
auth=self.auth | other.auth,
previews=[*self.previews, *other.previews],
desc=self.desc + other.desc,
group=other.group or self.group,
group_id=other.group_id or self.group_id,
Expand Down
13 changes: 8 additions & 5 deletions src/ui_tk/selector_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections.abc import Mapping

from app import img
from app.mdown import MarkdownData
from app.selector_win import (
SelectorWinBase, AttrDef, Options, NavKeys,
BTN_PLAY, BTN_STOP, BTN_PREV, BTN_NEXT,
Expand Down Expand Up @@ -73,6 +74,7 @@ class SelectorWin(SelectorWinBase[
def __init__(self, parent: tk.Tk | tk.Toplevel, opt: Options) -> None:
super().__init__(opt)

self.parent = parent
self.win = tk.Toplevel(parent, name='selwin_' + opt.save_id)
self.win.withdraw()
self.win.transient(master=parent)
Expand Down Expand Up @@ -479,15 +481,16 @@ def _ui_props_set_name(self, name: TransToken) -> None:
set_text(self.prop_name, name)

@override
def _ui_props_set_icon(self, image: img.Handle, can_preview: bool) -> None:
def _ui_props_set_desc(self, desc: MarkdownData) -> None:
"""Set the description for the selected item."""
self.prop_desc.set_text(desc)

@override
def _ui_props_set_icon(self, image: img.Handle) -> None:
"""Set the large icon's image, and whether to show a zoom-in cursor."""
TK_IMG.apply(self.prop_icon, image)
# Change aspect ratio to match the large icon.
self.prop_icon_frm.configure(width=image.width, height=image.height)
if can_preview:
self.prop_icon['cursor'] = tk_tools.Cursors.ZOOM_IN
else:
self.prop_icon['cursor'] = tk_tools.Cursors.REGULAR

@override
def _ui_enable_reset(self, enabled: bool) -> None:
Expand Down
3 changes: 0 additions & 3 deletions src/ui_tk/tk_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ class Cursors(StrEnum):
REGULAR = 'arrow'
LINK = 'hand2'
WAIT = 'watch'
ZOOM_IN = 'size_nw_se'
STRETCH_VERT = 'sb_v_double_arrow'
STRETCH_HORIZ = 'sb_h_double_arrow'
MOVE_ITEM = 'fleur'
Expand All @@ -178,7 +177,6 @@ class Cursors(StrEnum): # type: ignore
REGULAR = 'arrow'
LINK = 'pointinghand'
WAIT = 'spinning'
ZOOM_IN = 'zoom-in'
STRETCH_VERT = 'resizeupdown'
STRETCH_HORIZ = 'resizeleftright'
MOVE_ITEM = 'movearrow'
Expand All @@ -194,7 +192,6 @@ class Cursors(StrEnum): # type: ignore
REGULAR = 'arrow'
LINK = 'hand1'
WAIT = 'watch'
ZOOM_IN = 'sizing'
STRETCH_VERT = 'bottom_side'
STRETCH_HORIZ = 'right_side'
MOVE_ITEM = 'fleur'
Expand Down

0 comments on commit 9235ee0

Please sign in to comment.