### Basic Profiling Setup in Gio Event Loop (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/profiling Demonstrates how to initialize a CSV timing recorder at the start of the event loop and defer its stopping until the window is closed. It also shows how to record profile data for each frame. This method ensures frame timings are logged to a CSV file, with disk I/O handled asynchronously. ```Go func loop(w *app.Window) error { // log to a CSV file with a randomly-chosen name. The file's path will be // logged to stderr. recorder, err := NewRecorder(nil) if err != nil { // handle } defer recorder.Stop() var ops op.Ops for event := range window.Events() { switch event := event.(type) { case system.DestroyEvent: // returning will execute the deferred call to Stop(), which // flushes the CSV file. return event.Err case system.FrameEvent: gtx := layout.NewContext(&ops, event) // record the last frame's timing info and prepare the next one recorder.profile(gtx) // lay out your UI here } } } ``` -------------------------------- ### Start progress animation in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component Implements the `Start` method for the `Progress` type. This method initiates the progress animation, specifying the start time, direction, and duration. ```go func (p *Progress) Start(began time.Time, direction ProgressDirection, duration time.Duration) ``` -------------------------------- ### Animation Controller in Go (gioui.org/x/outlay) Source: https://pkg.go.dev/gioui.org/x/%40v0.9.0/outlay_tab=versions Manages animation progress and duration. Provides methods to check if an animation is active, get its progress, set duration, and start the animation. Requires a time.Time object for starting. ```go package outlay import "time" type Animation struct { Start time.Time } func (n *Animation) Animating(gtx layout.Context) bool func (n *Animation) Progress(gtx layout.Context) float32 func (n *Animation) SetDuration(d time.Duration) func (n *Animation) Start(now time.Time) ``` -------------------------------- ### Go: Animation Helper Source: https://pkg.go.dev/gioui.org/x/%40v0.8.0/outlay_tab=versions A utility for managing animations within the Gio UI framework. It provides methods to check if an animation is running, get its progress, set duration, and start the animation. Requires the 'time' package. ```Go package outlay import ( "time" "gioui.org/layout" ) type Animation struct { Start_time time.Time } func (n *Animation) Animating(gtx layout.Context) bool { // Implementation details for checking animation status return false } func (n *Animation) Progress(gtx layout.Context) float32 { // Implementation details for getting animation progress return 0.0 } func (n *Animation) SetDuration(d time.Duration) { // Implementation details for setting animation duration } func (n *Animation) Start(now time.Time) { // Implementation details for starting an animation } ``` -------------------------------- ### Start Visibility Animation to Appear (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Initiates the animation to make the component visible starting at the specified time. This is a no-operation if the component is already visible. ```go func (v *VisibilityAnimation) Appear(now time.Time) { // ... implementation details ... } ``` -------------------------------- ### Animation Helper in Go Source: https://pkg.go.dev/gioui.org/x/outlay_tab=versions Provides utilities for managing animations, including checking if an animation is active, getting its progress, setting its duration, and starting it. It relies on the time package and layout.Context. ```go package outlay import ( "time" "gioui.org/layout" ) type Animation struct { Start time.Time duration time.Duration } func (n *Animation) Animating(gtx layout.Context) bool { // Implementation details for checking animation status return false } func (n *Animation) Progress(gtx layout.Context) float32 { // Implementation details for getting animation progress return 0 } func (n *Animation) SetDuration(d time.Duration) { n.duration = d } func (n *Animation) Start(now time.Time) { n.Start = now } ``` -------------------------------- ### Animation Control in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/outlay_tab=versions This snippet demonstrates the Animation struct for managing UI animations. It provides methods to check if an animation is running, get its progress, set duration, and start the animation. ```Go package outlay import ( "time" layout "gioui.org/layout" ) type Animation struct { Start time.Time Duration time.Duration } func (n *Animation) Animating(gtx layout.Context) bool { // Implementation details... return false } func (n *Animation) Progress(gtx layout.Context) float32 { // Implementation details... return 0.0 } func (n *Animation) SetDuration(d time.Duration) { n.Duration = d } func (n *Animation) Start(now time.Time) { n.Start = now } ``` -------------------------------- ### Initialize and Use Explorer for File Operations Go Source: https://pkg.go.dev/gioui.org/x/%40v0.5.0/explorer Provides an example of creating a new Explorer instance using NewExplorer and subsequently using its methods like ChooseFile. It highlights the need to create a unique Explorer per app.Window and listen for events. ```go // NewExplorer creates a new Explorer for the given *app.Window. // It's mandatory to use Explorer.ListenEvents on the same *app.Window. ``` ```go // ChooseFile shows the file selector, allowing the user to select a single file. ``` -------------------------------- ### Animation Utilities in Gio UI Source: https://pkg.go.dev/gioui.org/x/%40v0.1.0/outlay_tab=versions Offers animation capabilities for UI elements. The `Animation` struct tracks the animation's start time and duration, allowing developers to check if an animation is currently running and retrieve its progress. It provides methods to set the duration and start the animation. ```go package layout import "time" type Animation struct { Start time.Time Duration time.Duration } func (n *Animation) Animating(gtx Context) bool func (n *Animation) Progress(gtx Context) float32 func (n *Animation) SetDuration(d time.Duration) func (n *Animation) Start(now time.Time) ``` -------------------------------- ### Progress: Start Animation (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/component Initiates the progress animation. It requires the start time, direction (Forward or Reverse), and total duration. ```go func (p *Progress) Start(began time.Time, direction ProgressDirection, duration time.Duration) ``` -------------------------------- ### Initializing and Listening for Explorer Events Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/explorer Demonstrates the creation of a new Explorer instance associated with an application window and the mandatory step of listening for events related to that window. This setup is crucial for the explorer's functionality. ```go window := app.NewWindow() explorer := explorer.NewExplorer(window) // It's mandatory to use Explorer.ListenEvents on the same *app.Window. // This is typically done within the app's event loop. go func() { for event := range window.Events() { explorer.ListenEvents(event) } }() // Now you can use explorer.ChooseFile, explorer.CreateFile, etc. ``` -------------------------------- ### Create New AppBar Instance (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/component Initializes and returns a new AppBar instance. It requires a pointer to a ModalLayer for managing overflow menus. The function facilitates the setup of the AppBar before its layout phase. ```go func NewAppBar(modal *ModalLayer) *AppBar { // Implementation to create and initialize AppBar // ... return &AppBar{} } ``` -------------------------------- ### Start Visibility Animation (Appear) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Initiates the animation to make the entity visible, starting at the provided time. If the entity is already visible, this function has no effect. ```go func (v *VisibilityAnimation) Appear(now time.Time) { // Implementation details for starting the appear animation } ``` -------------------------------- ### Match User Language Preference for Multi-language Apps in Gio Source: https://pkg.go.dev/gioui.org/x/%40v0.9.0/pref Illustrates how to get the user's preferred language using `locale.Language()`, create a language catalog with translations, and match the best available language. It then shows how to display localized text using `message.NewPrinter` and `widget.Label`. ```go import ( "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" "golang.org/x/text/catalog" "golang.org/x/text/language" "golang.org/x/text/message" "pkg.go.dev/example/locale" ) // Your dictionary (in that case using x/text/catalog): cat := catalog.NewBuilder() cat.SetString(language.English, "Hello World", "Hello World") cat.SetString(language.Portuguese, "Hello World", "Olá Mundo") cat.SetString(language.Spanish, "Hello World", "Hola Mundo") cat.SetString(language.Czech, "Hello World", "Ahoj světe") cat.SetString(language.French, "Hello World", "Bonjour le monde") // Get the user preferences: userLanguage, _ := locale.Language() // Get the best match based on the preferred language: userLanguage, _, confidence := cat.Matcher().Match(userLanguage) if confidence <= language.Low { // Switch to the default language, due to low confidence. userLanguage = language.English } // Creates the printer with the user language: printer := message.NewPrinter(userLanguage, message.Catalog(cat)) // Display the text based on the language: // widget.Label{}.Layout(gtx, // yourTheme.Shaper, // text.Font{}, // unit.Dp(12), // printer.Sprintf("Hello World"), // ) ``` -------------------------------- ### Start Visibility Animation to Disappear (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Initiates the animation to make the component invisible starting at the specified time. This is a no-operation if the component is already invisible. ```go func (v *VisibilityAnimation) Disappear(now time.Time) { // ... implementation details ... } ``` -------------------------------- ### File Explorer Initialization and Event Handling (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.7.1/explorer Initializes a new Explorer instance for a given Gio application window and sets up event listening. This is the recommended way to use the explorer functionality. It's crucial to call ListenEvents on the same window. ```go package main import ( "gioui.org/app" "gioui.org/io/event" "your_module/exp/explorer" ) func main() { var w *app.Window // ... setup window ... e := explorer.NewExplorer(w) for evt := range w.Events() { switch e := evt.(type) { case app.DestroyEvent: return case app.FrameEvent: // ... drawing logic ... w.Invalidate() } e.ListenEvents(evt) } } ``` -------------------------------- ### Go NewModalLayer Initialization Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Creates and initializes a new instance of a ModalLayer. This is the starting point for using the modal layer functionality in your application. ```go func NewModal() *ModalLayer { // ... implementation details ... } ``` -------------------------------- ### Create a New GioUI Explorer Instance Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/explorer Illustrates the creation of a new `Explorer` instance for a given `app.Window`. It's crucial to call this once per unique window and to use `Explorer.ListenEvents` with the same window. ```go newExplorer := explorer.NewExplorer(window) ``` -------------------------------- ### Start Animation in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.8.0/outlay Initiates or restarts the animation, recording the current time as the start time. It takes the current time as input. ```go func (n *Animation) Start(now time.Time) ``` -------------------------------- ### Create a New Explorer Instance Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/explorer Initializes a new Explorer instance for a given app.Window. It's recommended to create one Explorer per window and ensure Explorer.ListenEvents is called on the same window. ```go func NewExplorer(w *app.Window) (e *Explorer) { // ... implementation ... } ``` -------------------------------- ### Animation Control with Animation in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.8.1/outlay_tab=versions The Animation struct manages the progress and duration of animations. It provides methods to check if an animation is currently running, get its progress (0.0 to 1.0), set the duration, and start the animation. ```go package main import ( "time" "gioui.org/layout" ) type Animation struct { StartT time.Time Duration time.Duration } func (n *Animation) Animating(gtx layout.Context) bool { // Implementation details for checking animation status go here return false } func (n *Animation) Progress(gtx layout.Context) float32 { // Implementation details for getting animation progress go here return 0.0 } func (n *Animation) SetDuration(d time.Duration) { n.Duration = d } func (n *Animation) Start(now time.Time) { n.StartT = now } ``` -------------------------------- ### Visibility Animation Logic (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component_tab=versions Implements visibility animations for UI elements. It manages the animation state, tracks start times, and provides methods to control appearing, disappearing, and toggling visibility. The `Revealed` method returns the current visibility progress, useful for transitions. ```go type VisibilityAnimation struct { Started time.Time State VisibilityAnimationState } func (v *VisibilityAnimation) Appear(now time.Time) func (v *VisibilityAnimation) Disappear(now time.Time) func (v *VisibilityAnimation) Revealed(gtx layout.Context) float32 func (v *VisibilityAnimation) String(gtx layout.Context) string func (v *VisibilityAnimation) ToggleVisibility(now time.Time) func (v VisibilityAnimation) Animating() bool func (v VisibilityAnimation) Visible() bool ``` -------------------------------- ### GIO UI: Create and Configure an AppBar Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/component_tab=versions Demonstrates the creation and configuration of an AppBar component in GIO UI. This includes setting actions, contextual actions, and managing its visibility. ```go func NewAppBar(modal *ModalLayer) *AppBar func (a *AppBar) CloseOverflowMenu(when time.Time) func (a *AppBar) Events(gtx layout.Context) []AppBarEvent func (a *AppBar) Layout(gtx layout.Context, theme *material.Theme, navDesc, overflowDesc string) layout.Dimensions func (a *AppBar) SetActions(actions []AppBarAction, overflows []OverflowAction) func (a *AppBar) SetContextualActions(actions []AppBarAction, overflows []OverflowAction) func (a *AppBar) StartContextual(when time.Time, title string) func (a *AppBar) StopContextual(when time.Time) func (a *AppBar) ToggleContextual(when time.Time, title string) ``` -------------------------------- ### Get Battery Level Percentage in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.5.0/pref/battery This function retrieves the current battery level as a percentage, ranging from 0 to 100. It returns the level as a uint8 and an error if the battery level cannot be determined. ```go func Level() (uint8, error) ``` ``` -------------------------------- ### Get Battery Charging Status in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.5.0/pref/battery This function checks if the device is currently charging. It returns a boolean indicating the charging status and an error if the operation fails. Note that on devices not relying on batteries, this will always return true. ```go func IsCharging() (bool, error) ``` ``` -------------------------------- ### Go: Create a New Explorer Instance Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/explorer Initializes a new Explorer instance for a given app.Window. It's crucial to create only one Explorer per app.Window and to subsequently call Explorer.ListenEvents on the same window. ```go func NewExplorer(w *app.Window) (e *Explorer) { // ... implementation details ... } ``` -------------------------------- ### Animation Widget for Gio UI Source: https://pkg.go.dev/gioui.org/x/%40v0.1.0/outlay Defines an Animation struct for managing animations between two states. Includes methods to check if animating, get progress, set duration, and start the animation. This is useful for UI transitions and dynamic elements. ```Go type Animation struct { time.Duration StartTime time.Time } func (n *Animation) Animating(gtx layout.Context) bool func (n *Animation) Progress(gtx layout.Context) float32 func (n *Animation) SetDuration(d time.Duration) func (n *Animation) Start(now time.Time) ``` -------------------------------- ### Gio UI Tooltip Creation Functions (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/component_tab=versions Provides functions to create Tooltip instances tailored for desktop and mobile platforms, utilizing a theme and text content. The PlatformTooltip function acts as a unified entry point. ```Go func DesktopTooltip(th *material.Theme, text string) Tooltip func MobileTooltip(th *material.Theme, text string) Tooltip func PlatformTooltip(th *material.Theme, text string) Tooltip ``` -------------------------------- ### Create a ContextArea Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/component Initializes a ContextArea, which is a widget that can be activated or dismissed. It manages its own state for activation and dismissal, providing methods to check these states and to trigger dismissal. ```go func (r *ContextArea) Activated() bool ``` ```go func (r ContextArea) Active() bool ``` ```go func (r *ContextArea) Dismiss() ``` ```go func (r *ContextArea) Dismissed() bool ``` ```go func (r *ContextArea) Layout(gtx C, w layout.Widget) D ``` ```go func (r *ContextArea) Update(gtx C) ``` -------------------------------- ### Create Desktop Tooltip in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.8.1/component Constructs a Tooltip suitable for desktop devices, taking a theme and text content as input. ```go func DesktopTooltip(th *material.Theme, text string) Tooltip ``` -------------------------------- ### Toggle Visibility Animation (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Switches the visibility state: if invisible, starts appearing; if visible, starts disappearing. Uses the provided time to set the animation start time. ```go func (v *VisibilityAnimation) ToggleVisibility(now time.Time) { // ... implementation details ... } ``` -------------------------------- ### Toggle Visibility Animation State (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component Switches the animation state: if invisible, starts appearing; if visible, starts disappearing. Uses the provided time to update the animation's start time. ```Go func (v *VisibilityAnimation) ToggleVisibility(now time.Time) ``` -------------------------------- ### Create and Layout Menu (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component Defines a menu with a given theme and state, and provides a method to render it. Assumes the existence of material.Theme and MenuState types. ```go func Menu(th *material.Theme, state *MenuState) MenuStyle func (m MenuStyle) Layout(gtx C) D ``` -------------------------------- ### Trigger Visibility Animation to Appear (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component Starts the animation to make the element visible at the specified time. If the element is already visible, this is a no-op. ```Go func (v *VisibilityAnimation) Appear(now time.Time) ``` -------------------------------- ### Create New Explorer Instance (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.8.0/explorer Creates a new Explorer instance for a given app.Window. Each window should have its own unique Explorer instance. It's mandatory to use Explorer.ListenEvents with the same window. ```Go func NewExplorer(w *app.Window) (e *Explorer) { // ... implementation ... } ``` -------------------------------- ### Progress: Check if Started (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/component Indicates whether the progress animation has been initiated. Returns true if `Start` has been called, false otherwise. ```go func (p Progress) Started() bool ``` -------------------------------- ### File Selection and Creation with Explorer Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/explorer_tab=versions The Explorer struct and its methods allow users to choose single or multiple files with specific extensions, create new files, and listen for window events related to file operations. ```go type Explorer struct var DefaultExplorer *Explorer func NewExplorer(w *app.Window) (e *Explorer) func (e *Explorer) ChooseFile(extensions ...string) (io.ReadCloser, error) func (e *Explorer) ChooseFiles(extensions ...string) ([]io.ReadCloser, error) func (e *Explorer) CreateFile(name string) (io.WriteCloser, error) func (e *Explorer) ListenEvents(evt event.Event) func ListenEventsWindow(win *app.Window, event event.Event) ``` -------------------------------- ### Go: Function to Create New Explorer Instance Source: https://pkg.go.dev/gioui.org/x/%40v0.1.0/explorer_goos=js Provides the implementation for `NewExplorer`, a constructor function for creating new `Explorer` instances in the Gio UI package. It requires an `app.Window` and should be called once per unique window. Crucially, `Explorer.ListenEvents` must be called on the same window. ```go func NewExplorer(w *app.Window) (e *Explorer) ``` -------------------------------- ### Toggle Visibility Animation Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Switches the animation state: if invisible, it starts appearing; if visible, it starts disappearing. Takes the current time as input. ```go func (v *VisibilityAnimation) ToggleVisibility(now time.Time) { // Implementation details for toggling visibility } ``` -------------------------------- ### Match User Language Preference for Multi-language Apps Source: https://pkg.go.dev/gioui.org/x/pref Shows how to set up a catalog of strings for different languages and then retrieve the user's preferred language to display the correct text. It uses the x/text/catalog and locale packages to match languages and create a message printer. ```go import ( "gioui.org/font/bytes" "gioui.org/layout" "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" "golang.org/x/text/language" "golang.org/x/text/message" "gioui.org/x/pref/internal/catalog" "gioui.org/x/pref/internal/locale" ) func layoutLocalizedUI(gtx layout.Context, yourTheme *material.Theme) { // Your dictionary (in that case using x/text/catalog): cat := catalog.NewBuilder() cat.SetString(language.English, "Hello World", "Hello World") cat.SetString(language.Portuguese, "Hello World", "Olá Mundo") cat.SetString(language.Spanish, "Hello World", "Hola Mundo") cat.SetString(language.Czech, "Hello World", "Ahoj světe") cat.SetString(language.French, "Hello World", "Bonjour le monde") // Get the user preferences: userLanguage, _ := locale.Language() // Get the best match based on the preferred language: userLanguage, _, confidence := cat.Matcher().Match(userLanguage) if confidence <= language.Low { userLanguage = language.English // Switch to the default language, due to low confidence. } // Creates the printer with the user language: printer := message.NewPrinter(userLanguage, message.Catalog(cat)) // Display the text based on the language: widget.Label{ Text: printer.Sprintf("Hello World"), }.Layout(gtx, yourTheme.Shaper, text.Font{}, unit.Dp(12)) } ``` -------------------------------- ### Start Visibility Animation (Disappear) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Initiates the animation to make the entity invisible, starting at the provided time. If the entity is already invisible, this function has no effect. ```go func (v *VisibilityAnimation) Disappear(now time.Time) { // Implementation details for starting the disappear animation } ``` -------------------------------- ### InteractiveSpan Layout and Get in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/richtext_tab=versions Provides methods for laying out an InteractiveSpan within the UI context and retrieving specific data associated with it using a key. ```Go func (i *InteractiveSpan) Layout(gtx layout.Context) layout.Dimensions func (i *InteractiveSpan) Get(key string) interface{} ``` -------------------------------- ### AppBar Component Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/component Documentation for the AppBar component, including its creation, actions, and layout. ```APIDOC ## AppBar Component Implements the material design App Bar. Supports navigation, contextual actions, and overflow menus. ### Types #### AlphaPalette Represents alpha values for material design states like hover and selected. * **Hover** (uint8) * **Selected** (uint8) #### AppBar The main AppBar widget. * **NavigationButton** (widget.Clickable) * **NavigationIcon** (*widget.Icon) * **Title** (string) * **ContextualTitle** (string) * **ModalLayer** (*ModalLayer) - Required for menu layout functionality. * **Anchor** (VerticalAnchorPosition) - Anchors the app bar to the top or bottom. ### Constructors #### NewAppBar Creates and initializes a new AppBar. * **modal** (*ModalLayer) - The modal layer for menu layout. * **Returns**: *AppBar ### Methods #### CloseOverflowMenu Requests that the overflow menu be closed if it is open. * **when** (time.Time) #### Events Retrieves a slice of all AppBarEvents that occurred since the last frame. * **gtx** (layout.Context) * **Returns**: []AppBarEvent #### Layout Renders the AppBar. Spans available horizontal space with a fixed height. * **gtx** (layout.Context) * **theme** (*material.Theme) * **navDesc** (string) - Accessibility description for navigation icon. * **overflowDesc** (string) - Accessibility description for overflow button. * **Returns**: layout.Dimensions #### SetActions Configures the primary actions for the AppBar. * **actions** ([]AppBarAction) - Actions displayed from left to right. * **overflows** ([]OverflowAction) - Actions that will always be in the overflow menu. #### SetContextualActions Configures actions for the contextual mode of the AppBar. * **actions** ([]AppBarAction) * **overflows** ([]OverflowAction) #### StartContextual Transforms the AppBar into contextual mode with a specified title. * **when** (time.Time) * **title** (string) #### StopContextual Exits the contextual mode of the AppBar. * **when** (time.Time) #### ToggleContextual Switches between contextual and non-contextual modes. * **when** (time.Time) * **title** (string) - Title to use when entering contextual mode. ### Types #### AppBarAction Configures an action item within the AppBar. * **OverflowAction** (OverflowAction) - Embedded for overflow menu information. * **Layout** (func(gtx layout.Context, bg, fg color.NRGBA) layout.Dimensions) #### SimpleIconAction Creates an AppBarAction that functions as a simple IconButton. * **state** (*widget.Clickable) * **icon** (*widget.Icon) * **overflow** (OverflowAction) * **Returns**: AppBarAction #### AppBarContextMenuDismissed Indicates that the app bar context menu was dismissed. * **AppBarEvent()**: Method to identify this as an AppBarEvent. * **String()**: String representation of the event. ``` -------------------------------- ### Handle User Language Preference for Multi-language Apps - Go Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/pref Illustrates how to manage user language preferences using the `locale` package and `x/text/catalog` to provide a multi-lingual experience. It includes setting up a catalog, matching user language, and displaying translated text. ```go import ( "gioui.org/app/locale" "gioui.org/io/event" "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" "golang.org/x/text/catalog" "golang.org/x/text/language" "golang.org/x/text/message" ) // Your dictionary (in that case using x/text/catalog): cat := catalog.NewBuilder() cat.SetString(language.English, "Hello World", "Hello World") cat.SetString(language.Portuguese, "Hello World", "Olá Mundo") cat.SetString(language.Spanish, "Hello World", "Hola Mundo") cat.SetString(language.Czech, "Hello World", "Ahoj světe") cat.SetString(language.French, "Hello World", "Bonjour le monde") // Get the user preferences: userLanguage, _ := locale.Language() // Get the best match based on the preferred language: userLanguage, _, confidence := cat.Matcher().Match(userLanguage) if confidence <= language.Low { userLanguage = language.English // Switch to the default language, due to low confidence. } // Creates the printer with the user language: printer := message.NewPrinter(userLanguage, message.Catalog(cat)) // Display the text based on the language: // Assuming 'yourTheme' and 'gtx' are available in the scope. // widget.Label{}.Layout(gtx, // yourTheme.Shaper, // text.Font{}, // unit.Dp(12), // printer.Sprintf("Hello World"), // ) ``` -------------------------------- ### Check if progress has started in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component Implements the `Started` method for the `Progress` type. This method returns a boolean indicating whether the progress animation has been initiated. ```go func (p Progress) Started() bool ``` -------------------------------- ### Go: Create and Layout a Modal Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/component Lays out a content widget centered atop a clickable scrim, forming a modal dialog. Clicking the scrim dismisses the modal. The Layout method handles the rendering of both the scrim and the content. ```go func Modal(th *material.Theme, modal *ModalState) ModalStyle func (m ModalStyle) Layout(gtx C) D // ModalStyle struct definition with ModalState and ScrimStyle would be needed here. ``` -------------------------------- ### Inset Padding in Go (gioui.org/x/outlay) Source: https://pkg.go.dev/gioui.org/x/%40v0.9.0/outlay_tab=versions Applies uniform or custom padding around a widget. The Inset struct defines top, bottom, start, and end padding values. Input is context, widget, and returns layout dimensions. ```go package outlay type Inset struct { Top unit.Dp Bottom unit.Dp Start unit.Dp End unit.Dp } func UniformInset(v unit.Dp) Inset func (in Inset) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions ``` -------------------------------- ### Layout Utilities: Animation Control (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.7.1/outlay_tab=versions Manages UI animations by tracking start time and progress. It allows for controlling the duration and checking if an animation is currently active, essential for dynamic visual feedback. ```Go type Animation struct { StartT time.Time } func (n *Animation) Animating(gtx layout.Context) bool func (n *Animation) Progress(gtx layout.Context) float32 func (n *Animation) SetDuration(d time.Duration) func (n *Animation) Start(now time.Time) ``` -------------------------------- ### Start Contextual Mode in App Bar (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.7.1/component Transitions the AppBar into a contextual mode with a different set of actions. If already in contextual mode, this has no effect. A title is provided for the contextual app bar. Requires a time.Time value for the transition. ```Go func (a *AppBar) StartContextual(when time.Time, title string) StartContextual causes the AppBar to transform into a contextual App Bar with a different set of actions than normal. If the App Bar is already in contextual mode, this has no effect. The title will be used as the contextual app bar title. ``` -------------------------------- ### Create Menu Item Configuration (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.0/component MenuItem constructs a default MenuItemStyle using the provided theme, clickable state, and label string. It simplifies the initialization of menu items. ```Go func MenuItem(th *material.Theme, state *widget.Clickable, label string) MenuItemStyle ``` -------------------------------- ### Toggle Visibility of Animation in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/component The ToggleVisibility method switches the animation's state: if it's invisible, it starts appearing; if it's visible, it starts disappearing. The transition is based on the provided time. ```go func (v *VisibilityAnimation) ToggleVisibility(now time.Time) { // ... implementation details ... } ``` -------------------------------- ### Toggle Visibility of Animation in Gio UI Source: https://pkg.go.dev/gioui.org/x/%40v0.1.0/component ToggleVisibility switches the animation state: if the element is invisible, it starts appearing; if it's visible, it starts disappearing. This method takes the current time to initiate the state change. ```Go func (v *VisibilityAnimation) ToggleVisibility(now time.Time) ``` -------------------------------- ### Start Contextual Mode in AppBar (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Transforms the AppBar into a contextual AppBar with a different set of actions. Takes a time.Time for timing and a title for the contextual bar. Has no effect if already in contextual mode. ```go func (a *AppBar) StartContextual(when time.Time, title string) { // StartContextual causes the AppBar to transform into a contextual App Bar with a different set of actions than normal. // If the App Bar is already in contextual mode, this has no effect. The title will be used as the contextual app bar title. } ``` -------------------------------- ### Progress Component Methods in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.8.0/component Defines methods for the Progress component to manage and query its state. This includes starting, stopping, updating progress, and checking if it's finished or started. It uses time and progress direction for state management. ```Go func (p Progress) Absolute() float32 func (p Progress) Direction() ProgressDirection func (p Progress) Finished() bool func (p Progress) Progress() float32 func (p *Progress) Start(began time.Time, direction ProgressDirection, duration time.Duration) func (p Progress) Started() bool func (p *Progress) Stop() func (p *Progress) Update(now time.Time) ``` -------------------------------- ### Inset Handling in Gio UI (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/outlay_tab=versions Manages padding or margins around a widget. Allows setting uniform insets or specific top, bottom, start, and end values. ```Go package outlay import ( "gioui.org/layout" "gioui.org/unit" ) type Inset struct { Bottom unit.Dp End unit.Dp Start unit.Dp Top unit.Dp } func UniformInset(v unit.Dp) Inset { return Inset{Top: v, Bottom: v, Start: v, End: v} } func (in Inset) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions { // Implementation details for Inset layout return layout.Dimensions{} } ``` -------------------------------- ### NewBuzzer Constructor in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/haptic Constructs a new Buzzer instance for managing haptic feedback. It requires a pointer to an app.Window as an argument. ```Go func NewBuzzer(_ *app.Window) *Buzzer ``` -------------------------------- ### Animation and Progress Tracking in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/outlay_tab=versions Provides an Animation type to manage UI animations. It includes methods to set duration, start animations, check if animating, and retrieve the current progress. This is useful for creating smooth transitions and visual feedback in Go applications using the 'gioui.org/layout' context. ```go package outlay import ( "gioui.org/layout" "time" ) type Animation struct { Start time.Time Duration time.Duration } func (n *Animation) Animating(gtx layout.Context) bool func (n *Animation) Progress(gtx layout.Context) float32 func (n *Animation) SetDuration(d time.Duration) func (n *Animation) Start(now time.Time) ``` -------------------------------- ### Create and Layout Scrim in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.8.1/component Provides the constructor NewScrim for creating a ScrimStyle and its Layout method for rendering the scrim. The ScrimStyle configures the scrim's appearance, including its color and final alpha, and uses ScrimState for its behavior. ```go func NewScrim(th *material.Theme, scrim *ScrimState, alpha uint8) ScrimStyle func (scrim ScrimStyle) Layout(gtx C) D ``` -------------------------------- ### Progress: Get Current Direction (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/component Reports the current direction of the progress animation (Forward or Reverse). ```go func (p Progress) Direction() ProgressDirection ``` -------------------------------- ### Implement Flow layouts in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/outlay_tab=versions Supports different flow layouts like wrapping and standard flow. Allows arranging items based on specified constraints and alignment. Requires layout.Context and a FlowElement function. ```go type Flow struct { Alignment layout.Alignment Axis layout.Axis Num int } func (g *Flow) Layout(gtx layout.Context, num int, el FlowElement) layout.Dimensions type FlowElement func(gtx layout.Context, i int) layout.Dimensions type FlowWrap struct { Alignment layout.Alignment Axis layout.Axis } func (g FlowWrap) Layout(gtx layout.Context, num int, el FlowElement) layout.Dimensions ``` -------------------------------- ### Trigger ModalSheet Disappearance (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Starts the animation to hide the ModalSheet. This function is called when the side sheet should be dismissed. ```Go func (s *ModalSheet) Disappear(when time.Time) ``` -------------------------------- ### Visibility Animation (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.9.0/component_tab=versions Manages the animation of UI element visibility. It tracks the animation state, start time, and provides methods to control appearing, disappearing, and checking the animation status. The `String` method provides a string representation of the current state. ```Go type VisibilityAnimation struct { Started time.Time State VisibilityAnimationState } func (v *VisibilityAnimation) Appear(now time.Time) func (v *VisibilityAnimation) Disappear(now time.Time) func (v *VisibilityAnimation) Revealed(gtx layout.Context) float32 func (v *VisibilityAnimation) String(gtx layout.Context) string func (v *VisibilityAnimation) ToggleVisibility(now time.Time) func (v VisibilityAnimation) Animating() bool func (v VisibilityAnimation) Visible() bool type VisibilityAnimationState int const ( Appearing VisibilityAnimationState Disappearing Invisible Visible ) func (v VisibilityAnimationState) String() string ``` -------------------------------- ### Toggle ModalSheet Visibility (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Toggles the visibility of the ModalSheet, animating its appearance or disappearance. The animation starts at the provided time. ```Go func (s *ModalSheet) ToggleVisibility(when time.Time) ``` -------------------------------- ### File Selection and Creation Functions (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.8.0/explorer_tab=versions Provides functions to interact with the file system, including choosing single or multiple files with specific extensions, and creating new files with a given name. These functions abstract platform-specific file dialogs. ```Go func ReadFile(extensions ...string) (io.ReadCloser, error) func WriteFile(name string) (io.WriteCloser, error) func (e *Explorer) ChooseFile(extensions ...string) (io.ReadCloser, error) func (e *Explorer) ChooseFiles(extensions ...string) ([]io.ReadCloser, error) func (e *Explorer) CreateFile(name string) (io.WriteCloser, error) ``` -------------------------------- ### Gio UI Configuration and Defaults (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.1.0/markdown_tab=versions Illustrates the configuration structure and default settings for Gio UI components. It defines a 'Config' struct and constants for default font, sizes, and colors. These are used to customize the UI rendering behavior. Dependencies include 'font', 'color', and 'unit' packages. ```Go type Config struct { MonospaceFont font.Font } const MetadataURL string = "http://example.com/metadata" var DefaultColor color.NRGBA = color.NRGBA{R: 255, G: 255, B: 255, A: 255} var DefaultFont font.Font var DefaultSize unit.Sp var H1Size unit.Sp var H2Size unit.Sp var H3Size unit.Sp var H4Size unit.Sp var H5Size unit.Sp var H6Size unit.Sp var InteractiveColor color.NRGBA ``` -------------------------------- ### Trigger ModalSheet Appearance (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Starts the animation to make the ModalSheet visible. This function should be called when the side sheet needs to be displayed. ```Go func (s *ModalSheet) Appear(when time.Time) ``` -------------------------------- ### AppBar Component Initialization (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Creates and initializes a new AppBar component. Requires a ModalLayer for its operation. ```go func NewAppBar(modal *ModalLayer) *AppBar { // NewAppBar creates and initializes an App Bar. return &AppBar{} } ``` -------------------------------- ### Create Uniform Inset - Go Source: https://pkg.go.dev/gioui.org/x/%40v0.3.0/outlay The UniformInset function simplifies creating an Inset by applying a single specified Dp value to all four edges (top, bottom, start, end). This is useful for consistent padding. ```go func UniformInset(v unit.Dp) Inset ``` -------------------------------- ### Outlay Package Overview Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/outlay_tab=versions This section provides an overview of the outlay package and its various versions, highlighting changes and new features introduced in each release. ```APIDOC ## Package: gioui.org/x/outlay This package provides advanced layout widgets for the Gio UI framework. ### Versions - **v0.9.0 (Sep 16, 2025)** - New functions: EmptyFlexed, EmptyRigid, EmptyRigidHorizontal, EmptyRigidVertical, Space. - New type: If with methods for conditional layouting. - New type: MultiList for list layouts. - New type: Segment for list item layouts. - New type: Spacer for adding space. - **v0.8.1 (Jan 14, 2025)** - No specific changes listed. - **v0.8.0 (Jan 14, 2025)** - New type: RigidRows for row-based layouts. - **v0.7.1 (Jul 17, 2024)** - No specific changes listed. - **v0.7.0 (Jun 17, 2024)** - No specific changes listed. - **v0.6.1 (Apr 11, 2024)** - No specific changes listed. - **v0.6.0 (Apr 10, 2024)** - No specific changes listed. - **v0.5.0 (Feb 15, 2024)** - New type: Grid with an Update method for dynamic grid adjustments. - **v0.4.0 (Nov 29, 2023)** - No specific changes listed. - **v0.3.2 (Oct 16, 2023)** - No specific changes listed. - **v0.3.1 (Oct 4, 2023)** - No specific changes listed. - **v0.3.0 (Aug 31, 2023)** - New type: Flex for flexible box layouts. - New type: FlexChild for flexible item layouts (Flexed, Rigid). - New type: Inset for padding. - **v0.2.0 (Aug 3, 2023)** - No specific changes listed. - **v0.1.0 (Jul 1, 2023)** - New type: Animation for managing animations. - New type: AxisPosition for defining axis positions. - New type: Cell for grid cell definitions. - New type: Dimensioner for constraint-based dimensioning. - New type: Fan for radial layouts. - New type: FanItem for fan layout items. - New type: Flow for wrapping element layouts. - New type: FlowElement for flow item layouts. - New type: FlowWrap for wrapping flow layouts. - New type: Grid for grid layouts. ``` -------------------------------- ### Trigger ModalNavDrawer Disappearance (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Initiates the disappearance animation for the ModalNavDrawer, starting at a specified time. Used to hide the navigation drawer. ```Go func (m *ModalNavDrawer) Disappear(when time.Time) ``` -------------------------------- ### Inset Padding in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/outlay_tab=versions Defines an Inset struct for applying uniform or specific padding (top, bottom, start, end) to widgets. Includes a helper function for creating uniform insets. ```Go package outlay import "gioui.org/unit" type Inset struct { Bottom unit.Dp End unit.Dp Start unit.Dp Top unit.Dp } func UniformInset(v unit.Dp) Inset func (in Inset) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions ``` -------------------------------- ### Trigger ModalNavDrawer Appearance (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Initiates the appearance animation for the ModalNavDrawer, starting at a specified time. Used to show the navigation drawer. ```Go func (m *ModalNavDrawer) Appear(when time.Time) ``` -------------------------------- ### Create MultiList and Segment layouts in Go Source: https://pkg.go.dev/gioui.org/x/%40v0.7.0/outlay_tab=versions Enables creating lists with multiple segments, each potentially having a header, footer, and range definition. Suitable for complex list views. Requires layout.Context. ```go type MultiList []Segment func (ml MultiList) Layout(gtx layout.Context, ii int) layout.Dimensions func (ml MultiList) Len() int type Segment struct { Header layout.Widget Footer layout.Widget Range int Element layout.ListElement } func (s Segment) Layout(gtx layout.Context, ii int) layout.Dimensions func (s Segment) Len() int ``` -------------------------------- ### Get AppBar Events (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.1/component Retrieves all AppBarEvents that have occurred since the last frame. This is crucial for handling user interactions with the AppBar. ```go func (a *AppBar) Events(gtx layout.Context) []AppBarEvent { // Events returns a slice of all AppBarActions to occur since the last frame. return []AppBarEvent{} } ``` -------------------------------- ### Start Contextual Mode in AppBar (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.6.0/component StartContextual transforms the AppBar into a contextual AppBar with a specified title. If already in contextual mode, it has no effect. ```Go func (a *AppBar) StartContextual(when time.Time, title string) { // Causes the AppBar to transform into a contextual App Bar. } ``` -------------------------------- ### Gio UI Renderer Implementation (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/markdown_tab=versions Demonstrates the implementation of a Renderer in the Gio UI module. It includes a constructor function and a Render method that processes byte slices into richtext spans and handles potential errors. ```go package main import ( "image/color" "gioui.org/font" "gioui.org/unit" "gioui.org/x/richtext" ) type Config struct { MonospaceFont font.Font } type Renderer struct { Config Config } func NewRenderer() *Renderer { return &Renderer{} } func (r *Renderer) Render(src []byte) ([]richtext.SpanStyle, error) { // Implementation details would go here return nil, nil } ``` -------------------------------- ### Create Platform Tooltip (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Creates a tooltip styled for the current platform (desktop or mobile) by detecting the OS. Note that this automatic detection may not always be optimal. ```go func PlatformTooltip(th *material.Theme, text string) Tooltip { // ... implementation details ... } ``` -------------------------------- ### Trigger Visibility Animation to Disappear (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.1/component Starts the animation to make the element invisible at the specified time. If the element is already invisible, this is a no-op. ```Go func (v *VisibilityAnimation) Disappear(now time.Time) ``` -------------------------------- ### Create Spy and Context Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/eventx Creates a new Spy and returns it along with a modified layout.Context configured to use this spy. This is the entry point for event observation. ```go func Enspy(gtx layout.Context) (*Spy, layout.Context) { // Implementation details omitted for brevity } ``` -------------------------------- ### Progress: Get Absolute Progress Value (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.2.0/component Returns the absolute progress value, disregarding the current direction of progression (forward or reverse). ```go func (p Progress) Absolute() float32 ``` -------------------------------- ### Toggle ModalNavDrawer Visibility (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.3.2/component Switches the visibility state of the ModalNavDrawer between visible and hidden, starting the animation at a specified time. This is a primary interaction method. ```Go func (m *ModalNavDrawer) ToggleVisibility(when time.Time) ``` -------------------------------- ### Fan Layout in Go (gioui.org/x/outlay) Source: https://pkg.go.dev/gioui.org/x/%40v0.9.0/outlay_tab=versions Lays out items in a radial fan pattern. Allows configuration of hollow radius, offset radians, and width radians. Input includes context, items, and optional configuration pointers. ```go package outlay import "gioui.org/unit" type FanItem struct { Elevate bool W layout.Widget } func Item(evelvate bool, w layout.Widget) FanItem type Fan struct { HollowRadius *unit.Dp OffsetRadians float32 WidthRadians float32 } func (f *Fan) Layout(gtx layout.Context, items ...FanItem) layout.Dimensions ``` -------------------------------- ### Get String Representation of Visibility Animation in Gio UI Source: https://pkg.go.dev/gioui.org/x/%40v0.4.0/component Returns a string representation of the current state of the visibility animation. This is primarily for debugging purposes. ```Go func (v VisibilityAnimation) String(gtx layout.Context) string ``` -------------------------------- ### Grid Component: Configuration and Layout (Go) Source: https://pkg.go.dev/gioui.org/x/%40v0.4.0/component Provides functions for configuring and laying out a grid. `Grid` initializes the `GridStyle` with a theme and state, and the `Layout` method renders the grid with specified dimensions and a cell rendering function. ```go func Grid(th *material.Theme, state *GridState) GridStyle func (g GridStyle) Layout(gtx layout.Context, rows, cols int, dimensioner outlay.Dimensioner, cellFunc outlay.Cell) layout.Dimensions ```