fynetooltip

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: BSD-3-Clause Imports: 4 Imported by: 12 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

fyne-tooltip

Add-on package for Fyne adding tooltip support.

GitHub Release Fyne Go Reference GitHub License

Contents

Overview

fyne-tooltip is an add-on package for Fyne adding tooltip support. It provides a tool tip rendering system, along with drop-in tooltip-enabled extensions of several of the Fyne builtin widgets, as well as an easy means to extend existing widgets to add tool tip support, or creating new tooltip-enabled custom widgets. It aims to be easy and low-friction to integrate into your existing Fyne projects, as well as easy to remove and switch back to Fyne core if and when tooltips are supported natively.

Tool tip widgets implement desktop.Hoverable to show the tool tips after a short delay on MouseIn, and hide them on MouseOut.

Demo

In the following video you can see how tooltips look with fyne-tooltip in a Fyne app. The source code is available at cmd/example.go.

https://github.com/user-attachments/assets/e9a2b9dd-9d6c-49f2-9c3a-b896fec4609d

Example

The following is a complete Fyne app that shows how to use fyne-tooltip with a label widget.

package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	fynetooltip "github.com/dweymouth/fyne-tooltip"
	ttwidget "github.com/dweymouth/fyne-tooltip/widget"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Example")

	label := ttwidget.NewLabel("This is a label")
	label.SetToolTip("This is a tooltip")

	content := container.NewCenter(label)
	myWindow.SetContent(fynetooltip.AddWindowToolTipLayer(content, myWindow.Canvas()))

	myWindow.Resize(fyne.NewSize(800, 600))
	myWindow.ShowAndRun()
}

Guide

This chapter explains how fyne-tooltip works and how to use it in your own Fyne apps.

Enabling tool tips in a window

fyne-tooltip requires tool tip layers to be created for the tool tips to be rendered into, and provides APIs to do so for windows and pop-ups. It is called when setting the window's content:

window.SetContent(fynetooltip.AddWindowToolTipLayer(myContent, window.Canvas()))

If the window is not your main window, and will be closed while your app continues to run, it is important to call DestroyWindowToolTipLayer to release memory resources associated with the tool tip layer.

window.SetCloseIntercept(func() {
    // do any other close intercept app logic
    window.Close()
    fynetooltip.DestroyWindowToolTipLayer(window.Canvas())
})
Using built-in tool tip widgets

Drop-in replacements for several built-in Fyne widgets are provided, which have been extended from the base implementation to add tool tip support.

import ttwidget "github.com/dweymouth/fyne-tooltip/widget"

func main() {
    // app setup

    var btn = ttwidget.NewButton("Hello", func() {
        log.Println("Hello!")
    })
    btn.SetToolTip("world")

    // set up and show app window
}
Enabling tool tips in a PopUp

Similarly to windows, fyne-tooltip requires a tool tip layer to be created for pop ups to enable tool tips to be shown. It is called after creating the pop up with content, but before showing it.

pop := widget.NewPopUp(content, canvas)
fynetooltip.AddPopUpToolTipLayer(pop) // works for modal popup too
pop.Show()

The pop up may be hidden and re-shown. It is only necessary to create the pop up layer once. When the popup is hidden and will not be shown again, it is important to call DestroyPopUpToolTipLayer to release memory resources associated with the tool tip layer.

Creating new tool tip widgets

To create custom widgets that are tool tip enabled, just extend from the ToolTipWidget base struct instead of BaseWidget. The widget will automatically have a SetToolTip API, and the mouse event hooks added to show and hide the tool tip.

import ttwidget "github.com/dweymouth/fyne-tooltip/widget"

type MyWidget struct {
    ttwidget.ToolTipWidget
}

func NewMyWidget() *MyWidget {
    w := &MyWidget{}
    w.ExtendBaseWidget(w)
    return w
}

If your custom widget implements desktop.Hoverable, e.g. to make it tappable, you must also forward calls to ToolTipWidget in your overridden methods to enable tooltips to work. Here is an example for how this could be implemented:

