From 7aac5a4f52e77b21f96d6ad3e6754a036ff9fcc2 Mon Sep 17 00:00:00 2001 From: piotr Date: Mon, 30 Sep 2024 22:09:56 +0200 Subject: [PATCH 1/6] add expression calculator --- go.mod | 1 + go.sum | 2 ++ main.go | 19 ++++++++++++++--- tools.go | 14 +++++++------ uicomponents.go | 56 +++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 27e82b6..20a4991 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( ) require ( + github.com/expr-lang/expr v1.16.9 // indirect github.com/joshuarubin/lifecycle v1.1.4 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/sync v0.8.0 // indirect diff --git a/go.sum b/go.sum index 3586d10..f1f4b94 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlasky/gotk3-layershell v0.0.0-20240515133811-5c5115f0d774 h1:o87OVL4olQBlVwN3+NSVQpS6gj9FWUYtxOfHXWZigUE= github.com/dlasky/gotk3-layershell v0.0.0-20240515133811-5c5115f0d774/go.mod h1:JHLx2Wz4mAPVwn4PFhC69ydwyHP4A3wQvlg7HKVVc1U= +github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI= +github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gotk3/gotk3 v0.6.1/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= diff --git a/main.go b/main.go index 1aaa261..ff5986f 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/expr-lang/expr" "os" "os/signal" "path" @@ -21,7 +22,7 @@ import ( "github.com/gotk3/gotk3/gtk" ) -const version = "0.5.0" +const version = "0.5.1" var ( appDirs []string @@ -493,12 +494,24 @@ func main() { } } return true + } else if key.KeyVal() == gdk.KEY_Tab { if firstPowerBtn != nil { firstPowerBtn.ToWidget().GrabFocus() } + + } else if key.KeyVal() == gdk.KEY_Return { + s, _ := searchEntry.GetText() + if s != "" { + result, err := expr.Eval(s, nil) + if err == nil { + log.Debugf("Setting up mathemathical operation result window. Operation: %s, result: %v", s, result) + setUpOperationResultWindow(s, fmt.Sprintf("%v", result)) + } + } + return true } - return false + return true }) win.Connect("key-press-event", func(_ *gtk.Window, event *gdk.Event) bool { @@ -512,8 +525,8 @@ func main() { if !searchEntry.IsFocus() { searchEntry.GrabFocusWithoutSelecting() } - return false } + return false }) /* diff --git a/tools.go b/tools.go index 1aed16d..cc20cba 100644 --- a/tools.go +++ b/tools.go @@ -571,7 +571,7 @@ func savePinned() { } } -func launch(command string, terminal bool) { +func launch(command string, terminal bool, terminate bool) { // trim % and everything afterwards if strings.Contains(command, "%") { cutAt := strings.Index(command, "%") @@ -615,7 +615,7 @@ func launch(command string, terminal bool) { cmd = exec.Command("riverctl", "spawn", strings.Join(elements, " ")) } - msg := fmt.Sprintf("command: %q; args: %q\n", cmd.Args[0], cmd.Args[1:]) + msg := fmt.Sprintf("Executing command: %q; args: %q\n", cmd.Args[0], cmd.Args[1:]) log.Info(msg) cmd.SysProcAttr = &syscall.SysProcAttr{ @@ -632,10 +632,12 @@ func launch(command string, terminal bool) { }() } - if *resident { - restoreStateAndHide() - } else { - gtk.MainQuit() + if terminate { + if *resident { + restoreStateAndHide() + } else { + gtk.MainQuit() + } } } diff --git a/uicomponents.go b/uicomponents.go index fc71085..e68874d 100644 --- a/uicomponents.go +++ b/uicomponents.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/dlasky/gotk3-layershell/layershell" "io/fs" "path/filepath" "strings" @@ -66,7 +67,7 @@ func setUpPinnedFlowBox() *gtk.FlowBox { btn.Connect("button-release-event", func(row *gtk.Button, e *gdk.Event) bool { btnEvent := gdk.EventButtonNewFromEvent(e) if btnEvent.Button() == 1 { - launch(entry.Exec, entry.Terminal) + launch(entry.Exec, entry.Terminal, true) return true } else if btnEvent.Button() == 3 { unpinItem(entry.DesktopID) @@ -75,7 +76,7 @@ func setUpPinnedFlowBox() *gtk.FlowBox { return false }) btn.Connect("activate", func() { - launch(entry.Exec, entry.Terminal) + launch(entry.Exec, entry.Terminal, true) }) btn.Connect("enter-notify-event", func() { statusLabel.SetText(entry.CommentLoc) @@ -274,7 +275,7 @@ func flowBoxButton(entry desktopEntry) *gtk.Button { btnEvent := gdk.EventButtonNewFromEvent(e) if btnEvent.Button() == 1 { if !beenScrolled { - launch(exec, terminal) + launch(exec, terminal, true) return true } } else if btnEvent.Button() == 3 { @@ -284,7 +285,7 @@ func flowBoxButton(entry desktopEntry) *gtk.Button { return false }) button.Connect("activate", func() { - launch(exec, terminal) + launch(exec, terminal, true) }) button.Connect("enter-notify-event", func() { statusLabel.SetText(desc) @@ -322,13 +323,13 @@ func powerButton(iconPathOrName, command string) *gtk.Button { button.Connect("button-release-event", func(btn *gtk.Button, e *gdk.Event) bool { btnEvent := gdk.EventButtonNewFromEvent(e) if btnEvent.Button() == 1 { - launch(command, false) + launch(command, false, true) return true } return false }) button.Connect("activate", func() { - launch(command, false) + launch(command, false, true) }) button.Connect("enter-notify-event", func() { statusLabel.SetText(command) @@ -584,3 +585,46 @@ func setUpUserFileSearchResultButton(fileName, filePath string) *gtk.Box { box.PackStart(button, false, true, 0) return box } + +func setUpOperationResultWindow(operation string, result string) { + win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) + if err != nil { + log.Fatal("Unable to create result window:", err) + } + win.SetModal(true) + + if wayland() { + layershell.InitForWindow(win) + layershell.SetLayer(win, layershell.LAYER_SHELL_LAYER_OVERLAY) + layershell.SetKeyboardMode(win, layershell.LAYER_SHELL_KEYBOARD_MODE_EXCLUSIVE) + } + + win.Connect("key-release-event", func(_ *gtk.Window, event *gdk.Event) bool { + win.Destroy() + return true + }) + + outerVBox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 6) + win.Add(outerVBox) + + vBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5) + outerVBox.PackStart(vBox, true, true, 6) + lbl, _ := gtk.LabelNew(fmt.Sprintf("%s = %s", operation, result)) + vBox.PackStart(lbl, true, true, 12) + + mRefProvider, _ := gtk.CssProviderNew() + css := "window { background-color: rgba (0, 0, 0, 255); color: #fff; font-weight: bold; border: solid 1px grey}" + err = mRefProvider.LoadFromData(css) + if err != nil { + log.Warn(err) + } + ctx, _ := win.GetStyleContext() + ctx.AddProvider(mRefProvider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) + + win.ShowAll() + + if wayland() { + cmd := fmt.Sprintf("wl-copy %v", result) + launch(cmd, false, false) + } +} From 484c6ccb293418f84fa2ddddb2e846c9f1a733cd Mon Sep 17 00:00:00 2001 From: piotr Date: Thu, 3 Oct 2024 00:33:54 +0200 Subject: [PATCH 2/6] add comments --- main.go | 2 ++ uicomponents.go | 1 + 2 files changed, 3 insertions(+) diff --git a/main.go b/main.go index ff5986f..e11972b 100644 --- a/main.go +++ b/main.go @@ -503,6 +503,8 @@ func main() { } else if key.KeyVal() == gdk.KEY_Return { s, _ := searchEntry.GetText() if s != "" { + // Check if the search box content is an arithmetic expression. If so, display the result + // and copy to the clipboard with wl-copy. result, err := expr.Eval(s, nil) if err == nil { log.Debugf("Setting up mathemathical operation result window. Operation: %s, result: %v", s, result) diff --git a/uicomponents.go b/uicomponents.go index e68874d..7818727 100644 --- a/uicomponents.go +++ b/uicomponents.go @@ -599,6 +599,7 @@ func setUpOperationResultWindow(operation string, result string) { layershell.SetKeyboardMode(win, layershell.LAYER_SHELL_KEYBOARD_MODE_EXCLUSIVE) } + // any key to close the window win.Connect("key-release-event", func(_ *gtk.Window, event *gdk.Event) bool { win.Destroy() return true From 12da77fbfc8130b6552d4944ca89b475d1f6090c Mon Sep 17 00:00:00 2001 From: piotr Date: Thu, 3 Oct 2024 00:48:23 +0200 Subject: [PATCH 3/6] update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 8e9dd7e..673d062 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,12 @@ You may want to exclude some paths inside your XDG user directories from searchi node_modules ``` +### Calculations in the search box + +If the search box is not empty, and you press Enter, the search box content will be evaluated as an arithmetic operation. +If the result is not an error, it will be displayed in a small window, and copied to the clipboard with wl-copy. +Press any key to close the window. + ## Credits This program uses some great libraries: @@ -217,3 +223,4 @@ Copyright (c) 2015-2018 gotk3 contributors - [go-singleinstance](github.com/allan-simon/go-singleinstance) Copyright (c) 2015 Allan Simon - [logrus](https://github.com/sirupsen/logrus) Copyright (c) 2014 Simon Eskildsen - [fsnotify](https://github.com/fsnotify/fsnotify) Copyright (c) 2012-2019 fsnotify Authors +- [expr](https://github.com/expr-lang/expr) Copyright (c) 2018 Anton Medvedev From 46a526ced8b8a86b4e0600760c40f6318b571be5 Mon Sep 17 00:00:00 2001 From: piotr Date: Thu, 3 Oct 2024 01:04:53 +0200 Subject: [PATCH 4/6] close result win on btn release --- uicomponents.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/uicomponents.go b/uicomponents.go index 7818727..6f69964 100644 --- a/uicomponents.go +++ b/uicomponents.go @@ -605,6 +605,12 @@ func setUpOperationResultWindow(operation string, result string) { return true }) + // any button to close the window + win.Connect("button-release-event", func(_ *gtk.Window, event *gdk.Event) bool { + win.Destroy() + return true + }) + outerVBox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 6) win.Add(outerVBox) From 3977c4fc7c04b9671a086599719e33bebe6c97cc Mon Sep 17 00:00:00 2001 From: piotr Date: Thu, 3 Oct 2024 01:05:14 +0200 Subject: [PATCH 5/6] go -> 1.23 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 20a4991..bc67d89 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/nwg-piotr/nwg-drawer -go 1.22 +go 1.23 require ( github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37 From 9c48c585b78f6cda4c0841551754237e6bbf32ae Mon Sep 17 00:00:00 2001 From: piotr Date: Thu, 3 Oct 2024 01:10:35 +0200 Subject: [PATCH 6/6] add rounded corners --- uicomponents.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uicomponents.go b/uicomponents.go index 6f69964..1128460 100644 --- a/uicomponents.go +++ b/uicomponents.go @@ -620,7 +620,7 @@ func setUpOperationResultWindow(operation string, result string) { vBox.PackStart(lbl, true, true, 12) mRefProvider, _ := gtk.CssProviderNew() - css := "window { background-color: rgba (0, 0, 0, 255); color: #fff; font-weight: bold; border: solid 1px grey}" + css := "window { background-color: rgba (0, 0, 0, 255); color: #fff; font-weight: bold; border: solid 1px grey; border-radius: 5px}" err = mRefProvider.LoadFromData(css) if err != nil { log.Warn(err)