adaptive

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2021 License: MPL-2.0 Imports: 11 Imported by: 5

README

adaptive

Adaptive GTK4 mobile components for gotk4 to replace libadwaita.

Why?

libadwaita currently enforces the Adwaita theme on all applications that use it. This gives the user no choice on any of its themes and customizations.

This library doesn't enforce any themes. It provides only the widget components.

License

This package is licensed under the Mozilla Public License v2.0.

Documentation

Overview

Package adaptive provides adaptive GTK4 widget components. It's mainly for use in applications that aim to support both mobile and desktop viewports. It is an alternative to libadwaita.

Index

Examples

Constants

View Source
const FoldRevealButtonIcon = "open-menu-symbolic"

FoldRevealButtonIcon is the default icon name for a fold reveal button.

Variables

This section is empty.

Functions

func Init

func Init()

Init initializes package adaptive. The caller should call this on application activation.

func TransformInitials

func TransformInitials(in string) string

TransformInitials transforms the given input string into initials.

Types

type Avatar

type Avatar struct {
	*Bin
	Image *gtk.Image
	Label *gtk.Label // for initials
	// contains filtered or unexported fields
}

Avatar wraps around an Image and makes it appear round.

Example
testapp.Run("avatar", func(app *gtk.Application) {
	adaptive.Init()

	sizes := []int{16, 24, 32, 48, 56, 64}

	main := gtk.NewBox(gtk.OrientationVertical, 8)
	main.SetMarginStart(8)
	main.SetMarginEnd(8)
	main.SetMarginTop(8)
	main.SetMarginBottom(8)

	avatarFns := []func(*adaptive.Avatar){
		func(a *adaptive.Avatar) { a.SetInitials("Ferris Argyle") },
		func(a *adaptive.Avatar) { a.SetFromPixbuf(testdata.MustAvatarPixbuf()) },
	}

	for _, avatarFn := range avatarFns {
		box := gtk.NewBox(gtk.OrientationHorizontal, 8)
		box.SetHExpand(true)
		box.SetVExpand(true)
		box.SetHAlign(gtk.AlignCenter)
		box.SetVAlign(gtk.AlignCenter)

		for _, size := range sizes {
			avy := adaptive.NewAvatar(size)
			avatarFn(avy)
			box.Append(avy)
		}

		main.Append(box)
	}

	w := testapp.NewWindow(app, "Avatars", -1, -1)
	w.SetChild(main)
	w.Show()
})
Output:

func NewAvatar

func NewAvatar(size int) *Avatar

NewAvatar creates a new round image. If radius is 0, then it will be half the dimensions. If the radius is less than 0, then nothing is rounded.

func (*Avatar) Initials

func (i *Avatar) Initials() string

Initials returns the full initials string.

func (*Avatar) SetAttributes

func (a *Avatar) SetAttributes(attrs *pango.AttrList)

SetAttributes sets the initial label's Pango attributes.

func (*Avatar) SetFromFile

func (a *Avatar) SetFromFile(file string)

SetFromFile sets the avatar from the given filename.

func (*Avatar) SetFromPaintable

func (a *Avatar) SetFromPaintable(p gdk.Paintabler)

SetFromPaintable sets the avatar from the given paintable.

func (*Avatar) SetFromPixbuf

func (a *Avatar) SetFromPixbuf(p *gdkpixbuf.Pixbuf)

SetFromPixbuf sets the avatar from the given pixbuf.

func (*Avatar) SetInitials

func (a *Avatar) SetInitials(initials string)

SetInitials sets the string to be displayed as initials.

func (*Avatar) SetInitialsTransformer

func (a *Avatar) SetInitialsTransformer(initialsFn func(string) string)

SetInitialsTransformer sets the initials transformer function for the Avatar. The function will be called to get the initials from the set string.

func (*Avatar) SetSizeRequest

func (a *Avatar) SetSizeRequest(size int)

SetSizeRequest sets the avatar size.

func (*Avatar) SizeRequest

func (a *Avatar) SizeRequest() int

SizeRequest gets the avatar's size request.

type Bin

type Bin struct {
	*gtk.Box
	// contains filtered or unexported fields
}

Bin is a widget that holds a single widget.

func NewBin

func NewBin() *Bin

NewBin creates a new bin.

func (*Bin) Child

func (b *Bin) Child() gtk.Widgetter

Child returns the Bin's child.

func (*Bin) IsChild

func (b *Bin) IsChild(child gtk.Widgetter) bool

IsChild returns true if the given child is the bin's child.

func (*Bin) SetChild

