graphics

package
v0.0.0-...-7e1b392 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UIFontParagraph  = "paragraph"
	UIFontSubheading = "subheading"
	UIFontHeading    = "heading"
	UIFontTitle      = "title"
)

Variables

View Source
var (
	ColorBlack       = Color{0, 0, 0, 255}
	ColorWhite       = Color{255, 255, 255, 255}
	ColorRed         = Color{255, 0, 0, 255}
	ColorGreen       = Color{0, 255, 0, 255}
	ColorBlue        = Color{0, 0, 255, 255}
	ColorGray        = Color{128, 128, 128, 255}
	ColorLightGray   = Color{200, 200, 200, 255}
	ColorDarkGray    = Color{64, 64, 64, 255}
	ColorTransparent = Color{0, 0, 0, 0}
)

Common colors

View Source
var AvyosDark = Theme{
	Background:      NewColorHex(0x0B1220),
	Foreground:      NewColorHex(0xEAF0FF),
	Primary:         NewColorHex(0x3B7CFF),
	PrimaryHover:    NewColorHex(0x3871E3),
	PrimaryActive:   NewColorHex(0x2F67D1),
	Secondary:       NewColorHex(0x1A2744B8),
	Border:          NewColorHex(0xEAF0FF1F),
	BorderFocused:   NewColorHex(0x3B7CFF73),
	InputBackground: NewColorHex(0x1A2744B8),
	InputForeground: NewColorHex(0xEAF0FF),
	ButtonText:      NewColorHex(0xFFFFFFF2),
	SurfaceGlass:    NewColorHex(0x121B2FB8),
	SurfaceRaised:   NewColorHex(0x18233DD1),
	SurfaceSidebar:  NewColorHex(0x0F1830C2),
	StrokeDivider:   NewColorHex(0xEAF0FF14),
	TextSecondary:   NewColorHex(0xB7C2DA),
	TextMuted:       NewColorHex(0x8F9AB4),
	TextDisabled:    NewColorHex(0xEAF0FF66),
	ControlHover:    NewColorHex(0x223255C7),
	ControlPressed:  NewColorHex(0x2A3D66D1),
	AccentSubtle:    NewColorHex(0x3B7CFF2E),
	Success:         NewColorHex(0x2FBF6D),
	Warning:         NewColorHex(0xFF8A3D),
	Danger:          NewColorHex(0xFF4D5E),
	BorderRadius:    10,
}
View Source
var AvyosLight = Theme{
	Background:      NewColorHex(0xEEF2FA),
	Foreground:      NewColorHex(0x121A2B),
	Primary:         NewColorHex(0x2F6BFF),
	PrimaryHover:    NewColorHex(0x2A5FE7),
	PrimaryActive:   NewColorHex(0x2455D0),
	Secondary:       NewColorHex(0xFFFFFFB2),
	Border:          NewColorHex(0x1B2A4A24),
	BorderFocused:   NewColorHex(0x2F6BFF59),
	InputBackground: NewColorHex(0xFFFFFFB2),
	InputForeground: NewColorHex(0x121A2B),
	ButtonText:      NewColorHex(0xFFFFFFF2),
	SurfaceGlass:    NewColorHex(0xF7F9FFB8),
	SurfaceRaised:   NewColorHex(0xFFFFFFD1),
	SurfaceSidebar:  NewColorHex(0xF3F6FFC2),
	StrokeDivider:   NewColorHex(0x1B2A4A1A),
	TextSecondary:   NewColorHex(0x3A4963),
	TextMuted:       NewColorHex(0x63708A),
	TextDisabled:    NewColorHex(0x121A2B59),
	ControlHover:    NewColorHex(0xFFFFFFD1),
	ControlPressed:  NewColorHex(0xDCE6FFB2),
	AccentSubtle:    NewColorHex(0x2F6BFF24),
	Success:         NewColorHex(0x2FBF6D),
	Warning:         NewColorHex(0xFF8A3D),
	Danger:          NewColorHex(0xFF4D5E),
	BorderRadius:    10,
}
View Source
var BitmapFont = &Font{
	Width:    8,
	Height:   16,
	FirstRun: 32,
	LastRun:  126,
	Glyphs:   defaultGlyphs,
}

