tgframe

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NotifyTypeCreate = 1
	NotifyTypeUpdate = 2
	NotifyTypeDelete = 3
)
View Source
const MainContainerID string = "container_main"

MainContainerID is the id of root container. The creation of root container won't trigger SendNotifyPackFunc.

View Source
const SidebarContainerID string = "container_sidebar"

SidebarContainerID is the id of sidebar container. The creation of root container won't trigger SendNotifyPackFunc.

Variables

View Source
var ContainerComponentName = "container_component"
View Source
var ErrPageNotFound = errors.New("page not found")

ErrPageNotFound is the error that the page is not found.

View Source
var ErrPanic = errors.New("panic")

ErrPanic is the error that the app panics.

Functions

func NewNotifyPackCreate

func NewNotifyPackCreate(containerID string, comp Component) *notifyPackCreate

NewNotifyPackCreate creates a new notify pack for creating a component.

func NewNotifyPackDelete

func NewNotifyPackDelete(compID string) *notifyPackDelete

NewNotifyPackDelete creates a new notify pack for deleting a component.

func NewNotifyPackUpdate

func NewNotifyPackUpdate(comp Component) *notifyPackUpdate

NewNotifyPackUpdate creates a new notify pack for updating a component.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App is an app

func NewApp

func NewApp() *App

NewApp return App

func (*App) AddPage

func (app *App) AddPage(name, title string, runFunc RunFunc)

AddPage add a handled page by name, title, and runFunc.

app.AddPage("index", "Index", f})

func (*App) AddPageByConfig

func (app *App) AddPageByConfig(conf *PageConfig, runFunc RunFunc)

AddPageByConfig add a handled page by name, title, icon, and runFunc.

app.AddPageByConfig(&tgframe.PageConfig{
	Name:  "page1",
	Title: "Page1",
	Emoji: "🐱",
}, Page1)

func (*App) AppConf

func (app *App) AppConf() *AppConf

AppConf return AppConf.

func (*App) FirstPage

func (app *App) FirstPage() (string, bool)

FirstPage return the first page in the app.

func (*App) HasPage

func (app *App) HasPage(name string) bool

HasPage return existence of page which named `name`.

func (*App) Run

func (app *App) Run(name string, state *State, notifyFunc SendNotifyPackFunc) error

Run run a page which named `name` with state.

func (*App) RunWithHandlingPanic

func (app *App) RunWithHandlingPanic(
	name string, state *State, notifyFunc SendNotifyPackFunc) (err error)

Run run a page which named `name` with state. Return a error wrap with ErrPanic if encounter panic.

func (*App) SetHashPageNameMode

func (app *App) SetHashPageNameMode(v bool)

SetHashPageMode set value of hash page name mode flag.

type AppConf

type AppConf struct {
	PageNames []string               `json:"page_names"`
	PageConfs map[string]*PageConfig `json:"page_confs"`

	HashPageNameMode bool `json:"hash_page_name_mode"`

	MainContainerID    string `json:"main_container_id"`
	SidebarContainerID string `json:"sidebar_container_id"`
}

AppConf store configs for frontend

type BaseComponent

type BaseComponent struct {
	// Name is the typename of the component.
	Name string `json:"name"`

	// ID shouldn't be duplicated.
	ID string `json:"id"`
}

BaseComponent stores the basic info of a component.

func (*BaseComponent) GetID

func (c *BaseComponent) GetID() string

GetID return component's ID.

func (*BaseComponent) SetID

func (c *BaseComponent) SetID(id string)

SetID set component's ID.

type Component

type Component interface {
	GetID() string
}

Component is the interface of a component.

type Container

type Container struct {
	*BaseComponent

	SendNotifyPack SendNotifyPackFunc `json:"-"`
}

Container contain list of components.

func NewContainer

func NewContainer(id string, notifyComp SendNotifyPackFunc) *Container

func (*Container) AddComponent

func (c *Container) AddComponent(comp Component) Component

func (*Container) AddContainer

func (c *Container) AddContainer(id string) *Container

func (*Container) With added in v0.2.2

func (c *Container) With(f func(c *Container))

With is a helper function to add a component to the container. Example:

container.With(func(c *Container) {
	Button(c, "button", "Click me"))
})

type Event added in v0.2.2

type Event interface {
	ApplyState(state *State)
}

Event is the state change made by app user

func ParseEvent added in v0.2.2

func ParseEvent(data []byte) (Event, error)

type EventClick added in v0.2.2

type EventClick struct {
	ID string `json:"id"`
}

EventClick is the event of a button click event

func (*EventClick) ApplyState added in v0.2.2

func (e *EventClick) ApplyState(state *State)

type EventEmpty added in v0.2.2

