Skip to content

Commit

Permalink
Seperate LinkButton out of Button
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Jan 18, 2024
1 parent bd80b5d commit 3e2c9e8
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 186 deletions.
6 changes: 4 additions & 2 deletions docs/changelogs/v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ This is a major breaking release, adding REST bot support, dependency injection,

- **BREAKING:** Raised the minimum supported Python version to **3.10** or greater.
- **BREAKING:** Change all `@miru` decorators to take `Context` as their first argument and the item (button/select etc..) as their second.
- **BREAKING:** Seperate link buttons out of `miru.Button` as `miru.LinkButton`.
- **BREAKING:** Remove `miru.install()`. Use `miru.Client` instead.
- **BREAKING:** Remove `View.start()` and `Modal.start()`. Use `Client.start_view()` and `Client.start_modal()` respectively instead.
- **BREAKING:** Remove `NavigatorView.send()`. Use `NavigatorView.build_response()` instead and send the builder.
- **BREAKING:** Remove `Modal.send()`. Use `Modal.build_response()` instead and send the builder.
- **BREAKING:** Remove `Menu.send()`. Use `Menu.build_response()` instead and send the builder.
- **BREAKING:** Remove `miru.ModalInteractionCreateEvent` and `miru.ComponentInteractionCreateEvent`. Use the unhandled interaction hooks instead.
- **BREAKING:** Made `ViewItem.callback` only accept positional arguments. This is to allow renaming the context variable's name when overriding it in subclasses. This should not affect most people.
- **BREAKING:** Move `miru.context.base.Context` to `miru.abc.context.Context`.
- **BREAKING:** Move `miru.select.base.SelectBase` to `miru.abc.select.SelectBase`.
- **BREAKING:** Move `miru.Context` to `miru.abc.Context`.
- **BREAKING:** Move `miru.SelectBase` to `miru.abc.SelectBase`.
- **DEPRECATION:** Passing `buttons=` to `ext.nav.NavigatorView()` constructor. Use the newly added `items=` instead. The `buttons=` argument will be removed in v4.2.0.

- Add `miru.Client`. The client manages the state of all currently running views & modals & routes interactions to them.
Expand All @@ -38,3 +39,4 @@ This is a major breaking release, adding REST bot support, dependency injection,
- Add response builders for entire responses from views or modals.
- Add `Context.respond_with_builder()`.
- Add `@Client.set_unhandled_component_interaction_hook` and `@Client.set_unhandled_modal_interaction_hook`. These are called when an interaction is received that is not handled by any running modal or view.
- Add `miru.abc.InteractiveViewItem` for all view items that have callbacks. This includes all current `miru.abc.ViewItem` except `miru.LinkButton`.
18 changes: 17 additions & 1 deletion docs/guides/migrating_from_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ For more information on how to use these builders with each of the major command

`NavigatorView.send()`, `Menu.send()` have been removed.

Similarly to modals, menus & navigators are also now turned into builders. However, since the payloads are built asynchronously, you need to use [`Menu.build_response_async()`][miru.ext.menu.menu.Menu.build_response_async] and [`NavigatorView.build_response_async()`][miru.ext.nav.NavigatorView.build_response_async] respectively, instead. If you're handling an interaction, you may also need to defer beforehand if building your initial screen takes a long time.
Similarly to modals, menus & navigators are also now turned into builders. However, since the payloads are built asynchronously, you need to use [`Menu.build_response_async()`][miru.ext.menu.menu.Menu.build_response_async] and [`NavigatorView.build_response_async()`][miru.ext.nav.NavigatorView.build_response_async] respectively. If you're handling an interaction, you may also need to defer beforehand depending on how long it takes to build the payload.

=== "v4"

