Skip to content

Commit

Permalink
Merge pull request #4089 from andydotxyz/fix/2722
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz authored Jul 30, 2023
2 parents da07447 + 87c7f02 commit 27cc5d3
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
31 changes: 23 additions & 8 deletions widget/menu_item.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package widget

import (
"image/color"
"strings"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -240,11 +241,12 @@ func (r *menuItemRenderer) Layout(size fyne.Size) {
}

rightOffset -= theme.InnerPadding()
textHeight := r.text.MinSize().Height
for i := len(r.shortcutTexts) - 1; i >= 0; i-- {
text := r.shortcutTexts[i]
text.Resize(text.MinSize())
rightOffset -= text.MinSize().Width
text.Move(fyne.NewPos(rightOffset, theme.InnerPadding()))
text.Move(fyne.NewPos(rightOffset, theme.InnerPadding()+(textHeight-text.Size().Height)))

if i == 0 {
rightOffset -= theme.InnerPadding()
Expand All @@ -261,7 +263,7 @@ func (r *menuItemRenderer) Layout(size fyne.Size) {
leftOffset += theme.InnerPadding()
}

r.text.Resize(fyne.NewSize(rightOffset-leftOffset, r.text.MinSize().Height))
r.text.Resize(fyne.NewSize(rightOffset-leftOffset, textHeight))
r.text.Move(fyne.NewPos(leftOffset, theme.InnerPadding()))

r.background.Resize(size)
Expand Down Expand Up @@ -301,9 +303,9 @@ func (r *menuItemRenderer) Refresh() {
}
r.background.Refresh()
r.text.Alignment = r.i.alignment
r.refreshText(r.text)
r.refreshText(r.text, false)
for _, text := range r.shortcutTexts {
r.refreshText(text)
r.refreshText(text, true)
}

if r.i.Item.Checked {
Expand Down Expand Up @@ -343,16 +345,26 @@ func (r *menuItemRenderer) refreshIcon(img *canvas.Image, rsc fyne.Resource) {
img.Refresh()
}

func (r *menuItemRenderer) refreshText(text *canvas.Text) {
func (r *menuItemRenderer) refreshText(text *canvas.Text, shortcut bool) {
text.TextSize = theme.TextSize()
if r.i.Item.Disabled {
text.Color = theme.DisabledColor()
} else {
text.Color = theme.ForegroundColor()
if shortcut {
text.Color = shortcutColor()
} else {
text.Color = theme.ForegroundColor()
}
}
text.Refresh()
}

func shortcutColor() color.Color {
r, g, b, a := theme.ForegroundColor().RGBA()
a = uint32(float32(a) * 0.95)
return color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)}
}

func textsForShortcut(s fyne.KeyboardShortcut) (texts []*canvas.Text) {
b := strings.Builder{}
mods := s.Mod()
Expand All @@ -372,11 +384,14 @@ func textsForShortcut(s fyne.KeyboardShortcut) (texts []*canvas.Text) {
if r != 0 {
b.WriteRune(r)
}
t := canvas.NewText(b.String(), theme.ForegroundColor())
shortColor := shortcutColor()
t := canvas.NewText(b.String(), shortColor)
t.TextStyle.Symbol = true
texts = append(texts, t)
if r == 0 {
texts = append(texts, canvas.NewText(string(s.Key()), theme.ForegroundColor()))
text := canvas.NewText(string(s.Key()), shortColor)
text.TextStyle.Monospace = true
texts = append(texts, text)
}
return
}
Binary file modified widget/testdata/menu/desktop/layout_shortcuts_darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 20 additions & 20 deletions widget/testdata/menu/desktop/layout_shortcuts_darwin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,73 +60,73 @@
<container size="96x660">
<widget size="96x35" type="*widget.menuItem">
<text pos="8,8" size="52x19">a</text>
<text pos="68,8" size="11x16">⌃</text>
<text pos="79,8" size="8x19">A</text>
<text color="rgba(255,255,255,50)" pos="68,10" size="11x16">⌃</text>
<text color="rgba(255,255,255,50)" monospace pos="80,10" size="8x16">A</text>
</widget>
<widget pos="0,39" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="41x19">b</text>
<text pos="57,8" size="31x16">⌥⌫</text>
<text color="rgba(255,255,255,50)" pos="57,10" size="31x16">⌥⌫</text>
</widget>
<widget pos="0,78" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="41x19">c</text>
<text pos="57,8" size="31x16">⌘⌦</text>
<text color="rgba(255,255,255,50)" pos="57,10" size="31x16">⌘⌦</text>
</widget>
<widget pos="0,117" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="44x19">d</text>
<text pos="60,8" size="27x16">⇧↓</text>
<text color="rgba(255,255,255,50)" pos="60,10" size="27x16">⇧↓</text>
</widget>
<widget pos="0,156" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="33x19">e</text>
<text pos="49,8" size="38x16">⌃⌥↘</text>
<text color="rgba(255,255,255,50)" pos="49,10" size="38x16">⌃⌥↘</text>
</widget>
<widget pos="0,195" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="31x19">f</text>
<text pos="47,8" size="41x16">⌃⌘↩</text>
<text color="rgba(255,255,255,50)" pos="47,10" size="41x16">⌃⌘↩</text>
</widget>
<widget pos="0,234" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="33x19">g</text>
<text pos="49,8" size="38x16">⌃⇧⎋</text>
<text color="rgba(255,255,255,50)" pos="49,10" size="38x16">⌃⇧⎋</text>
</widget>
<widget pos="0,273" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="30x19">h</text>
<text pos="46,8" size="41x16">⌥⌘↖</text>
<text color="rgba(255,255,255,50)" pos="46,10" size="41x16">⌥⌘↖</text>
</widget>
<widget pos="0,312" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="30x19">i</text>
<text pos="46,8" size="41x16">⌥⇧←</text>
<text color="rgba(255,255,255,50)" pos="46,10" size="41x16">⌥⇧←</text>
</widget>
<widget pos="0,351" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="35x19">j</text>
<text pos="51,8" size="36x16">⇧⌘⇟</text>
<text color="rgba(255,255,255,50)" pos="51,10" size="36x16">⇧⌘⇟</text>
</widget>
<widget pos="0,390" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="24x19">k</text>
<text pos="40,8" size="47x16">⌃⌥⌘⇞</text>
<text color="rgba(255,255,255,50)" pos="40,10" size="47x16">⌃⌥⌘⇞</text>
</widget>
<widget pos="0,429" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="16x19">l</text>
<text pos="32,8" size="55x16">⌃⌥⇧↩</text>
<text color="rgba(255,255,255,50)" pos="32,10" size="55x16">⌃⌥⇧↩</text>
</widget>
<widget pos="0,468" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="19x19">m</text>
<text pos="35,8" size="52x16">⌃⇧⌘→</text>
<text color="rgba(255,255,255,50)" pos="35,10" size="52x16">⌃⇧⌘→</text>
</widget>
<widget pos="0,508" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="8x19">n</text>
<text pos="24,8" size="63x16">⌃⌥⇧⌘␣</text>
<text color="rgba(255,255,255,50)" pos="24,10" size="63x16">⌃⌥⇧⌘␣</text>
</widget>
<widget pos="0,547" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="47x19">o</text>
<text pos="63,8" size="25x16">⌃⇥</text>
<text color="rgba(255,255,255,50)" pos="63,10" size="25x16">⌃⇥</text>
</widget>
<widget pos="0,586" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="48x19">p</text>
<text pos="64,8" size="24x16">⌃↑</text>
<text color="rgba(255,255,255,50)" pos="64,10" size="24x16">⌃↑</text>
</widget>
<widget pos="0,625" size="96x35" type="*widget.menuItem">
<text pos="8,8" size="57x19">q</text>
<text pos="73,8" size="0x16"></text>
<text pos="73,8" size="15x19">F6</text>
<text pos="8,8" size="55x19">q</text>
<text color="rgba(255,255,255,50)" pos="71,10" size="0x16"></text>
<text color="rgba(255,255,255,50)" monospace pos="71,10" size="16x16">F6</text>
</widget>
</container>
</widget>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/menu/desktop/layout_shortcuts_other.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 20 additions & 20 deletions widget/testdata/menu/desktop/layout_shortcuts_other.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,73 +60,73 @@
<container size="97x660">
<widget size="97x35" type="*widget.menuItem">
<text pos="8,8" size="53x19">a</text>
<text pos="69,8" size="11x16">⌃</text>
<text pos="80,8" size="8x19">A</text>
<text color="rgba(255,255,255,50)" pos="69,10" size="11x16">⌃</text>
<text color="rgba(255,255,255,50)" monospace pos="80,10" size="8x16">A</text>
</widget>
<widget pos="0,39" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="41x19">b</text>
<text pos="57,8" size="31x16">⌥⌫</text>
<text color="rgba(255,255,255,50)" pos="57,10" size="31x16">⌥⌫</text>
</widget>
<widget pos="0,78" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="41x19">c</text>
<text pos="57,8" size="31x16">❖⌦</text>
<text color="rgba(255,255,255,50)" pos="57,10" size="31x16">❖⌦</text>
</widget>
<widget pos="0,117" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="45x19">d</text>
<text pos="61,8" size="27x16">⇧↓</text>
<text color="rgba(255,255,255,50)" pos="61,10" size="27x16">⇧↓</text>
</widget>
<widget pos="0,156" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="34x19">e</text>
<text pos="50,8" size="38x16">⌃⌥↘</text>
<text color="rgba(255,255,255,50)" pos="50,10" size="38x16">⌃⌥↘</text>
</widget>
<widget pos="0,195" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="31x19">f</text>
<text pos="47,8" size="41x16">⌃❖↩</text>
<text color="rgba(255,255,255,50)" pos="47,10" size="41x16">⌃❖↩</text>
</widget>
<widget pos="0,234" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="34x19">g</text>
<text pos="50,8" size="38x16">⌃⇧⎋</text>
<text color="rgba(255,255,255,50)" pos="50,10" size="38x16">⌃⇧⎋</text>
</widget>
<widget pos="0,273" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="30x19">h</text>
<text pos="46,8" size="42x16">⌥❖↖</text>
<text color="rgba(255,255,255,50)" pos="46,10" size="42x16">⌥❖↖</text>
</widget>
<widget pos="0,312" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="31x19">i</text>
<text pos="47,8" size="41x16">⌥⇧←</text>
<text color="rgba(255,255,255,50)" pos="47,10" size="41x16">⌥⇧←</text>
</widget>
<widget pos="0,351" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="35x19">j</text>
<text pos="51,8" size="37x16">⇧❖⇟</text>
<text color="rgba(255,255,255,50)" pos="51,10" size="37x16">⇧❖⇟</text>
</widget>
<widget pos="0,390" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="24x19">k</text>
<text pos="40,8" size="48x16">⌃⌥❖⇞</text>
<text color="rgba(255,255,255,50)" pos="40,10" size="48x16">⌃⌥❖⇞</text>
</widget>
<widget pos="0,429" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="17x19">l</text>
<text pos="33,8" size="55x16">⌃⌥⇧↩</text>
<text color="rgba(255,255,255,50)" pos="33,10" size="55x16">⌃⌥⇧↩</text>
</widget>
<widget pos="0,468" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="19x19">m</text>
<text pos="35,8" size="53x16">⌃⇧❖→</text>
<text color="rgba(255,255,255,50)" pos="35,10" size="53x16">⌃⇧❖→</text>
</widget>
<widget pos="0,508" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="8x19">n</text>
<text pos="24,8" size="64x16">⌃⌥⇧❖␣</text>
<text color="rgba(255,255,255,50)" pos="24,10" size="64x16">⌃⌥⇧❖␣</text>
</widget>
<widget pos="0,547" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="47x19">o</text>
<text pos="63,8" size="25x16">⌃⇥</text>
<text color="rgba(255,255,255,50)" pos="63,10" size="25x16">⌃⇥</text>
</widget>
<widget pos="0,586" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="48x19">p</text>
<text pos="64,8" size="24x16">⌃↑</text>
<text color="rgba(255,255,255,50)" pos="64,10" size="24x16">⌃↑</text>
</widget>
<widget pos="0,625" size="97x35" type="*widget.menuItem">
<text pos="8,8" size="57x19">q</text>
<text pos="73,8" size="0x16"></text>
<text pos="73,8" size="15x19">F6</text>
<text pos="8,8" size="56x19">q</text>
<text color="rgba(255,255,255,50)" pos="72,10" size="0x16"></text>
<text color="rgba(255,255,255,50)" monospace pos="72,10" size="16x16">F6</text>
</widget>
</container>
</widget>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 27cc5d3

Please sign in to comment.