func (b *Bin) SetChild(child gtk.Widgetter)

SetChild sets the child in the bin. If child is nil, then the box is cleared.

type ErrorLabel

type ErrorLabel struct {
	*gtk.Box

	Short      *gtk.ToggleButton
	ShortLabel *gtk.Label
	ShortIcon  *gtk.Image

	Reveal *gtk.Revealer
	Full   *gtk.Label

	RevealedIcon  string
	CollapsedIcon string
}

ErrorLabel is a label that displays the short form of an error but allows the user to get the full error from the UI directly.

Example
testapp.Run("error-label", func(app *gtk.Application) {
	adaptive.Init()

	err := errors.New("failed to open hello.txt: filesystem error: missing hard drive")

	status := adaptive.NewErrorLabel(err)
	status.SetMarginTop(8)
	status.SetMarginBottom(8)
	status.SetMarginStart(8)
	status.SetMarginEnd(8)

	w := testapp.NewWindow(app, "Error", 150, 250)
	w.SetChild(status)
	w.Show()
})
Output:

func NewErrorLabel

func NewErrorLabel(err error) *ErrorLabel

NewErrorLabel creates a new error label from the given error. If err is nil, then the function panics.

func NewErrorLabelFull

func NewErrorLabelFull(short, full string) *ErrorLabel

NewErrorLabelFull creates a new error label from two strings.

type Fold

type Fold struct {
	gtk.Widgetter
	// contains filtered or unexported fields
}

Fold is a component that acts similar to libadwaita's AdwFlap.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/diamondburned/adaptive"
	"github.com/diamondburned/adaptive/internal/testapp"
	"github.com/diamondburned/gotk4/pkg/gtk/v4"
)

func main() {
	testapp.Run("fold", func(app *gtk.Application) {
		adaptive.Init()

		stack := newStack()
		stack.SetHExpand(true)

		stackside := gtk.NewStackSidebar()
		stackside.SetStack(stack)

		fold := adaptive.NewFold(gtk.PosLeft)
		fold.SetSideChild(stackside)
		fold.SetChild(stack)

		foldButton := adaptive.NewFoldRevealButton()
		foldButton.ConnectFold(fold)

		h := gtk.NewHeaderBar()
		h.PackStart(foldButton)

		w := testapp.NewWindow(app, "Example Sidebar", 450, 300)
		w.SetChild(fold)
		w.SetTitlebar(h)
		w.Show()
	})
}

func newStack() *gtk.Stack {
	stack := gtk.NewStack()
	stack.SetTransitionType(gtk.StackTransitionTypeSlideUpDown)

	for i := 0; i < 5; i++ {
		istr := strconv.Itoa(i)
		content := gtk.NewLabel(fmt.Sprintf("You're in stack number %s.", istr))
		stack.AddTitled(content, "stack-"+istr, "Stack "+istr)
	}

	return stack
}
Output:

func NewFold

func NewFold(position gtk.PositionType) *Fold

NewFold creates a new sidebar.

func (*Fold) FoldThreshold

func (f *Fold) FoldThreshold() int

FoldThreshold returns the fold width.

func (*Fold) FoldWidth

func (f *Fold) FoldWidth() int

FoldWidth returns the width of the sidebar. It is calculated from the fold threshold.

func (*Fold) NotifyFolded

func (f *Fold) NotifyFolded(fn func(folded bool))

NotifyFolded subscribes fn to be called if the sidebar is folded or unfolded.

func (*Fold) NotifyRevealed

func (f *Fold) NotifyRevealed(fn func(revealed bool))

NotifyRevealed subscribes fn to be called if the sidebar is revealed or not.

func (*Fold) QueueResize

func (f *Fold) QueueResize()

QueueResize should be called when Fold's parent widths are changed.

func (*Fold) SetChild

func (f *Fold) SetChild(child gtk.Widgetter)

SetChild sets the sidebar's main content.

func (*Fold) SetFoldThreshold

func (f *Fold) SetFoldThreshold(threshold int)

SetFoldThreshold sets the width threshold that the sidebar will determine whether or not to fold.

func (*Fold) SetFoldWidth

func (f *Fold) SetFoldWidth(width int)

SetFoldWidth sets the width of the sidebar. The width must be lower than the fold threshold.

func (*Fold) SetFolded

func (f *Fold) SetFolded(folded bool)

SetFolded sets whether or not the sidebar is folded.

func (*Fold) SetRevealSide

func (f *Fold) SetRevealSide(reveal bool)

SetRevealSide sets whether or not the sidebar is revealed. It does not change if the sidebar isn't currently folded.

