2021-09-07 17:36:50 +03:00
package rui
import (
2025-07-03 11:20:53 +03:00
"embed"
2022-04-14 12:05:45 +03:00
"os"
2021-09-07 17:36:50 +03:00
"path/filepath"
"strings"
)
2025-06-24 13:53:36 +03:00
var systemViewCreators = map [ string ] func ( Session ) View {
2021-09-07 17:36:50 +03:00
"View" : newView ,
"ColumnLayout" : newColumnLayout ,
"ListLayout" : newListLayout ,
"GridLayout" : newGridLayout ,
"StackLayout" : newStackLayout ,
"TabsLayout" : newTabsLayout ,
"AbsoluteLayout" : newAbsoluteLayout ,
"Resizable" : newResizable ,
"DetailsView" : newDetailsView ,
"TextView" : newTextView ,
"Button" : newButton ,
"Checkbox" : newCheckbox ,
"DropDownList" : newDropDownList ,
"ProgressBar" : newProgressBar ,
"NumberPicker" : newNumberPicker ,
"ColorPicker" : newColorPicker ,
"DatePicker" : newDatePicker ,
"TimePicker" : newTimePicker ,
2021-11-04 14:59:25 +03:00
"FilePicker" : newFilePicker ,
2021-09-07 17:36:50 +03:00
"EditView" : newEditView ,
"ListView" : newListView ,
"CanvasView" : newCanvasView ,
"ImageView" : newImageView ,
2023-01-25 01:06:36 +03:00
"SvgImageView" : newSvgImageView ,
2021-09-07 17:36:50 +03:00
"TableView" : newTableView ,
"AudioPlayer" : newAudioPlayer ,
"VideoPlayer" : newVideoPlayer ,
}
2025-06-25 13:53:08 +03:00
// ViewCreateListener is the listener interface of a view create event
type ViewCreateListener interface {
// OnCreate is a function of binding object that is called by the CreateViewFromText, CreateViewFromResources,
// and CreateViewFromObject functions after the creation of a view
OnCreate ( view View )
}
2025-06-24 13:53:36 +03:00
var viewCreate map [ string ] func ( Session ) View = nil
func viewCreators ( ) map [ string ] func ( Session ) View {
if viewCreate == nil {
viewCreate = map [ string ] func ( Session ) View { }
for tag , fn := range systemViewCreators {
viewCreate [ strings . ToLower ( tag ) ] = fn
}
2021-09-07 17:36:50 +03:00
}
2025-06-24 13:53:36 +03:00
return viewCreate
}
2021-09-07 17:36:50 +03:00
2025-06-24 13:53:36 +03:00
// RegisterViewCreator register function of creating view
func RegisterViewCreator ( tag string , creator func ( Session ) View ) bool {
loTag := strings . ToLower ( tag )
for name := range systemViewCreators {
if name == loTag {
ErrorLog ( ` It is forbidden to override the function of ` + tag + ` creating ` )
2021-09-07 17:36:50 +03:00
return false
}
}
2025-06-24 13:53:36 +03:00
viewCreators ( ) [ loTag ] = creator
2021-09-07 17:36:50 +03:00
return true
}
2025-06-24 13:53:36 +03:00
// CreateViewFromObject create new View and initialize it by DataObject data. Parameters:
// - session - the session to which the view will be attached (should not be nil);
2025-06-25 13:53:08 +03:00
// - object - data describing View;
2025-06-24 13:53:36 +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 CreateViewFromObject ( session Session , object DataObject , binding any ) View {
if session == nil {
2026-10-18 16:50:24 +03:00
ErrorLog ( ` CreateViewFromObject: session must not be nil ` )
return nil
}
if object == nil {
ErrorLog ( ` CreateViewFromObject: object must not be nil ` )
2025-06-24 13:53:36 +03:00
return nil
}
2021-09-07 17:36:50 +03:00
tag := object . Tag ( )
2025-06-24 13:53:36 +03:00
creator , ok := viewCreators ( ) [ strings . ToLower ( tag ) ]
if ! ok {
2026-10-18 16:50:24 +03:00
ErrorLog ( ` CreateViewFromObject: unknown view type " ` + tag + ` " ` )
2025-06-24 13:53:36 +03:00
return nil
}
2021-09-07 17:36:50 +03:00
2025-06-24 13:53:36 +03:00
if ! session . ignoreViewUpdates ( ) {
session . setIgnoreViewUpdates ( true )
defer session . setIgnoreViewUpdates ( false )
2021-09-07 17:36:50 +03:00
}
2025-06-24 13:53:36 +03:00
view := creator ( session )
view . init ( session )
if customView , ok := view . ( CustomView ) ; ok {
if ! InitCustomView ( customView , tag , session , nil ) {
return nil
}
}
parseProperties ( view , object )
if binding != nil {
view . setRaw ( Binding , binding )
2025-06-25 13:53:08 +03:00
if listener , ok := binding . ( ViewCreateListener ) ; ok {
listener . OnCreate ( view )
}
2025-06-24 13:53:36 +03:00
}
return view
2021-09-07 17:36:50 +03:00
}
2025-06-24 13:53:36 +03:00
// CreateViewFromText create new View 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 View;
// - binding - object assigned to the Binding property (optional parameter).
//
// If the function fails, it returns nil and an error message is written to the log.
func CreateViewFromText ( session Session , text string , binding ... any ) View {
2025-06-25 17:42:32 +03:00
data , err := ParseDataText ( text )
if err != nil {
ErrorLog ( err . Error ( ) )
return nil
2021-09-07 17:36:50 +03:00
}
2025-06-25 17:42:32 +03:00
var b any = nil
if len ( binding ) > 0 {
b = binding [ 0 ]
}
return CreateViewFromObject ( session , data , b )
2021-09-07 17:36:50 +03:00
}
// CreateViewFromResources create new View and initialize it by the content of
2025-06-24 13:53:36 +03:00
// the resource file from "views" directory. Parameters:
// - session - the session to which the view will be attached (should not be nil);
2025-07-03 11:20:53 +03:00
// - name - file name in the "views" directory of the application resources (it is not necessary to specify the .rui extension, it is added automatically);
2025-06-24 13:53:36 +03:00
// - binding - object assigned to the Binding property (optional parameter).
//
// If the function fails, it returns nil and an error message is written to the log.
func CreateViewFromResources ( session Session , name string , binding ... any ) View {
2021-09-07 17:36:50 +03:00
if strings . ToLower ( filepath . Ext ( name ) ) != ".rui" {
name += ".rui"
}
2025-06-24 13:53:36 +03:00
var b any = nil
if len ( binding ) > 0 {
b = binding [ 0 ]
}
2025-07-03 11:20:53 +03:00
createEmbed := func ( fs * embed . FS , path string ) View {
if data , err := fs . ReadFile ( path ) ; err == nil {
data , err := ParseDataText ( string ( data ) )
if err == nil {
return CreateViewFromObject ( session , data , b )
}
ErrorLog ( err . Error ( ) )
}
return nil
}
2021-09-07 17:36:50 +03:00
for _ , fs := range resources . embedFS {
2024-05-20 16:51:30 +03:00
rootDirs := resources . embedRootDirs ( fs )
2021-09-07 17:36:50 +03:00
for _ , dir := range rootDirs {
switch dir {
case imageDir , themeDir , rawDir :
// do nothing
case viewDir :
2025-07-03 11:20:53 +03:00
if result := createEmbed ( fs , dir + "/" + name ) ; result != nil {
return result
2021-09-07 17:36:50 +03:00
}
default :
2025-07-03 11:20:53 +03:00
if result := createEmbed ( fs , dir + "/" + viewDir + "/" + name ) ; result != nil {
return result
2021-09-07 17:36:50 +03:00
}
}
}
}
2022-04-14 12:05:45 +03:00
if resources . path != "" {
if data , err := os . ReadFile ( resources . path + viewDir + "/" + name ) ; err == nil {
2025-06-25 17:42:32 +03:00
data , err := ParseDataText ( string ( data ) )
if err != nil {
ErrorLog ( err . Error ( ) )
} else {
2025-06-24 13:53:36 +03:00
return CreateViewFromObject ( session , data , b )
2022-04-14 12:05:45 +03:00
}
}
}
2021-09-07 17:36:50 +03:00
return nil
}