Skip to content

Commit

Permalink
Add withDisable on ButtonContent/ButtonFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Feb 2, 2025
1 parent 9797e8c commit 8471d34
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
*/
@CheckReturnValue
fun of(style: ButtonStyle, label: String): ButtonFactory =
ButtonFactory(componentController, style, label, null)
ButtonFactory(componentController, style, label, null, disabled = false)

/**
* Creates a button factory with the style and emoji provided.
Expand All @@ -176,7 +176,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
*/
@CheckReturnValue
fun of(style: ButtonStyle, emoji: Emoji): ButtonFactory =
ButtonFactory(componentController, style, null, emoji)
ButtonFactory(componentController, style, null, emoji, disabled = false)

/**
* Creates a button factory with the style, label and emoji provided.
Expand All @@ -190,7 +190,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
*/
@CheckReturnValue
fun of(style: ButtonStyle, label: String, emoji: Emoji): ButtonFactory =
ButtonFactory(componentController, style, label, emoji)
ButtonFactory(componentController, style, label, emoji, disabled = false)

/**
* Creates a button factory with the style, label and emoji provided by the [ButtonContent].
Expand All @@ -204,7 +204,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
*/
@CheckReturnValue
fun of(content: ButtonContent): ButtonFactory =
ButtonFactory(componentController, content.style, content.label, content.emoji)
ButtonFactory(componentController, content.style, content.label, content.emoji, content.disabled)

/**
* Creates a primary button factory with the label provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ButtonFactory internal constructor(
private val componentController: ComponentController,
private val style: ButtonStyle,
private val label: String?,
private val emoji: Emoji?
private val emoji: Emoji?,
private val disabled: Boolean,
) {
init {
require(label != null || emoji != null) { "A label or an emoji needs to be set" }
Expand Down Expand Up @@ -58,14 +59,20 @@ class ButtonFactory internal constructor(
EmojiUtils.resolveJDAEmojiOrNull(it) ?: Emoji.fromFormatted(it)
}

return ButtonFactory(componentController, style, label, newEmoji)
return ButtonFactory(componentController, style, label, newEmoji, disabled)
}

/**
* Creates a new button factory with the provided JDA emoji.
*/
@CheckReturnValue
fun withEmoji(emoji: Emoji?): ButtonFactory = ButtonFactory(componentController, style, label, emoji)
fun withEmoji(emoji: Emoji?): ButtonFactory = ButtonFactory(componentController, style, label, emoji, disabled)

/**
* Creates a new button factory with the provided disabled state.
*/
@CheckReturnValue
fun withDisabled(disabled: Boolean): ButtonFactory = ButtonFactory(componentController, style, label, emoji, disabled)

/**
* Creates an ephemeral button builder.
Expand All @@ -76,7 +83,7 @@ class ButtonFactory internal constructor(
*/
@CheckReturnValue
fun ephemeral(): EphemeralButtonBuilder =
EphemeralButtonBuilderImpl(componentController, style, label, emoji, InstanceRetriever())
EphemeralButtonBuilderImpl(componentController, style, label, emoji, disabled, InstanceRetriever())

/**
* Creates an ephemeral button.
Expand All @@ -98,7 +105,7 @@ class ButtonFactory internal constructor(
*/
@CheckReturnValue
fun persistent(): PersistentButtonBuilder =
PersistentButtonBuilderImpl(componentController, style, label, emoji, InstanceRetriever())
PersistentButtonBuilderImpl(componentController, style, label, emoji, disabled, InstanceRetriever())

/**
* Creates a persistent button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import javax.annotation.CheckReturnValue
/**
* Represents the visual content of a [Button], this contains at least an [Emoji] or a [String]
*/
data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji: Emoji?) {
data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji: Emoji?, val disabled: Boolean) {
init {
require(label != null || emoji != null) { "A label or an emoji needs to be set" }

Expand Down Expand Up @@ -47,38 +47,44 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
EmojiUtils.resolveJDAEmojiOrNull(it) ?: Emoji.fromFormatted(it)
}

return ButtonContent(style, label, newEmoji)
return ButtonContent(style, label, newEmoji, disabled)
}

/**
* Creates a new button content with the provided JDA emoji.
*/
@CheckReturnValue
fun withEmoji(emoji: Emoji?): ButtonContent = ButtonContent(style, label, emoji)
fun withEmoji(emoji: Emoji?): ButtonContent = ButtonContent(style, label, emoji, disabled)

/**
* Creates a new button content with the provided disabled state.
*/
@CheckReturnValue
fun withDisabled(disabled: Boolean): ButtonContent = ButtonContent(style, label, emoji, disabled)