func (*Fold) SetSideChild

func (f *Fold) SetSideChild(child gtk.Widgetter)

SetSideChild sets the sidebar's side content.

func (*Fold) SideIsRevealed

func (f *Fold) SideIsRevealed() bool

SideIsRevealed returns true if the sidebar is revealed. If the sidebar is not folded, then true is returned regardless of what's given into SetRevealSide.

type FoldRevealButton

type FoldRevealButton struct {
	*gtk.Revealer
	Button *gtk.ToggleButton
}

FoldRevealButton is a button that toggles whether or not the fold's sidebar should be revealed.

func NewFoldRevealButton

func NewFoldRevealButton() *FoldRevealButton

NewFoldRevealButton creates a new fold reveal button. The button is hidden by default until a sidebar is connected to it.

func (*FoldRevealButton) ConnectFold

func (b *FoldRevealButton) ConnectFold(fold *Fold)

ConnectFold connects the current sidebar reveal button to the given sidebar.

func (*FoldRevealButton) SetIconName

func (b *FoldRevealButton) SetIconName(icon string)

SetIconName sets the reveal button's icon name.

type LoadablePage

type LoadablePage struct {
	*gtk.Stack
	Spinner   *gtk.Spinner
	ErrorPage *StatusPage
	// contains filtered or unexported fields
}

LoadablePage wraps a child that can be loading at times and error out.

Example
const margin = 8

testapp.Run("loadable-page", func(app *gtk.Application) {
	adaptive.Init()

	errorCheck := gtk.NewCheckButtonWithLabel("Erroneous")
	errorCheck.SetHExpand(true)

	loadButton := gtk.NewButtonWithLabel("Load")
	loadButton.SetHExpand(true)

	child := gtk.NewBox(gtk.OrientationVertical, margin)
	child.SetMarginTop(margin)
	child.SetMarginBottom(margin)
	child.SetMarginStart(margin)
	child.SetMarginEnd(margin)
	child.Append(errorCheck)
	child.Append(loadButton)

	main := adaptive.NewLoadablePage()
	main.SetChild(child)

	loadButton.ConnectClicked(func() {
		erroneous := errorCheck.Active()
		main.SetLoading()

		glib.TimeoutSecondsAdd(5, func() {
			if erroneous {
				main.SetError(errors.New("failed to load busy box: checkmark was active"))
			} else {
				main.SetChild(child)
			}
		})
	})

	w := testapp.NewWindow(app, "Loadable", 250, -1)
	w.SetChild(main)
	w.Show()
})
Output:

func NewLoadablePage

func NewLoadablePage() *LoadablePage

NewLoadablePage creates a new LoadablePage widget.

func (*LoadablePage) SetChild

func (p *LoadablePage) SetChild(child gtk.Widgetter)

SetChild sets the main child of the busy box.

func (*LoadablePage) SetError

func (p *LoadablePage) SetError(err error)

SetError shows an error in the busy box.

func (*LoadablePage) SetLoading

func (p *LoadablePage) SetLoading()

SetLoading shows a loading animation in the busy box.

type StatusPage

type StatusPage struct {
	*gtk.Grid
	Icon  *gtk.Image
	Title *gtk.Label
}

StatusPage is a widget component that contains an icon, a title and a description, which are all optional.

Example
testapp.Run("status-page", func(app *gtk.Application) {
	adaptive.Init()

	status := adaptive.NewStatusPage()
	status.SetIconName("computer-fail-symbolic")
	status.SetTitle("Uh oh!")
	status.SetDescriptionText("An oopsie-whoopsie has occured. Please throw your computer out the window.")

	w := testapp.NewWindow(app, "Status Page", 350, 200)
	w.SetChild(status)
	w.Show()
})
Output:

func NewStatusPage

func NewStatusPage() *StatusPage

NewStatusPage creates a new empty status page. All its widgets are properly initialized, but they're not added into the box until set.

func (*StatusPage) SetDescription

func (p *StatusPage) SetDescription(desc gtk.Widgetter)

SetDescription ensures the description is in the page and sets its content.

func (*StatusPage) SetDescriptionText

func (p *StatusPage) SetDescriptionText(desc string)

SetDescriptionText calls SetDescription with a new description label. The label is justified to the middle and has a 50 characters wide width cap.

func (*StatusPage) SetIconName

func (p *StatusPage) SetIconName(icon string)

SetIconName ensures the icon is in the page and sets its icon name.

func (*StatusPage) SetTitle

func (p *StatusPage) SetTitle(title string)

SetTitle ensures the title is in the page and sets its content.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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