rui_orig/popupUtils.go

216 lines
4.8 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
disabled []int
session Session
popup Popup
result func(int)
2021-09-07 17:36:50 +03:00
}
func (popup *popupMenuData) itemClick(list ListView, n int) {
if popup.IsListItemEnabled(n) {
if popup.popup != nil {
popup.popup.Dismiss()
popup.popup = nil
}
if popup.result != nil {
popup.result(n)
}
2021-09-07 17:36:50 +03:00
}
}
func (popup *popupMenuData) ListSize() int {
return len(popup.items)
}
func (popup *popupMenuData) ListItem(index int, session Session) View {
2023-05-03 15:16:03 +03:00
view := NewTextView(popup.session, Params{
2021-09-07 17:36:50 +03:00
Text: popup.items[index],
Style: "ruiPopupMenuItem",
})
2023-05-03 15:16:03 +03:00
if !popup.IsListItemEnabled(index) {
view.Set(TextColor, "@ruiDisabledTextColor")
}
return view
2021-09-07 17:36:50 +03:00
}
func (popup *popupMenuData) IsListItemEnabled(index int) bool {
if popup.disabled != nil {
for _, n := range popup.disabled {
if index == n {
return false
}
}
}
2021-09-07 17:36:50 +03:00
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
}
if value, ok := params[PopupMenuResult]; ok && value != nil {
2021-09-07 17:36:50 +03:00
if result, ok := value.(func(int)); ok {
data.result = result
}
}
if value, ok := params[DisabledItems]; ok && value != nil {
if value, ok := value.([]int); ok {
data.disabled = value
}
}
2021-09-07 17:36:50 +03:00
listView := NewListView(session, Params{
Items: adapter,
Orientation: TopDownOrientation,
ListItemClickedEvent: data.itemClick,
})
popupParams := Params{}
for tag, value := range params {
switch tag {
case Items, PopupMenuResult, DisabledItems:
// 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
}