complete rework of the objects, functioning at correct size

This commit is contained in:
2024-07-23 08:55:40 -05:00
parent fa897496a2
commit f00b134228
3 changed files with 93 additions and 42 deletions

View File

@@ -9,54 +9,48 @@ import (
// obj_ComputerName returns a canvas object containing the computer name text // obj_ComputerName returns a canvas object containing the computer name text
func obj_ComputerName() fyne.CanvasObject { func obj_ComputerName() fyne.CanvasObject {
// Get the computer name from the utils package
computername := utils.GetComputerName() computername := utils.GetComputerName()
// Create a new text label with the computer name object := utils.NewClickableText(computername)
object := utils.NewTextButton(computername) object.Text.TextSize = 100
object.TextSize = 100
object.TextStyle = fyne.TextStyle{Bold: true}
object.Alignment = fyne.TextAlignCenter
// Return the label as a canvas object
return object 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 {
objects := []fyne.CanvasObject{} // ipaddress := utils.GetIPAddress()
ipaddress := utils.GetIPAddressObject()
// Get the IP addresses of the computer InterfaceObjects := []fyne.CanvasObject{}
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 { for _, ip := range ipaddress {
// Create a new text label with the IP address interfaceName := ip["name"]
object := utils.NewTextButton(ip) ip := ip["ip"]
// object := canvas.NewText(ip, theme.Color("foreground")) object := container.NewHBox()
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 ClickableName := utils.NewClickableText(interfaceName)
objects = append(objects, object) ClickableName.Text.TextSize = 20
object.Add(ClickableName)
ClickableIP := utils.NewClickableText(ip)
ClickableIP.Text.TextSize = 20
object.Add(ClickableIP)
wrapper := container.NewCenter(object)
InterfaceObjects = append(InterfaceObjects, wrapper)
} }
// Return the slice of labels as canvas objects return InterfaceObjects
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
func allObjects() []fyne.CanvasObject { func allObjects() []fyne.CanvasObject {
return []fyne.CanvasObject{ return []fyne.CanvasObject{
// Get the computer name canvas object
obj_ComputerName(), obj_ComputerName(),
// Create a VBox container containing the IP addresses
container.NewVBox( container.NewVBox(
obj_IPAddress()..., obj_IPAddress()...,
), ),
@@ -70,7 +64,7 @@ func Container() fyne.CanvasObject {
// Create a new container using the vertical layout and the canvas objects // Create a new container using the vertical layout and the canvas objects
cont := container.New(verticalLayout, allObjects()...) cont := container.New(verticalLayout, allObjects()...)
cont.Resize(fyne.NewSize(200, 200)) // cont.Resize(fyne.NewSize(200, 200))
// Return the container // Return the container
return cont return cont

View File

@@ -1,10 +1,12 @@
package utils package utils
import ( import (
"image/color"
"time" "time"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"
) )
@@ -13,31 +15,49 @@ func setClipboard(text string) {
fyne.CurrentApp().Driver().AllWindows()[0].Clipboard().SetContent(text) fyne.CurrentApp().Driver().AllWindows()[0].Clipboard().SetContent(text)
} }
type TextButton struct { type ClickableText struct {
*canvas.Text widget.BaseWidget
tapBG *canvas.Rectangle
Text *canvas.Text
} }
func NewTextButton(text string) *TextButton { func NewClickableText(text string) *ClickableText {
return &TextButton{ object := &ClickableText{
Text: canvas.NewText(text, theme.Color("foreground")), Text: canvas.NewText(text, Theme{}.Color(fyne.ThemeColorName("foreground"), fyne.CurrentApp().Settings().ThemeVariant())),
tapBG: canvas.NewRectangle(Theme{}.Color(fyne.ThemeColorName("background"), fyne.CurrentApp().Settings().ThemeVariant())),
} }
object.Text.TextStyle.Bold = true
object.ExtendBaseWidget(object)
return object
} }
func (tb *TextButton) CreateRenderer() fyne.WidgetRenderer { func (ct *ClickableText) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(tb.Text)
object := container.NewCenter()
ct.tapBG.SetMinSize(ct.Text.MinSize().Add(fyne.NewSize(10, 2)))
ct.tapBG.CornerRadius = 10
object.Add(ct.tapBG)
object.Add(ct.Text)
return widget.NewSimpleRenderer(object)
} }
func (tb *TextButton) Tapped(_ *fyne.PointEvent) { func (ct *ClickableText) Tapped(_ *fyne.PointEvent) {
setClipboard(tb.Text.Text) setClipboard(ct.Text.Text)
// Set the text color to green when tapped, and reset it after 1 second // Set the text color to green when tapped, and reset it after 1 second
tb.Color = theme.Color("success") ct.tapBG.FillColor = color.RGBA{0, 120, 20, 200}
go func() { go func() {
<-time.After(time.Second) <-time.After(time.Millisecond * 400)
tb.Color = theme.Color("foreground") ct.tapBG.FillColor = theme.Color("background")
tb.Refresh() ct.Refresh()
}() }()
tb.Refresh() ct.Refresh()
} }

View File

@@ -49,6 +49,43 @@ func GetIPAddress() []string {
return addrs return addrs
} }
// Now lets make a function to return them as an object with separate name and IP address fields
func GetIPAddressObject() []map[string]string {
// Get all interfaces from net package
netInterfaces, _ := net.Interfaces()
// Get all interface names and addresses from each interface
var addrs []map[string]string
for _, netInterface := range netInterfaces {
// get the interface name
interfaceName := netInterface.Name
flags := netInterface.Flags.String()
// get the interface adapter
address, _ := netInterface.Addrs()
// check if the address is ipv4
for _, addr := range address {
if ipnet, ok := addr.(*net.IPNet); ok &&
!ipnet.IP.IsLoopback() &&
// check if up flag is set
strings.Contains(flags, "up") &&
!strings.Contains(ipnet.IP.String(), "169.254") &&
!strings.Contains(interfaceName, "VirtualBox") &&
!strings.Contains(interfaceName, "Virtual") {
if ipnet.IP.To4() != nil {
addrs = append(addrs, map[string]string{"name": interfaceName, "ip": ipnet.IP.String()})
}
}
}
}
return addrs
}
func ConvertIPAddress(addrs []net.Addr) []string { func ConvertIPAddress(addrs []net.Addr) []string {
var ipaddress []string var ipaddress []string
for _, addr := range addrs { for _, addr := range addrs {