Files
kryon/go/kryui/runtime.go
T
waozi 4a865f45fd theme: default to the system theme source on every platform
GetDefaultPlatformThemeSource() no longer falls back to THEME_SOURCE_APP on Android; GetCurrentThemeColor() already falls back to the app palette when no system palette is readable, so SYSTEM is safe everywhere. Also expose SetThemeSource/SetThemeMode (plus ThemeSource/ThemeMode consts) in the go/kryui bindings and the Runtime parity interface.
2026-08-19 12:09:07 -03:00

244 lines
8.6 KiB
Go

package kryui
import "fmt"
// AppConfig is the window configuration emitted by k2g.
type AppConfig struct {
Title string
Width, Height int
FPS int
Flags uint // window flags applied before InitWindow (0 = none)
MinWidth int // minimum window size (0 = no limit)
MinHeight int
}
// Runtime is the stable Go target used by generated Kry programs. It keeps
// window and nested-widget state behind Go methods rather than exposing cgo.
type Runtime interface {
Close()
WindowShouldClose() bool
BeginDrawing()
EndDrawing()
ClearBackground(Color)
Background(Color)
Text(string, int32, int32, int32, Color)
TextFormat(string, ...any) string
ScaleUIPx(int32) int32
GetScreenWidth() int32
GetScreenHeight() int32
GetThemeBackground() Color
GetThemeText() Color
GetThemeIcon() Color
NewVector2(any, any) Vector2
DrawCircleV(Vector2, any, Color)
DrawRing(Vector2, any, any, any, any, int32, Color)
Rect(int32, int32, int32, int32, Color, ...Color)
RectGradientH(int32, int32, int32, int32, Color, Color)
Line(int32, int32, int32, int32, Color)
Scroll(int32, int32, int32, int32, int32, *int32)
EndScroll()
Button(ButtonProps) bool
TabBar(Rectangle, []string, *int32, *int32) int32
Progress(Rectangle, int32, int32, int32, string)
Checkbox(int32, int32, int32, string, *int32) bool
Dropdown(id, x, y, w, h int32, options any, rest ...any) bool
BeginUI(UIKey)
EndUI()
Column(ColumnProps)
Row(ColumnProps)
Stack(ColumnProps)
End()
TextField(TextFieldProps)
Key(text string) UIKey
Fade(Color, float32) Color
GetThemeSurface() Color
GetThemeButton() Color
GetThemeButtonHover() Color
GetThemeLink() Color
TextInRect(text string, rect Rectangle, fontSize int32, color Color)
TextLines(lines any, count int32, x int32, y *int32, font, lineH int32, color Color)
Bevel(x, y, w, h int32, light, dark Color)
IconTexture(id, x, y, size int32, iconType int32, tint Color)
Picture(props PictureProps)
Paragraph(spec UIParagraphSpec, x int32, y *int32)
IconButton(props IconButtonProps) bool
Href(props HrefProps) bool
Slider(id, x, y, w int32, label string, min, max int32, value *int32, rest ...any) bool
Toggle(id, x, y, w, h int32, value *int32, offLabel, onLabel string) bool
Modal(title, message, cancelBtn, confirmBtn string) int
TitleBar(title string, height int32)
BottomNav(props BottomNavProps)
TopNav(props TopNavProps)
Toolbar(props ToolbarProps)
GenericButton(id, x, y, w, h int32, label string, style UIButtonStyle, disabled int32, hover *int32) bool
TextButton(id, x, y int32, label string, hover *int32) bool
LocaleDropdown(id, x, y, w, h int32, selected *int32) bool
VerticalSlider(id, x, y, h, min, max int32, value *int32) bool
CanvasGrid(bounds Rectangle, step int32, color Color)
SelectableText(value string, x, y, fontSize int32, color Color)
ShowUIToast(message string)
ShowUIToastFor(message string, seconds float64)
TextInputControl(props TextInputProps) bool
ReadonlyTextBox(props ReadonlyTextBoxProps)
TextArea(props TextAreaProps) bool
Radio(props RadioButtonProps) int32
Spinbox(props SpinboxProps) bool
Combobox(props ComboboxProps) bool
LabelFrame(props LabelFrameProps)
Notebook(props NotebookProps) int32
PanedView(props PanedViewProps) int32
Collapsible(props CollapsibleProps) int32
ListBox(props ListBoxProps) int32
SourceView(props SourceViewProps) int32
TableView(props TableViewProps) int32
MessageDialog(props MessageDialogProps) int32
ConfirmDialog(props ConfirmDialogProps) int32
PromptDialog(props PromptDialogProps) int32
BeginUICanvas(canvas UICanvas) UICanvasResult
EndUICanvas(canvas UICanvas)
BeginUIFrameBox(bounds Rectangle, padX, padY, gap int32) UIFrame
UIFramePack(frame *UIFrame, side UISide, size int32) Rectangle
UIGridCell(grid UIGrid, row, col, rowSpan, colSpan int32) Rectangle
UIPlace(parent Rectangle, x, y, w, h int32) Rectangle
SetCurrentTheme(themeID int32, darkMode int32)
SetThemeDarkMode(dark int32)
SetThemeStyle(style ThemeStyle)
SetThemeSource(source ThemeSource)
SetThemeMode(mode ThemeMode)
}
type runtime struct {
scrolls []runtimeScroll
}
type runtimeScroll struct {
bounds Rectangle
view ScrollView
}
func New(config AppConfig) Runtime {
if config.Flags != 0 {
SetConfigFlags(config.Flags)
}
InitWindow(int32(config.Width), int32(config.Height), config.Title)
if config.MinWidth > 0 || config.MinHeight > 0 {
SetWindowMinSize(int32(config.MinWidth), int32(config.MinHeight))
}
if config.FPS > 0 {
SetTargetFPS(int32(config.FPS))
}
return &runtime{}
}
func (r *runtime) Close() { CloseWindow() }
func (r *runtime) WindowShouldClose() bool { return WindowShouldClose() }
func (r *runtime) BeginDrawing() {
BeginDrawing()
w, h := GetScreenWidth(), GetScreenHeight()
dpi := GetWindowScaleDPI().X
if dpi <= 0 {
dpi = 1
}
BeginUIFrame(w, h, dpi)
}
func (r *runtime) EndDrawing() {
EndUIFrame()
EndDrawing()
}
func (r *runtime) ClearBackground(c Color) { ClearBackground(c) }
func (r *runtime) Background(c Color) { Background(c) }
func (r *runtime) Text(s string, x, y, size int32, c Color) { Text(s, x, y, size, c) }
func (r *runtime) TextFormat(format string, args ...any) string { return fmt.Sprintf(format, args...) }
func (r *runtime) ScaleUIPx(px int32) int32 { return ScaleUIPx(px) }
func (r *runtime) GetThemeBackground() Color { return GetThemeBackground() }
func (r *runtime) GetThemeText() Color { return GetThemeText() }
func (r *runtime) GetThemeIcon() Color { return GetThemeIcon() }
func runtimeFloat(v any) float32 {
switch n := v.(type) {
case int:
return float32(n)
case int32:
return float32(n)
case int64:
return float32(n)
case float32:
return n
case float64:
return float32(n)
default:
return 0
}
}
func (r *runtime) NewVector2(x, y any) Vector2 {
return NewVector2(runtimeFloat(x), runtimeFloat(y))
}
func (r *runtime) DrawCircleV(center Vector2, radius any, c Color) {
DrawCircleV(center, runtimeFloat(radius), c)
}
func (r *runtime) DrawRing(center Vector2, inner, outer, start, end any, segments int32, c Color) {
DrawRing(center, runtimeFloat(inner), runtimeFloat(outer), runtimeFloat(start), runtimeFloat(end), segments, c)
}
func (r *runtime) Scroll(x, y, w, h, contentHeight int32, offset *int32) {
bounds := NewRectangle(float32(x), float32(y), float32(w), float32(h))
goOffset := int(*offset)
view := BeginScrollContainer(bounds, int(contentHeight), &goOffset, ScrollOptions{})
*offset = int32(goOffset)
r.scrolls = append(r.scrolls, runtimeScroll{bounds: bounds, view: view})
}
func (r *runtime) EndScroll() {
if len(r.scrolls) == 0 {
return
}
i := len(r.scrolls) - 1
s := r.scrolls[i]
r.scrolls = r.scrolls[:i]
EndScrollContainer(s.bounds, s.view)
}
func (r *runtime) Button(props ButtonProps) bool { return Button(props) }
func (r *runtime) BeginUI(key UIKey) { BeginUI(key) }
func (r *runtime) EndUI() { EndUI() }
func (r *runtime) Column(props ColumnProps) { Column(props) }
func (r *runtime) End() { End() }
func (r *runtime) TextField(props TextFieldProps) { declareTextField(props) }
func (r *runtime) GetScreenWidth() int32 { return GetScreenWidth() }
func (r *runtime) GetScreenHeight() int32 { return GetScreenHeight() }
func (r *runtime) Rect(x, y, w, h int32, fill Color, border ...Color) {
if len(border) > 0 {
Rect(x, y, w, h, fill, border[0])
return
}
DrawRectangleRec(NewRectangle(float32(x), float32(y), float32(w), float32(h)), fill)
}
func (r *runtime) RectGradientH(x, y, w, h int32, left, right Color) {
DrawRectangleGradientH(x, y, w, h, left, right)
}
func (r *runtime) Line(x1, y1, x2, y2 int32, c Color) { DrawLine(x1, y1, x2, y2, c) }
func (r *runtime) TabBar(bounds Rectangle, labels []string, selected *int32, scrollOffset *int32) int32 {
return TabBar(bounds, labels, selected, scrollOffset)
}
func (r *runtime) Progress(bounds Rectangle, min, max, value int32, label string) {
Progress(ProgressBarProps{Bounds: bounds, Min: min, Max: max, Value: value, Label: label})
}
func (r *runtime) Checkbox(id, x, y int32, label string, value *int32) bool {
on := *value != 0
changed := DrawUICheckboxToggle(x, y, label, &on)
if on {
*value = 1
} else {
*value = 0
}
return changed
}
func (r *runtime) Dropdown(id, x, y, w, h int32, options any, rest ...any) bool {
var selected *int32
/* the C call carries an option count the Go side derives itself */
for _, v := range rest {
if p, ok := v.(*int32); ok {
selected = p
}
}
return DrawUIDropdown(id, x, y, w, h, labelsOf(options), selected)
}