-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from rodit/new-chat-menu
Add support for new chat context menu
- Loading branch information
Showing
17 changed files
with
463 additions
and
161 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package xyz.rodit.snapmod | ||
|
||
import java.lang.reflect.InvocationHandler | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.Proxy | ||
|
||
typealias DelegateFunction = (Any, Array<Any>) -> Any? | ||
|
||
class DelegateProxy(private val delegate: DelegateFunction) : InvocationHandler { | ||
|
||
override fun invoke(target: Any, method: Method, args: Array<Any>?): Any? { | ||
return delegate(target, args ?: emptyArray()) | ||
} | ||
} | ||
|
||
fun Class<*>.createDelegate(classLoader: ClassLoader, delegate: DelegateFunction): Any { | ||
return Proxy.newProxyInstance( | ||
classLoader, | ||
arrayOf(this), | ||
DelegateProxy(delegate) | ||
) | ||
} |
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
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
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
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
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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/xyz/rodit/snapmod/features/chatmenu/new/MenuPlugin.kt
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package xyz.rodit.snapmod.features.chatmenu.new | ||
|
||
abstract class MenuPlugin { | ||
|
||
abstract fun shouldCreate(): Boolean | ||
|
||
abstract fun createModel(key: String): Any | ||
|
||
open fun performHooks() { | ||
|
||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
app/src/main/java/xyz/rodit/snapmod/features/chatmenu/new/NewChatMenuModifier.kt
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package xyz.rodit.snapmod.features.chatmenu.new | ||
|
||
import xyz.rodit.snapmod.createDelegate | ||
import xyz.rodit.snapmod.features.Feature | ||
import xyz.rodit.snapmod.features.FeatureContext | ||
import xyz.rodit.snapmod.features.chatmenu.shared.export | ||
import xyz.rodit.snapmod.features.chatmenu.shared.previewChat | ||
import xyz.rodit.snapmod.mappings.* | ||
import xyz.rodit.snapmod.util.before | ||
|
||
class NewChatMenuModifier(context: FeatureContext) : Feature(context) { | ||
|
||
private val plugins = mutableListOf<MenuPlugin>() | ||
|
||
override fun init() { | ||
registerPlain("Export") { export(context, it) } | ||
registerPlain("Preview") { previewChat(context, it) } | ||
|
||
registerSwitch("pinning", "Pin Conversation") { it.pinned } | ||
registerSwitch("stealth", "Stealth Mode") { it.stealth } | ||
registerSwitch("auto_save", "Auto-Save Messages") { it.autoSave } | ||
registerSwitch("auto_download", "Auto-Download Snaps") { it.autoDownload } | ||
} | ||
|
||
private fun registerPlugin(plugin: MenuPlugin) { | ||
plugins.add(plugin) | ||
} | ||
|
||
private fun registerPlain(text: String, click: ClickHandler) { | ||
registerPlugin(PlainOption(context, text, click)) | ||
} | ||
|
||
private fun registerSwitch(name: String, text: String, manager: Manager) { | ||
registerPlugin(SwitchOption(context, name, text, manager)) | ||
} | ||
|
||
override fun performHooks() { | ||
// Force new chat action menu | ||
ProfileActionSheetChooser.choose.before { | ||
it.args[0] = context.config.getBoolean("enable_new_chat_menu", true) | ||
} | ||
|
||
// Add subsection | ||
ProfileActionSheetCreator.apply.before { | ||
if (it.args[0] !is List<*>) return@before | ||
|
||
val newItems = (it.args[0] as List<*>).toMutableList() | ||
val creator = ProfileActionSheetCreator.wrap(it.thisObject) | ||
val nestedContext = NestedActionMenuContext.wrap(creator.nestedContext) | ||
val actionContext = ActionMenuContext.wrap(creator.actionMenuContext) | ||
val key = actionContext.feedInfo.key | ||
|
||
val subOptions = plugins.filter(MenuPlugin::shouldCreate).map { p -> | ||
p.createModel(key) | ||
} | ||
val clickProxy = | ||
Func0.getMappedClass().createDelegate(context.classLoader) { _, _ -> | ||
NestedActionMenuContext.display( | ||
nestedContext, | ||
"SnapMod", | ||
subOptions | ||
) | ||
null | ||
} | ||
val snapModSettings = | ||
ActionClickableCaret("SnapMod Settings", null, Func0.wrap(clickProxy)).instance | ||
newItems.add(snapModSettings) | ||
|
||
it.args[0] = newItems | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/xyz/rodit/snapmod/features/chatmenu/new/PlainOption.kt
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package xyz.rodit.snapmod.features.chatmenu.new | ||
|
||
import xyz.rodit.snapmod.createDelegate | ||
import xyz.rodit.snapmod.features.FeatureContext | ||
import xyz.rodit.snapmod.mappings.ActionPlain | ||
import xyz.rodit.snapmod.mappings.Func0 | ||
|
||
typealias ClickHandler = (key: String) -> Unit | ||
|
||
class PlainOption( | ||
private val context: FeatureContext, | ||
private val text: String, | ||
private val click: ClickHandler | ||
) : MenuPlugin() { | ||
|
||
override fun shouldCreate() = true | ||
|
||
override fun createModel(key: String): Any = ActionPlain( | ||
text, | ||
Func0.wrap(Func0.getMappedClass().createDelegate(context.classLoader) { _, _ -> | ||
click(key) | ||
}) | ||
).instance | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/xyz/rodit/snapmod/features/chatmenu/new/SwitchOption.kt
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package xyz.rodit.snapmod.features.chatmenu.new | ||
|
||
import xyz.rodit.snapmod.createDelegate | ||
import xyz.rodit.snapmod.features.FeatureContext | ||
import xyz.rodit.snapmod.mappings.ActionSwitch | ||
import xyz.rodit.snapmod.mappings.Func1 | ||
import xyz.rodit.snapmod.util.ConversationManager | ||
import xyz.rodit.snapmod.util.getList | ||
|
||
typealias Manager = (FeatureContext) -> ConversationManager | ||
|
||
class SwitchOption( | ||
private val context: FeatureContext, | ||
private val name: String, | ||
private val text: String, | ||
private val manager: Manager | ||
) : MenuPlugin() { | ||
|
||
override fun shouldCreate() = !context.config.getList("hidden_chat_options").contains(name) | ||
|
||
override fun createModel(key: String): Any = ActionSwitch( | ||
text, | ||
manager(context).isEnabled(key), | ||
Func1.wrap(Func1.getMappedClass().createDelegate(context.classLoader) { _, _ -> true }), | ||
Func1.wrap(Func1.getMappedClass().createDelegate(context.classLoader) { _, _ -> | ||
manager(context).toggle(key) | ||
true | ||
}), | ||
null, | ||
0 | ||
).instance | ||
} |
Oops, something went wrong.