~eliasnaur/gio-example

847dd7c06b6c4dfbc3413688d61072ad6d81ccb0 — Chris Waldon 5 months ago fa09300
bidi: add example with bidi text

This commit adds a simple example with an editor pre-populated with
bidirectional text. It serves as an example both of how to load extra
fonts, font fallback, and how bidirectional text functions in the
editor.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 76 insertions(+), 0 deletions(-)

A bidi/bidi.go
A bidi/bidi.go => bidi/bidi.go +76 -0
@@ 0,0 1,76 @@
// SPDX-License-Identifier: Unlicense OR MIT

package main

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

import (
	_ "embed"
	"fmt"
	"log"
	"os"

	"gioui.org/app"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/text"
	"gioui.org/widget"
	"gioui.org/widget/material"

	nsareg "eliasnaur.com/font/noto/sans/arabic/regular"
	"eliasnaur.com/font/roboto/robotoregular"
	"gioui.org/font/opentype"
)

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 {
	arabicFace, _ := opentype.Parse(nsareg.TTF)
	englishFace, _ := opentype.Parse(robotoregular.TTF)
	collection := []text.FontFace{}
	collection = append(collection, text.FontFace{Font: text.Font{Typeface: "Latin"}, Face: englishFace})
	collection = append(collection, text.FontFace{Font: text.Font{Typeface: "Arabic"}, Face: arabicFace})
	th := material.NewTheme(collection)
	var ed widget.Editor
	txt := "Hello أهلا my good friend صديقي الجيد bidirectional text نص ثنائي الاتجاه."
	ed.SetText(txt)
	ed.Focus()
	ed.Alignment = text.Middle
	var ops op.Ops
	for {
		e := <-w.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			layout.Flex{Axis: layout.Vertical}.Layout(gtx,
				layout.Rigid(func(gtx C) D {
					med := material.Editor(th, &ed, "")
					med.TextSize = material.H3(th, "").TextSize
					return med.Layout(gtx)
				}),
				layout.Rigid(func(gtx C) D {
					start, end := ed.Selection()
					return material.Body1(th, fmt.Sprintf("Selection start %d, end %d", start, end)).Layout(gtx)
				}),
			)
			e.Frame(gtx.Ops)
		}
	}
}