BitmapFont is the built-in 8x16 bitmap font.

View Source
var DefaultFont = BitmapFont

DefaultFont is the active font used by graphics and UI.

View Source
var DefaultTheme = AvyosLight

DefaultTheme is the default color theme.

View Source
var MayurDark = AvyosDark
View Source
var MayurLight = AvyosLight

Backwards-compatible aliases.

Functions

func ResolveIconPath

func ResolveIconPath(name string, size int) string

ResolveIconPath returns the best icon file path for name+size. Falls back to help.png if the requested icon is missing.

func SetUIFont

func SetUIFont(role string, font *Font)

SetUIFont sets a font for a logical UI text role.

func SetUIFonts

func SetUIFonts(fonts map[string]*Font)

SetUIFonts sets multiple UI role fonts at once.

Types

type Backend

type Backend interface {
	Open() error
	Close() error
	Size() (width, height int)
	Buffer() *Buffer
	Flush() error
	FlushRect(Rect) error
	Info() string
	HasSystemCursor() bool
}

Backend is the display backend interface. Implementations include framebuffer (Linux /dev/fb0) and Wayland.

type BaseWidget

type BaseWidget struct {
	// contains filtered or unexported fields
}

BaseWidget provides common functionality for widgets.

func NewBaseWidget

func NewBaseWidget() BaseWidget

NewBaseWidget creates a new base widget.

func (*BaseWidget) Bounds

func (w *BaseWidget) Bounds() Rect

Bounds returns the widget's bounds.

func (*BaseWidget) IsDirty

func (w *BaseWidget) IsDirty() bool

IsDirty returns true if the widget needs to be redrawn.

func (*BaseWidget) IsFocused

func (w *BaseWidget) IsFocused() bool

IsFocused returns true if the widget has focus.

func (*BaseWidget) IsVisible

func (w *BaseWidget) IsVisible() bool

IsVisible returns true if the widget is visible.

func (*BaseWidget) MarkClean

func (w *BaseWidget) MarkClean()

MarkClean marks the widget as not needing a redraw.

func (*BaseWidget) MarkDirty

func (w *BaseWidget) MarkDirty()

MarkDirty marks the widget as needing a redraw.

func (*BaseWidget) MinSize

func (w *BaseWidget) MinSize() Point

MinSize returns the minimum size the widget needs.

func (*BaseWidget) SelfDirty

func (w *BaseWidget) SelfDirty() bool

SelfDirty returns true if the widget itself needs a redraw. Containers can use this to distinguish their own dirty state from children.

func (*BaseWidget) SetBounds

func (w *BaseWidget) SetBounds(r Rect)

SetBounds sets the widget's bounds.

func (*BaseWidget) SetFocused

func (w *BaseWidget) SetFocused(focused bool)

SetFocused sets the focus state of the widget.

func (*BaseWidget) SetMinSize

func (w *BaseWidget) SetMinSize(size Point)

SetMinSize sets the minimum size for the widget.

func (*BaseWidget) SetVisible

func (w *BaseWidget) SetVisible(visible bool)

SetVisible sets the visibility of the widget.

type Buffer

type Buffer struct {
	Width  int
	Height int
	Stride int
	Format PixelFormat
	Data   []byte
	// contains filtered or unexported fields
}

Buffer represents a pixel buffer for drawing.

func DecodeImageToBuffer

func DecodeImageToBuffer(path string, reqSize int) (*Buffer, error)

DecodeImageToBuffer decodes raster formats directly and rasterizes SVG files. reqSize is used for SVG rasterization. If <= 0, a size is inferred from SVG metadata.

func LoadIcon

