Generic UI Components¶
This page documents the reusable UI components used across various views of the bot. These components encapsulate logic for state management, navigation, and database interaction.
Interactive Buttons¶
The button logic is split between src.oscar.ui.buttons (generic toggles) and src.oscar.ui.select_button (domain-specific actions).
1. ToggleButton¶
A general-purpose button that switches between two states (On/Off) and updates its appearance automatically.
- Logic: Executes an async
on_changedcallback when clicked, passing the new boolean state. - Usage: Used in
TranslatedViewto switch languages.
src.oscar.ui.buttons.ToggleButton ¶
ToggleButton(state: bool = True, label_on: str = '✔', label_off: str = '✘', style_on: ButtonStyle = discord.ButtonStyle.success, style_off: ButtonStyle = discord.ButtonStyle.danger, on_changed: Callable[[Interaction, bool], Awaitable[None]] | None = None)
Bases: Button[LayoutView]
Toggle button class that switches between two states
Source code in src/oscar/ui/buttons.py
2. SelectButton (Radio Group)¶
A button designed for single-choice selections within a group (Radio Button behavior).
- Group Logic: It iterates recursively through the parent view's children. When clicked, it deselects all other
SelectButtoninstances that share the samegroupstring. - Side Effect: If the group is named
"Semester", it automatically writes the selection to the user's preferences in the database.
src.oscar.ui.select_button.SelectButton ¶
3. FilterButton (Multi-Select)¶
A specialized button for the filter mask (SelectView). Unlike SelectButton, it allows multiple active selections.
- Visual Feedback: Changes style to
Success(Green) when selected and back toSecondary(Grey) when deselected. - Data Flow: The parent view iterates over these buttons to build the filter query.
src.oscar.ui.select_button.FilterButton ¶
Bases: Button[LayoutView]
Class for a filter button.
The label is what the student reads and therefore changes with the language. The value is what the search reads and stays the same in every language. It falls back to the label, so an older button without a value still works.
Source code in src/oscar/ui/select_button.py
4. Action Buttons (Info & Delete)¶
Buttons that perform specific operations on Module objects.
- InfoButton: Fetches a module by name (
Module.from_name) and opens aModuleView. - DeleteButton: Removes a module from the semester plan via the database.
- Requirement: Must be attached to a
CustomView. It callsview.build_view()to trigger a full UI refresh after deletion.
- Requirement: Must be attached to a
src.oscar.ui.select_button.InfoButton ¶
src.oscar.ui.select_button.DeleteButton ¶
Bases: Button[LayoutView]
Class for a delete button
Source code in src/oscar/ui/select_button.py
Pagination System¶
The PaginatedView is an abstract base class for navigating large datasets (e.g., search results or example lists).
Core Logic¶
- Navigation: Implements 4 buttons: First (
❙❮), Previous (❮), Next (❯), Last (❯❙). - State Management: Automatically disables navigation buttons when reaching the start or end of the list (e.g., disables "Next" on the last page).
- Security: Includes a
check_usermethod to ensure only the user who initiated the command can navigate the view.
Implementation¶
To use this, you must provide a get_page callback function that returns a discord.Embed for a given index.
src.oscar.ui.paginated_view.PaginatedView ¶
Bases: View
Class holding a Paginated View
Source code in src/oscar/ui/paginated_view.py
setup
async
¶
Setup the embed and button callbacks
Source code in src/oscar/ui/paginated_view.py
check_user
async
¶
Chec weather the user has the right to perform the interaction
Source code in src/oscar/ui/paginated_view.py
first
async
¶
Callback for the skip to the first page button
Source code in src/oscar/ui/paginated_view.py
previous
async
¶
Callback for the previous page button
Source code in src/oscar/ui/paginated_view.py
next
async
¶
Callback for the next page button
Source code in src/oscar/ui/paginated_view.py
last
async
¶
Callback for the skip to the last page button
Source code in src/oscar/ui/paginated_view.py
see Views