func (w *CustomWidget) MouseIn(e *desktop.MouseEvent) {
	w.ToolTipWidget.MouseIn(e)
	// custom logic
}

func (w *CustomWidget) MouseMoved(e *desktop.MouseEvent) {
	w.ToolTipWidget.MouseMoved(e)
    // custom logic
}

func (w *CustomWidget) MouseOut() {
	w.ToolTipWidget.MouseOut()
	// custom logic
}
Extending existing widgets for tool tip support

To extend existing widgets with tool tip support, use the ToolTipWidgetExtend struct. You must override ExtendBaseWidget to call both the parent widget's ExtendBaseWidget, as well as ExtendToolTipWidget

import (
    "fyne.io/fyne/v2/widget"
    ttwidget "github.com/dweymouth/fyne-tooltip/widget"
)

type ToolTipIcon struct {
    widget.Icon
    ttwidget.ToolTipWidgetExtend
}

func NewToolTipIcon(resource fyne.Resource) *ToolTipIcon {
    w := &ToolTipIcon{
        Icon: widget.Icon{
            Resource: resource,
        }
    }
    w.ExtendBaseWidget(w)
    return w
}

func (w *ToolTipIcon) ExtendBaseWidget(wid fyne.Widget) {
	w.ExtendToolTipWidget(wid)
	w.Icon.ExtendBaseWidget(wid)
}

If the base widget already implements desktop.Hoverable, you must override the Hoverable APIs to call both the parent widget's implementation, as well as the ToolTipWidgetExtend implementation. For example, for the tooltip-enabled button:

func (b *Button) MouseIn(e *desktop.MouseEvent) {
	b.ToolTipWidgetExtend.MouseIn(e)
	b.Button.MouseIn(e)
}

func (b *Button) MouseOut() {
	b.ToolTipWidgetExtend.MouseOut()
	b.Button.MouseOut()
}

func (b *Button) MouseMoved(e *desktop.MouseEvent) {
	b.ToolTipWidgetExtend.MouseMoved(e)
	b.Button.MouseMoved(e)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPopUpToolTipLayer

func AddPopUpToolTipLayer(p *widget.PopUp)

AddPopUpToolTipLayer adds a layer to the given `*widget.PopUp` for tool tips to be drawn into. This call is required for each new PopUp you create to enable tool tips to be shown in it. It is invoked after the PopUp has been created with content, but before it is shown. Once the pop up is hidden and will not be shown anymore, it is important to call `DestroyPopUpToolTipLayer` to release memory resources associated with the tool tip layer. A pop up that will be shown again should not have DestroyPopUpToolTipLayer called.

func AddWindowToolTipLayer

func AddWindowToolTipLayer(windowContent fyne.CanvasObject, canvas fyne.Canvas) fyne.CanvasObject

AddWindowToolTipLayer adds a layer to the given window content for tool tips to be drawn into. This call is required for each new window you create to enable tool tips to be shown in it. It is typically invoked with `window.SetContent` as

`window.SetContent(fynetooltip.AddWindowToolTipLayer(myContent, window.Canvas()))``

If the window not your main window and is closed before the app exits, it is important to call `DestroyWindowToolTipLayer` to release memory resources associated with the tool tip layer.

func DestroyPopUpToolTipLayer

func DestroyPopUpToolTipLayer(p *widget.PopUp)

DestroyPopUpToolTipLayer destroys the tool tip layer for a given pop up. It should be called after the pop up is hidden and will no longer be shown to free associated memory resources.

func DestroyWindowToolTipLayer

func DestroyWindowToolTipLayer(canvas fyne.Canvas)

DestroyWindowToolTipLayer destroys the tool tip layer for a given window canvas. It should be called after a window is closed to free associated memory resources.

func SetToolTipTextSizeName

func SetToolTipTextSizeName(sizeName fyne.ThemeSizeName)

SetToolTipTextSizeName sets the theme size name that will control the size of tool tip text. By default, tool tips use theme.SizeNameCaptionText.

func SetToolTipTextStyle

func SetToolTipTextStyle(style fyne.TextStyle)

SetToolTipTextStyle sets the TextStyle that will be used to render tool tip text.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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