partial work on clickable canvas, pretty buggy

This commit is contained in:
2024-07-17 09:31:25 -05:00
parent 1e108f1443
commit 920f61d3a2
2 changed files with 64 additions and 14 deletions

View File

@@ -2,10 +2,8 @@ package computerinfo
import ( import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
fyneLayout "fyne.io/fyne/v2/layout" fyneLayout "fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"github.com/ryanehamil/computerinfo/src/utils" "github.com/ryanehamil/computerinfo/src/utils"
) )
@@ -15,33 +13,42 @@ func obj_ComputerName() fyne.CanvasObject {
computername := utils.GetComputerName() computername := utils.GetComputerName()
// Create a new text label with the computer name // Create a new text label with the computer name
label := canvas.NewText(computername, theme.TextColor()) object := utils.NewTextButton(computername)
label.TextSize = 100 object.TextSize = 100
label.TextStyle = fyne.TextStyle{Bold: true} object.TextStyle = fyne.TextStyle{Bold: true}
label.Alignment = fyne.TextAlignCenter object.Alignment = fyne.TextAlignCenter
// Return the label as a canvas object // Return the label as a canvas object
return label return object
} }
// obj_IPAddress returns a slice of canvas objects containing the IP addresses of the computer // obj_IPAddress returns a slice of canvas objects containing the IP addresses of the computer
func obj_IPAddress() []fyne.CanvasObject { func obj_IPAddress() []fyne.CanvasObject {
labels := []fyne.CanvasObject{} objects := []fyne.CanvasObject{}
// Get the IP addresses of the computer // Get the IP addresses of the computer
ipaddress := utils.GetIPAddress() ipaddress := utils.GetIPAddress()
// Get only the first IP address
ipaddress = ipaddress[:1]
// Loop over the IP addresses and create a label for each one // Loop over the IP addresses and create a label for each one
for _, ip := range ipaddress { for _, ip := range ipaddress {
label := canvas.NewText(ip, theme.TextColor()) // Create a new text label with the IP address
label.TextSize = 20 object := utils.NewTextButton(ip)
label.TextStyle = fyne.TextStyle{Bold: true} // object := canvas.NewText(ip, theme.Color("foreground"))
label.Alignment = fyne.TextAlignCenter object.TextSize = 20
labels = append(labels, label) object.TextStyle = fyne.TextStyle{Bold: true}
object.Alignment = fyne.TextAlignCenter
// object.Color = theme.Color("foreground")
// Append the label to the slice of labels
objects = append(objects, object)
} }
// Return the slice of labels as canvas objects // Return the slice of labels as canvas objects
return labels return objects
} }
// allObjects returns a slice of canvas objects containing both the computer name and IP addresses // allObjects returns a slice of canvas objects containing both the computer name and IP addresses

View File

@@ -0,0 +1,43 @@
package utils
import (
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func setClipboard(text string) {
fyne.CurrentApp().Driver().AllWindows()[0].Clipboard().SetContent(text)
}
type TextButton struct {
*canvas.Text
}
func NewTextButton(text string) *TextButton {
return &TextButton{
Text: canvas.NewText(text, theme.Color("foreground")),
}
}
func (tb *TextButton) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(tb.Text)
}
func (tb *TextButton) Tapped(_ *fyne.PointEvent) {
setClipboard(tb.Text.Text)
// Set the text color to green when tapped, and reset it after 1 second
tb.Color = theme.Color("success")
go func() {
<-time.After(time.Second)
tb.Color = theme.Color("foreground")
tb.Refresh()
}()
tb.Refresh()
}