Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Context Menu on Button Click #259

Open
Diy2210 opened this issue Jul 8, 2020 · 4 comments
Open

Implement Context Menu on Button Click #259

Diy2210 opened this issue Jul 8, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@Diy2210
Copy link

Diy2210 commented Jul 8, 2020

How implement Context Menu on button click like:

unnamed

@Alex009 Alex009 added the enhancement New feature or request label Jul 21, 2020
@Alex009
Copy link
Member

Alex009 commented Jul 21, 2020

hi! thanks for suggestion!
now it not implemented, but you can implement it yourself. for it you should create own Widget, for example as https://github.com/icerockdev/moko-widgets/blob/master/widgets/src/commonMain/kotlin/dev/icerock/moko/widgets/ClickableWidget.kt

widget:

@WidgetDef(ContextMenuViewFactory::class)
class ContextMenuWidget<WS : WidgetSize>(
    private val factory: ViewFactory<ContextMenuWidget<out WidgetSize>>,
    override val id: Id?,
    val child: Widget<WS>,
    val menuItems: List<MenuItem>
) : Widget<WS>(), OptionalId<ContextMenuWidget.Id> {

    override val size: WS = child.size

    override fun buildView(viewFactoryContext: ViewFactoryContext): ViewBundle<WS> {
        return factory.build(this, size, viewFactoryContext)
    }

    interface Id : Theme.Id<ContextMenuWidget<out WidgetSize>>
    interface Category : Theme.Category<ContextMenuWidget<out WidgetSize>>

    object DefaultCategory : Category

    data class MenuItem(
        val title: StringDesc,
        val onClick: () -> Unit
    )
}

for widget you should implement ViewFactory - it will construct views for each target systems:
common code:

expect class ContextMenuViewFactory() : ViewFactory<ContextMenuWidget<out WidgetSize>>

android code:

actual class ContextMenuViewFactory actual constructor(
) : ViewFactory<ContextMenuWidget<out WidgetSize>> {

    override fun <WS : WidgetSize> build(
        widget: ContextMenuWidget<out WidgetSize>,
        size: WS,
        viewFactoryContext: ViewFactoryContext
    ): ViewBundle<WS> {
        val childViewBundle =
            widget.child.buildView(viewFactoryContext) as ViewBundle<WS>

        return childViewBundle.copy(
            view = childViewBundle.view.apply {
                // set here context menu showing on click to `this`
                // https://developer.android.com/guide/topics/ui/menus#context-menu
                // https://developer.android.com/guide/topics/ui/menus#PopupMenu
            }
        )
    }
}

ios code:

actual class ContextMenuViewFactory actual constructor(
) : ViewFactory<ContextMenuWidget<out WidgetSize>> {

    override fun <WS : WidgetSize> build(
        widget: ContextMenuWidget<out WidgetSize>,
        size: WS,
        viewFactoryContext: ViewFactoryContext
    ): ViewBundle<WS> {
        val childViewBundle =
            widget.child.buildView(viewFactoryContext) as ViewBundle<WS>

        childViewBundle.view.apply {
                // set here context menu showing on click to `this`
                // https://developer.apple.com/design/human-interface-guidelines/ios/controls/context-menus/
                // https://developer.apple.com/design/human-interface-guidelines/ios/views/action-sheets/
        }

        return childViewBundle
    }
}

@Diy2210
Copy link
Author

Diy2210 commented Jul 21, 2020

Ok, I try it tnx!

@Diy2210 Diy2210 closed this as completed Jul 21, 2020
@Alex009
Copy link
Member

Alex009 commented Jul 21, 2020

please left issue open - we implement it in library later

@Alex009 Alex009 reopened this Jul 21, 2020
@Diy2210
Copy link
Author

Diy2210 commented Jul 27, 2020

I try implement context menu, but I dont understand how it use.

My ContextMenuWidget:
Снимок экрана 2020-07-27 в 14 56 41

Common Expect class:
Снимок экрана 2020-07-27 в 14 56 52

Android actual:
Снимок экрана 2020-07-27 в 14 57 06

How use it this on button click?
Снимок экрана 2020-07-27 в 14 56 31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants