Skip to content

Library Usage Chrome

Chris Watson edited this page Nov 21, 2024 · 2 revisions

Window Chrome

Window chrome adds decorative elements to your code screenshots, making them look like they're being displayed in a real application window. Goshot provides several window chrome styles and extensive customization options.

Available Chrome Styles

  1. macOS Style
  2. Windows 11 Style
  3. GNOME Style

Basic Usage

import (
    "github.com/watzon/goshot/pkg/chrome"
    "github.com/watzon/goshot/pkg/render"
)

// Create a canvas with macOS-style chrome
canvas := render.NewCanvas().
    SetChrome(chrome.NewMacChrome(
        chrome.WithTitle("main.go"),
        chrome.WithThemeByName("sequoia", chrome.ThemeVariantDark),
    ))

Chrome Options

All chrome styles support these common configuration options:

chrome.WithTitle("my-file.go")                    // Set window title
chrome.WithThemeByName("theme", ThemeVariantDark) // Set theme by name and variant
chrome.WithVariant(ThemeVariantLight)             // Change theme variant
chrome.WithTitleBar(true)                         // Show/hide title bar
chrome.WithCornerRadius(10)                       // Set window corner radius

macOS Style

The macOS style chrome mimics the appearance of macOS windows, including the iconic traffic light controls.

chrome := chrome.NewMacChrome(
    chrome.WithTitle("main.go"),
    chrome.WithThemeByName("sequoia", chrome.ThemeVariantDark),
    chrome.WithCornerRadius(6),
)

Available macOS Themes

  • sequoia - Modern dark theme inspired by macOS Sonoma
  • monterey - Classic macOS Monterey look
  • big-sur - macOS Big Sur style
  • catalina - Classic macOS Catalina look

Windows 11 Style

The Windows 11 style chrome replicates the modern Windows 11 window appearance.

chrome := chrome.NewWindows11Chrome(
    chrome.WithTitle("main.go"),
    chrome.WithThemeByName("default", chrome.ThemeVariantDark),
    chrome.WithCornerRadius(8),
)

GNOME Style

The GNOME style chrome provides both Adwaita and Breeze themes, matching popular Linux desktop environments.

// Create with Adwaita style
chrome := chrome.NewGNOMEChrome(
    chrome.GNOMEStyleAdwaita,
    chrome.WithTitle("main.go"),
    chrome.WithThemeByName("adwaita", chrome.ThemeVariantDark),
)

// Or with Breeze style
chrome := chrome.NewGNOMEChrome(
    chrome.GNOMEStyleBreeze,
    chrome.WithTitle("main.go"),
    chrome.WithThemeByName("breeze", chrome.ThemeVariantDark),
)

Theme Customization

You can create custom themes with detailed control over visual properties:

theme := chrome.Theme{
    Type:    chrome.ThemeTypeMac,  // or ThemeTypeWindows, ThemeTypeGNOME
    Variant: chrome.ThemeVariantDark,
    Name:    "custom",
    Properties: chrome.ThemeProperties{
        // Basic properties
        TitleFont:         "SF Pro",
        TitleBackground:   color.RGBA{40, 40, 40, 255},
        TitleText:         color.RGBA{200, 200, 200, 255},
        ControlsColor:     color.RGBA{255, 255, 255, 255},
        ContentBackground: color.RGBA{30, 30, 30, 255},
        TextColor:         color.RGBA{200, 200, 200, 255},

        // Extended properties
        AccentColor:        color.RGBA{0, 122, 255, 255},
        BorderColor:        color.RGBA{60, 60, 60, 255},
        InactiveTitleBg:    color.RGBA{50, 50, 50, 255},
        InactiveTitleText:  color.RGBA{150, 150, 150, 255},
        ButtonHoverColor:   color.RGBA{70, 70, 70, 255},
        ButtonPressedColor: color.RGBA{90, 90, 90, 255},
        CornerRadius:       6.0,
        BorderWidth:        1.0,
    },
}

// Apply the theme
chrome := chrome.NewMacChrome(
    chrome.WithTitle("main.go"),
    chrome.WithTheme(theme),
)

Theme Registry

You can register custom themes for reuse:

// Register a custom theme
chrome.DefaultRegistry.RegisterTheme(
    chrome.ThemeTypeMac,
    "custom",
    chrome.ThemeVariantDark,
    theme,
)

// Use the registered theme
chrome := chrome.NewMacChrome(
    chrome.WithThemeByName("custom", chrome.ThemeVariantDark),
)

Creating Custom Chrome Styles

You can create your own window chrome styles by implementing the Chrome interface:

type Chrome interface {
    SetTitle(title string) Chrome
    SetCornerRadius(radius float64) Chrome
    SetTitleBar(enabled bool) Chrome
    SetTheme(theme Theme) Chrome
    SetThemeByName(name string, variant ThemeVariant) Chrome
    SetVariant(variant ThemeVariant) Chrome
    GetCurrentThemeName() string
    GetCurrentVariant() ThemeVariant
    CurrentTheme() Theme
    Render(content image.Image) (image.Image, error)
    MinimumSize() (width, height int)
    ContentInsets() (top, right, bottom, left int)
}

Best Practices

  1. Theme Selection

    • Use system-appropriate themes (e.g., Adwaita on Linux)
    • Match theme variant with system dark/light mode
    • Consider accessibility when customizing colors
  2. Performance

    • Chrome objects can be reused across multiple renderings
    • Register custom themes if you'll use them multiple times
    • Use the theme registry to manage theme variants
  3. Customization

    • Start with an existing theme as a base
    • Test themes with different content backgrounds
    • Consider inactive window states
    • Maintain consistent corner radii across UI elements