Skip to content

Instantly share code, notes, and snippets.

@mudream4869
Created June 25, 2024 10:27
Show Gist options
  • Save mudream4869/f663b4dbe5151bdd5c8c98aaa59b785a to your computer and use it in GitHub Desktop.
Save mudream4869/f663b4dbe5151bdd5c8c98aaa59b785a to your computer and use it in GitHub Desktop.
ToolGUI + webview_go
package main
import (
_ "embed"
"fmt"
"log"
"github.com/mudream4869/toolgui/toolgui/component"
"github.com/mudream4869/toolgui/toolgui/executor"
"github.com/mudream4869/toolgui/toolgui/framework"
webview "github.com/webview/webview_go"
)
//go:embed main.go
var code string
func SourceCodePage(s *framework.Session, c *framework.Container) error {
component.Title(c, "Example for ToolGUI")
component.Code(c, code, "go")
return nil
}
func ContentPage(s *framework.Session, c *framework.Container) error {
headerCompCol, headerCodeCol := component.Column2(c, "header_of_rows")
component.Subtitle(headerCompCol, "Component")
component.Subtitle(headerCodeCol, "Code")
titleCompCol, titleCodeCol := component.Column2(c, "show_title")
component.Title(titleCompCol, "Title")
component.Code(titleCodeCol, `component.Title(titleCompCol, "Title")`, "go")
component.Divider(c)
subtitleCompCol, subtitleCodeCol := component.Column2(c, "show_subtitle")
component.Subtitle(subtitleCompCol, "Subtitle")
component.Code(subtitleCodeCol, `component.Subtitle(subtitleCompCol, "Subtitle")`, "go")
component.Divider(c)
textCompCol, textCodeCol := component.Column2(c, "show_text")
component.Text(textCompCol, "Text")
component.Code(textCodeCol, `component.Text(textCompCol, "Text")`, "go")
component.Divider(c)
imageCompCol, imageCodeCol := component.Column2(c, "show_image")
component.ImageByURL(imageCompCol, "https://http.cat/100")
component.Code(imageCodeCol,
`component.ImageByURL(imageCompCol, "https://http.cat/100")`, "go")
component.Divider(c)
jsonCompCol, jsonCodeCol := component.Column2(c, "show_json")
type DemoJSONHeader struct {
Type int
}
type DemoJSON struct {
Header DemoJSONHeader
IntValue int
URL string
IsOk bool
}
component.JSON(jsonCompCol, &DemoJSON{})
component.Code(jsonCodeCol, `type DemoJSONHeader struct {
Type int
}
type DemoJSON struct {
Header DemoJSONHeader
IntValue int
URL string
IsOk bool
}
component.JSON(jsonCompCol, &DemoJSON{})`, "go")
component.Divider(c)
dividerCompCol, dividerCodeCol := component.Column2(c, "show_divider")
component.Divider(dividerCompCol)
component.Code(dividerCodeCol, `component.Divider(dividerCompCol)`, "go")
return nil
}
func LayoutPage(s *framework.Session, c *framework.Container) error {
headerCompCol, headerCodeCol := component.Column2(c, "header_of_rows")
component.Subtitle(headerCompCol, "Component")
component.Subtitle(headerCodeCol, "Code")
colCompCol, colCodeCol := component.Column2(c, "show_col")
cols := component.Column(colCompCol, "cols", 3)
for i, col := range cols {
component.Text(col, fmt.Sprintf("col-%d", i))
}
component.Code(colCodeCol, `cols := component.Column(colCompCol, "cols", 3)
for i, col := range cols {
component.Text(col, fmt.Sprintf("col-%d", i))
}`, "go")
component.Divider(c)
boxCompCol, boxCodeCol := component.Column2(c, "show_box")
box := component.Box(boxCompCol, "box")
component.Text(box, "A box!")
component.Code(boxCodeCol, `box := component.Box(boxCompCol, "box")
component.Text(box, "A box!")`, "go")
return nil
}
func InputPage(s *framework.Session, c *framework.Container) error {
headerCompCol, headerCodeCol := component.Column2(c, "header_of_rows")
component.Subtitle(headerCompCol, "Component")
component.Subtitle(headerCodeCol, "Code")
textboxCompCol, textboxCodeCol := component.Column2(c, "show_textbox")
textboxValue := component.Textbox(s, textboxCompCol, "Textbox")
component.Text(textboxCompCol, "Textbox Value: "+textboxValue)
component.Code(textboxCodeCol, `textboxValue := component.Textbox(s, textboxCompCol, "Textbox")
component.Text(c, "Textbox Value: "+textboxValue)`, "go")
component.Divider(c)
checkboxCompCol, checkboxCodeCol := component.Column2(c, "show_checkbox")
checkboxValue := component.Checkbox(s, checkboxCompCol, "Checkbox")
if checkboxValue {
component.Text(checkboxCompCol, "Checkbox Value: true")
} else {
component.Text(checkboxCompCol, "Checkbox Value: false")
}
component.Code(checkboxCodeCol, `checkboxValue := component.Checkbox(s, checkboxCompCol, "Checkbox")
if checkboxValue {
component.Text(c, "Checkbox Value: true")
} else {
component.Text(c, "Checkbox Value: false")
}`, "go")
component.Divider(c)
buttonCompCol, buttonCodeCol := component.Column2(c, "show_button")
btnClicked := component.Button(s, buttonCompCol, "button")
if btnClicked {
component.Text(buttonCompCol, "Button Value: true")
} else {
component.Text(buttonCompCol, "Button Value: false")
}
component.Code(buttonCodeCol, `btnClicked := component.Button(s, buttonCompCol, "button")
if btnClicked {
component.Text(buttonCompCol, "Button Value: true")
} else {
component.Text(buttonCompCol, "Button Value: false")
}`, "go")
component.Divider(c)
selectCompCol, selectCodeCol := component.Column2(c, "show_select")
selValue := component.Select(s, selectCompCol,
"Select", []string{"Value1", "Value2"})
component.Text(selectCompCol, "Select Value: "+selValue)
component.Code(selectCodeCol, `selValue := component.Select(s, selectCompCol,
"Select", []string{"Value1", "Value2"})
component.Text(selectCodeCol, "Select Value: "+selValue)`, "go")
return nil
}
func main() {
e := executor.NewWebExecutor()
e.AddPage("content", "Content", ContentPage)
e.AddPage("layout", "Layout", LayoutPage)
e.AddPage("input", "Input", InputPage)
e.AddPage("code", "Source Code", SourceCodePage)
log.Println("Starting service...")
go e.StartService(":3000")
w := webview.New(false)
defer w.Destroy()
w.SetSize(1280, 640, webview.HintNone)
w.Navigate("http://localhost:3000/")
w.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment