Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions core/pages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2024, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package core

import (
"fmt"

"cogentcore.org/core/styles"
)

// Pages is a frame that can easily swap its content between that of
// different possible pages.
type Pages struct {
Frame

// Page is the currently open page.
Page string

// Pages is a map of page names to functions that configure a page.
Pages map[string]func(pg *Pages) `set:"-"`

// page is the currently rendered page.
page string
}

func (pg *Pages) Init() {
pg.Frame.Init()
pg.Pages = map[string]func(pg *Pages){}
pg.Styler(func(s *styles.Style) {
s.Direction = styles.Column
s.Grow.Set(1, 1)
})
pg.Updater(func() {
if len(pg.Pages) == 0 {
return
}
if pg.page == pg.Page {
return
}
pg.DeleteChildren()
fun, ok := pg.Pages[pg.Page]
if !ok {
ErrorSnackbar(pg, fmt.Errorf("page %q not found", pg.Page))
return
}
pg.page = pg.Page
fun(pg)
})
}

// AddPage adds a page with the given name and configuration function.
// If [Pages.Page] is currently unset, it will be set to the given name.
func (pg *Pages) AddPage(name string, f func(pg *Pages)) {
pg.Pages[name] = f
if pg.Page == "" {
pg.Page = name
}
}

// Open sets the current page to the given name and updates the display.
func (pg *Pages) Open(name string) {
pg.SetPage(name)
pg.Update()
}
11 changes: 11 additions & 0 deletions core/typegen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions docs/content/1-tutorials/2-sign-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```Go
type user struct {
Username string
Password string
}
u := &user{}
pg := core.NewPages(b)
pg.AddPage("sign-in", func(pg *core.Pages) {
core.NewForm(pg).SetStruct(u)
core.NewButton(pg).SetText("Sign in").OnClick(func(e events.Event) {
pg.Open("home")
})
})
pg.AddPage("home", func(pg *core.Pages) {
core.NewText(pg).SetText("Welcome, "+u.Username+"!").SetType(core.TextHeadlineSmall)
core.NewButton(pg).SetText("Sign out").OnClick(func(e events.Event) {
*u = user{}
pg.Open("sign-in")
})
})
```
4 changes: 4 additions & 0 deletions yaegicore/symbols/cogentcore_org-core-base-fileinfo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions yaegicore/symbols/cogentcore_org-core-base-fsx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions yaegicore/symbols/cogentcore_org-core-base-reflectx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions yaegicore/symbols/cogentcore_org-core-core.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions yaegicore/symbols/cogentcore_org-core-filetree.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.