companion object {
/**
* Constructs a [ButtonContent] with a label.
*/
@JvmStatic
fun fromLabel(style: ButtonStyle, label: String): ButtonContent {
return ButtonContent(style, label, null)
return ButtonContent(style, label, null, disabled = false)
}

/**
* Constructs a [ButtonContent] with an [Emoji].
*/
@JvmStatic
fun fromEmoji(style: ButtonStyle, emoji: Emoji): ButtonContent {
return ButtonContent(style, null, emoji)
return ButtonContent(style, null, emoji, disabled = false)
}

/**
* Constructs a [ButtonContent] with a label and an [Emoji].
*/
@JvmStatic
fun fromEmoji(style: ButtonStyle, label: String, emoji: Emoji): ButtonContent {
return ButtonContent(style, label, emoji)
return ButtonContent(style, label, emoji, disabled = false)
}

/**
Expand All @@ -87,7 +93,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
*/
@JvmStatic
fun fromUnicode(style: ButtonStyle, unicode: String): ButtonContent {
return ButtonContent(style, null, Emoji.fromUnicode(unicode))
return ButtonContent(style, null, Emoji.fromUnicode(unicode), disabled = false)
}

/**
Expand All @@ -102,7 +108,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
replaceWith = ReplaceWith(expression = "fromEmoji(style, label, Emojis.)", imports = ["dev.freya02.jda.emojis.Emojis"])
)
fun fromUnicode(style: ButtonStyle, label: String, unicode: String): ButtonContent {
return ButtonContent(style, label, Emoji.fromUnicode(unicode))
return ButtonContent(style, label, Emoji.fromUnicode(unicode), disabled = false)
}

/**
Expand All @@ -116,7 +122,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
replaceWith = ReplaceWith(expression = "fromEmoji(style, Emojis.)", imports = ["dev.freya02.jda.emojis.Emojis"])
)
fun fromShortcode(style: ButtonStyle, shortcode: String): ButtonContent {
return ButtonContent(style, null, EmojiUtils.resolveJDAEmoji(shortcode))
return ButtonContent(style, null, EmojiUtils.resolveJDAEmoji(shortcode), disabled = false)
}

/**
Expand All @@ -130,7 +136,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
replaceWith = ReplaceWith(expression = "fromEmoji(style, text, Emojis.)", imports = ["dev.freya02.jda.emojis.Emojis"])
)
fun fromShortcode(style: ButtonStyle, text: String, shortcode: String): ButtonContent {
return ButtonContent(style, text, EmojiUtils.resolveJDAEmoji(shortcode))
return ButtonContent(style, text, EmojiUtils.resolveJDAEmoji(shortcode), disabled = false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal sealed class AbstractButtonBuilder<T : ButtonBuilder<T>>(
private val style: ButtonStyle,
private val label: String?,
private val emoji: Emoji?,
private val disabled: Boolean,
instanceRetriever: InstanceRetriever<T>
) : AbstractComponentBuilder<T>(instanceRetriever),
ButtonBuilder<T> {
Expand All @@ -37,7 +38,7 @@ internal sealed class AbstractButtonBuilder<T : ButtonBuilder<T>>(
ButtonImpl(
componentController,
internalId,
JDAButton.of(style, componentId, label, emoji)
JDAButton.of(style, componentId, label, emoji).withDisabled(disabled)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ internal class EphemeralButtonBuilderImpl internal constructor(
style: ButtonStyle,
label: String?,
emoji: Emoji?,
disabled: Boolean,
instanceRetriever: InstanceRetriever<EphemeralButtonBuilder>
) : AbstractButtonBuilder<EphemeralButtonBuilder>(componentController, style, label, emoji, instanceRetriever),
) : AbstractButtonBuilder<EphemeralButtonBuilder>(componentController, style, label, emoji, disabled, instanceRetriever),
EphemeralButtonBuilder,
IEphemeralActionableComponentMixin<EphemeralButtonBuilder, ButtonEvent> by EphemeralActionableComponentImpl(
componentController.context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ internal class PersistentButtonBuilderImpl internal constructor(
style: ButtonStyle,
label: String?,
emoji: Emoji?,
disabled: Boolean,
instanceRetriever: InstanceRetriever<PersistentButtonBuilder>
) : AbstractButtonBuilder<PersistentButtonBuilder>(componentController, style, label, emoji, instanceRetriever),
) : AbstractButtonBuilder<PersistentButtonBuilder>(componentController, style, label, emoji, disabled, instanceRetriever),
PersistentButtonBuilder,
IPersistentActionableComponentMixin<PersistentButtonBuilder> by PersistentActionableComponentImpl(
componentController.context,
Expand Down

0 comments on commit 8471d34

Please sign in to comment.