~eliasnaur/gio-example

ref: c261571c2dde00d1fa1e2dae256cade3075f15b9 gio-example/markdown/main.go -rw-r--r-- 2.4 KiB
c261571cChris Waldon markdown: adapt to new richtext API 3 months ago
                                                                                
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
105
106
107
108
109
110
// SPDX-License-Identifier: Unlicense OR MIT

package main

// A simple Gio program. See https://gioui.org for more information.

import (
	"image"
	"image/color"
	"log"
	"os"

	"gioui.org/app"
	"gioui.org/gesture"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"gioui.org/x/component"
	"gioui.org/x/markdown"
	"gioui.org/x/richtext"

	"gioui.org/font/gofont"
	"github.com/inkeliz/giohyperlink"
)

func main() {
	go func() {
		w := app.NewWindow()
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

type (
	C = layout.Context
	D = layout.Dimensions
)

func loop(w *app.Window) error {
	fontCollection := gofont.Collection()
	shaper := text.NewCache(fontCollection)
	th := material.NewTheme(fontCollection)
	renderer := markdown.NewRenderer()
	var ops op.Ops

	var ed widget.Editor
	var rs component.Resize
	rs.Ratio = .5
	var textState richtext.InteractiveText
	var rendered []richtext.SpanStyle
	inset := layout.UniformInset(unit.Dp(4))
	for {
		e := <-w.Events()
		giohyperlink.ListenEvents(e)
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			if to := textState.LongPressed(); to != nil {
				w.Option(app.Title(to.Get(markdown.MetadataURL)))
			}
			for o, events := textState.Events(gtx); o != nil; o, events = textState.Events(gtx) {
				for _, e := range events {
					switch e.Type {
					case gesture.TypeClick:
						if url := o.Get(markdown.MetadataURL); url != "" {
							giohyperlink.Open(url)
						}
					}
				}
			}

			for _, edEvent := range ed.Events() {
				if _, ok := edEvent.(widget.ChangeEvent); ok {
					rendered, _ = renderer.Render(th, []byte(ed.Text()))
				}
			}

			rs.Layout(gtx,
				func(gtx C) D { return inset.Layout(gtx, material.Editor(th, &ed, "markdown").Layout) },
				func(gtx C) D {
					return inset.Layout(gtx, func(gtx C) D {
						return richtext.Text(&textState, rendered...).Layout(gtx, shaper)
					})
				},
				func(gtx C) D {
					rect := image.Rectangle{
						Max: image.Point{
							X: (gtx.Px(unit.Dp(4))),
							Y: (gtx.Constraints.Max.Y),
						},
					}
					paint.FillShape(gtx.Ops, color.NRGBA{A: 200}, clip.Rect(rect).Op())
					return D{Size: rect.Max}
				},
			)
			e.Frame(gtx.Ops)
		}
	}
}