func LoadIcon(name string, size int) (*Buffer, error)

LoadIcon loads an icon by name and size as a graphics.Buffer. If the icon does not exist, it falls back to help.png.

func NewBuffer

func NewBuffer(width, height int) *Buffer

NewBuffer creates a new buffer with the given dimensions (BGRA format).

func (*Buffer) Blit

func (b *Buffer) Blit(src *Buffer, x, y int)

Blit copies a source buffer onto this buffer at the given position.

func (*Buffer) BlitOpaque

func (b *Buffer) BlitOpaque(src *Buffer, x, y int)

BlitOpaque copies a source buffer onto this buffer, treating all pixels as fully opaque. This is useful for compositing surfaces with undefined alpha (e.g. XRGB8888).

func (*Buffer) BlitOpaqueRect

func (b *Buffer) BlitOpaqueRect(src *Buffer, srcRect Rect, x, y int)

BlitOpaqueRect copies a region of a source buffer onto this buffer, treating all source pixels as fully opaque.

func (*Buffer) BlitRect

func (b *Buffer) BlitRect(src *Buffer, srcRect Rect, x, y int)

BlitRect copies a region of a source buffer onto this buffer.

func (*Buffer) BlitScaled

func (b *Buffer) BlitScaled(src *Buffer, srcRect, dstRect Rect)

BlitScaled copies a rectangular region of a source buffer onto this buffer, scaling to fit the destination rectangle using bilinear sampling.

func (*Buffer) Clear

func (b *Buffer) Clear(c Color)

Clear fills the entire buffer with a color.

func (*Buffer) ClearClip

func (b *Buffer) ClearClip()

ClearClip removes any active drawing clip.

func (*Buffer) Clip

func (b *Buffer) Clip() (Rect, bool)

Clip returns the active clip rectangle and whether clipping is enabled.

func (*Buffer) DrawLine

func (b *Buffer) DrawLine(x0, y0, x1, y1 int, c Color)

DrawLine draws a line between two points using Bresenham's algorithm.

func (*Buffer) DrawRect

func (b *Buffer) DrawRect(r Rect, c Color)

DrawRect draws a rectangle outline with a color.

func (*Buffer) DrawRoundedRect

func (b *Buffer) DrawRoundedRect(r Rect, radius int, c Color)

DrawRoundedRect draws a rounded rectangle outline.

func (*Buffer) FillRect

func (b *Buffer) FillRect(r Rect, c Color)

FillRect fills a rectangle with a color.

func (*Buffer) FillRoundedRect

func (b *Buffer) FillRoundedRect(r Rect, radius int, c Color)

FillRoundedRect fills a rectangle with rounded corners.

func (*Buffer) GetPixel

func (b *Buffer) GetPixel(x, y int) Color

GetPixel returns the color at the given coordinates.

func (*Buffer) SetClip

func (b *Buffer) SetClip(r Rect)

SetClip restricts subsequent drawing operations to the given rectangle. The clip is intersected with the buffer bounds.

func (*Buffer) SetPixel

func (b *Buffer) SetPixel(x, y int, c Color)

SetPixel sets a pixel at the given coordinates.

func (*Buffer) SubBuffer

func (b *Buffer) SubBuffer(r Rect) *Buffer

SubBuffer returns a new buffer that is a view into a region of this buffer.

type Color

type Color struct {
	R, G, B, A uint8
}

Color represents an RGBA color.

func NewColor

func NewColor(r, g, b, a uint8) Color

NewColor creates a new color from RGBA values.

func NewColorHex

func NewColorHex(hex uint32) Color

NewColorHex creates a color from a hex value (0xRRGGBB or 0xRRGGBBAA).

func NewColorRGB

func NewColorRGB(r, g, b uint8) Color

NewColorRGB creates a new opaque color from RGB values.

func (Color) BGRA

func (c Color) BGRA() uint32

BGRA returns the color as 32-bit BGRA value (common framebuffer format).

