rui_orig/popup.go

1957 lines
48 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
2022-07-31 15:37:26 +03:00
import (
"embed"
2024-11-25 23:13:29 +03:00
"fmt"
"os"
"path/filepath"
2025-06-18 14:19:57 +03:00
"reflect"
"slices"
2022-07-31 15:37:26 +03:00
"strings"
2026-06-25 20:25:56 +03:00
"sync"
2022-07-31 15:37:26 +03:00
)
2021-09-07 17:36:50 +03:00
// Constants for [Popup] specific properties and events
2021-09-07 17:36:50 +03:00
const (
// Title is the constant for "title" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup, TabsLayout.
//
2024-12-05 20:15:39 +03:00
// Usage in Popup:
// Define the title.
//
2024-12-05 20:15:39 +03:00
// Supported types: string.
//
2024-12-05 20:15:39 +03:00
// Usage in TabsLayout:
// Set the title of the tab. The property is set for the child view of TabsLayout.
//
2024-12-05 20:15:39 +03:00
// Supported types: string.
2025-03-13 11:50:57 +03:00
Title PropertyName = "title"
// TitleStyle is the constant for "title-style" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Set popup title style. Default title style is "ruiPopupTitle".
//
2024-12-05 20:15:39 +03:00
// Supported types: string.
2024-11-13 12:56:39 +03:00
TitleStyle PropertyName = "title-style"
// CloseButton is the constant for "close-button" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Controls whether a close button can be added to the popup. Default value is false.
//
2024-12-05 20:15:39 +03:00
// Supported types: bool, int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - true, 1, "true", "yes", "on", "1" - Close button will be added to a title bar of a window.
// - false, 0, "false", "no", "off", "0" - Popup without a close button.
2024-11-13 12:56:39 +03:00
CloseButton PropertyName = "close-button"
// OutsideClose is the constant for "outside-close" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Controls whether popup can be closed by clicking outside of the window. Default value is false.
//
2024-12-05 20:15:39 +03:00
// Supported types: bool, int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - true, 1, "true", "yes", "on", "1" - Clicking outside the popup window will automatically call the Dismiss() method.
// - false, 0, "false", "no", "off", "0" - Clicking outside the popup window has no effect.
2024-11-13 12:56:39 +03:00
OutsideClose PropertyName = "outside-close"
// Buttons is the constant for "buttons" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Buttons that will be placed at the bottom of the popup.
//
2024-12-05 20:15:39 +03:00
// Supported types: PopupButton, []PopupButton.
//
2024-12-05 20:15:39 +03:00
// Internal type is []PopupButton, other types converted to it during assignment.
// See PopupButton description for more details.
2024-11-13 12:56:39 +03:00
Buttons PropertyName = "buttons"
// ButtonsAlign is the constant for "buttons-align" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Set the horizontal alignment of popup buttons.
//
2024-12-05 20:15:39 +03:00
// Supported types: int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - 0 (LeftAlign) or "left" - Left alignment.
// - 1 (RightAlign) or "right" - Right alignment.
// - 2 (CenterAlign) or "center" - Center alignment.
// - 3 (StretchAlign) or "stretch" - Width alignment.
2024-11-13 12:56:39 +03:00
ButtonsAlign PropertyName = "buttons-align"
// DismissEvent is the constant for "dismiss-event" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Used to track the closing state of the Popup. It occurs after the Popup disappears from the screen.
//
// General listener format:
2024-12-05 20:15:39 +03:00
//
// func(popup rui.Popup)
//
// where:
// popup - Interface of a popup which generated this event.
//
// Allowed listener formats:
2024-12-05 20:15:39 +03:00
//
// func()
2024-11-13 12:56:39 +03:00
DismissEvent PropertyName = "dismiss-event"
2022-07-27 20:31:57 +03:00
// Arrow is the constant for "arrow" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Add an arrow to popup. Default value is "none".
//
2024-12-05 20:15:39 +03:00
// Supported types: int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - 0 (NoneArrow) or "none" - No arrow.
// - 1 (TopArrow) or "top" - Arrow at the top side of the pop-up window.
// - 2 (RightArrow) or "right" - Arrow on the right side of the pop-up window.
// - 3 (BottomArrow) or "bottom" - Arrow at the bottom of the pop-up window.
// - 4 (LeftArrow) or "left" - Arrow on the left side of the pop-up window.
2024-11-13 12:56:39 +03:00
Arrow PropertyName = "arrow"
2022-07-31 15:37:26 +03:00
// ArrowAlign is the constant for "arrow-align" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Set the horizontal alignment of the popup arrow. Default value is "center".
//
2024-12-05 20:15:39 +03:00
// Supported types: int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - 0 (TopAlign/LeftAlign) or "top" - Top/left alignment.
// - 1 (BottomAlign/RightAlign) or "bottom" - Bottom/right alignment.
// - 2 (CenterAlign) or "center" - Center alignment.
2024-11-13 12:56:39 +03:00
ArrowAlign PropertyName = "arrow-align"
2022-07-31 15:37:26 +03:00
// ArrowSize is the constant for "arrow-size" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Set the size(length) of the popup arrow. Default value is 16px defined by @ruiArrowSize constant.
//
2024-12-05 20:15:39 +03:00
// Supported types: SizeUnit, SizeFunc, string, float, int.
//
2024-12-05 20:15:39 +03:00
// Internal type is SizeUnit, other types converted to it during assignment.
// See SizeUnit description for more details.
2024-11-13 12:56:39 +03:00
ArrowSize PropertyName = "arrow-size"
2022-07-31 15:37:26 +03:00
// ArrowWidth is the constant for "arrow-width" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Set the width of the popup arrow. Default value is 16px defined by @ruiArrowWidth constant.
//
2024-12-05 20:15:39 +03:00
// Supported types: SizeUnit, SizeFunc, string, float, int.
//
2024-12-05 20:15:39 +03:00
// Internal type is SizeUnit, other types converted to it during assignment.
// See SizeUnit description for more details.
2024-11-13 12:56:39 +03:00
ArrowWidth PropertyName = "arrow-width"
2022-08-07 18:59:56 +03:00
// ArrowOffset is the constant for "arrow-offset" property tag.
//
// Used by Popup.
// Set the offset of the popup arrow.
//
// Supported types: SizeUnit, SizeFunc, string, float, int.
//
// Internal type is SizeUnit, other types converted to it during assignment.
// See SizeUnit description for more details.
ArrowOffset PropertyName = "arrow-offset"
2024-11-25 23:13:29 +03:00
// ShowTransform is the constant for "show-transform" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-11-25 23:13:29 +03:00
// Specify start translation, scale and rotation over x, y and z axes as well as a distortion
2025-06-25 13:53:08 +03:00
// for an animated Popup showing/hiding.
2024-11-25 23:13:29 +03:00
//
2024-12-05 20:15:39 +03:00
// Supported types: TransformProperty, string.
2024-11-25 23:13:29 +03:00
//
2024-12-05 20:15:39 +03:00
// See TransformProperty description for more details.
2024-11-25 23:13:29 +03:00
//
// Conversion rules:
2024-12-05 20:15:39 +03:00
// - TransformProperty - stored as is, no conversion performed.
// - string - string representation of Transform interface. Example:
//
// "_{ translate-x = 10px, scale-y = 1.1}"
2025-03-13 11:50:57 +03:00
ShowTransform PropertyName = "show-transform"
2024-11-25 23:13:29 +03:00
// ShowDuration is the constant for "show-duration" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-11-25 23:13:29 +03:00
// Sets the length of time in seconds that a Popup show/hide animation takes to complete.
//
2024-12-05 20:15:39 +03:00
// Supported types: float, int, string.
2024-11-25 23:13:29 +03:00
//
2024-12-05 20:15:39 +03:00
// Internal type is float, other types converted to it during assignment.
2025-03-13 11:50:57 +03:00
ShowDuration PropertyName = "show-duration"
2024-11-25 23:13:29 +03:00
// ShowTiming is the constant for "show-timing" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-11-25 23:13:29 +03:00
// Set how a Popup show/hide animation progresses through the duration of each cycle.
//
2024-12-05 20:15:39 +03:00
// Supported types: string.
2024-11-25 23:13:29 +03:00
//
// Values:
2024-12-05 20:15:39 +03:00
// - "ease" (EaseTiming) - Speed increases towards the middle and slows down at the end.
// - "ease-in" (EaseInTiming) - Speed is slow at first, but increases in the end.
// - "ease-out" (EaseOutTiming) - Speed is fast at first, but decreases in the end.
// - "ease-in-out" (EaseInOutTiming) - Speed is slow at first, but quickly increases and at the end it decreases again.
// - "linear" (LinearTiming) - Constant speed.
// - "step(n)" (StepTiming(n int) function) - Timing function along stepCount stops along the transition, displaying each stop for equal lengths of time.
// - "cubic-bezier(x1, y1, x2, y2)" (CubicBezierTiming(x1, y1, x2, y2 float64) function) - Cubic-Bezier curve timing function. x1 and x2 must be in the range [0, 1].
2025-03-13 11:50:57 +03:00
ShowTiming PropertyName = "show-timing"
2024-11-25 23:13:29 +03:00
2024-11-26 12:56:52 +03:00
// ShowOpacity is the constant for "show-opacity" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-11-26 12:56:52 +03:00
// In [1..0] range sets the start opacity of Popup show animation (the finish animation opacity is 1).
// Opacity is the degree to which content behind the view is hidden, and is the opposite of transparency.
//
2024-12-05 20:15:39 +03:00
// Supported types: float, int, string.
2024-11-26 12:56:52 +03:00
//
2024-12-05 20:15:39 +03:00
// Internal type is float, other types converted to it during assignment.
2025-03-13 11:50:57 +03:00
ShowOpacity PropertyName = "show-opacity"
2024-11-25 23:13:29 +03:00
// OutsideColor is the constant for "outside-color" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
// Specifies the color used to paint the space outside and below the Popup.
//
2026-07-09 21:36:44 +03:00
// Supported types: Color, ColorPair, string.
//
// Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details.
OutsideColor PropertyName = "outside-color"
// OutsideFilter is the constant for "outside-filter" property tag.
//
// Used by Popup.
// Applies graphical effects to the area outside and below a popup, such as blurring, color shifting, changing brightness/contrast,
// etc.
//
// Supported types: FilterProperty.
//
// See FilterProperty description for more details.
OutsideFilter PropertyName = "outside-filter"
2022-07-31 15:37:26 +03:00
2022-08-07 18:59:56 +03:00
// NoneArrow is value of the popup "arrow" property: no arrow
NoneArrow = 0
// TopArrow is value of the popup "arrow" property:
// Arrow at the top side of the pop-up window
TopArrow = 1
// RightArrow is value of the popup "arrow" property:
// Arrow on the right side of the pop-up window
RightArrow = 2
// BottomArrow is value of the popup "arrow" property:
// Arrow at the bottom of the pop-up window
2022-07-31 15:37:26 +03:00
BottomArrow = 3
2022-08-07 18:59:56 +03:00
// LeftArrow is value of the popup "arrow" property:
// Arrow on the left side of the pop-up window
LeftArrow = 4
)
2023-05-15 15:27:37 +03:00
// Constants which are used as a values of [PopupButtonType] variables
const (
2023-05-15 15:27:37 +03:00
// NormalButton is the constant of the popup button type: the normal button
NormalButton PopupButtonType = 0
2023-05-15 15:27:37 +03:00
// DefaultButton is the constant of the popup button type: button that fires when the "Enter" key is pressed
DefaultButton PopupButtonType = 1
2023-05-15 15:27:37 +03:00
// CancelButton is the constant of the popup button type: button that fires when the "Escape" key is pressed
CancelButton PopupButtonType = 2
2021-09-07 17:36:50 +03:00
)
const (
popupLayerID = "ruiPopupLayer"
popupArrowID = "ruiPopupArrow"
popupButtonsID = "ruiPopupButtons"
popupID = "ruiPopup"
popupContentID = "ruiPopupContent"
popupTitleID = "ruiPopupTitle"
)
// PopupButtonType represent popup button type
2023-05-15 15:27:37 +03:00
type PopupButtonType int
// PopupButton describes a button that will be placed at the bottom of the window.
2021-09-07 17:36:50 +03:00
type PopupButton struct {
// Title of the button
Title string
// Type of the button
Type PopupButtonType
// OnClick is the handler function that gets called when the button is pressed
2021-09-07 17:36:50 +03:00
OnClick func(Popup)
2026-08-10 16:00:40 +03:00
Binding string
2021-09-07 17:36:50 +03:00
}
type popupButton struct {
title string
buttonType PopupButtonType
onClick popupListener
}
// Popup represents a Popup view
2021-09-07 17:36:50 +03:00
type Popup interface {
Properties
fmt.Stringer
// View returns a content view of the popup
2021-09-07 17:36:50 +03:00
View() View
// Session returns current client session
2021-09-07 17:36:50 +03:00
Session() Session
// Show displays a popup
2021-09-07 17:36:50 +03:00
Show()
// Dismiss closes a popup
2021-09-07 17:36:50 +03:00
Dismiss()
// DismissWithoutAnimation closes a popup without animation
DismissWithoutAnimation()
// SetHotKey sets the function that will be called when the given hotkey is pressed.
// Invoke SetHotKey(..., ..., nil) for remove hotkey function.
SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Popup))
onDismiss()
2026-06-25 20:25:56 +03:00
html(hidden bool) string
2021-09-07 17:36:50 +03:00
viewByHTMLID(id string) View
2023-05-15 15:27:37 +03:00
keyEvent(event KeyEvent) bool
2024-11-25 23:13:29 +03:00
showAnimation()
2026-06-04 12:51:33 +03:00
dismissAnimation(listener func(View, PropertyName)) bool
2021-09-07 17:36:50 +03:00
}
2025-06-18 14:19:57 +03:00
type popupListener interface {
Run(Popup)
rawListener() any
}
type popupListener0 struct {
fn func()
}
type popupListener1 struct {
fn func(Popup)
}
type popupListenerBinding struct {
name string
dismiss bool
2025-06-18 14:19:57 +03:00
}
2021-09-07 17:36:50 +03:00
type popupData struct {
propertyList
2026-06-04 12:51:33 +03:00
session Session
2026-08-26 13:47:37 +03:00
layerBackgroudView ColumnLayout
layerContentView GridLayout
2026-06-04 12:51:33 +03:00
popupView GridLayout
contentContainer ColumnLayout
contentView View
hotkeys map[string]func(Popup)
dismissAnimationRunning bool
2021-09-07 17:36:50 +03:00
}
type popupManager struct {
2026-06-04 12:51:33 +03:00
popups []Popup
dismissAnimation []Popup
2026-06-25 20:25:56 +03:00
mutex sync.Mutex
2021-09-07 17:36:50 +03:00
}
2025-07-01 19:03:39 +03:00
func (popup *popupData) createArrowView(location int) View {
2022-08-07 18:59:56 +03:00
session := popup.Session()
2025-07-01 19:03:39 +03:00
getSize := func(tag PropertyName, constTag string) SizeUnit {
size, _ := sizeProperty(popup, tag, session)
if size.Type != Auto && size.Value > 0 {
return size
2022-08-07 18:59:56 +03:00
}
if value, ok := session.Constant(constTag); ok {
2025-07-01 19:03:39 +03:00
if size, ok := StringToSizeUnit(value); ok && size.Type != Auto && size.Value > 0 {
2022-08-07 18:59:56 +03:00
return size
}
}
return Px(16)
2022-08-07 18:59:56 +03:00
}
2025-07-01 19:03:39 +03:00
size := getSize(ArrowSize, "ruiArrowSize")
width := getSize(ArrowWidth, "ruiArrowWidth")
2022-08-07 18:59:56 +03:00
2025-07-01 19:03:39 +03:00
params := Params{BackgroundColor: GetBackgroundColor(popup.popupView)}
2022-08-08 17:00:49 +03:00
2025-07-01 19:03:39 +03:00
if shadow := GetShadowProperty(popup.popupView); shadow != nil {
2022-08-08 17:00:49 +03:00
params[Shadow] = shadow
}
2025-07-01 19:03:39 +03:00
if filter := GetBackdropFilter(popup.popupView); filter != nil {
2022-08-08 17:00:49 +03:00
params[BackdropFilter] = filter
}
2022-08-07 18:59:56 +03:00
2025-07-01 19:03:39 +03:00
switch location {
2022-08-07 18:59:56 +03:00
case TopArrow:
params[Row] = 0
params[Column] = 1
2024-12-06 18:38:43 +03:00
params[Clip] = NewPolygonClip([]any{"0%", "100%", "50%", "0%", "100%", "100%"})
2025-07-01 19:03:39 +03:00
params[Width] = width
params[Height] = size
2022-08-07 18:59:56 +03:00
case RightArrow:
params[Row] = 1
params[Column] = 0
2024-12-06 18:38:43 +03:00
params[Clip] = NewPolygonClip([]any{"0%", "0%", "100%", "50%", "0%", "100%"})
2025-07-01 19:03:39 +03:00
params[Width] = size
params[Height] = width
2022-08-07 18:59:56 +03:00
case BottomArrow:
params[Row] = 0
params[Column] = 1
2024-12-06 18:38:43 +03:00
params[Clip] = NewPolygonClip([]any{"0%", "0%", "50%", "100%", "100%", "0%"})
2025-07-01 19:03:39 +03:00
params[Width] = width
params[Height] = size
2022-08-07 18:59:56 +03:00
case LeftArrow:
params[Row] = 1
params[Column] = 0
2024-12-06 18:38:43 +03:00
params[Clip] = NewPolygonClip([]any{"100%", "0%", "0%", "50%", "100%", "100%"})
2025-07-01 19:03:39 +03:00
params[Width] = size
params[Height] = width
2022-08-07 18:59:56 +03:00
}
arrowView := NewView(session, params)
params = Params{
ID: popupArrowID,
2022-08-07 18:59:56 +03:00
Content: arrowView,
}
2025-07-01 19:03:39 +03:00
switch location {
case TopArrow:
params[Row] = 1
params[Column] = 2
case BottomArrow:
params[Row] = 3
params[Column] = 2
case LeftArrow:
params[Row] = 2
params[Column] = 1
case RightArrow:
params[Row] = 2
params[Column] = 3
}
off, _ := sizeProperty(popup, ArrowOffset, session)
align, _ := enumProperty(popup, ArrowAlign, session, CenterAlign)
if align != CenterAlign && off.Type == Auto {
r := GetRadius(popup.popupView)
switch location {
case TopArrow:
switch align {
case LeftAlign:
off = r.TopLeftX
case RightAlign:
off = r.TopRightX
}
case BottomArrow:
switch align {
case LeftAlign:
off = r.BottomLeftX
case RightAlign:
off = r.BottomRightX
}
case RightArrow:
switch align {
case TopAlign:
off = r.TopRightY
case BottomAlign:
off = r.BottomRightY
}
case LeftArrow:
switch align {
case TopAlign:
off = r.TopLeftY
2022-08-07 18:59:56 +03:00
2025-07-01 19:03:39 +03:00
case BottomAlign:
off = r.BottomLeftY
}
}
}
switch location {
2022-08-07 18:59:56 +03:00
case TopArrow, BottomArrow:
cellWidth := make([]SizeUnit, 3)
2025-07-01 19:03:39 +03:00
switch align {
2022-08-07 18:59:56 +03:00
case LeftAlign:
2025-07-01 19:03:39 +03:00
cellWidth[0] = off
2022-08-07 18:59:56 +03:00
cellWidth[2] = Fr(1)
case RightAlign:
cellWidth[0] = Fr(1)
2025-07-01 19:03:39 +03:00
cellWidth[2] = off
2022-08-07 18:59:56 +03:00
default:
cellWidth[0] = Fr(1)
cellWidth[2] = Fr(1)
2025-07-01 19:03:39 +03:00
if off.Type != Auto && off.Value != 0 {
arrowView.Set(MarginLeft, off)
2022-08-07 18:59:56 +03:00
}
}
params[CellWidth] = cellWidth
case RightArrow, LeftArrow:
cellHeight := make([]SizeUnit, 3)
2025-07-01 19:03:39 +03:00
switch align {
2022-08-07 18:59:56 +03:00
case TopAlign:
2025-07-01 19:03:39 +03:00
cellHeight[0] = off
2022-08-07 18:59:56 +03:00
cellHeight[2] = Fr(1)
case BottomAlign:
cellHeight[0] = Fr(1)
2025-07-01 19:03:39 +03:00
cellHeight[2] = off
2022-08-07 18:59:56 +03:00
default:
cellHeight[0] = Fr(1)
cellHeight[2] = Fr(1)
2025-07-01 19:03:39 +03:00
if off.Type != Auto && off.Value != 0 {
arrowView.Set(MarginTop, off)
2022-08-07 18:59:56 +03:00
}
}
params[CellHeight] = cellHeight
}
return NewGridLayout(session, params)
}
2025-07-01 19:03:39 +03:00
func (popup *popupData) layerCellWidth() []SizeUnit {
cellWidth := make([]SizeUnit, 5)
switch hAlign, _ := enumProperty(popup, HorizontalAlign, popup.session, CenterAlign); hAlign {
2022-08-07 18:59:56 +03:00
case LeftAlign:
2025-07-01 19:03:39 +03:00
cellWidth[4] = Fr(1)
2022-08-07 18:59:56 +03:00
case RightAlign:
cellWidth[0] = Fr(1)
default:
cellWidth[0] = Fr(1)
2025-07-01 19:03:39 +03:00
cellWidth[4] = Fr(1)
2022-08-07 18:59:56 +03:00
}
2024-11-25 23:13:29 +03:00
return cellWidth
}
2025-07-01 19:03:39 +03:00
func (popup *popupData) layerCellHeight() []SizeUnit {
cellHeight := make([]SizeUnit, 5)
switch vAlign, _ := enumProperty(popup, VerticalAlign, popup.session, CenterAlign); vAlign {
2022-08-07 18:59:56 +03:00
case LeftAlign:
2025-07-01 19:03:39 +03:00
cellHeight[4] = Fr(1)
2022-08-07 18:59:56 +03:00
case RightAlign:
cellHeight[0] = Fr(1)
default:
cellHeight[0] = Fr(1)
2025-07-01 19:03:39 +03:00
cellHeight[4] = Fr(1)
2022-08-07 18:59:56 +03:00
}
2024-11-25 23:13:29 +03:00
return cellHeight
}
func (popup *popupData) Get(tag PropertyName) any {
2024-11-25 23:13:29 +03:00
switch tag = defaultNormalize(tag); tag {
case Content:
return popup.contentView
case "layer-view":
2026-08-26 13:47:37 +03:00
if popup.layerBackgroudView == nil {
popup.createLayerView()
}
2026-08-26 13:47:37 +03:00
return popup.layerBackgroudView
case DismissEvent:
if value := popup.getRaw(DismissEvent); value != nil {
if listeners, ok := value.([]popupListener); ok {
if count := len(listeners); count > 0 {
result := make([]any, 0, count)
for _, listener := range listeners {
result = append(result, listener.rawListener())
}
return result
}
}
}
return nil
2026-08-10 16:00:40 +03:00
case Buttons:
2026-08-10 16:17:42 +03:00
if value := popup.getRaw(Buttons); value != nil {
2026-08-10 16:00:40 +03:00
if buttons, ok := value.([]popupButton); ok {
result := make([]PopupButton, 0, len(buttons))
for _, button := range buttons {
btn := PopupButton{
Title: button.title,
Type: button.buttonType,
}
2026-08-10 16:17:42 +03:00
if button.onClick != nil {
switch listener := button.onClick.rawListener().(type) {
case string:
btn.Binding = listener
case func(Popup):
btn.OnClick = listener
}
2026-08-10 16:00:40 +03:00
}
result = append(result, btn)
}
return result
}
}
return nil
2024-11-25 23:13:29 +03:00
}
return popup.getRaw(tag)
}
2024-11-25 23:13:29 +03:00
2025-07-02 11:58:34 +03:00
func (popup *popupData) arrowType() int {
2025-07-01 19:03:39 +03:00
result, _ := enumProperty(popup, Arrow, popup.session, NoneArrow)
return result
}
func (popup *popupData) supported(tag PropertyName) bool {
switch tag {
case Row, Column, CellWidth, CellHeight, Gap, GridColumnGap, GridRowGap,
CellVerticalAlign, CellHorizontalAlign,
CellVerticalSelfAlign, CellHorizontalSelfAlign:
return false
}
return true
}
2024-11-25 23:13:29 +03:00
func (popup *popupData) Set(tag PropertyName, value any) bool {
2024-11-25 23:13:29 +03:00
if value == nil {
popup.Remove(tag)
return true
2024-11-25 23:13:29 +03:00
}
switch tag = defaultNormalize(tag); tag {
case Buttons:
return popup.setButtons(value)
case Title:
switch value := value.(type) {
case string:
popup.setRaw(Title, value)
case View:
popup.setRaw(Title, value)
default:
notCompatibleType(Title, value)
return false
}
popup.propertyChanged(Title)
return true
case Content:
switch value := value.(type) {
case View:
popup.contentView = value
popup.setRaw(Content, popup.contentView)
case DataObject:
view := CreateViewFromObject(popup.session, value, nil)
if view == nil {
return false
}
popup.contentView = view
popup.setRaw(Content, popup.contentView)
case string:
2025-07-06 11:20:37 +03:00
if ok, constName := isConstantName(value); ok {
view := CreateViewFromResources(popup.session, constName)
if view != nil {
popup.contentView = view
break
}
}
popup.contentView = NewTextView(popup.session, Params{Text: value})
popup.setRaw(Content, value)
default:
notCompatibleType(Buttons, value)
return false
}
if binding := popup.getRaw(Binding); binding != nil {
popup.contentView.Set(Binding, binding)
}
popup.propertyChanged(Content)
return true
case Binding:
if popup.contentView != nil {
popup.contentView.Set(Binding, value)
}
popup.setRaw(Binding, value)
popup.propertyChanged(Binding)
return true
case DismissEvent:
if listeners, ok := valueToPopupEventListeners(value); ok {
if listeners != nil {
popup.setRaw(DismissEvent, listeners)
popup.propertyChanged(DismissEvent)
return true
}
}
notCompatibleType(tag, value)
return false
case ShowTransform:
return setTransformProperty(popup, tag, value)
case ShowOpacity:
return len(setFloatProperty(popup, ShowOpacity, value, 0, 1)) > 0
case ShowDuration:
return len(setFloatProperty(popup, ShowDuration, value, 0, 60)) > 0
case ShowTiming:
if timing, ok := value.(string); ok && isTimingFunctionValid(timing) {
popup.setRaw(ShowTiming, timing)
return true
}
case OutsideColor:
if len(setColorProperty(popup, OutsideColor, value)) > 0 {
popup.propertyChanged(OutsideColor)
return true
}
return false
case OutsideFilter:
if len(setFilterProperty(popup, OutsideFilter, value)) > 0 {
popup.propertyChanged(OutsideColor)
return true
}
return false
//if filter := GetBackdropFilter(popup.popupView); filter != nil {
//params[BackdropFilter] = filter
2022-08-07 18:59:56 +03:00
}
if popup.supported(tag) {
tags := viewStyleSet(popup, tag, value)
if len(tags) > 0 {
for _, tag := range tags {
popup.propertyChanged(tag)
}
return true
}
} else {
ErrorLogF(`"%s" property is not supported by the popup.`, string(tag))
}
return false
}
2024-11-25 23:13:29 +03:00
func (popup *popupData) Remove(tag PropertyName) {
tag = defaultNormalize(tag)
2021-11-17 12:32:37 +03:00
switch tag {
case Content:
popup.contentView = nil
}
2022-07-31 15:37:26 +03:00
if popup.supported(tag) {
tags := viewStyleRemove(popup, tag)
for _, tag := range tags {
popup.propertyChanged(tag)
}
} else {
ErrorLogF(`"%s" property is not supported by the popup.`, string(tag))
}
}
2021-11-17 12:32:37 +03:00
func (popup *popupData) propertyChanged(tag PropertyName) {
2026-08-26 13:47:37 +03:00
if popup.layerBackgroudView == nil {
return
}
2021-11-17 12:32:37 +03:00
switch tag {
case Content:
2025-07-02 11:58:34 +03:00
popup.contentContainer.Set(Content, popup.contentView)
case Arrow, ArrowWidth, ArrowSize, ArrowAlign, ArrowOffset:
2026-08-26 13:47:37 +03:00
popup.layerContentView.RemoveViewByID(popupArrowID)
2025-07-02 11:58:34 +03:00
if location := popup.arrowType(); location != NoneArrow {
2026-08-26 13:47:37 +03:00
popup.layerContentView.Append(popup.createArrowView(location))
2025-07-02 11:58:34 +03:00
}
case Buttons:
popup.popupView.RemoveViewByID(popupButtonsID)
if view := popup.createButtonsPanel(); view != nil {
popup.popupView.Append(view)
}
case Margin:
if margin, ok := getBounds(popup, Margin, popup.session); ok {
2026-08-26 13:47:37 +03:00
popup.layerContentView.Set(Padding, margin)
2025-07-02 11:58:34 +03:00
} else {
2026-08-26 13:47:37 +03:00
popup.layerContentView.Remove(Padding)
2025-07-02 11:58:34 +03:00
}
case Title, CloseButton:
popup.popupView.RemoveViewByID(popupTitleID)
if view := popup.createTitleView(); view != nil {
popup.popupView.Append(view)
}
case TitleStyle:
if title := ViewByID(popup.popupView, popupTitleID); title != nil {
titleStyle := "ruiPopupTitle"
if style, ok := stringProperty(popup, TitleStyle, popup.session); ok {
titleStyle = style
}
title.Set(Style, titleStyle)
}
case OutsideClose:
outsideClose, _ := boolProperty(popup, OutsideClose, popup.session)
if outsideClose {
2026-08-26 13:47:37 +03:00
popup.layerContentView.Set(ClickEvent, popup.cancel)
2025-07-02 11:58:34 +03:00
} else {
2026-08-26 13:47:37 +03:00
popup.layerContentView.Set(ClickEvent, func() {})
2025-07-02 11:58:34 +03:00
}
case ShowDuration, ShowTiming:
animation := popup.animationProperty()
opacity, _ := floatProperty(popup, ShowOpacity, popup.session, 1)
if opacity != 1 {
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.SetTransition(Opacity, animation)
2025-07-02 11:58:34 +03:00
}
transform := getTransformProperty(popup, ShowTransform)
if transform != nil {
2026-08-26 13:47:37 +03:00
popup.layerContentView.SetTransition(Transform, animation)
2025-07-02 11:58:34 +03:00
}
case ShowOpacity:
opacity, _ := floatProperty(popup, ShowOpacity, popup.session, 1)
if opacity != 1 {
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.SetTransition(Opacity, popup.animationProperty())
2025-07-02 11:58:34 +03:00
}
case ShowTransform:
transform := getTransformProperty(popup, ShowTransform)
if transform != nil {
2026-08-26 13:47:37 +03:00
popup.layerContentView.SetTransition(Transform, popup.animationProperty())
2025-07-02 11:58:34 +03:00
}
case OutsideColor:
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.Set(BackgroundColor, popup.Get(OutsideColor))
case OutsideFilter:
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.Set(BackdropFilter, popup.Get(OutsideFilter))
2025-07-02 11:58:34 +03:00
default:
if popup.supported(tag) {
popup.popupView.Set(tag, popup.getRaw(tag))
}
}
}
2022-07-31 15:37:26 +03:00
func (popup *popupData) defaultPopupFloat(tag PropertyName, defaultValue float64) float64 {
if value := popup.session.PopupDefault(tag); value != nil {
if f, ok := valueToFloat(value, popup.session, defaultValue); ok {
return f
} else {
DebugLogF(`"%v" is an invalid float64 value`, value)
}
}
return defaultValue
}
func (popup *popupData) animationProperty() AnimationProperty {
2025-07-02 11:58:34 +03:00
timing, ok := stringProperty(popup, ShowTiming, popup.session)
if ok && !isTimingFunctionValid(timing) {
DebugLog(`"` + timing + `" is an invalid timing function`)
ok = false
}
2025-07-02 11:58:34 +03:00
if !ok {
timing = EaseTiming
if value := popup.session.PopupDefault(ShowTiming); value != nil {
if text, ok := value.(string); ok && text != "" && isTimingFunctionValid(text) {
timing = text
}
}
2025-07-02 11:58:34 +03:00
}
duration, _ := floatProperty(popup, ShowDuration, popup.session, popup.defaultPopupFloat(ShowDuration, 1))
2025-07-02 11:58:34 +03:00
return NewAnimationProperty(Params{
Duration: duration,
TimingFunction: timing,
})
}
func (popup *popupData) View() View {
return popup.contentView
}
2021-11-17 12:32:37 +03:00
func (popup *popupData) Session() Session {
return popup.session
}
2021-09-07 17:36:50 +03:00
func (popup *popupData) String() string {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
writeViewStyle("Popup", popup, buffer, "", nil)
return buffer.String()
}
func (popup *popupData) setButtons(value any) bool {
popupButtonFromObject := func(obj DataObject) popupButton {
var button popupButton
2021-09-07 17:36:50 +03:00
button.title, _ = obj.PropertyText(string(Title))
2021-09-07 17:36:50 +03:00
if text, ok := obj.PropertyText("type"); ok {
text, _ = popup.session.resolveConstants(text)
t, _ := enumStringToInt(text, []string{"normal", "default", "cancel"}, true)
button.buttonType = PopupButtonType(t)
}
2021-09-07 17:36:50 +03:00
if fn, ok := obj.PropertyText("click"); ok {
button.onClick = newPopupListenerBinding(fn, true)
} else if button.buttonType == CancelButton {
button.onClick = newPopupListener0(popup.Dismiss)
}
2021-09-07 17:36:50 +03:00
return button
}
2021-09-07 17:36:50 +03:00
2025-07-02 11:58:34 +03:00
buttonConvert := func(button PopupButton) popupButton {
result := popupButton{
title: button.Title,
buttonType: button.Type,
}
if button.OnClick != nil {
result.onClick = newPopupListener1(button.OnClick)
}
2026-08-10 16:17:42 +03:00
if button.Binding != "" {
result.onClick = newPopupListenerBinding(button.Binding, false)
}
2025-07-02 11:58:34 +03:00
return result
}
switch value := value.(type) {
case PopupButton:
2025-07-02 11:58:34 +03:00
popup.setRaw(Buttons, []popupButton{buttonConvert(value)})
2021-09-07 17:36:50 +03:00
case []PopupButton:
buttons := make([]popupButton, 0, len(value))
for _, button := range value {
2025-07-02 11:58:34 +03:00
buttons = append(buttons, buttonConvert(button))
}
popup.setRaw(Buttons, buttons)
2024-11-25 23:13:29 +03:00
case []popupButton:
popup.setRaw(Buttons, value)
2024-11-25 23:13:29 +03:00
case DataValue:
switch value.Type() {
case ObjectDataValue:
obj := value.Object()
if obj == nil {
return false
}
popup.setRaw(Buttons, []popupButton{popupButtonFromObject(obj)})
2024-11-25 23:13:29 +03:00
case ArrayDataValue:
array := value.Array()
if array == nil {
return false
}
buttons := make([]popupButton, 0, array.Size())
for val := range array.Elements() {
if obj := val.Object(); obj != nil {
buttons = append(buttons, popupButtonFromObject(obj))
} else {
notCompatibleType(Buttons, val)
return false
}
}
if len(buttons) > 0 {
popup.setRaw(Buttons, buttons)
}
default:
return false
}
case []any:
buttons := make([]popupButton, 0, len(value))
for _, val := range value {
switch val := val.(type) {
case DataObject:
buttons = append(buttons, popupButtonFromObject(val))
case popupButton:
buttons = append(buttons, val)
case PopupButton:
buttons = append(buttons, popupButton{
title: val.Title,
buttonType: val.Type,
onClick: newPopupListener1(val.OnClick),
})
2024-11-25 23:13:29 +03:00
2021-09-07 17:36:50 +03:00
default:
notCompatibleType(Buttons, val)
2021-09-07 17:36:50 +03:00
}
}
if len(buttons) > 0 {
popup.setRaw(Buttons, buttons)
}
default:
notCompatibleType(Buttons, value)
return false
2021-09-07 17:36:50 +03:00
}
popup.propertyChanged(Buttons)
return true
}
2022-07-31 15:37:26 +03:00
func (popup *popupData) buttons() []popupButton {
if value := popup.getRaw(Buttons); value != nil {
if result, ok := value.([]popupButton); ok {
return result
2022-08-07 18:59:56 +03:00
}
}
return nil
}
func (popup *popupData) cancel() {
if buttons := popup.buttons(); buttons != nil {
for _, button := range buttons {
if button.buttonType == CancelButton && button.onClick != nil {
button.onClick.Run(popup)
return
}
}
}
popup.Dismiss()
}
func (popup *popupData) SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Popup)) {
hotkey := hotkeyCode(keyCode, controlKeys)
if fn == nil {
delete(popup.hotkeys, hotkey)
} else {
popup.hotkeys[hotkey] = fn
}
}
func (popup *popupData) Dismiss() {
popup.Session().popupManager().dismissPopup(popup, true)
}
func (popup *popupData) DismissWithoutAnimation() {
popup.Session().popupManager().dismissPopup(popup, false)
}
func (popup *popupData) Show() {
popup.Session().popupManager().showPopup(popup)
}
func (popup *popupData) showTransformAndOpacity() (TransformProperty, float64) {
opacity, _ := floatProperty(popup, ShowOpacity, popup.session, popup.defaultPopupFloat(ShowOpacity, 1))
transform := getTransformProperty(popup, ShowTransform)
if transform == nil {
if value := popup.session.PopupDefault(ShowTransform); value != nil {
if t, ok := value.(TransformProperty); ok {
transform = t
}
}
}
return transform, opacity
}
func (popup *popupData) showAnimation() {
transform, opacity := popup.showTransformAndOpacity()
if opacity != 1 || transform != nil {
2026-06-04 13:34:46 +03:00
session := popup.Session()
2026-08-26 13:47:37 +03:00
var eventID string
if transform != nil {
eventID = popup.layerContentView.htmlID()
} else {
eventID = popup.layerBackgroudView.htmlID()
}
session.updateProperty(eventID, "ontransitionend", "scanElementsSize()")
session.updateProperty(eventID, "ontransitioncancel", "scanElementsSize()")
2026-06-04 13:34:46 +03:00
animation := popup.animationProperty()
if opacity != 1 {
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.SetTransition(Opacity, animation)
}
if transform != nil {
2026-08-26 13:47:37 +03:00
popup.layerContentView.SetTransition(Transform, animation)
}
if opacity != 1 {
2026-08-26 13:47:37 +03:00
session.updateCSSProperty(popup.layerBackgroudView.htmlID(), string(Opacity), "1")
}
if transform != nil {
2026-08-26 13:47:37 +03:00
session.updateCSSProperty(popup.layerContentView.htmlID(), string(Transform), "")
}
}
}
2026-06-04 12:51:33 +03:00
func (popup *popupData) dismissAnimation(listener func(View, PropertyName)) bool {
transform, opacity := popup.showTransformAndOpacity()
if opacity != 1 || transform != nil {
session := popup.Session()
2026-08-26 13:47:37 +03:00
if transform != nil {
popup.layerContentView.Set(TransitionEndEvent, listener)
popup.layerContentView.Set(TransitionCancelEvent, listener)
} else {
popup.layerBackgroudView.Set(TransitionEndEvent, listener)
popup.layerBackgroudView.Set(TransitionCancelEvent, listener)
}
if opacity != 1 {
2026-08-26 13:47:37 +03:00
session.updateCSSProperty(popup.layerBackgroudView.htmlID(), string(Opacity), fmt.Sprintf("%.2f", opacity))
}
if transform != nil {
2026-08-26 13:47:37 +03:00
session.updateCSSProperty(popup.layerContentView.htmlID(), string(Transform), transform.transformCSS(session))
}
2026-05-17 20:42:49 +03:00
2026-06-04 12:51:33 +03:00
popup.dismissAnimationRunning = true
return true
}
return false
}
2026-06-25 20:25:56 +03:00
func (popup *popupData) html(hidden bool) string {
2026-08-26 13:47:37 +03:00
if popup.layerBackgroudView == nil {
popup.createLayerView()
}
2026-06-25 20:25:56 +03:00
if hidden {
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.Set(Visibility, Invisible)
2026-06-25 20:25:56 +03:00
}
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
2026-08-26 13:47:37 +03:00
viewHTML(popup.layerBackgroudView, buffer, "")
2026-06-25 20:25:56 +03:00
return buffer.String()
}
func (popup *popupData) viewByHTMLID(id string) View {
2026-08-26 13:47:37 +03:00
if popup.layerBackgroudView != nil {
return viewByHTMLID(id, popup.layerBackgroudView)
}
return nil
}
func (popup *popupData) onDismiss() {
2026-08-26 13:47:37 +03:00
if popup.layerBackgroudView != nil {
popup.Session().callFunc("removeView", popup.layerBackgroudView.htmlID())
popup.layerBackgroudView = nil
if value := popup.getRaw(DismissEvent); value != nil {
if listeners, ok := value.([]popupListener); ok {
for _, listener := range listeners {
listener.Run(popup)
}
}
2022-08-07 18:59:56 +03:00
}
2026-06-04 12:51:33 +03:00
popup.session.popupManager().dismissAnimationFinished(popup)
2021-09-07 17:36:50 +03:00
}
}
2021-09-07 17:36:50 +03:00
func (popup *popupData) keyEvent(event KeyEvent) bool {
var controlKeys ControlKeyMask = 0
if event.AltKey {
controlKeys |= AltKey
}
if event.CtrlKey {
controlKeys |= CtrlKey
}
if event.MetaKey {
controlKeys |= MetaKey
}
if event.ShiftKey {
controlKeys |= ShiftKey
}
if controlKeys == 0 {
switch event.Code {
case EnterKey:
for _, button := range popup.buttons() {
if button.buttonType == DefaultButton && button.onClick != nil {
button.onClick.Run(popup)
return true
}
}
2021-09-07 17:36:50 +03:00
case EscapeKey:
if popup.isCancelable() {
popup.cancel()
return true
}
}
}
key := hotkeyCode(KeyCode(event.Code), controlKeys)
if fn, ok := popup.hotkeys[key]; ok && fn != nil {
fn(popup)
return true
}
return false
}
func (popup *popupData) isCancelable() bool {
if closeButton, _ := boolProperty(popup, CloseButton, popup.session); closeButton {
return true
}
if outsideClose, _ := boolProperty(popup, OutsideClose, popup.session); outsideClose {
return true
}
for _, button := range popup.buttons() {
if button.buttonType == CancelButton {
return true
}
}
return false
}
func (popup *popupData) createButtonsPanel() GridLayout {
buttons := popup.buttons()
if buttonCount := len(buttons); buttonCount > 0 {
session := popup.session
buttonsAlign, _ := enumProperty(popup, ButtonsAlign, session, RightAlign)
2021-11-17 12:32:37 +03:00
gap, _ := sizeConstant(session, "ruiPopupButtonGap")
2021-09-07 17:36:50 +03:00
cellWidth := []SizeUnit{}
2025-06-11 14:20:41 +03:00
for range buttonCount {
2021-09-07 17:36:50 +03:00
cellWidth = append(cellWidth, Fr(1))
}
buttonsPanel := NewGridLayout(session, Params{
CellWidth: cellWidth,
})
if gap.Type != Auto && gap.Value > 0 {
buttonsPanel.Set(Gap, gap)
buttonsPanel.Set(Margin, gap)
}
for i, button := range buttons {
title := button.title
if title == "" && button.buttonType == CancelButton {
2023-05-15 15:27:37 +03:00
title = "Cancel"
}
buttonView := NewButton(session, Params{
Column: i,
Content: title,
})
if button.onClick != nil {
fn := button.onClick.Run
2023-05-15 15:27:37 +03:00
buttonView.Set(ClickEvent, func() {
fn(popup)
})
} else if button.buttonType == CancelButton {
2023-05-15 15:27:37 +03:00
buttonView.Set(ClickEvent, popup.cancel)
}
if button.buttonType == DefaultButton {
2023-05-15 15:27:37 +03:00
buttonView.Set(Style, "ruiDefaultButton")
}
buttonsPanel.Append(buttonView)
2021-09-07 17:36:50 +03:00
}
return NewGridLayout(session, Params{
ID: popupButtonsID,
Column: 0,
Row: 2,
2021-09-07 17:36:50 +03:00
CellHorizontalAlign: buttonsAlign,
Content: buttonsPanel,
})
2021-09-07 17:36:50 +03:00
}
return nil
}
2021-09-07 17:36:50 +03:00
func (popup *popupData) createTitleView() GridLayout {
session := popup.Session()
2022-08-07 18:59:56 +03:00
var closeButton View = nil
if hasButton, _ := boolProperty(popup, CloseButton, popup.session); hasButton {
closeButton = NewGridLayout(session, Params{
Column: 1,
Height: "@ruiPopupTitleHeight",
Width: "@ruiPopupTitleHeight",
CellHorizontalAlign: CenterAlign,
CellVerticalAlign: CenterAlign,
TextSize: Px(20),
Content: "✕",
NotTranslate: true,
ClickEvent: popup.cancel,
})
2022-08-07 18:59:56 +03:00
}
var title View = nil
if value := popup.getRaw(Title); value != nil {
switch value := value.(type) {
case string:
2025-07-06 11:20:37 +03:00
if ok, constName := isConstantName(value); ok {
title = CreateViewFromResources(session, constName)
if title != nil {
break
}
}
title = NewTextView(session, Params{Text: value})
2024-11-25 23:13:29 +03:00
case View:
title = value
2024-11-25 23:13:29 +03:00
}
}
if title == nil && closeButton == nil {
return nil
2021-09-07 17:36:50 +03:00
}
titleStyle := "ruiPopupTitle"
if style, ok := stringProperty(popup, TitleStyle, session); ok {
titleStyle = style
}
titleContent := []View{}
if title != nil {
titleContent = append(titleContent, title)
}
if closeButton != nil {
titleContent = append(titleContent, closeButton)
2024-11-25 23:13:29 +03:00
}
return NewGridLayout(session, Params{
ID: popupTitleID,
Row: 0,
Column: 0,
Style: titleStyle,
CellWidth: []any{Fr(1), AutoSize()},
CellVerticalAlign: CenterAlign,
PaddingLeft: Px(12),
Content: titleContent,
})
2024-11-25 23:13:29 +03:00
}
func (popup *popupData) createContentContainer() ColumnLayout {
params := Params{
ID: popupContentID,
Column: 0,
Row: 1,
}
2024-11-25 23:13:29 +03:00
if popup.contentView != nil {
params[Content] = popup.contentView
2024-11-25 23:13:29 +03:00
}
popup.contentContainer = NewColumnLayout(popup.session, params)
return popup.contentContainer
2021-09-07 17:36:50 +03:00
}
2026-08-26 13:47:37 +03:00
func (popup *popupData) createLayerView() {
2021-09-07 17:36:50 +03:00
session := popup.session
params := Params{
Style: "ruiPopup",
ID: popupID,
2025-07-01 19:03:39 +03:00
Row: 2,
Column: 2,
MaxWidth: Percent(100),
MaxHeight: Percent(100),
CellVerticalAlign: StretchAlign,
CellHorizontalAlign: StretchAlign,
CellHeight: []SizeUnit{AutoSize(), Fr(1), AutoSize()},
ClickEvent: func(View) {},
Shadow: NewShadowProperty(Params{
SpreadRadius: Px(4),
Blur: Px(16),
ColorTag: "@ruiPopupShadow",
}),
}
popupProperties := []PropertyName{
Content,
Title,
TitleStyle,
CloseButton,
OutsideClose,
OutsideColor,
OutsideFilter,
Buttons,
ButtonsAlign,
DismissEvent,
Arrow,
ArrowAlign,
ArrowSize,
ArrowWidth,
ArrowOffset,
ShowTransform,
ShowDuration,
ShowTiming,
ShowOpacity,
VerticalAlign,
HorizontalAlign,
Margin,
Row,
Column,
CellWidth,
CellHeight,
CellVerticalAlign,
CellHorizontalAlign,
}
2026-08-15 11:33:22 +03:00
popup.mutex.Lock()
for tag, value := range popup.properties {
if !slices.Contains(popupProperties, tag) {
params[tag] = value
2023-05-15 15:27:37 +03:00
}
}
2026-08-15 11:33:22 +03:00
popup.mutex.Unlock()
2023-05-15 15:27:37 +03:00
views := make([]View, 0, 3)
if title := popup.createTitleView(); title != nil {
views = append(views, title)
}
2021-09-07 17:36:50 +03:00
views = append(views, popup.createContentContainer())
2021-09-07 17:36:50 +03:00
if buttons := popup.createButtonsPanel(); buttons != nil {
views = append(views, buttons)
}
2021-09-07 17:36:50 +03:00
params[Content] = views
2021-09-07 17:36:50 +03:00
popup.popupView = NewGridLayout(session, params)
2024-11-25 23:13:29 +03:00
layerParams := Params{
2026-08-30 11:39:37 +03:00
Width: Percent(100),
Height: Percent(100),
Margin: Px(0),
2025-07-01 19:03:39 +03:00
CellWidth: popup.layerCellWidth(),
CellHeight: popup.layerCellHeight(),
}
if margin, ok := getBounds(popup, Margin, session); ok {
2025-07-01 19:03:39 +03:00
layerParams[Padding] = margin
2026-08-30 11:39:37 +03:00
} else {
layerParams[Padding] = Px(0)
}
2023-05-15 15:27:37 +03:00
2025-07-02 11:58:34 +03:00
if location := popup.arrowType(); location != NoneArrow {
2025-07-01 19:03:39 +03:00
layerParams[Content] = []View{popup.popupView, popup.createArrowView(location)}
} else {
layerParams[Content] = []View{popup.popupView}
}
2025-07-02 11:58:34 +03:00
if outsideClose, _ := boolProperty(popup, OutsideClose, session); outsideClose {
layerParams[ClickEvent] = popup.cancel
}
2026-08-26 13:47:37 +03:00
popup.layerContentView = NewGridLayout(session, layerParams)
backgroundParams := Params{
2026-08-30 11:39:37 +03:00
Style: popupLayerID,
2026-08-26 13:47:37 +03:00
Content: []View{popup.layerContentView},
}
if value := popup.getRaw(OutsideColor); value != nil {
2026-08-26 13:47:37 +03:00
backgroundParams[BackgroundColor] = value
}
if value := popup.getRaw(OutsideFilter); value != nil {
if filter, ok := value.(FilterProperty); ok {
2026-08-26 13:47:37 +03:00
backgroundParams[BackdropFilter] = filter
}
}
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView = NewColumnLayout(session, backgroundParams)
2025-07-02 11:58:34 +03:00
opacity, _ := floatProperty(popup, ShowOpacity, session, 1)
transform := getTransformProperty(popup, ShowTransform)
if opacity != 1 || transform != nil {
2025-07-02 11:58:34 +03:00
animation := popup.animationProperty()
if opacity != 1 {
2026-08-26 13:47:37 +03:00
popup.layerBackgroudView.Set(Opacity, opacity)
popup.layerBackgroudView.SetTransition(Opacity, animation)
}
if transform != nil {
2026-08-26 13:47:37 +03:00
popup.layerContentView.Set(Transform, transform)
popup.layerContentView.SetTransition(Transform, animation)
}
}
2023-05-15 15:27:37 +03:00
}
2021-09-07 17:36:50 +03:00
// NewPopup creates a new Popup
func NewPopup(view View, param Params) Popup {
if view == nil {
2026-11-05 13:31:38 +03:00
ErrorLog(`the View argument cannot be nil`)
2021-09-07 17:36:50 +03:00
return nil
}
popup := new(popupData)
2026-08-15 11:33:22 +03:00
popup.init()
popup.session = view.Session()
popup.contentView = view
popup.hotkeys = map[string]func(Popup){}
for tag, value := range popup.session.PopupDefaultsSeq() {
if _, ok := param[tag]; !ok && value != nil {
param[tag] = value
}
}
for tag, value := range param {
popup.Set(tag, value)
}
2021-09-07 17:36:50 +03:00
return popup
}
// CreatePopupFromObject create new Popup and initialize it by content of object. Parameters:
// - session - the session to which the view will be attached (should not be nil);
// - text - text describing Popup;
2025-07-03 11:28:14 +03:00
// - binding - object assigned to the Binding property (may be nil).
//
// If the function fails, it returns nil and an error message is written to the log.
func CreatePopupFromObject(session Session, object DataObject, binding any) Popup {
if session == nil {
2026-11-05 13:31:38 +03:00
ErrorLog(`the Session argument cannot be nil`)
return nil
}
if object == nil {
ErrorLog(`"object" argument is nil`)
return nil
}
if strings.ToLower(object.Tag()) != "popup" {
ErrorLog(`the object name must be "Popup"`)
return nil
}
popup := new(popupData)
2026-08-15 11:33:22 +03:00
popup.init()
popup.session = session
popup.hotkeys = map[string]func(Popup){}
for key, value := range object.Properties() {
popup.Set(PropertyName(key), value)
}
if binding != nil {
popup.Set(Binding, binding)
}
return popup
}
// CreatePopupFromText create new Popup and initialize it by content of text. Parameters:
// - session - the session to which the view will be attached (should not be nil);
// - text - text describing Popup;
2025-07-03 11:28:14 +03:00
// - binding - object assigned to the Binding property (may be nil).
//
// If the function fails, it returns nil and an error message is written to the log.
func CreatePopupFromText(session Session, text string, binding any) Popup {
2026-11-05 13:31:38 +03:00
if session == nil {
ErrorLog(`the Session argument cannot be nil`)
return nil
}
data, err := ParseDataText(text)
if err != nil {
ErrorLog(err.Error())
return nil
}
return CreatePopupFromObject(session, data, binding)
}
// CreatePopupFromResources create new Popup and initialize it by the content of
// the resource file from "popups" directory. Parameters:
// - session - the session to which the view will be attached (should not be nil);
// - text - file name in the "popups" folder of the application resources (it is not necessary to specify the .rui extension, it is added automatically);
2025-07-03 11:28:14 +03:00
// - binding - object assigned to the Binding property (may be nil).
//
// If the function fails, it returns nil and an error message is written to the log.
func CreatePopupFromResources(session Session, name string, binding any) Popup {
2026-11-05 13:31:38 +03:00
if session == nil {
ErrorLog(`the Session argument cannot be nil`)
return nil
}
if strings.ToLower(filepath.Ext(name)) != ".rui" {
name += ".rui"
}
createEmbed := func(fs *embed.FS, path string) Popup {
if data, err := fs.ReadFile(path); err == nil {
data, err := ParseDataText(string(data))
if err == nil {
return CreatePopupFromObject(session, data, binding)
}
ErrorLog(err.Error())
}
return nil
}
for _, fs := range resources.embedFS {
rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs {
switch dir {
case imageDir, themeDir, rawDir:
// do nothing
case viewDir:
if result := createEmbed(fs, dir+"/"+name); result != nil {
return result
}
case popupDir:
if result := createEmbed(fs, dir+"/"+name); result != nil {
return result
}
default:
if result := createEmbed(fs, dir+"/"+popupDir+"/"+name); result != nil {
return result
}
if result := createEmbed(fs, dir+"/"+viewDir+"/"+name); result != nil {
return result
}
}
}
}
if resources.path == "" {
return nil
}
createFromFile := func(path string) Popup {
if data, err := os.ReadFile(path); err == nil {
data, err := ParseDataText(string(data))
if err == nil {
return CreatePopupFromObject(session, data, binding)
}
ErrorLog(err.Error())
}
return nil
}
if result := createFromFile(resources.path + popupDir + "/" + name); result != nil {
return result
}
return createFromFile(resources.path + viewDir + "/" + name)
}
// ShowPopup creates a new Popup and shows it
func ShowPopup(view View, param Params) Popup {
popup := NewPopup(view, param)
if popup != nil {
popup.Show()
}
return popup
}
2021-09-07 17:36:50 +03:00
func (manager *popupManager) showPopup(popup Popup) {
if popup == nil {
return
}
2026-06-25 20:25:56 +03:00
manager.mutex.Lock()
dismissAnimation := manager.dismissAnimation
manager.dismissAnimation = nil
manager.mutex.Unlock()
for _, p := range dismissAnimation {
2026-06-04 12:51:33 +03:00
manager.removePopup(p)
2026-06-25 20:25:56 +03:00
p.onDismiss()
2026-06-04 12:51:33 +03:00
}
2026-06-25 20:25:56 +03:00
manager.mutex.Lock()
if len(manager.popups) == 0 {
2021-09-07 17:36:50 +03:00
manager.popups = []Popup{popup}
} else {
manager.popups = append(manager.popups, popup)
}
2026-06-25 20:25:56 +03:00
manager.mutex.Unlock()
2026-06-25 20:25:56 +03:00
session := popup.Session()
2022-11-02 20:10:19 +03:00
session.callFunc("blurCurrent")
2026-08-30 12:43:58 +03:00
session.callFunc("appendElement", popupLayerID, popup.html(false))
2026-06-25 20:25:56 +03:00
2026-11-02 14:13:08 +03:00
session.updateCSSProperty("ruiTooltip", "visibility", "hidden")
session.updateCSSProperty("ruiTooltip", "opacity", "0")
session.updateCSSProperty(popupLayerID, "visibility", "visible")
2022-10-30 17:22:33 +03:00
session.updateCSSProperty("ruiRoot", "pointer-events", "none")
2024-11-25 23:13:29 +03:00
popup.showAnimation()
2021-09-07 17:36:50 +03:00
}
func (manager *popupManager) dismissPopup(popup Popup, animation bool) {
2026-06-25 20:25:56 +03:00
if len(manager.popups) <= 0 || popup == nil {
2021-09-07 17:36:50 +03:00
return
}
2026-06-04 12:51:33 +03:00
index := slices.Index(manager.popups, popup)
2024-11-25 23:13:29 +03:00
if index < 0 {
2021-09-07 17:36:50 +03:00
return
}
2026-06-04 12:51:33 +03:00
listener := func(View, PropertyName) {
manager.removePopup(popup)
popup.onDismiss()
}
2025-06-25 13:53:08 +03:00
2026-06-04 12:51:33 +03:00
if animation && popup.dismissAnimation(listener) {
2026-06-25 20:25:56 +03:00
manager.mutex.Lock()
2026-06-04 12:51:33 +03:00
if len(manager.dismissAnimation) == 0 {
manager.dismissAnimation = []Popup{popup}
} else {
manager.dismissAnimation = append(manager.dismissAnimation, popup)
2021-09-07 17:36:50 +03:00
}
2026-06-25 20:25:56 +03:00
manager.mutex.Unlock()
2026-06-04 12:51:33 +03:00
} else {
manager.removePopup(popup)
2024-11-25 23:13:29 +03:00
popup.onDismiss()
}
2026-06-04 12:51:33 +03:00
}
2024-11-25 23:13:29 +03:00
2026-06-04 12:51:33 +03:00
func (manager *popupManager) removePopup(popup Popup) {
2026-06-25 20:25:56 +03:00
manager.mutex.Lock()
defer manager.mutex.Unlock()
2026-06-04 12:51:33 +03:00
index := slices.Index(manager.popups, popup)
if index < 0 {
return
}
manager.popups = append(manager.popups[:index], manager.popups[index+1:]...)
if len(manager.popups) == 0 {
session := popup.Session()
session.updateCSSProperty("ruiRoot", "pointer-events", "auto")
session.updateCSSProperty(popupLayerID, "visibility", "hidden")
}
}
func (manager *popupManager) dismissAnimationFinished(popup Popup) {
2026-06-25 20:25:56 +03:00
manager.mutex.Lock()
defer manager.mutex.Unlock()
2026-06-04 12:51:33 +03:00
if manager.dismissAnimation != nil {
if i := slices.Index(manager.dismissAnimation, popup); i >= 0 {
manager.dismissAnimation = append(manager.dismissAnimation[:i], manager.dismissAnimation[i+1:]...)
}
2021-09-07 17:36:50 +03:00
}
}
2025-06-18 14:19:57 +03:00
func newPopupListener0(fn func()) popupListener {
obj := new(popupListener0)
obj.fn = fn
return obj
}
func (data *popupListener0) Run(_ Popup) {
data.fn()
}
func (data *popupListener0) rawListener() any {
return data.fn
}
func newPopupListener1(fn func(Popup)) popupListener {
obj := new(popupListener1)
obj.fn = fn
return obj
}
func (data *popupListener1) Run(popup Popup) {
data.fn(popup)
}
func (data *popupListener1) rawListener() any {
return data.fn
}
func newPopupListenerBinding(name string, dismiss bool) popupListener {
2025-06-18 14:19:57 +03:00
obj := new(popupListenerBinding)
obj.name = name
obj.dismiss = dismiss
2025-06-18 14:19:57 +03:00
return obj
}
func (data *popupListenerBinding) runDismiss(popup Popup) bool {
if strings.ToLower(data.name) == "dismiss" {
popup.Dismiss()
return true
}
return false
}
2025-06-18 14:19:57 +03:00
func (data *popupListenerBinding) Run(popup Popup) {
bind := popup.View().binding()
if bind == nil {
if !data.dismiss || !data.runDismiss(popup) {
ErrorLogF(`There is no a binding object for call "%s"`, data.name)
}
2025-06-18 14:19:57 +03:00
return
}
val := reflect.ValueOf(bind)
method := val.MethodByName(data.name)
if !method.IsValid() {
if !data.dismiss || !data.runDismiss(popup) {
ErrorLogF(`The "%s" method is not valid`, data.name)
}
2025-06-18 14:19:57 +03:00
return
}
methodType := method.Type()
var args []reflect.Value = nil
switch methodType.NumIn() {
case 0:
args = []reflect.Value{}
case 1:
if equalType(methodType.In(0), reflect.TypeOf(popup)) {
2025-06-18 14:19:57 +03:00
args = []reflect.Value{reflect.ValueOf(popup)}
}
}
if args != nil {
method.Call(args)
} else {
ErrorLogF(`Unsupported prototype of "%s" method`, data.name)
}
}
func (data *popupListenerBinding) rawListener() any {
return data.name
}
func valueToPopupEventListeners(value any) ([]popupListener, bool) {
if value == nil {
return nil, true
}
switch value := value.(type) {
case []popupListener:
return value, true
case popupListener:
return []popupListener{value}, true
case string:
return []popupListener{newPopupListenerBinding(value, false)}, true
2025-06-18 14:19:57 +03:00
case func(Popup):
return []popupListener{newPopupListener1(value)}, true
case func():
return []popupListener{newPopupListener0(value)}, true
case []func(Popup):
result := make([]popupListener, 0, len(value))
for _, fn := range value {
if fn != nil {
result = append(result, newPopupListener1(fn))
}
}
return result, len(result) > 0
case []func():
result := make([]popupListener, 0, len(value))
for _, fn := range value {
if fn != nil {
result = append(result, newPopupListener0(fn))
}
}
return result, len(result) > 0
case []any:
result := make([]popupListener, 0, len(value))
for _, v := range value {
if v != nil {
switch v := v.(type) {
case func(Popup):
result = append(result, newPopupListener1(v))
case func():
result = append(result, newPopupListener0(v))
case string:
result = append(result, newPopupListenerBinding(v, false))
2025-06-18 14:19:57 +03:00
default:
return nil, false
}
}
}
return result, len(result) > 0
}
return nil, false
}
func getPopupListenerBinding(listeners []popupListener) string {
for _, listener := range listeners {
raw := listener.rawListener()
if text, ok := raw.(string); ok && text != "" {
return text
}
}
return ""
}