Skip to content

Commit

Permalink
ref: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leukipp committed Apr 7, 2024
1 parent a0aaeee commit 11a7a38
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
20 changes: 17 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"fmt"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -35,6 +36,13 @@ func IsType(a interface{}, b interface{}) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}

func IsInt(s string) bool {
s = strings.TrimPrefix(s, "-")
re := fmt.Sprintf("\\d{%d}", len(s))
match, err := regexp.MatchString(re, s)
return match && err == nil
}

func IsZero(items []uint) bool {
mask := uint(0)
for _, s := range items {
Expand All @@ -54,14 +62,20 @@ func IsInList(item string, items []string) bool {

func IsInsideRect(p *Pointer, r xrect.Rect) bool {
x, y, w, h := r.Pieces()

// Check if x and y are inside rectangle
xInRect := int(p.X) >= x && int(p.X) <= (x+w)
yInRect := int(p.Y) >= y && int(p.Y) <= (y+h)

return xInRect && yInRect
}

func ReverseList[T any](items []T) []T {
for i, j := 0, len(items)-1; i < j; {
items[i], items[j] = items[j], items[i]
i++
j--
}
return items
}

func VersionToInt(version string) int {

// Remove non-numeric characters
Expand Down
14 changes: 8 additions & 6 deletions input/traybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ func messages(tr *desktop.Tracker) {
// Request owner of shared session
conn, err := dbus.SessionBus()
if err != nil {
log.Warn("Error initializing tray events ", err)
log.Warn("Error initializing tray owner ", err)
return
}
name := fmt.Sprintf("org.kde.StatusNotifierItem-%d-1", os.Getpid())
conn.BusObject().Call("org.freedesktop.DBus.GetNameOwner", 0, name).Store(&destination)

if len(destination) == 0 {
log.Warn("Error requesting tray owner ", name)
return
Expand All @@ -140,22 +139,25 @@ func messages(tr *desktop.Tracker) {
// Monitor method calls in separate session
conn, err = dbus.ConnectSessionBus()
if err != nil {
log.Warn("Error monitoring tray messages ", err)
log.Warn("Error initializing tray methods ", err)
return
}
conn.BusObject().Call("org.freedesktop.DBus.Monitoring.BecomeMonitor", 0, []string{
call := conn.BusObject().Call("org.freedesktop.DBus.Monitoring.BecomeMonitor", 0, []string{
fmt.Sprintf("type='method_call',path='/StatusNotifierMenu',interface='com.canonical.dbusmenu',destination='%s'", destination),
fmt.Sprintf("type='method_call',path='/StatusNotifierItem',interface='org.kde.StatusNotifierItem',destination='%s'", destination),
}, uint(0))
if call.Err != nil {
log.Warn("Error monitoring tray methods ", call.Err)
return
}

// Listen to channel events
ch := make(chan *dbus.Message, 10)
conn.Eavesdrop(ch)

// Listen to channel events
go func() {
var iface string
var method string

for msg := range ch {
msg.Headers[2].Store(&iface)
msg.Headers[3].Store(&method)
Expand Down
4 changes: 2 additions & 2 deletions store/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type Info struct {
}

type Location struct {
DeskNum uint // Client workspace desktop number
ScreenNum uint // Client workspace screen number
DeskNum uint // Workspace desktop number
ScreenNum uint // Workspace screen number
}

type Dimensions struct {
Expand Down
23 changes: 12 additions & 11 deletions store/corner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

type Corner struct {
Name string // Corner name used in config
Active bool // Mouse pointer is in this corner
ScreenNum uint // Screen number the corner is located
Area xrect.Rect // Rectangle area of the corner section
Active bool // Mouse pointer is in this corner
}

func CreateCorner(name string, screenNum uint, x int, y int, w int, h int) *Corner {
Expand All @@ -25,22 +25,23 @@ func CreateCorner(name string, screenNum uint, x int, y int, w int, h int) *Corn
func CreateCorners() []*Corner {
corners := []*Corner{}

for i, s := range Displays.Screens {
xw, yw, ww, hw := s.Pieces()
for i, screen := range Displays.Screens {
screenNum := uint(i)
xw, yw, ww, hw := screen.Pieces()

// Corner dimensions
wcs, hcs := common.Config.EdgeCornerSize, common.Config.EdgeCornerSize
wcl, hcl := common.Config.EdgeCenterSize, common.Config.EdgeCenterSize

// Define corners and positions
tl := CreateCorner("top_left", uint(i), xw, yw, wcs, hcs)
tc := CreateCorner("top_center", uint(i), (xw+ww)/2-wcl/2, yw, wcl, hcs)
tr := CreateCorner("top_right", uint(i), xw+ww-wcs, yw, wcs, hcs)
cr := CreateCorner("center_right", uint(i), xw+ww-wcs, (yw+hw)/2-hcl/2, wcs, hcl)
br := CreateCorner("bottom_right", uint(i), xw+ww-wcs, yw+hw-hcs, wcs, hcs)
bc := CreateCorner("bottom_center", uint(i), (xw+ww)/2-wcl/2, yw+hw-hcs, wcl, hcl)
bl := CreateCorner("bottom_left", uint(i), xw, yw+hw-hcs, wcs, hcs)
cl := CreateCorner("center_left", uint(i), xw, (yw+hw)/2-hcl/2, wcs, hcl)
tl := CreateCorner("top_left", screenNum, xw, yw, wcs, hcs)
tc := CreateCorner("top_center", screenNum, (xw+ww)/2-wcl/2, yw, wcl, hcs)
tr := CreateCorner("top_right", screenNum, xw+ww-wcs, yw, wcs, hcs)
cr := CreateCorner("center_right", screenNum, xw+ww-wcs, (yw+hw)/2-hcl/2, wcs, hcl)
br := CreateCorner("bottom_right", screenNum, xw+ww-wcs, yw+hw-hcs, wcs, hcs)
bc := CreateCorner("bottom_center", screenNum, (xw+ww)/2-wcl/2, yw+hw-hcs, wcl, hcl)
bl := CreateCorner("bottom_left", screenNum, xw, yw+hw-hcs, wcs, hcs)
cl := CreateCorner("center_left", screenNum, xw, (yw+hw)/2-hcl/2, wcs, hcl)

corners = append(corners, []*Corner{tl, tc, tr, cr, br, bc, bl, cl}...)
}
Expand Down

0 comments on commit 11a7a38

Please sign in to comment.