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
19 changes: 19 additions & 0 deletions content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"cogentcore.org/core/htmlcore"
"cogentcore.org/core/math32"
"cogentcore.org/core/styles"
"cogentcore.org/core/styles/abilities"
"cogentcore.org/core/styles/units"
"cogentcore.org/core/system"
"cogentcore.org/core/text/csl"
Expand Down Expand Up @@ -186,6 +187,24 @@ func (ct *Content) Init() {
}
return false
}
ct.Context.AddWidgetHandler(func(w core.Widget) {
switch x := w.(type) {
case *core.Text:
x.Styler(func(s *styles.Style) {
s.Max.X.Ch(120)
})
case *core.Image:
x.Styler(func(s *styles.Style) {
s.SetAbilities(true, abilities.Clickable, abilities.DoubleClickable)
s.Overflow.Set(styles.OverflowAuto)
})
x.OnDoubleClick(func(e events.Event) {
d := core.NewBody("Image")
core.NewImage(d).SetImage(x.Image)
d.RunWindowDialog(x)
})
}
})

ct.Maker(func(p *tree.Plan) {
if ct.currentPage == nil {
Expand Down
21 changes: 21 additions & 0 deletions htmlcore/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ type Context struct {
// will not be generated.
AttributeHandlers map[string]func(ctx *Context, w io.Writer, node ast.Node, entering bool, tag, value string) bool

// WidgetHandlers is a list of handler functions for each Widget,
// called in sequential order for each widget that is created,
// after it has been configured by the existing handlers etc.
// This allows for additional styling to be applied based on the
// type of widget, for example.
WidgetHandlers []func(w core.Widget)

// firstRow indicates the start of a table, where number of columns is counted.
firstRow bool
}
Expand Down Expand Up @@ -168,6 +175,7 @@ func (c *Context) config(w core.Widget) {
}
}
})
c.handleWidget(w)
}

// InlineParent returns the current parent widget that inline
Expand Down Expand Up @@ -244,3 +252,16 @@ func (c *Context) LinkButtonUpdating(bt *core.Button, url func() string) *core.B
})
return bt
}

// AddWidgetHandler adds given widget handler function
func (c *Context) AddWidgetHandler(f func(w core.Widget)) {
c.WidgetHandlers = append(c.WidgetHandlers, f)
}

// handleWidget calls WidgetHandlers functions on given widget,
// in order added so last one has override priority.
func (c *Context) handleWidget(w core.Widget) {
for _, f := range c.WidgetHandlers {
f(w)
}
}
12 changes: 11 additions & 1 deletion htmlcore/examples/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cogentcore.org/core/base/errors"
"cogentcore.org/core/core"
"cogentcore.org/core/htmlcore"
"cogentcore.org/core/styles"
_ "cogentcore.org/core/text/tex" // include this to get math
)

Expand All @@ -18,6 +19,15 @@ var content string

func main() {
b := core.NewBody("MD Example")
errors.Log(htmlcore.ReadMDString(htmlcore.NewContext(), b, content))
ctx := htmlcore.NewContext()
ctx.AddWidgetHandler(func(w core.Widget) {
switch x := w.(type) {
case *core.Text:
x.Styler(func(s *styles.Style) {
s.Max.X.Ch(80)
})
}
})
errors.Log(htmlcore.ReadMDString(ctx, b, content))
b.RunMainWindow()
}
Loading