complete rework of the objects, functioning at correct size
This commit is contained in:
@@ -9,54 +9,48 @@ import (
|
||||
|
||||
// obj_ComputerName returns a canvas object containing the computer name text
|
||||
func obj_ComputerName() fyne.CanvasObject {
|
||||
// Get the computer name from the utils package
|
||||
computername := utils.GetComputerName()
|
||||
|
||||
// Create a new text label with the computer name
|
||||
object := utils.NewTextButton(computername)
|
||||
object.TextSize = 100
|
||||
object.TextStyle = fyne.TextStyle{Bold: true}
|
||||
object.Alignment = fyne.TextAlignCenter
|
||||
object := utils.NewClickableText(computername)
|
||||
object.Text.TextSize = 100
|
||||
|
||||
// Return the label as a canvas object
|
||||
return object
|
||||
}
|
||||
|
||||
// obj_IPAddress returns a slice of canvas objects containing the IP addresses of the computer
|
||||
func obj_IPAddress() []fyne.CanvasObject {
|
||||
objects := []fyne.CanvasObject{}
|
||||
// ipaddress := utils.GetIPAddress()
|
||||
ipaddress := utils.GetIPAddressObject()
|
||||
|
||||
// Get the IP addresses of the computer
|
||||
ipaddress := utils.GetIPAddress()
|
||||
InterfaceObjects := []fyne.CanvasObject{}
|
||||
|
||||
// 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 {
|
||||
// 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")
|
||||
interfaceName := ip["name"]
|
||||
ip := ip["ip"]
|
||||
object := container.NewHBox()
|
||||
|
||||
// Append the label to the slice of labels
|
||||
objects = append(objects, object)
|
||||
ClickableName := utils.NewClickableText(interfaceName)
|
||||
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 objects
|
||||
return InterfaceObjects
|
||||
}
|
||||
|
||||
// allObjects returns a slice of canvas objects containing both the computer name and IP addresses
|
||||
func allObjects() []fyne.CanvasObject {
|
||||
return []fyne.CanvasObject{
|
||||
// Get the computer name canvas object
|
||||
obj_ComputerName(),
|
||||
// Create a VBox container containing the IP addresses
|
||||
container.NewVBox(
|
||||
obj_IPAddress()...,
|
||||
),
|
||||
@@ -70,7 +64,7 @@ func Container() fyne.CanvasObject {
|
||||
|
||||
// Create a new container using the vertical layout and the canvas objects
|
||||
cont := container.New(verticalLayout, allObjects()...)
|
||||
cont.Resize(fyne.NewSize(200, 200))
|
||||
// cont.Resize(fyne.NewSize(200, 200))
|
||||
|
||||
// Return the container
|
||||
return cont
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"time"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
@@ -13,31 +15,49 @@ func setClipboard(text string) {
|
||||
fyne.CurrentApp().Driver().AllWindows()[0].Clipboard().SetContent(text)
|
||||
}
|
||||
|
||||
type TextButton struct {
|
||||
*canvas.Text
|
||||
type ClickableText struct {
|
||||
widget.BaseWidget
|
||||
tapBG *canvas.Rectangle
|
||||
Text *canvas.Text
|
||||
}
|
||||
|
||||
func NewTextButton(text string) *TextButton {
|
||||
return &TextButton{
|
||||
Text: canvas.NewText(text, theme.Color("foreground")),
|
||||
func NewClickableText(text string) *ClickableText {
|
||||
object := &ClickableText{
|
||||
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 {
|
||||
return widget.NewSimpleRenderer(tb.Text)
|
||||
func (ct *ClickableText) CreateRenderer() fyne.WidgetRenderer {
|
||||
|
||||
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) {
|
||||
setClipboard(tb.Text.Text)
|
||||
func (ct *ClickableText) Tapped(_ *fyne.PointEvent) {
|
||||
setClipboard(ct.Text.Text)
|
||||
|
||||
// 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() {
|
||||
<-time.After(time.Second)
|
||||
tb.Color = theme.Color("foreground")
|
||||
tb.Refresh()
|
||||
<-time.After(time.Millisecond * 400)
|
||||
ct.tapBG.FillColor = theme.Color("background")
|
||||
ct.Refresh()
|
||||
}()
|
||||
|
||||
tb.Refresh()
|
||||
ct.Refresh()
|
||||
}
|
||||
|
||||
@@ -49,6 +49,43 @@ func GetIPAddress() []string {
|
||||
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 {
|
||||
var ipaddress []string
|
||||
for _, addr := range addrs {
|
||||
|
||||
Reference in New Issue
Block a user