Expand Down Expand Up @@ -160,3 +160,19 @@ Similarly to modals, menus & navigators are also now turned into builders. Howev
```

For more information on how to use these builders with each of the major command handler frameworks, please see the updated [menu](./menus.md) and [navigator](./navigators.md) guides.

## Link buttons

Link buttons have been seperated out of `miru.Button` and received their own class: `miru.LinkButton`. For your existing link buttons, it should be as simple as updating the class:

=== "v4"

```py
view.add_item(miru.LinkButton(url="https://google.com"))
```

=== "v3"

```py
view.add_item(miru.Button(url="https://google.com"))
```
3 changes: 2 additions & 1 deletion miru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from miru import abc, ext, select
from miru.abc.context import InteractionResponse
from miru.button import Button, button
from miru.button import Button, LinkButton, button
from miru.client import Client
from miru.context import AutodeferMode, AutodeferOptions, ModalContext, ViewContext
from miru.exceptions import HandlerFullError, ItemAlreadyAttachedError, MiruError, RowFullError
Expand Down Expand Up @@ -48,6 +48,7 @@
"ViewContext",
"ModalContext",
"Button",
"LinkButton",
"button",
"MiruError",
"RowFullError",
Expand Down
1 change: 1 addition & 0 deletions miru/abc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Item",
"ItemHandler",
"ViewItem",
"InteractiveViewItem",
"ModalItem",
"DecoratedItem",
"SelectBase",
Expand Down
39 changes: 31 additions & 8 deletions miru/abc/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from miru.view import View


__all__ = ("Item", "DecoratedItem", "ViewItem", "ModalItem")
__all__ = ("Item", "DecoratedItem", "ViewItem", "InteractiveViewItem", "ModalItem")


class Item(abc.ABC, t.Generic[BuilderT, ContextT, HandlerT]):
Expand Down Expand Up @@ -122,12 +122,10 @@ def __init__(
position: int | None = None,
width: int = 1,
disabled: bool = False,
autodefer: bool | AutodeferOptions | hikari.UndefinedType = hikari.UNDEFINED,
) -> None:
super().__init__(custom_id=custom_id, row=row, position=position, width=width)
self._handler: View | None = None
self._disabled: bool = disabled
self._autodefer = AutodeferOptions.parse(autodefer) if autodefer is not hikari.UNDEFINED else autodefer

@property
def view(self) -> View:
Expand All @@ -146,18 +144,43 @@ def disabled(self) -> bool:
def disabled(self, value: bool) -> None:
self._disabled = value

@abstractmethod
def _build(self, action_row: hikari.api.MessageActionRowBuilder) -> None:
"""Called internally to build and append the item to an action row."""
...

@classmethod
@abstractmethod
def _from_component(cls, component: hikari.PartialComponent, row: int | None = None) -> te.Self:
"""Converts the passed hikari component into a miru ViewItem."""
...


class InteractiveViewItem(ViewItem, abc.ABC):
"""An abstract base class for view components that have callbacks.
Cannot be directly instantiated.
"""

def __init__(
self,
*,
custom_id: str | None = None,
row: int | None = None,
position: int | None = None,
width: int = 1,
disabled: bool = False,
autodefer: bool | AutodeferOptions | hikari.UndefinedType = hikari.UNDEFINED,
) -> None:
super().__init__(custom_id=custom_id, row=row, position=position, width=width, disabled=disabled)
self._autodefer = AutodeferOptions.parse(autodefer) if autodefer is not hikari.UNDEFINED else autodefer

@property
def autodefer(self) -> AutodeferOptions | hikari.UndefinedType:
"""Indicates whether the item should be deferred automatically.
If left as `UNDEFINED`, the view's autodefer option will be used.
"""
return self._autodefer

@abstractmethod
def _build(self, action_row: hikari.api.MessageActionRowBuilder) -> None:
"""Called internally to build and append the item to an action row."""
...

@classmethod
@abstractmethod
def _from_component(cls, component: hikari.PartialComponent, row: int | None = None) -> te.Self:
Expand Down
4 changes: 2 additions & 2 deletions miru/abc/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import hikari

from miru.abc.item import ViewItem
from miru.abc.item import InteractiveViewItem

if t.TYPE_CHECKING:
import typing_extensions as te
Expand All @@ -18,7 +18,7 @@
__all__ = ("SelectBase",)


class SelectBase(ViewItem, abc.ABC):
class SelectBase(InteractiveViewItem, abc.ABC):
"""A view component representing some type of select menu. All types of selects derive from this class.
Parameters
Expand Down
Loading

0 comments on commit 3e2c9e8

Please sign in to comment.