assistant

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2022 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package assistant provides an implementation of gtk.Assistant but mobile-friendly.

Example
package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"hash/fnv"
	"log"
	"time"

	"github.com/diamondburned/gotk4/pkg/core/glib"
	"github.com/diamondburned/gotk4/pkg/gtk/v4"
	"github.com/diamondburned/gotk4/pkg/pango"
	"github.com/diamondburned/gotktrix/internal/components/assistant"
)

type Login struct {
	*gtk.Grid
	Username *gtk.Entry
	Password *gtk.Entry
}

func NewLogin() *Login {
	username := gtk.NewEntry()
	username.SetInputPurpose(gtk.InputPurposeName)

	password := gtk.NewEntry()
	password.SetInputPurpose(gtk.InputPurposePassword)

	labels := [2]*gtk.Label{
		gtk.NewLabel("Username:"),
		gtk.NewLabel("Password:"),
	}

	labels[0].SetXAlign(1)
	labels[1].SetXAlign(1)

	grid := gtk.NewGrid()
	grid.SetVAlign(gtk.AlignCenter)
	grid.SetHAlign(gtk.AlignCenter)
	grid.SetVExpand(true)
	grid.SetHExpand(true)
	grid.SetRowSpacing(5)
	grid.SetColumnSpacing(7)
	grid.Attach(labels[0], 0, 0, 1, 1)
	grid.Attach(labels[1], 0, 1, 1, 1)
	grid.Attach(username, 1, 0, 1, 1)
	grid.Attach(password, 1, 1, 1, 1)

	return &Login{grid, username, password}
}

// Passhash hashes the password entry's content using FNV 128-bit.
func (l *Login) Passhash() string {
	hash := fnv.New128a().Sum([]byte(l.Password.Text()))
	return string(base64.StdEncoding.EncodeToString(hash))
}

func main() {
	app := gtk.NewApplication("com.github.diamondburned.example-app", 0)
	app.ConnectActivate(func(app *gtk.Application) {
		login := NewLogin()

		window := gtk.NewApplicationWindow(app)
		window.SetDefaultSize(400, 300)
		window.SetTitle("Example Assistant.")
		window.SetChild(gtk.NewLabel("Example assistant."))
		window.Show()

		greetings := gtk.NewLabel("Please press Continue.")
		greetings.SetHExpand(true)

		onDone := func(step *assistant.Step) {
			msg := fmt.Sprintf(
				"Your username is %s.\nYour password hash is %s.",
				login.Username.Text(),
				login.Passhash(),
			)

			msgLabel := gtk.NewLabel(msg)
			msgLabel.SetWrap(true)
			msgLabel.SetWrapMode(pango.WrapWordChar)

			next := assistant.NewStep("Finish", "Done")
			next.ContentArea().Append(msgLabel)

			assistant := step.Assistant()
			assistant.AddStep(next)
			assistant.SetStep(next)
			assistant.Connect("close", window.Close)
		}

		steps := assistant.BuildSteps(
			assistant.NewStepData("Welcome", "Continue", greetings),
			assistant.StepData{
				Title:    "Authenticate",
				OKLabel:  "Login",
				Contents: []gtk.Widgetter{login},
				Done: func(step *assistant.Step) {
					assistant := step.Assistant()
					ctx := assistant.CancellableBusy(context.Background())

					go func() {
						select {
						case <-time.Tick(5 * time.Second):
							glib.IdleAdd(func() { onDone(step) })
						case <-ctx.Done():
							glib.IdleAdd(func() {
								assistant.Continue()
								assistant.Close()
							})
						}
					}()
				},
			},
		)

		a := assistant.New(&window.Window, steps)
		a.SetTitle("Hello")
		a.Connect("close-request", window.Close)
		a.Show()
	})

	if code := app.Run(nil); code > 0 {
		log.Panicf("exit status %d", code)
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustNotDone

func MustNotDone(*Step)

MustNotDone panics. It's useful if the user doesn't want Done to be activated whatsoever.

func Show

func Show(parent *gtk.Window, title string, steps []*Step)

Show is a helper function for showing an Assistant dialog immediately.

Types

type Assistant

type Assistant struct {
	*gtk.Window
	// contains filtered or unexported fields
}

Assistant is a widget that behaves similarly to gtk.Assistant.

func New

func New(parent *gtk.Window, steps []*Step) *Assistant

New creates a new Assistant.

func Use

func Use(window *gtk.Window, steps []*Step) *Assistant

Use uses an existing window instead of creating a dialog.

func (*Assistant) AddNewStep

func (a *Assistant) AddNewStep(title, okLabel string) *Step

AddNewStep creates a new step to be added into the assistant. The new step is returned.

func (*Assistant) AddStep

func (a *Assistant) AddStep(step *Step)

AddStep adds a step into the assistant.

func (*Assistant) Busy

func (a *Assistant) Busy()

Busy marks the dialog as busy and shows the spinner.

func (*Assistant) CanBack

func (a *Assistant) CanBack() bool

CanBack returns true if the current step can be undoed.

func (*Assistant) CancelButton

func (a *Assistant) CancelButton() *gtk.Button

CancelButton returns the Cancel button.

func (*Assistant) CancellableBusy

func (a *Assistant) CancellableBusy(parent context.Context) context.Context

CancellableBusy marks the dialog as busy but allows the Cancel button to stop the context. The caller must call Continue() to restore the dialog.

func (*Assistant) Continue

func (a *Assistant) Continue()

Continue restores the dialog and brings it out of busy mode.

func (*Assistant) GoBack

func (a *Assistant) GoBack()

GoBack scrolls the page back. It does nothing if CanBack returns false.

func (*Assistant) NextStep

func (a *Assistant) NextStep()

NextStep moves the assistant to the next step. It panics if the assistant is currently at the last step.

func (*Assistant) OKButton

func (a *Assistant) OKButton() *gtk.Button

OKButton returns the OK button.

func (*Assistant) SetIndex

func (a *Assistant) SetIndex(ix int)

SetIndex sets the assistant to the step at the given index.

func (*Assistant) SetStep

func (a *Assistant) SetStep(step *Step)

SetStep sets the assistant to show the given step. If the Assistant is currently busy, then it'll continue.

type Step

type Step struct {
	// Done is called when the OK button is clicked and there isn't a next page.
	Done func(*Step)
	// SwitchedTo is called once the step is shown.
	SwitchedTo func(*Step)
	// CanBack, if true, will allow the assistant to go from this step to the
	// last step. If this is the first step, then this does nothing.
	CanBack bool
	// Loading, if true, will be the widget shown when busy instead of the
	// default spinner.
	Loading gtk.Widgetter
	// contains filtered or unexported fields
}

Step describes each step of the assistant.

func BuildSteps

func BuildSteps(data ...StepData) []*Step

BuildSteps builds multiple steps using the given data.

func NewStep

func NewStep(title, okLabel string) *Step

NewStep creates a new assistant step.

func (*Step) Assistant

func (step *Step) Assistant() *Assistant

Assistant returns the step's parent assistant. Nil is returned if the Assistant is a zero-value.

func (*Step) ContentArea

func (step *Step) ContentArea() *gtk.Box

ContentArea returns the underlying step's content area box.

type StepData

type StepData struct {
	Title    string
	OKLabel  string
	Contents []gtk.Widgetter
	Done     func(*Step)
}

StepData is the primitive data of a step that is used to build steps.

func NewStepData

func NewStepData(title, okLabel string, contents ...gtk.Widgetter) StepData

NewStepData builds a new step data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL