diff --git a/src/computerinfo/computerinfo.go b/src/computerinfo/computerinfo.go index 3f3384a..48ff72e 100644 --- a/src/computerinfo/computerinfo.go +++ b/src/computerinfo/computerinfo.go @@ -2,10 +2,8 @@ package computerinfo import ( "fyne.io/fyne/v2" - "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" fyneLayout "fyne.io/fyne/v2/layout" - "fyne.io/fyne/v2/theme" "github.com/ryanehamil/computerinfo/src/utils" ) @@ -15,33 +13,42 @@ func obj_ComputerName() fyne.CanvasObject { computername := utils.GetComputerName() // Create a new text label with the computer name - label := canvas.NewText(computername, theme.TextColor()) - label.TextSize = 100 - label.TextStyle = fyne.TextStyle{Bold: true} - label.Alignment = fyne.TextAlignCenter + object := utils.NewTextButton(computername) + object.TextSize = 100 + object.TextStyle = fyne.TextStyle{Bold: true} + object.Alignment = fyne.TextAlignCenter // 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 func obj_IPAddress() []fyne.CanvasObject { - labels := []fyne.CanvasObject{} + objects := []fyne.CanvasObject{} // Get the IP addresses of the computer ipaddress := utils.GetIPAddress() + // Get only the first IP address + ipaddress = ipaddress[:1] + // Loop over the IP addresses and create a label for each one for _, ip := range ipaddress { - label := canvas.NewText(ip, theme.TextColor()) - label.TextSize = 20 - label.TextStyle = fyne.TextStyle{Bold: true} - label.Alignment = fyne.TextAlignCenter - labels = append(labels, label) + // Create a new text label with the IP address + object := utils.NewTextButton(ip) + // object := canvas.NewText(ip, theme.Color("foreground")) + object.TextSize = 20 + 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 labels + return objects } // allObjects returns a slice of canvas objects containing both the computer name and IP addresses diff --git a/src/utils/customwidgets.go b/src/utils/customwidgets.go new file mode 100644 index 0000000..63ef5bf --- /dev/null +++ b/src/utils/customwidgets.go @@ -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() +}