Files
gio-patched/widget/material/doc.go
T
Elias Naur 060cff257f material: make theme constructors stand-alone functions
The multitude of widget methods on Theme is unnecessary coupling in that all
possible widgets either have to be included in package material, or be
different than 3rd party widgets:

	var th *Theme

	// Core widget, calling a method on Theme.
	th.Button(...).Layout(...)

	// 3rd party widget, calling a function taking a Theme.
	datepicker.New(th, ...).Layout(...)

Another reason for the Theme methods was to enable a poor man's
theme replacement, so that you could use the same code for
compatible themes. For example,

	mat.Button(...).Layout(...)

would not need to change if the type of mat changed, as long as
the new type had a compatible method Button.

However, that point misses the fact that the mat variable had to
be declared somewhere, naming the theme package:

	var mat *material.Theme (or, say, *cocoa.Theme)

A better and complete way to replace a theme is to use import renaming.
For example, to replace the material theme with a hypothetical Windows
theme, replace

	import theme "gioui.org/widget/material"

with

	import theme "github.com/somebody/windows

This change moves all Theme widget methods to be standalone functions,
and renames the widget style types accordingly.

For example, instead of the method

	func (t *Theme) Button(...) Button

there is now a function

	func Button(t *Theme, ...) ButtonStyle

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-03 13:03:04 +02:00

60 lines
2.0 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
// Package material implements the Material design.
//
// To maximize reusability and visual flexibility, user interface controls are
// split into two parts: the stateful widget and the stateless drawing of it.
//
// For example, widget.Button encapsulates the state and event
// handling of all buttons, while the Theme can draw a single Button
// in various styles.
//
// This snippet defines a button that prints a message when clicked:
//
// var gtx *layout.Context
// button := new(widget.Button)
//
// for button.Clicked(gtx) {
// fmt.Println("Clicked!")
// }
//
// Use a Theme to draw the button:
//
// theme := material.NewTheme(...)
//
// material.Button(theme, "Click me!").Layout(gtx, button)
//
// Customization
//
// Quite often, a program needs to customize the theme-provided defaults. Several
// options are available, depending on the nature of the change.
//
// Mandatory parameters: Some parameters are not part of the widget state but
// have no obvious default. In the program above, the button text is a
// parameter to the Theme.Button method.
//
// Theme-global parameters: For changing the look of all widgets drawn with a
// particular theme, adjust the `Theme` fields:
//
// theme.Color.Primary = color.RGBA{...}
//
// Widget-local parameters: For changing the look of a particular widget,
// adjust the widget specific theme object:
//
// btn := material.Button(theme, "Click me!")
// btn.Font.Style = text.Italic
// btn.Layout(gtx)
//
// Widget variants: A widget can have several distinct representations even
// though the underlying state is the same. A widget.Button can be drawn as a
// round icon button:
//
// icon := material.NewIcon(...)
//
// material.IconButton(theme, icon).Layout(gtx, button)
//
// Specialized widgets: Theme both define a generic Label method
// that takes a text size, and specialized methods for standard text
// sizes such as Theme.H1 and Theme.Body2.
package material