type EventEmpty struct {
}

EventEmpty is the event of an empty event, rerun button will send this

func (*EventEmpty) ApplyState added in v0.2.2

func (e *EventEmpty) ApplyState(*State)

type EventForm added in v0.2.2

type EventForm struct {
	Events []Event `json:"events"`
}

EventForm is the event of a form event it's used for form component

func (*EventForm) ApplyState added in v0.2.2

func (e *EventForm) ApplyState(state *State)

type EventInput added in v0.2.2

type EventInput struct {
	ID    string `json:"id"`
	Value any    `json:"value"`
}

EventInput is the event of a input event it's used for all input components

func (*EventInput) ApplyState added in v0.2.2

func (e *EventInput) ApplyState(state *State)

type EventSelect added in v0.2.2

type EventSelect struct {
	ID    string `json:"id"`
	Value int    `json:"value"`
}

EventSelect is the event of a select event it's used for select/radio component

func (*EventSelect) ApplyState added in v0.2.2

func (e *EventSelect) ApplyState(state *State)

type EventStruct added in v0.2.2

type EventStruct struct {
	Type EventType `json:"type"`
}

type EventType added in v0.2.2

type EventType string

EventType is the type of event

const (
	EventEmptyName  EventType = ""
	EventClickName  EventType = "click"
	EventInputName  EventType = "input"
	EventSelectName EventType = "select"
	EventFormName   EventType = "form"
)

type NotifyPack

type NotifyPack interface {
	GetType() int
}

NotifyPack is the interface that notify packs must implement.

type PageConfig

type PageConfig struct {
	// Name should not duplicate to another page
	Name string `json:"name"`

	// Title will show as the title of page
	Title string `json:"title"`

	// Emoji will show as icon of a page
	Emoji string `json:"emoji"`
}

PageConfig stores basic setting of a page

type Params

type Params struct {
	State   *State
	Main    *Container
	Sidebar *Container
}

type RunFunc

type RunFunc func(*Params) error

RunFunc is the type of a function handling page

type SendNotifyPackFunc

type SendNotifyPackFunc func(NotifyPack)

SendNotifyPackFunc is the function type that sends a notify pack to the GUI client.

type State

type State struct {
	// contains filtered or unexported fields
}

State is the state of a user's session.

func NewState

func NewState() *State

NewState creates a new state.

func (*State) Clone

func (s *State) Clone() *State

Clone do a swallow copy on State.

func (*State) Default

func (s *State) Default(key string, v any) any

Default sets the value of a key if the key is not set. If the key is set, it returns the value. If the key is not set, it sets the value and returns the value. The v should be a pointer. Example: ```go

type TODOList struct {
	Items []string `json:"items"`
}

todoList := state.Default("todoList", &TODOList{}).(*TODOList)

```

func (*State) Destroy

func (s *State) Destroy()

Destroy release the resource.

func (*State) GetBool

func (s *State) GetBool(key string) bool

GetBool gets the value of a key and returns it as a bool.

func (*State) GetClickID

func (s *State) GetClickID() string

GetClickID get the id of clicked button.

func (*State) GetFile

func (s *State) GetFile(key string) []byte

func (*State) GetFloat added in v0.2.1

func (s *State) GetFloat(key string) *float64

GetFloat gets the value of a key and returns it as a float64.

func (*State) GetFuncCache

func (s *State) GetFuncCache(key string) any

GetFuncCache gets the value of a key in the function cache.

func (*State) GetFuncCacheWithFuncName

func (s *State) GetFuncCacheWithFuncName(key string, funcName string) any

GetFuncCacheWithFuncName gets the value of a key in the function cache with a specific function name.

func (*State) GetInt

func (s *State) GetInt(key string) *int

GetInt gets the value of a key and returns it as an int. If the key is not set, it returns nil.

func (*State) GetObject

func (s *State) GetObject(key string, out any) error

GetObject gets the value of a key and unmarshals it to the out object.

func (*State) GetString

func (s *State) GetString(key string) *string

GetString gets the value of a key and returns it as a string.

func (*State) Set

func (s *State) Set(key string, v any)

Set sets the value of a key.

func (*State) SetClickID

func (s *State) SetClickID(id string)

SetClickID set the id of clicked button.

func (*State) SetFile

func (s *State) SetFile(key string, bs []byte)

SetFile sets the value of a key to a file.

func (*State) SetFuncCache

func (s *State) SetFuncCache(key string, value any)

SetFuncCache sets the value of a key in the function cache.

func (*State) SetFuncCacheWithFuncName

func (s *State) SetFuncCacheWithFuncName(key string, value any, funcName string)

SetFuncCacheWithFuncName sets the value of a key in the function cache with a specific function name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL