This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): ✨ search modpacks and filter
- Loading branch information
1 parent
fc1abcb
commit c478858
Showing
4 changed files
with
153 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,52 @@ | ||
from tkinter import Frame, Label | ||
from tkinter.ttk import Combobox | ||
from typing import TYPE_CHECKING, List | ||
|
||
from utils.constant import SEARCH | ||
|
||
if TYPE_CHECKING: | ||
from utils.window.main import Main | ||
|
||
|
||
class Filters(Frame): | ||
def __init__(self, master): | ||
def __init__(self, master: 'Main'): | ||
super().__init__(master, height=50) | ||
|
||
self.main_window = master | ||
|
||
self.sort_label = Label(self, text='排序方式:') | ||
self.sort_combobox = Combobox(self) | ||
self.sort_combobox = Combobox(self, state='readonly') | ||
self.game_version_label = Label(self, text='游戏版本:') | ||
self.game_version_combobox = Combobox(self) | ||
self.game_version_combobox = Combobox(self, state='readonly') | ||
self.modpack_version_label = Label(self, text='整合包版本:') | ||
self.modpack_version_combobox = Combobox(self) | ||
self.modpack_version_combobox = Combobox(self, state='readonly') | ||
|
||
self.sort_label.pack(side='left') | ||
self.sort_combobox.pack(side='left') | ||
self.game_version_label.pack(side='left', padx=(10, 0)) | ||
self.game_version_combobox.pack(side='left') | ||
self.modpack_version_label.pack(side='left', padx=(10, 0)) | ||
self.modpack_version_combobox.pack(side='left') | ||
|
||
# Set combobox values | ||
self.init() | ||
|
||
@property | ||
def sort(self): | ||
return self.sort_combobox.get() | ||
|
||
@property | ||
def game_version(self): | ||
return self.game_version_combobox.get() | ||
|
||
@property | ||
def modpack_version(self): | ||
return self.modpack_version_combobox.get() | ||
|
||
def init(self): | ||
self.sort_combobox['values'] = list(SEARCH.SORT.keys()) | ||
self.sort_combobox.current(1) | ||
self.game_version_combobox['values'] = SEARCH.VERSIONS | ||
|
||
def set_modpack_version(self, values: List[str]): | ||
self.modpack_version_combobox['values'] = values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,33 @@ | ||
from tkinter import Frame, Entry, Label, Button | ||
from tkinter import Frame, Entry, Button | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from utils.window.main import Main | ||
|
||
|
||
class Search(Frame): | ||
def __init__(self, master): | ||
def __init__(self, master: 'Main'): | ||
super().__init__(master, height=50) | ||
|
||
self.main_window = master | ||
|
||
self.search_entry = Entry(self) | ||
self.search_button = Button(self, text='搜索', background='white') | ||
self.search_button = Button( | ||
self, | ||
text='搜索', | ||
background='white', | ||
command=self.on_search | ||
) | ||
|
||
# Allow press enter in entry to search | ||
self.search_entry.bind('<Return>', self.on_search) | ||
|
||
self.search_entry.pack(side='left', fill='both', expand=True) | ||
self.search_button.pack(side='left', fill='y', padx=(10, 0)) | ||
|
||
@property | ||
def keyword(self): | ||
return self.search_entry.get() | ||
|
||
def on_search(self, event=None): | ||
self.main_window.show_frame.update_list() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,76 @@ | ||
from tkinter import Frame, Listbox | ||
from threading import Thread | ||
from tkinter import Frame, Listbox, Scrollbar | ||
from typing import TYPE_CHECKING | ||
|
||
from utils.constant import SEARCH | ||
from utils.requester import Requester | ||
|
||
if TYPE_CHECKING: | ||
from utils.window.main import Main | ||
|
||
|
||
class Show(Frame): | ||
def __init__(self, master): | ||
def __init__(self, master: 'Main'): | ||
super().__init__(master) | ||
|
||
self.main_window = master | ||
self.index = 0 | ||
self.updating = False | ||
|
||
self.list_listbox = Listbox(self, font=('Arial', 12)) | ||
self.list_listbox_scrollbar = Scrollbar(self) | ||
|
||
# Scrollbar setting | ||
self.list_listbox.config(yscrollcommand=self.on_scroll) | ||
self.list_listbox_scrollbar.config(command=self.list_listbox.yview) | ||
|
||
self.list_listbox.pack(side='left', fill='both', expand=True) | ||
self.list_listbox_scrollbar.pack(side='left', fill='y') | ||
|
||
def update_list(self, append=False): | ||
def request(index): | ||
# Get filters | ||
keyword = sort = game_version = None | ||
if self.main_window.search_frame.keyword: | ||
keyword = self.main_window.search_frame.keyword | ||
if self.main_window.filters_frame.sort: | ||
sort = SEARCH.SORT[self.main_window.filters_frame.sort] | ||
if self.main_window.filters_frame.game_version: | ||
game_version = self.main_window.filters_frame.game_version | ||
|
||
return Requester.search_modpack( | ||
game_version=game_version, | ||
search_filter=keyword, | ||
sort=sort, | ||
index=index | ||
).json() | ||
|
||
def run(): | ||
self.updating = True | ||
|
||
# Refresh list or append | ||
if not append: | ||
self.list_listbox.delete(0, 'end') | ||
else: | ||
self.index += 1 | ||
|
||
# Add request results | ||
for i in request(self.index): | ||
self.list_listbox.insert('end', i['name'].strip()) | ||
|
||
self.updating = False | ||
|
||
# Protection | ||
if not self.updating: | ||
Thread( | ||
target=run, | ||
name='Update List Data' | ||
).start() | ||
|
||
def on_scroll(self, first, last): | ||
# Call origin function | ||
self.list_listbox_scrollbar.set(first, last) | ||
|
||
self.list_listbox.pack(fill='both', expand=True) | ||
# Update at the end | ||
if float(last) == 1.0 and not self.updating: | ||
self.update_list(append=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters