/
body.go
104 lines (93 loc) · 2.72 KB
/
body.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2023, 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 (
"cogentcore.org/core/colors"
"cogentcore.org/core/errors"
"cogentcore.org/core/styles"
)
// Body holds the primary content of a Scene
type Body struct { //core:no-new
Frame
// title of the Body, also used for window title where relevant
Title string `set:"-"`
}
// NewBody creates a new Body that will serve as the content of a Scene
// (e.g., a Window, Dialog, etc). Body forms the central region
// of a Scene, and has OverflowAuto scrollbars by default.
// It will create its own parent Scene at this point, and has wrapper
// functions to transparently manage everything that the Scene
// typically manages during configuration, so you can usually avoid
// having to access the Scene directly. If a name is given and
// the name of [TheApp] is unset, it sets it to the given name.
func NewBody(name ...string) *Body {
bd := &Body{}
nm := "body"
if len(name) > 0 {
nm = name[0]
if TheApp.Name() == "" {
TheApp.SetName(nm)
}
}
if AppearanceSettings.Zoom == 0 {
// we load the settings in NewBody so that people can
// add their own settings to AllSettings first
errors.Log(LoadAllSettings())
}
bd.InitName(bd, nm)
bd.Title = nm
bd.Scene = NewBodyScene(bd)
return bd
}
func (bd *Body) OnInit() {
bd.Frame.OnInit()
bd.SetStyles()
}
func (bd *Body) SetStyles() {
bd.Style(func(s *styles.Style) {
s.Overflow.Set(styles.OverflowAuto)
s.Direction = styles.Column
s.Grow.Set(1, 1)
})
}
// SetTitle sets the title in the Body, Scene, and Stage, RenderWin, and title widget.
// This is the one place to change the title for everything.
func (bd *Body) SetTitle(title string) *Body {
bd.Nm = title
bd.Title = title
bd.Scene.Nm = title
if bd.Scene.Stage != nil {
bd.Scene.Stage.Title = title
win := bd.Scene.RenderWin()
if win != nil {
win.SetName(title)
win.SetTitle(title)
}
}
if lb, ok := bd.ChildByName("title", 0).(*Label); ok {
lb.SetText(title)
}
return bd
}
// AddTitle adds a Label with given title, and sets the Title text
// which will be used by the Scene etc.
func (bd *Body) AddTitle(title string) *Body {
bd.SetTitle(title)
NewLabel(bd, "title").SetText(title).SetType(LabelHeadlineSmall)
return bd
}
// AddText adds the given supporting text Label, typically added
// after a title.
func (bd *Body) AddText(text string) *Body {
NewLabel(bd, "text").SetText(text).
SetType(LabelBodyMedium).Style(func(s *styles.Style) {
s.Color = colors.C(colors.Scheme.OnSurfaceVariant)
})
return bd
}
// SetData sets the Body's [Scene.Data].
func (bd *Body) SetData(data any) *Body {
bd.Scene.SetData(data)
return bd
}