Subject: Re: bug#45502: [PATCH] Prettier key bindings in NS menu entries

From: Alan Third <[email-masked]>
Date: Mon, 28 Dec 2020 18:36:05 +0000

On Mon, Dec 28, 2020 at 03:23:25PM +0100, Mattias Engdegård wrote:
> The NS port shows key bindings in a rather cluttered way, with the
> key in brackets directly after the menu entry. The Mac port of Emacs
> is much neater with the bindings all aligned at a common tab
> position. We could do the same, but having done some experiments I
> actually prefer a right-alignment of the keys. Proof-of-concept
> patch attached.
> 
> The alignment is made by padding with spaces, and then with hair
> spaces for extra precision; the result is not perfect but probably
> better than what we have now. If I get some time, I might do an
> experiment with more precise formatting.
> 

> > I'd wondered about using NSAttributedString and NSParagraphStyle to
> > set a tab stop at a specific pixel point, that should allow perfect
> > alignment, or, and this is a bit hackier, display the binding in a
> > monospace font. Then you could use (variable space font) spaces and
> > tabs to line up the start of the binding text, then use (monospace
> > font) spaces to align the bindings. That's maybe a bit too much work,
> > though, and I'm not at all knowledgeable about how NSAttributedStrings
> > actually work, so maybe it's impossible.
> 
> It didn't look obvious how to do it, but I could use some help.

I'm not sure either, but I guess the tabstop thing would look
something like:

NSMutableParagraphStyle *p = [NSMutableParagraphStyle
                               defaultParagraphStyle];
NSArray *tabstops = [NSArray withObject:[[[NSTextTab alloc]
                        initWithTextAlignment:NSTextAlignmentRight
                                     location:50 // This is probably in points.
                                      options:nil];
[p setTabStops:tabstops];

NSAttributedString *s = [[NSAttributedString alloc]
                          initWithString:yourString
                              attributes:someDictionaryObjectIncludingTheParagraphStyle];

[releaseAllTheAllocedObjects];

I can never remember how to make dictionaries, so I'll leave that to
you to work out. ;)

Hopefully then all you have to do is set the string as the title and it will
be drawn with the correct tabstop. Maybe NSMenuItems are special and
it won't work, though.
-- 
Alan Third