forked from mbk-lab/rui_orig
2
0
Fork 0
rui/popupUtils.go

197 lines
4.4 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
// ShowMessage displays the popup with the title given in the "title" argument and the message text given in the "text" argument.
2021-09-07 17:36:50 +03:00
func ShowMessage(title, text string, session Session) {
textView := NewTextView(session, Params{
Text: text,
Style: "ruiMessageText",
})
params := Params{
CloseButton: true,
OutsideClose: true,
}
if title != "" {
params[Title] = title
}
NewPopup(textView, params).Show()
}
// ShowQuestion displays a message with the given title and text and two buttons "Yes" and "No".
// When the "Yes" button is clicked, the message is closed and the onYes function is called (if it is not nil).
// When the "No" button is pressed, the message is closed and the onNo function is called (if it is not nil).
2021-09-07 17:36:50 +03:00
func ShowQuestion(title, text string, session Session, onYes func(), onNo func()) {
textView := NewTextView(session, Params{
Text: text,
Style: "ruiMessageText",
})
params := Params{
CloseButton: false,
OutsideClose: false,
Buttons: []PopupButton{
{
Title: "No",
OnClick: func(popup Popup) {
popup.Dismiss()
if onNo != nil {
onNo()
}
},
},
{
Title: "Yes",
OnClick: func(popup Popup) {
popup.Dismiss()
if onYes != nil {
onYes()
}
},
},
},
}
if title != "" {
params[Title] = title
}
NewPopup(textView, params).Show()
}
// ShowCancellableQuestion displays a message with the given title and text and three buttons "Yes", "No" and "Cancel".
// When the "Yes", "No" or "Cancel" button is pressed, the message is closed and the onYes, onNo or onCancel function
// (if it is not nil) is called, respectively.
2021-09-07 17:36:50 +03:00
func ShowCancellableQuestion(title, text string, session Session, onYes func(), onNo func(), onCancel func()) {
textView := NewTextView(session, Params{
Text: text,
Style: "ruiMessageText",
})
params := Params{
CloseButton: false,
OutsideClose: false,
Buttons: []PopupButton{
{
Title: "Cancel",
OnClick: func(popup Popup) {
popup.Dismiss()
if onCancel != nil {
onCancel()
}
},
},
{
Title: "No",
OnClick: func(popup Popup) {
popup.Dismiss()
if onNo != nil {
onNo()
}
},
},
{
Title: "Yes",
OnClick: func(popup Popup) {
popup.Dismiss()
if onYes != nil {
onYes()
}
},
},
},
}
if title != "" {
params[Title] = title
}
NewPopup(textView, params).Show()
}
type popupMenuData struct {
items []string
session Session
popup Popup
result func(int)
}
func (popup *popupMenuData) itemClick(list ListView, n int) {
2021-11-17 12:32:37 +03:00
if popup.popup != nil {
popup.popup.Dismiss()
popup.popup = nil
}
2021-09-07 17:36:50 +03:00
if popup.result != nil {
popup.result(n)
}
}
func (popup *popupMenuData) ListSize() int {
return len(popup.items)
}
func (popup *popupMenuData) ListItem(index int, session Session) View {
return NewTextView(popup.session, Params{
Text: popup.items[index],
Style: "ruiPopupMenuItem",
})
}
func (popup *popupMenuData) IsListItemEnabled(index int) bool {
return true
}
// PopupMenuResult is the constant for the "popup-menu-result" property tag.
// The "popup-menu-result" property sets the function (format: func(int)) to be called when
// a menu item of popup menu is selected.
2021-09-07 17:36:50 +03:00
const PopupMenuResult = "popup-menu-result"
// ShowMenu displays the menu. Menu items are set using the Items property.
// The "popup-menu-result" property sets the function (format: func(int)) to be called when a menu item is selected.
2021-11-17 12:32:37 +03:00
func ShowMenu(session Session, params Params) Popup {
2021-09-07 17:36:50 +03:00
value, ok := params[Items]
if !ok || value == nil {
ErrorLog("Unable to show empty menu")
2021-11-17 12:32:37 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
var adapter ListAdapter
data := new(popupMenuData)
data.session = session
switch value := value.(type) {
case []string:
data.items = value
adapter = data
case ListAdapter:
adapter = value
default:
notCompatibleType(Items, value)
2021-11-17 12:32:37 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
value, ok = params[PopupMenuResult]
if ok && value != nil {
if result, ok := value.(func(int)); ok {
data.result = result
}
}
listView := NewListView(session, Params{
Items: adapter,
Orientation: TopDownOrientation,
ListItemClickedEvent: data.itemClick,
})
popupParams := Params{}
for tag, value := range params {
switch tag {
case Items, PopupMenuResult:
// do nothing
default:
popupParams[tag] = value
}
}
data.popup = NewPopup(listView, popupParams)
2021-09-07 17:36:50 +03:00
data.popup.Show()
FocusView(listView)
2021-11-17 12:32:37 +03:00
return data.popup
2021-09-07 17:36:50 +03:00
}