func (Color) Blend

func (c Color) Blend(bg Color) Color

Blend blends this color over a background color using alpha compositing.

func (Color) RGB565

func (c Color) RGB565() uint16

RGB565 returns the color as 16-bit RGB565 value.

func (Color) RGBA

func (c Color) RGBA() uint32

RGBA returns the color as 32-bit RGBA value.

type Event

type Event struct {
	Type EventType

	// Mouse events
	X, Y        int
	MouseButton MouseButton

	// Keyboard events
	Key       Key
	Rune      rune
	Modifiers Modifiers
}

Event represents an input event.

func (Event) HasMod

func (e Event) HasMod(mod Modifiers) bool

HasMod returns true if the modifier is set.

func (Event) IsAlt

func (e Event) IsAlt() bool

IsAlt returns true if alt is held.

func (Event) IsCtrl

func (e Event) IsCtrl() bool

IsCtrl returns true if ctrl is held.

func (Event) IsShift

func (e Event) IsShift() bool

IsShift returns true if shift is held.

type EventType

type EventType int

EventType represents the type of an event.

const (
	EventNone EventType = iota
	EventMouseMove
	EventMouseButtonPress
	EventMouseButtonRelease
	EventKeyPress
	EventKeyRelease
	EventFocusIn
	EventFocusOut
	EventQuit
	EventResize
)

type Font

type Font struct {
	Width    int
	Height   int
	Glyphs   map[rune][]byte
	FirstRun rune
	LastRun  rune
	// contains filtered or unexported fields
}

Font represents either a bitmap font or a vector TTF-backed font.

func LoadTTFFont

func LoadTTFFont(ttf []byte, opts *gfxfont.Options) (*Font, error)

LoadTTFFont creates a graphics.Font from raw TTF bytes.

func LoadTTFFontFile

func LoadTTFFontFile(path string, opts *gfxfont.Options) (*Font, error)

LoadTTFFontFile creates a graphics.Font from a TTF file path.

func NewDefaultTTFFont

func NewDefaultTTFFont(opts *gfxfont.Options) (*Font, error)

NewDefaultTTFFont creates a graphics.Font using gofont/goregular.

func NewTTFFont

func NewTTFFont(face *gfxfont.Font) *Font

NewTTFFont wraps a shaped TTF face for the graphics renderer.

func UIFont

func UIFont(role string) *Font

UIFont returns the font for a UI role, falling back to the default font.

func (*Font) Close

func (f *Font) Close() error

Close releases font resources when supported by the backing renderer.

func (*Font) DrawGlyph

func (f *Font) DrawGlyph(buf *Buffer, r rune, x, y int, fg, bg Color)

DrawGlyph draws a single character glyph to a buffer.

func (*Font) DrawText

func (f *Font) DrawText(buf *Buffer, text string, x, y int, fg, bg Color)

DrawText draws text to a buffer at the given position.

func (*Font) TextHeight

func (f *Font) TextHeight(text string) int

TextHeight returns the height of text in pixels.

func (*Font) TextWidth

func (f *Font) TextWidth(text string) int

TextWidth returns the width of text in pixels.

type InputHandler

type InputHandler interface {
	Open() error
	Start()
	Close() error
	Poll() *Event
	MousePosition() (x, y int)
	SetScreenSize(width, height int)
}

InputHandler is the input handler interface. Implementations include evdev (Linux raw input) and Wayland seat.

type Key

type Key int

Key represents a keyboard key.

const (
	KeyNone Key = iota
	KeyEscape
	KeyBackspace
	KeyTab
	KeyEnter
	KeySpace
	KeyLeft
	KeyRight
	KeyUp
	KeyDown
	KeyHome
	KeyEnd
	KeyPageUp
	KeyPageDown
	KeyInsert
	KeyDelete
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyLeftShift
	KeyRightShift
	KeyLeftCtrl
	KeyRightCtrl
	KeyLeftAlt
	KeyRightAlt
	KeyCapsLock
	KeyNumLock
	KeyScrollLock
)

