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 ¶
- func MustNotDone(*Step)
- func Show(parent *gtk.Window, title string, steps []*Step)
- type Assistant
- func (a *Assistant) AddNewStep(title, okLabel string) *Step
- func (a *Assistant) AddStep(step *Step)
- func (a *Assistant) Busy()
- func (a *Assistant) CanBack() bool
- func (a *Assistant) CancelButton() *gtk.Button
- func (a *Assistant) CancellableBusy(parent context.Context) context.Context
- func (a *Assistant) Continue()
- func (a *Assistant) GoBack()
- func (a *Assistant) NextStep()
- func (a *Assistant) OKButton() *gtk.Button
- func (a *Assistant) SetIndex(ix int)
- func (a *Assistant) SetStep(step *Step)
- type Step
- type StepData
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.
Types ¶
type Assistant ¶
type Assistant struct { *gtk.Window // contains filtered or unexported fields }
Assistant is a widget that behaves similarly to gtk.Assistant.
func (*Assistant) AddNewStep ¶
AddNewStep creates a new step to be added into the assistant. The new step is returned.
func (*Assistant) Busy ¶
func (a *Assistant) Busy()
Busy marks the dialog as busy and shows the spinner.
func (*Assistant) CancelButton ¶
func (a *Assistant) CancelButton() *gtk.Button
CancelButton returns the Cancel button.
func (*Assistant) CancellableBusy ¶
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.
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 ¶
BuildSteps builds multiple steps using the given data.
func (*Step) 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 ¶
StepData is the primitive data of a step that is used to build steps.
func NewStepData ¶
NewStepData builds a new step data.