Last active
November 22, 2025 09:53
-
-
Save ZeN220/5be129530671de32cee4fb83d7395e9a to your computer and use it in GitHub Desktop.
Adjust for aiogram_dialog
This file contains hidden or 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
| import itertools | |
| from aiogram.types import CallbackQuery | |
| from aiogram_dialog import DialogProtocol, DialogManager | |
| from aiogram_dialog.api.internal import RawKeyboard | |
| from aiogram_dialog.widgets.common import WhenCondition | |
| from aiogram_dialog.widgets.kbd import Keyboard | |
| class Adjust(Keyboard): | |
| def __init__( | |
| self, | |
| *buttons: Keyboard, | |
| adjust: tuple[int, ...], | |
| id: str, | |
| when: WhenCondition = None, | |
| ): | |
| super().__init__(id=id, when=when) | |
| self.buttons = buttons | |
| self.adjust = adjust | |
| async def _render_keyboard( | |
| self, | |
| data: dict, | |
| manager: DialogManager, | |
| ) -> RawKeyboard: | |
| kbd: RawKeyboard = [] | |
| for b in self.buttons: | |
| b_kbd = await b.render_keyboard(data, manager) | |
| kbd += b_kbd | |
| if self.adjust: | |
| kbd = self._wrap_kbd(kbd) | |
| return kbd | |
| def _wrap_kbd( | |
| self, | |
| kbd: RawKeyboard, | |
| ) -> RawKeyboard: | |
| res: RawKeyboard = [] | |
| flat = list(itertools.chain.from_iterable(kbd)) | |
| idx = 0 | |
| for width in itertools.chain( | |
| self.adjust, itertools.repeat(self.adjust[-1]) | |
| ): | |
| if idx >= len(flat): | |
| break | |
| res.append(flat[idx : idx + width]) | |
| idx += width | |
| return res | |
| def find(self, widget_id): | |
| widget = super().find(widget_id) | |
| if widget: | |
| return widget | |
| for btn in self.buttons: | |
| widget = btn.find(widget_id) | |
| if widget: | |
| return widget | |
| return None | |
| async def _process_other_callback( | |
| self, | |
| callback: CallbackQuery, | |
| dialog: DialogProtocol, | |
| manager: DialogManager, | |
| ) -> bool: | |
| for b in self.buttons: | |
| processed = await b.process_callback(callback, dialog, manager) | |
| if processed: | |
| return True | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment