2021-09-07 17:36:50 +03:00
package rui
2022-07-31 15:37:26 +03:00
import (
2025-07-03 11:20:53 +03:00
"embed"
2024-11-25 23:13:29 +03:00
"fmt"
2025-07-03 11:20:53 +03:00
"os"
"path/filepath"
2025-06-18 14:19:57 +03:00
"reflect"
2025-07-01 17:34:31 +03:00
"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
2024-09-12 14:05:11 +03:00
// Constants for [Popup] specific properties and events
2021-09-07 17:36:50 +03:00
const (
2024-09-18 13:50:06 +03:00
// Title is the constant for "title" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup, TabsLayout.
2024-09-18 13:50:06 +03:00
//
2024-12-05 20:15:39 +03:00
// Usage in Popup:
2024-09-18 13:50:06 +03:00
// Define the title.
//
2024-12-05 20:15:39 +03:00
// Supported types: string.
2024-09-18 13:50:06 +03:00
//
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-09-18 13:50:06 +03:00
//
2024-12-05 20:15:39 +03:00
// Supported types: string.
2025-03-13 11:50:57 +03:00
Title PropertyName = "title"
2022-07-08 13:16:42 +03:00
2024-09-18 13:50:06 +03:00
// TitleStyle is the constant for "title-style" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// 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"
2022-07-08 13:16:42 +03:00
2024-09-18 13:50:06 +03:00
// 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-09-18 13:50:06 +03:00
//
2024-12-05 20:15:39 +03:00
// Supported types: bool, int, string.
2024-09-18 13:50:06 +03:00
//
// 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"
2022-07-08 13:16:42 +03:00
2024-09-18 13:50:06 +03:00
// 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-09-18 13:50:06 +03:00
//
2024-12-05 20:15:39 +03:00
// Supported types: bool, int, string.
2024-09-18 13:50:06 +03:00
//
// 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"
2022-07-08 13:16:42 +03:00
2024-09-18 13:50:06 +03:00
// Buttons is the constant for "buttons" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// Buttons that will be placed at the bottom of the popup.
//
2024-12-05 20:15:39 +03:00
// Supported types: PopupButton, []PopupButton.
2024-09-18 13:50:06 +03:00
//
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"
2022-07-08 13:16:42 +03:00
2024-09-18 13:50:06 +03:00
// ButtonsAlign is the constant for "buttons-align" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// Set the horizontal alignment of popup buttons.
//
2024-12-05 20:15:39 +03:00
// Supported types: int, string.
2024-09-18 13:50:06 +03:00
//
// 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"
2022-07-08 13:16:42 +03:00
2024-09-18 13:50:06 +03:00
// 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.
2024-09-18 13:50:06 +03:00
//
// General listener format:
2024-12-05 20:15:39 +03:00
//
// func(popup rui.Popup)
2024-09-18 13:50:06 +03:00
//
// 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
2024-09-18 13:50:06 +03:00
// Arrow is the constant for "arrow" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// Add an arrow to popup. Default value is "none".
//
2024-12-05 20:15:39 +03:00
// Supported types: int, string.
2024-09-18 13:50:06 +03:00
//
// 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
2024-09-18 13:50:06 +03:00
// ArrowAlign is the constant for "arrow-align" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// Set the horizontal alignment of the popup arrow. Default value is "center".
//
2024-12-05 20:15:39 +03:00
// Supported types: int, string.
2024-09-18 13:50:06 +03:00
//
// 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
2024-09-18 13:50:06 +03:00
// ArrowSize is the constant for "arrow-size" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// 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-09-18 13:50:06 +03:00
//
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
2024-09-18 13:50:06 +03:00
// ArrowWidth is the constant for "arrow-width" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2024-09-18 13:50:06 +03:00
// 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-09-18 13:50:06 +03:00
//
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
2026-07-01 17:00:40 +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
2026-07-01 17:00:40 +03:00
// OutsideColor is the constant for "outside-color" property tag.
2024-09-18 13:50:06 +03:00
//
2024-12-05 20:15:39 +03:00
// Used by Popup.
2026-07-01 17:00:40 +03:00
// Specifies the color used to paint the space outside and below the Popup.
2024-09-18 13:50:06 +03:00
//
2026-07-09 21:36:44 +03:00
// Supported types: Color, ColorPair, string.
2024-09-18 13:50:06 +03:00
//
2026-07-01 17:00:40 +03:00
// 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
2024-09-12 14:05:11 +03:00
)
2023-05-15 15:27:37 +03:00
2024-09-12 14:05:11 +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
2024-09-18 13:50:06 +03:00
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
2024-09-18 13:50:06 +03:00
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
)
2025-07-01 17:34:31 +03:00
const (
popupLayerID = "ruiPopupLayer"
popupArrowID = "ruiPopupArrow"
popupButtonsID = "ruiPopupButtons"
popupID = "ruiPopup"
popupContentID = "ruiPopupContent"
popupTitleID = "ruiPopupTitle"
)
2024-09-12 14:05:11 +03:00
// PopupButtonType represent popup button type
2023-05-15 15:27:37 +03:00
type PopupButtonType int
2022-07-08 13:16:42 +03:00
// 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 {
2024-09-12 14:05:11 +03:00
// 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
}
2025-07-01 17:34:31 +03:00
type popupButton struct {
title string
buttonType PopupButtonType
onClick popupListener
}
2024-09-12 14:05:11 +03:00
// Popup represents a Popup view
2021-09-07 17:36:50 +03:00
type Popup interface {
2025-07-01 17:34:31 +03:00
Properties
2025-07-03 10:42:04 +03:00
fmt . Stringer
2025-07-01 17:34:31 +03:00
2024-09-12 14:05:11 +03:00
// View returns a content view of the popup
2021-09-07 17:36:50 +03:00
View ( ) View
2024-09-12 14:05:11 +03:00
// Session returns current client session
2021-09-07 17:36:50 +03:00
Session ( ) Session
2024-09-12 14:05:11 +03:00
// Show displays a popup
2021-09-07 17:36:50 +03:00
Show ( )
2024-09-12 14:05:11 +03:00
// Dismiss closes a popup
2021-09-07 17:36:50 +03:00
Dismiss ( )
2024-09-12 14:05:11 +03:00
2026-05-22 23:08:58 +03:00
// DismissWithoutAnimation closes a popup without animation
DismissWithoutAnimation ( )
2026-05-25 15:31:45 +03:00
// 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 ) )
2022-07-08 13:16:42 +03:00
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 {
2025-07-01 17:34:31 +03:00
name string
dismiss bool
2025-06-18 14:19:57 +03:00
}
2021-09-07 17:36:50 +03:00
type popupData struct {
2025-07-01 17:34:31 +03:00
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
2025-07-01 17:34:31 +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
}
}
2025-07-01 17:34:31 +03:00
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 {
2025-07-01 17:34:31 +03:00
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 )
2025-07-01 17:34:31 +03:00
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 )
2025-07-01 17:34:31 +03:00
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
}
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) Get ( tag PropertyName ) any {
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +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 ( )
2025-07-01 17:34:31 +03:00
}
2026-08-26 13:47:37 +03:00
return popup . layerBackgroudView
2026-08-04 15:24:50 +03:00
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
}
2026-08-04 15:24:50 +03:00
return popup . getRaw ( tag )
2025-07-01 17:34:31 +03:00
}
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
}
2025-07-01 17:34:31 +03:00
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
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) Set ( tag PropertyName , value any ) bool {
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +03:00
if value == nil {
popup . Remove ( tag )
return true
2024-11-25 23:13:29 +03:00
}
2025-07-01 17:34:31 +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
2025-07-03 10:42:04 +03:00
popup . setRaw ( Content , popup . contentView )
2025-07-01 17:34:31 +03:00
case DataObject :
view := CreateViewFromObject ( popup . session , value , nil )
if view == nil {
return false
}
popup . contentView = view
2025-07-03 10:42:04 +03:00
popup . setRaw ( Content , popup . contentView )
2025-07-01 17:34:31 +03:00
case string :
2025-07-06 11:20:37 +03:00
if ok , constName := isConstantName ( value ) ; ok {
view := CreateViewFromResources ( popup . session , constName )
2025-07-01 17:34:31 +03:00
if view != nil {
popup . contentView = view
break
}
}
popup . contentView = NewTextView ( popup . session , Params { Text : value } )
2025-07-03 10:42:04 +03:00
popup . setRaw ( Content , value )
2025-07-01 17:34:31 +03:00
default :
notCompatibleType ( Buttons , value )
return false
}
if binding := popup . getRaw ( Binding ) ; binding != nil {
popup . contentView . Set ( Binding , binding )
}
2025-07-03 10:42:04 +03:00
2025-07-01 17:34:31 +03:00
popup . propertyChanged ( Content )
return true
case Binding :
2025-07-03 10:42:04 +03:00
if popup . contentView != nil {
popup . contentView . Set ( Binding , value )
}
2025-07-01 17:34:31 +03:00
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 )
2026-05-17 20:18:20 +03:00
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
}
2026-07-01 17:00:40 +03:00
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
}
2025-07-01 17:34:31 +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 ) )
2022-07-08 13:16:42 +03:00
}
2025-07-01 17:34:31 +03:00
return false
}
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) Remove ( tag PropertyName ) {
tag = defaultNormalize ( tag )
2021-11-17 12:32:37 +03:00
2025-07-01 17:34:31 +03:00
switch tag {
case Content :
popup . contentView = nil
}
2022-07-31 15:37:26 +03:00
2025-07-01 17:34:31 +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
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) propertyChanged ( tag PropertyName ) {
2026-08-26 13:47:37 +03:00
if popup . layerBackgroudView == nil {
2025-07-01 17:34:31 +03:00
return
}
2021-11-17 12:32:37 +03:00
2025-07-01 17:34:31 +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
}
2026-07-01 17:00:40 +03:00
case OutsideColor :
2026-08-26 13:47:37 +03:00
popup . layerBackgroudView . Set ( BackgroundColor , popup . Get ( OutsideColor ) )
2026-07-01 17:00:40 +03:00
case OutsideFilter :
2026-08-26 13:47:37 +03:00
popup . layerBackgroudView . Set ( BackdropFilter , popup . Get ( OutsideFilter ) )
2026-07-01 17:00:40 +03:00
2025-07-02 11:58:34 +03:00
default :
if popup . supported ( tag ) {
popup . popupView . Set ( tag , popup . getRaw ( tag ) )
}
2025-07-01 17:34:31 +03:00
}
}
2022-07-31 15:37:26 +03:00
2026-07-01 13:37:23 +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
}
2026-05-17 20:18:20 +03:00
2026-07-01 13:37:23 +03:00
func ( popup * popupData ) animationProperty ( ) AnimationProperty {
2025-07-02 11:58:34 +03:00
timing , ok := stringProperty ( popup , ShowTiming , popup . session )
2026-07-01 13:37:23 +03:00
if ok && ! isTimingFunctionValid ( timing ) {
DebugLog ( ` " ` + timing + ` " is an invalid timing function ` )
ok = false
}
2025-07-02 11:58:34 +03:00
if ! ok {
2026-07-01 13:37:23 +03:00
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
}
2026-07-01 13:37:23 +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 ,
} )
}
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) View ( ) View {
return popup . contentView
}
2021-11-17 12:32:37 +03:00
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) Session ( ) Session {
return popup . session
}
2021-09-07 17:36:50 +03:00
2025-07-03 10:42:04 +03:00
func ( popup * popupData ) String ( ) string {
buffer := allocStringBuilder ( )
defer freeStringBuilder ( buffer )
writeViewStyle ( "Popup" , popup , buffer , "" , nil )
return buffer . String ( )
}
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) setButtons ( value any ) bool {
popupButtonFromObject := func ( obj DataObject ) popupButton {
var button popupButton
2021-09-07 17:36:50 +03:00
2026-10-18 16:50:24 +03:00
button . title , _ = obj . PropertyText ( string ( Title ) )
2021-09-07 17:36:50 +03:00
2026-10-18 16:50:24 +03:00
if text , ok := obj . PropertyText ( "type" ) ; ok {
2025-07-01 17:34:31 +03:00
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
2026-10-18 16:50:24 +03:00
if fn , ok := obj . PropertyText ( "click" ) ; ok {
2025-07-01 17:34:31 +03:00
button . onClick = newPopupListenerBinding ( fn , true )
} else if button . buttonType == CancelButton {
button . onClick = newPopupListener0 ( popup . Dismiss )
}
2021-09-07 17:36:50 +03:00
2025-07-01 17:34:31 +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
}
2025-07-01 17:34:31 +03:00
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
2025-07-01 17:34:31 +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 ) )
2025-07-01 17:34:31 +03:00
}
popup . setRaw ( Buttons , buttons )
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +03:00
case [ ] popupButton :
popup . setRaw ( Buttons , value )
2024-11-25 23:13:29 +03:00
2026-10-18 16:50:24 +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
2026-10-18 16:50:24 +03:00
case ArrayDataValue :
array := value . Array ( )
if array == nil {
return false
2025-07-01 17:34:31 +03:00
}
2026-10-18 16:50:24 +03:00
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
2025-07-01 17:34:31 +03:00
}
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 :
2025-07-01 17:34:31 +03:00
notCompatibleType ( Buttons , val )
2021-09-07 17:36:50 +03:00
}
}
2025-07-01 17:34:31 +03:00
if len ( buttons ) > 0 {
popup . setRaw ( Buttons , buttons )
}
default :
notCompatibleType ( Buttons , value )
return false
2021-09-07 17:36:50 +03:00
}
2025-07-01 17:34:31 +03:00
popup . propertyChanged ( Buttons )
return true
}
2022-07-31 15:37:26 +03:00
2025-07-01 17:34:31 +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
}
2025-07-01 17:34:31 +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 ( )
}
2026-05-25 15:31:45 +03:00
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
}
}
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) Dismiss ( ) {
2026-05-22 23:08:58 +03:00
popup . Session ( ) . popupManager ( ) . dismissPopup ( popup , true )
}
func ( popup * popupData ) DismissWithoutAnimation ( ) {
popup . Session ( ) . popupManager ( ) . dismissPopup ( popup , false )
2025-07-01 17:34:31 +03:00
}
func ( popup * popupData ) Show ( ) {
popup . Session ( ) . popupManager ( ) . showPopup ( popup )
}
2026-05-17 20:18:20 +03:00
func ( popup * popupData ) showTransformAndOpacity ( ) ( TransformProperty , float64 ) {
2026-07-01 13:37:23 +03:00
opacity , _ := floatProperty ( popup , ShowOpacity , popup . session , popup . defaultPopupFloat ( ShowOpacity , 1 ) )
2025-07-01 17:34:31 +03:00
transform := getTransformProperty ( popup , ShowTransform )
2026-05-17 20:18:20 +03:00
if transform == nil {
2026-07-01 13:37:23 +03:00
if value := popup . session . PopupDefault ( ShowTransform ) ; value != nil {
if t , ok := value . ( TransformProperty ) ; ok {
transform = t
}
}
2026-05-17 20:18:20 +03:00
}
2025-07-01 17:34:31 +03:00
2026-05-17 20:18:20 +03:00
return transform , opacity
}
func ( popup * popupData ) showAnimation ( ) {
transform , opacity := popup . showTransformAndOpacity ( )
2025-07-01 17:34:31 +03:00
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
2026-05-17 20:18:20 +03:00
animation := popup . animationProperty ( )
if opacity != 1 {
2026-08-26 13:47:37 +03:00
popup . layerBackgroudView . SetTransition ( Opacity , animation )
2026-05-17 20:18:20 +03:00
}
if transform != nil {
2026-08-26 13:47:37 +03:00
popup . layerContentView . SetTransition ( Transform , animation )
2026-05-17 20:18:20 +03:00
}
2025-07-01 17:34:31 +03:00
if opacity != 1 {
2026-08-26 13:47:37 +03:00
session . updateCSSProperty ( popup . layerBackgroudView . htmlID ( ) , string ( Opacity ) , "1" )
2025-07-01 17:34:31 +03:00
}
if transform != nil {
2026-08-26 13:47:37 +03:00
session . updateCSSProperty ( popup . layerContentView . htmlID ( ) , string ( Transform ) , "" )
2025-07-01 17:34:31 +03:00
}
}
}
2026-06-04 12:51:33 +03:00
func ( popup * popupData ) dismissAnimation ( listener func ( View , PropertyName ) ) bool {
2026-05-17 20:18:20 +03:00
transform , opacity := popup . showTransformAndOpacity ( )
2025-07-01 17:34:31 +03:00
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 )
}
2025-07-01 17:34:31 +03:00
if opacity != 1 {
2026-08-26 13:47:37 +03:00
session . updateCSSProperty ( popup . layerBackgroudView . htmlID ( ) , string ( Opacity ) , fmt . Sprintf ( "%.2f" , opacity ) )
2025-07-01 17:34:31 +03:00
}
if transform != nil {
2026-08-26 13:47:37 +03:00
session . updateCSSProperty ( popup . layerContentView . htmlID ( ) , string ( Transform ) , transform . transformCSS ( session ) )
2025-07-01 17:34:31 +03:00
}
2026-05-17 20:42:49 +03:00
2026-06-04 12:51:33 +03:00
popup . dismissAnimationRunning = true
2025-07-01 17:34:31 +03:00
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 ( )
2025-07-01 17:34:31 +03:00
}
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 ( )
2025-07-01 17:34:31 +03:00
}
func ( popup * popupData ) viewByHTMLID ( id string ) View {
2026-08-26 13:47:37 +03:00
if popup . layerBackgroudView != nil {
return viewByHTMLID ( id , popup . layerBackgroudView )
2025-07-01 17:34:31 +03:00
}
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
2025-07-01 17:34:31 +03:00
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
}
2025-07-01 17:34:31 +03:00
}
2021-09-07 17:36:50 +03:00
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) keyEvent ( event KeyEvent ) bool {
2026-05-25 15:31:45 +03:00
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 {
2025-07-01 17:34:31 +03:00
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
2025-07-01 17:34:31 +03:00
case EscapeKey :
2026-05-25 15:31:45 +03:00
if popup . isCancelable ( ) {
2025-07-01 17:34:31 +03:00
popup . cancel ( )
return true
}
}
}
2026-05-25 15:31:45 +03:00
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
}
}
2025-07-01 17:34:31 +03:00
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 )
}
2025-07-01 17:34:31 +03:00
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 ,
} )
2025-07-01 17:34:31 +03:00
if button . onClick != nil {
fn := button . onClick . Run
2023-05-15 15:27:37 +03:00
buttonView . Set ( ClickEvent , func ( ) {
fn ( popup )
} )
2025-07-01 17:34:31 +03:00
} else if button . buttonType == CancelButton {
2023-05-15 15:27:37 +03:00
buttonView . Set ( ClickEvent , popup . cancel )
}
2025-07-01 17:34:31 +03:00
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
}
2025-07-01 17:34:31 +03:00
return NewGridLayout ( session , Params {
ID : popupButtonsID ,
Column : 0 ,
Row : 2 ,
2021-09-07 17:36:50 +03:00
CellHorizontalAlign : buttonsAlign ,
Content : buttonsPanel ,
2025-07-01 17:34:31 +03:00
} )
2021-09-07 17:36:50 +03:00
}
2025-07-01 17:34:31 +03:00
return nil
}
2021-09-07 17:36:50 +03:00
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) createTitleView ( ) GridLayout {
session := popup . Session ( )
2022-08-07 18:59:56 +03:00
2025-07-01 17:34:31 +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
}
2025-07-01 17:34:31 +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 )
2025-07-01 17:34:31 +03:00
if title != nil {
break
}
}
title = NewTextView ( session , Params { Text : value } )
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +03:00
case View :
title = value
2024-11-25 23:13:29 +03:00
}
}
2025-07-01 17:34:31 +03:00
if title == nil && closeButton == nil {
return nil
2021-09-07 17:36:50 +03:00
}
2025-07-01 17:34:31 +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
}
2025-07-01 17:34:31 +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
}
2025-07-01 17:34:31 +03:00
func ( popup * popupData ) createContentContainer ( ) ColumnLayout {
params := Params {
ID : popupContentID ,
Column : 0 ,
Row : 1 ,
}
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +03:00
if popup . contentView != nil {
params [ Content ] = popup . contentView
2024-11-25 23:13:29 +03:00
}
2025-07-01 17:34:31 +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
2025-07-01 17:34:31 +03:00
session := popup . session
params := Params {
Style : "ruiPopup" ,
ID : popupID ,
2025-07-01 19:03:39 +03:00
Row : 2 ,
Column : 2 ,
2025-07-01 17:34:31 +03:00
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 ,
2026-07-01 17:00:40 +03:00
OutsideColor ,
OutsideFilter ,
2025-07-01 17:34:31 +03:00
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 ( )
2025-07-01 17:34:31 +03:00
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
2025-07-01 17:34:31 +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
2025-07-01 17:34:31 +03:00
views = append ( views , popup . createContentContainer ( ) )
2021-09-07 17:36:50 +03:00
2025-07-01 17:34:31 +03:00
if buttons := popup . createButtonsPanel ( ) ; buttons != nil {
views = append ( views , buttons )
}
2021-09-07 17:36:50 +03:00
2025-07-01 17:34:31 +03:00
params [ Content ] = views
2021-09-07 17:36:50 +03:00
2025-07-01 17:34:31 +03:00
popup . popupView = NewGridLayout ( session , params )
2024-11-25 23:13:29 +03:00
2025-07-01 17:34:31 +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 ( ) ,
2022-07-08 13:16:42 +03:00
}
2025-07-01 17:34:31 +03:00
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 )
2025-07-01 17:34:31 +03:00
}
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 ) }
2025-07-01 17:34:31 +03:00
} 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 } ,
}
2026-07-09 20:18:29 +03:00
if value := popup . getRaw ( OutsideColor ) ; value != nil {
2026-08-26 13:47:37 +03:00
backgroundParams [ BackgroundColor ] = value
2026-07-01 17:00:40 +03:00
}
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-07-01 17:00:40 +03:00
}
}
2026-08-26 13:47:37 +03:00
popup . layerBackgroudView = NewColumnLayout ( session , backgroundParams )
2025-07-01 17:34:31 +03:00
2025-07-02 11:58:34 +03:00
opacity , _ := floatProperty ( popup , ShowOpacity , session , 1 )
2025-07-01 17:34:31 +03:00
transform := getTransformProperty ( popup , ShowTransform )
if opacity != 1 || transform != nil {
2025-07-02 11:58:34 +03:00
animation := popup . animationProperty ( )
2025-07-01 17:34:31 +03:00
if opacity != 1 {
2026-08-26 13:47:37 +03:00
popup . layerBackgroudView . Set ( Opacity , opacity )
popup . layerBackgroudView . SetTransition ( Opacity , animation )
2025-07-01 17:34:31 +03:00
}
if transform != nil {
2026-08-26 13:47:37 +03:00
popup . layerContentView . Set ( Transform , transform )
popup . layerContentView . SetTransition ( Transform , animation )
2025-07-01 17:34:31 +03:00
}
}
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 ( )
2025-07-01 17:34:31 +03:00
popup . session = view . Session ( )
popup . contentView = view
2026-05-25 15:31:45 +03:00
popup . hotkeys = map [ string ] func ( Popup ) { }
2026-05-17 20:18:20 +03:00
2026-07-01 13:37:23 +03:00
for tag , value := range popup . session . PopupDefaultsSeq ( ) {
if _ , ok := param [ tag ] ; ! ok && value != nil {
param [ tag ] = value
2026-05-17 20:18:20 +03:00
}
}
2025-07-01 17:34:31 +03:00
for tag , value := range param {
popup . Set ( tag , value )
}
2021-09-07 17:36:50 +03:00
return popup
}
2025-07-01 17:34:31 +03:00
// 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).
2025-07-01 17:34:31 +03:00
//
// If the function fails, it returns nil and an error message is written to the log.
2025-06-25 17:42:32 +03:00
func CreatePopupFromObject ( session Session , object DataObject , binding any ) Popup {
2025-07-03 11:20:53 +03:00
if session == nil {
2026-11-05 13:31:38 +03:00
ErrorLog ( ` the Session argument cannot be nil ` )
2025-07-03 11:20:53 +03:00
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
}
2025-07-01 17:34:31 +03:00
popup := new ( popupData )
2026-08-15 11:33:22 +03:00
popup . init ( )
2025-07-01 17:34:31 +03:00
popup . session = session
2026-05-25 15:31:45 +03:00
popup . hotkeys = map [ string ] func ( Popup ) { }
2025-07-01 17:34:31 +03:00
2026-10-18 16:50:24 +03:00
for key , value := range object . Properties ( ) {
popup . Set ( PropertyName ( key ) , value )
2025-06-25 17:42:32 +03:00
}
2025-07-01 17:34:31 +03:00
if binding != nil {
popup . Set ( Binding , binding )
}
2025-06-25 17:42:32 +03:00
2025-07-01 17:34:31 +03:00
return popup
}
2025-06-25 17:42:32 +03:00
2025-07-01 17:34:31 +03:00
// 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).
2025-07-01 17:34:31 +03:00
//
// 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
}
2025-07-01 17:34:31 +03:00
data , err := ParseDataText ( text )
if err != nil {
ErrorLog ( err . Error ( ) )
2025-06-25 17:42:32 +03:00
return nil
}
2025-07-01 17:34:31 +03:00
return CreatePopupFromObject ( session , data , binding )
2025-06-25 17:42:32 +03:00
}
2025-07-01 17:34:31 +03:00
2025-07-03 11:20:53 +03:00
// 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).
2025-07-03 11:20:53 +03:00
//
// 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
}
2025-07-03 11:20:53 +03:00
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 )
}
2022-07-08 13:16:42 +03:00
// 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 ( )
2024-08-20 20:01:26 +03:00
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 ( )
2021-11-21 18:30:46 +03:00
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" )
2025-07-01 17:34:31 +03:00
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
}
2026-05-22 23:08:58 +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
}
2025-07-01 17:34:31 +03:00
func newPopupListenerBinding ( name string , dismiss bool ) popupListener {
2025-06-18 14:19:57 +03:00
obj := new ( popupListenerBinding )
obj . name = name
2025-07-01 17:34:31 +03:00
obj . dismiss = dismiss
2025-06-18 14:19:57 +03:00
return obj
}
2025-07-01 17:34:31 +03:00
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 {
2025-07-01 17:34:31 +03:00
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 ( ) {
2025-07-01 17:34:31 +03:00
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 :
2026-08-12 11:10:59 +03:00
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 :
2025-07-01 17:34:31 +03:00
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 :
2025-07-01 17:34:31 +03:00
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
}
2025-07-03 10:42:04 +03:00
func getPopupListenerBinding ( listeners [ ] popupListener ) string {
for _ , listener := range listeners {
raw := listener . rawListener ( )
if text , ok := raw . ( string ) ; ok && text != "" {
return text
}
}
return ""
}