type Modifiers

type Modifiers uint8

Modifiers represents keyboard modifiers.

const (
	ModShift Modifiers = 1 << iota
	ModCtrl
	ModAlt
	ModCapsLock
)

type MouseButton

type MouseButton int

MouseButton represents a mouse button.

const (
	MouseButtonNone MouseButton = iota
	MouseButtonLeft
	MouseButtonMiddle
	MouseButtonRight
	MouseButtonWheelUp
	MouseButtonWheelDown
	MouseButtonWheelLeft
	MouseButtonWheelRight
)

type PixelFormat

type PixelFormat int

PixelFormat represents the pixel format of a buffer.

const (
	PixelFormatBGRA PixelFormat = iota
	PixelFormatRGBA
	PixelFormatRGB565
)

type Point

type Point struct {
	X, Y int
}

Point represents a 2D point.

type Rect

type Rect struct {
	X, Y int
	W, H int
}

Rect represents a rectangle with position and size.

func NewRect

func NewRect(x, y, w, h int) Rect

NewRect creates a new rectangle.

func (Rect) Center

func (r Rect) Center() Point

Center returns the center point of the rectangle.

func (Rect) Contains

func (r Rect) Contains(p Point) bool

Contains returns true if the point is inside the rectangle.

func (Rect) ContainsXY

func (r Rect) ContainsXY(x, y int) bool

ContainsXY returns true if the coordinates are inside the rectangle.

func (Rect) Inset

func (r Rect) Inset(top, right, bottom, left int) Rect

Inset returns a rectangle inset by the given amounts.

func (Rect) InsetAll

func (r Rect) InsetAll(amount int) Rect

InsetAll returns a rectangle inset by the same amount on all sides.

func (Rect) Intersection

func (r Rect) Intersection(other Rect) Rect

Intersection returns the intersection of two rectangles.

func (Rect) Intersects

func (r Rect) Intersects(other Rect) bool

Intersects returns true if this rectangle intersects with another.

func (Rect) IsEmpty

func (r Rect) IsEmpty() bool

IsEmpty returns true if the rectangle has zero area.

func (Rect) Position

func (r Rect) Position() Point

Position returns the position of the rectangle as a Point.

func (Rect) Size

func (r Rect) Size() Point

Size returns the size of the rectangle as a Point.

func (Rect) Union

func (r Rect) Union(other Rect) Rect

Union returns the smallest rectangle containing both rectangles.

type TextAlign

type TextAlign int

TextAlign represents text alignment.

const (
	AlignLeft TextAlign = iota
	AlignCenter
	AlignRight
)

type Theme

type Theme struct {
	Background      Color
	Foreground      Color
	Primary         Color
	PrimaryHover    Color
	PrimaryActive   Color
	Secondary       Color
	Border          Color
	BorderFocused   Color
	InputBackground Color
	InputForeground Color
	ButtonText      Color
	SurfaceGlass    Color
	SurfaceRaised   Color
	SurfaceSidebar  Color
	StrokeDivider   Color
	TextSecondary   Color
	TextMuted       Color
	TextDisabled    Color
	ControlHover    Color
	ControlPressed  Color
	AccentSubtle    Color
	Success         Color
	Warning         Color
	Danger          Color
	BorderRadius    int
}

Theme holds the colors used for rendering widgets.

type Widget

type Widget interface {
	Draw(buf *Buffer)
	Bounds() Rect
	SetBounds(r Rect)
	MinSize() Point
	HandleEvent(ev Event) bool
	SetFocused(focused bool)
	IsFocused() bool
	IsDirty() bool
	MarkClean()
	SetVisible(visible bool)
	IsVisible() bool
}

Widget is the interface that all widgets must implement.

Directories

Path Synopsis
backend
drm
input

Jump to

Keyboard shortcuts

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