Merge pull request #83 from nwg-piotr/fix82

fixed trimming strings on non-ASCII characters
This commit is contained in:
Piotr Miller
2023-01-12 22:50:59 +01:00
committed by GitHub
3 changed files with 23 additions and 10 deletions

View File

@@ -21,7 +21,7 @@ import (
"github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gtk"
) )
const version = "0.3.6" const version = "0.3.7"
var ( var (
appDirs []string appDirs []string

View File

@@ -691,3 +691,19 @@ func mapOutputs() (map[string]*gdk.Monitor, error) {
} }
return result, nil return result, nil
} }
// KAdot / https://stackoverflow.com/a/38537764/4040598 - thanks!
func substring(s string, start int, end int) string {
startStrIdx := 0
i := 0
for j := range s {
if i == start {
startStrIdx = j
}
if i == end {
return s[startStrIdx:j]
}
i++
}
return s[startStrIdx:]
}

View File

@@ -58,9 +58,8 @@ func setUpPinnedFlowBox() *gtk.FlowBox {
name = entry.Name name = entry.Name
} }
if len(name) > 20 { if len(name) > 20 {
r := []rune(name) r := substring(name, 0, 17)
name = string(r[:17]) name = fmt.Sprintf("%s…", string(r))
name = fmt.Sprintf("%s…", name)
} }
btn.SetLabel(name) btn.SetLabel(name)
@@ -252,9 +251,8 @@ func flowBoxButton(entry desktopEntry) *gtk.Button {
button.SetImagePosition(gtk.POS_TOP) button.SetImagePosition(gtk.POS_TOP)
name := entry.NameLoc name := entry.NameLoc
if len(name) > 20 { if len(name) > 20 {
r := []rune(name[:17]) r := substring(name, 0, 17)
name = string(r) name = fmt.Sprintf("%s…", string(r))
name = fmt.Sprintf("%s…", name)
} }
button.SetLabel(name) button.SetLabel(name)
@@ -263,9 +261,8 @@ func flowBoxButton(entry desktopEntry) *gtk.Button {
terminal := entry.Terminal terminal := entry.Terminal
desc := entry.CommentLoc desc := entry.CommentLoc
if len(desc) > 120 { if len(desc) > 120 {
r := []rune(desc[:117]) r := substring(desc, 0, 117)
desc = string(r) desc = fmt.Sprintf("%s…", string(r))
desc = fmt.Sprintf("%s…", desc)
} }
button.Connect("button-release-event", func(btn *gtk.Button, e *gdk.Event) bool { button.Connect("button-release-event", func(btn *gtk.Button, e *gdk.Event) bool {
btnEvent := gdk.EventButtonNewFromEvent(e) btnEvent := gdk.EventButtonNewFromEvent(e)