-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: notify about layer changes in toast overlay
- Loading branch information
Showing
8 changed files
with
174 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//go:build gtk_overlay | ||
|
||
package notifications | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gotk3/gotk3/gdk" | ||
"github.com/gotk3/gotk3/gtk" | ||
) | ||
|
||
type GtkOverlay struct { | ||
windowVisibilityDuration time.Duration | ||
|
||
label *gtk.Label | ||
refreshWindowVisibilityDuration chan time.Time | ||
} | ||
|
||
func InitGtkOverlay(width int, height int, positionOffsetX int, positionOffsetY int, visibilityDuration time.Duration) (*GtkOverlay, error) { | ||
gtk.Init(nil) | ||
|
||
// Create a new window | ||
win, err := gtk.WindowNew(gtk.WINDOW_POPUP) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create window: %v", err) | ||
} | ||
win.SetDefaultSize(width, height) | ||
win.Connect("destroy", func() { | ||
gtk.MainQuit() | ||
}) | ||
win.SetAcceptFocus(false) // not respected in Hyprland | ||
win.SetCanFocus(false) // not respected in Hyprland | ||
win.SetDecorated(false) | ||
win.SetTypeHint(gdk.WINDOW_TYPE_HINT_NOTIFICATION) | ||
win.SetDeletable(false) // not respected in Hyprland | ||
win.SetSkipTaskbarHint(false) | ||
win.SetSkipPagerHint(false) | ||
win.SetResizable(false) // not respected in Hyprland | ||
win.SetFocusOnMap(false) | ||
win.SetFocusOnClick(false) | ||
win.SetCanDefault(false) | ||
// win.SetOpacity(0.5) // not respected in Hyprland | ||
|
||
win.SetPosition(gtk.WIN_POS_CENTER) // temporary position | ||
centerX, centerY := win.GetPosition() | ||
|
||
// Create a label for displaying the layer name | ||
label, err := gtk.LabelNew("Layer Switched!") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create label: %v", err) | ||
} | ||
|
||
// Add the label to the window | ||
win.Add(label) | ||
|
||
refreshVisibilityCh := make(chan time.Time) | ||
|
||
go func() { | ||
for { | ||
hideTime := <-refreshVisibilityCh | ||
timer := time.NewTimer(time.Until(hideTime)) | ||
|
||
// needs to be set every time window gets hidden, otherwise it's not respected by gtk | ||
win.Move(centerX+positionOffsetX, centerY+positionOffsetY) | ||
win.ShowAll() | ||
|
||
loop: | ||
for { | ||
select { | ||
case newHideTime := <-refreshVisibilityCh: | ||
if !timer.Stop() { | ||
<-timer.C | ||
} | ||
timer.Stop() | ||
timer.Reset(time.Until(newHideTime)) | ||
case <-timer.C: | ||
break loop | ||
} | ||
} | ||
|
||
win.Hide() | ||
} | ||
}() | ||
|
||
// Start the GTK main loop | ||
go gtk.Main() | ||
|
||
return &GtkOverlay{ | ||
windowVisibilityDuration: visibilityDuration, | ||
label: label, | ||
refreshWindowVisibilityDuration: refreshVisibilityCh, | ||
}, nil | ||
} | ||
|
||
func (n *GtkOverlay) LayerChange(newLayer string) error { | ||
n.label.SetText(newLayer) | ||
n.refreshWindowVisibilityDuration <- time.Now().Add(n.windowVisibilityDuration) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build !gtk_overlay | ||
|
||
package notifications | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type GtkOverlay struct{} | ||
|
||
func InitGtkOverlay(width int, height int, positionOffsetX int, positionOffsetY int, visibilityDuration time.Duration) (*GtkOverlay, error) { | ||
return nil, fmt.Errorf("'gtk_overlay' compilation flag was not enabled for this build") | ||
} | ||
|
||
func (n *GtkOverlay) LayerChange(newLayer string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package notifications | ||
|
||
type INotifier interface { | ||
LayerChange(newLayer string) error | ||
} | ||
|
||
type Disabled struct{} | ||
|
||
func (n *Disabled) LayerChange(_ string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
|
||
set windows-powershell := true | ||
|
||
just: | ||
just -l | ||
|
||
run: | ||
CGO_ENABLED=1 GO111MODULE=on go run . -ldflags "-H=windowsgui" | ||
CGO_ENABLED=1 GO111MODULE=on go run . | ||
|
||
build_release tags="gtk_overlay" version="latest": | ||
just _build_release_{{os()}} {{tags}} {{version}} | ||
|
||
build_release_linux version="latest": | ||
GOOS=linux CGO_ENABLED=1 GO111MODULE=on go build -ldflags "-s -w -X 'main.buildVersion={{version}}' -X 'main.buildHash=$(git rev-parse HEAD)' -X 'main.buildDate=$(date -u)'" -trimpath -o dist/kanata-tray | ||
_build_release_linux tags version: | ||
GOOS=linux CGO_ENABLED=1 GO111MODULE=on go build -tags={{tags}} -ldflags "-s -w -X 'main.buildVersion={{version}}' -X 'main.buildHash=$(git rev-parse HEAD)' -X 'main.buildDate=$(date -u)'" -trimpath -o dist/kanata-tray | ||
|
||
build_release_windows version="latest": | ||
GOOS=windows CGO_ENABLED=1 GO111MODULE=on go build -ldflags "-H=windowsgui -s -w -X 'main.buildVersion={{version}}' -X 'main.buildHash=$(git rev-parse HEAD)' -X 'main.buildDate=$(date -u)'" -trimpath -o dist/kanata-tray.exe | ||
# CGO cross-compilation is not supported with 'gtk_overlay' tag | ||
_build_release_windows tags version: | ||
GOOS=windows CGO_ENABLED=1 GO111MODULE=on go build -tags={{tags}} -ldflags "-H=windowsgui -s -w -X 'main.buildVersion={{version}}' -X 'main.buildHash=$(git rev-parse HEAD)' -X 'main.buildDate=$(date -u)'" -trimpath -o dist/kanata-tray.exe | ||
|
||
# e.g. "push_tag v0.1.0" | ||
push_tag tag: | ||
git tag {{tag}} | ||
git push --tags | ||
git push --tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters