Skip to content

MBP Touchbar Support (take 2) #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2018
Merged

MBP Touchbar Support (take 2) #715

merged 4 commits into from
Jul 30, 2018

Conversation

ychin
Copy link
Member

@ychin ychin commented Jul 29, 2018

This finishes up the previous TouchBar pull request (#568). It addresses feedbacks from the code review as well as adding documentation and features.

Changes on top of the original pull request (which adds support for TouchBar via a special menu item TouchBar similar to how toolbars work):

  • Enabled states now work properly. For example if you define a visual only button (e.g. vmenu TouchBar.Copy "+y), the button will now only show up in visual mode, and will not take up space otherwise.

  • You can specify default Apple template Touch Bar icons. E.g. :an icon=NSTouchBarListViewTemplate TouchBar.ShowList <Nop>. For full list see https://developer.apple.com/design/human-interface-guidelines/macos/touch-bar/touch-bar-icons-and-images/

  • Remove the old defaults Touch Bar buttons. They were mostly just copies of the ones from the standard toolbar and I doubt most users will actually use them (since there are direct Vim commands you can use to begin with). They also use the toolbar icons which don't work as well on the toolbar. Given how little space there is it's a good idea to not put too many default buttons there. Instead just put a single button there that allows the user to toggle fullscreen mode. Can add more later.

    • It's still nice to at least have a couple buttons to make sure the user knows that MacVim now has Touch Bar support.
    • The user can always use aunmenu TouchBar. in their gvimrc to clear all the default ones.
  • Added documentations.

The current implementation is sort of the bare minimum, but there are other rooms for expansion in the future. Some ideas:

  • Allow submenus, e.g. :an TouchBar.Debugger.StepOver <nop>. Requires some refactoring to support making use of popup items.

  • Change how the buttons are display or even widget types. I'm thinking hijacking the existing :macmenu command is the safest choice as it prevents us having to hack new syntax and options into the miscellaneous :menu commands. We would need to first make the menu using standard Vim command, then annotate it using macmenu with the properties we want. Things we can support include

    • button colors
    • button priorities. This determines what buttons get hidden when there are too many.
    • showing both text and images
    • non-interactible text display (this makes it almost behave like statusline, need to think more)
    • scrollable view
    • candidate list for showing spell correction

Close #568

wiomoc and others added 3 commits July 26, 2018 22:05
- addressed review feedbacks
- documentations
Enabled states now work. E.g. a mapped Touch Bar button using "vmenu
TouchBar.DoStuff <nop>" will not show up in normal mode.

Also support specifying default Apple template icons. E.g. "an
icon=NSTouchBarListViewTemplate TouchBar.ShowList <Nop>"

Remove default TouchBar buttons as there were too many of them and most
of them are unlikely to be heavily used as there are direct Vim command
equivalent. Instead just add a single fullscreen toggle button. This can
be changed later.
@@ -9415,6 +9415,7 @@ tgetent Compiled with tgetent support, able to use a termcap
timers Compiled with |timer_start()| support.
title Compiled with window title support |'title'|.
toolbar Compiled with support for |gui-toolbar|.
touchbar Compiled with support for Touch Bar in MacVim.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not 100% if we should expose a feature test for this, but I guess if toolbar is already one, why not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you that you've exposed it as Tool bar and Touch Bar are different. One can be enabled without other

@@ -1287,4 +1287,27 @@ if has("gui_macvim")
macm Help.MacVim\ Website action=openWebsite:
endif

if has("touchbar")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the previous review there was a comment about potentially having too many default Touch Bar buttons. I addressed this in the pull request descriptions, but I basically removed all the original ones and just added a full screen button that can toggle icon depending on what state it's in. I think most users will prefer a close to empty Touch Bar for customization so it's not worth having too many defaults (I just think having a couple that works well is a good thing to entice users to learn how to customize it).

@ychin ychin mentioned this pull request Jul 29, 2018
Copy link
Contributor

@jpetrie jpetrie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good; let me know what you think about the comment regarding the availability macros.

@@ -28,6 +28,12 @@
// TODO: Move all toolbar code to window controller?
NSToolbar *toolbar;
NSMutableDictionary *toolbarItemDict;
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in a few places and I'm not sure it is correct (it was also in the original PR; I missed it then). NSTouchBar is in the 10.12.2+ SDK and AvailabilityMacros.h has entries that are granular to the second decimal place. In other words, MAC_OS_X_VERSION_10_12_1 is a thing. That SDK won't have TouchBar support, per Apple's docs, but will be greater than MAC_OS_X_VERSION_10_12 and thus cause a compiler error inside all these guards.

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2 seems like what we'd want here, and in all the other cases, right?

Copy link
Member Author

@ychin ychin Jul 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that, but now I'm just wondering whether we even should check for this, or just remove the macro usage. This check only helps people build (not use) MacVim on older platforms but I presume most people who want to contribute to or build MacVim would want to have recent versions of macOS and Xcode. Are we going to keep supporting older versions of macOS development tools? I did notice the fullscreen code still checks for MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 which seems crazy to me. (Most other code checks for MIN_ALLOWED, not MAX_ALLOWED)

Another important thing to check is runtime checking to make sure the program won't crash on users with older devices. I think the current implementation actually doesn't check that… addMenuItemWithDescriptor will call addTouchbarItemWithLabel which will start initializing NSCustomTouchBarItem which I think will crash in pre-Sierra machines (I don't have one to check and I'm not super familiar with Cocoa / Objective-C). I think we just need to inject some NSClassFromString(@"NSTouchBar") checks before we start initializing the touch bar items, or delay making the touch bar items until we are in makeTouchBar which will only get called in systems supporting the touch bar as a delegate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that, but now I'm just wondering whether we even should check for this, or just remove the macro usage.

I'd like to, but I don't think we can. There are users out there who build MacVim from source on older versions of the OS, for a variety of reasons (such as not really liking some of Apple's changes in later versions, et cetera). MacVim has long supported those users, much like vim itself.

Similar arguments pro and con can be made towards MacVim's support of legacy features (the old text renderer, the custom fullscreen mode) as well: these legacy features increase MacVim's maintenance cost, but there are users out there who really love them.

I don't think it is feasible to drop that support, as painful as it is to work with right now.

However, it would be great to come up with an alternative. Branches were floated as an idea a while back, but it didn't really go anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I just added the better compile + runtime checks and pushed a change.

Make sure to check MAC_OS_X_VERSION_10_12_2 so older Xcode / SDK
versions will still be able to build MacVim, albeit without Touch Bar.

Also make sure to use `NSClassFromString(@"NSTouchBar")` to check during
runtime to avoid MacVim crashing for users using older versions of macOS
when they by mistake bind a menu item with TouchBar as main menu
(nothing will show up instead).
@jpetrie jpetrie merged commit ab1e570 into macvim-dev:master Jul 30, 2018
@ychin ychin deleted the touchbar branch July 30, 2018 18:01
@idoo
Copy link

idoo commented Aug 1, 2018

A.W.E.S.O.M.E.

splhack added a commit that referenced this pull request Aug 3, 2018
Binary targets macOS 10.8+

- Vim patch 8.1.0235
- Touch Bar support #715
- Force click support #716
- New guioption 'k' #708
- Fix CoreText renderer

Script interfaces have compatibility with these versions

- Lua 5.3
- Perl 5.18
- Python2 2.7
- Python3 3.7
- Ruby 2.5
@vitaly-zdanevich
Copy link

Can we see the statusline on the touchbar now?

@ychin
Copy link
Member Author

ychin commented Sep 17, 2018

@vitaly-zdanevich No. The current TouchBar implementation allows you to add buttons using the misc Vim menu commands (e.g. :amenu). Statusline is a different system.

Maybe it’s worth investigating it at some point but it’s non-trivial to get something like a statusline display to work. We will need to split out the statusline code in Vim to generate display commands for a new target and modify the CoreText renderer in MacVim to render specifically to TouchBar.

Doable, but will require some work and I argue the utility from doing that is limited outside the initial cool factor as statusline already works in Vim. TouchBar is really designed and ideal for interactive displays like buttons rather than a status display.

@vitaly-zdanevich
Copy link

For me touchbar is usually useless, but having statusline here will free line of text :)

image

@mustafaquraish
Copy link

Sorry - complete newbie to Macvim and the touchbar in general. Could someone give me a simple example on what I could add to my .vimrc (or do i need to use a .gvimrc? I don't know the difference) to add a simple button to the touch bar? Thanks!

@ychin
Copy link
Member Author

ychin commented Mar 8, 2020

Type :help touchbar in MacVim. It works similar to how toolbars work. Add a menu item under TouchBar and it will show up. Adding to .vimrc or .gvimrc both work.

Here's the docs:

7. Touch Bar						*macvim-touchbar*

Touch Bar in MacVim works similar to the toolbar (see |macvim-toolbar|).  The
difference is that you use the special menu "TouchBar" instead of "ToolBar": >
	:an TouchBar.Hello          :echo "Hello"<CR>

The separators work similar to how toolbars work: >
	:an TouchBar.-Sep-          <Nop>
	:an TouchBar.-space1-       <Nop>
	:an TouchBar.-flexspace2-   <Nop>

The first example is a Vim separator (see |menu-separator|) and injects a
space between two buttons. The second creates a smaller space than a normal
separator and are specified by names that begin with "-space" and ends with
"-". The third creates a flexible empty space which will shrink or expand so
that items after it will be right-aligned, and is specified by names that
begin with "-flexspace" and ends with "-".

You can specify icons for Touch Bar buttons the same way for toolbar icons.
Touch Bar icons should ideally be 36x36 pixels, and no larger than 44x44
pixels. You can also use default template icons provided by Apple by using
their template names. An example: >
	:an icon=NSTouchBarListViewTemplate TouchBar.ShowList <Nop>

This feature only works on Mac devices that come with Touch Bars. On the ones
that don't, nothing will show up.

@mustafaquraish
Copy link

Type :help touchbar in MacVim. It works similar to how toolbars work. Add a menu item under TouchBar and it will show up. Adding to .vimrc or .gvimrc both work.

Here's the docs:

7. Touch Bar						*macvim-touchbar*

Touch Bar in MacVim works similar to the toolbar (see |macvim-toolbar|).  The
difference is that you use the special menu "TouchBar" instead of "ToolBar": >
	:an TouchBar.Hello          :echo "Hello"<CR>

The separators work similar to how toolbars work: >
	:an TouchBar.-Sep-          <Nop>
	:an TouchBar.-space1-       <Nop>
	:an TouchBar.-flexspace2-   <Nop>

The first example is a Vim separator (see |menu-separator|) and injects a
space between two buttons. The second creates a smaller space than a normal
separator and are specified by names that begin with "-space" and ends with
"-". The third creates a flexible empty space which will shrink or expand so
that items after it will be right-aligned, and is specified by names that
begin with "-flexspace" and ends with "-".

You can specify icons for Touch Bar buttons the same way for toolbar icons.
Touch Bar icons should ideally be 36x36 pixels, and no larger than 44x44
pixels. You can also use default template icons provided by Apple by using
their template names. An example: >
	:an icon=NSTouchBarListViewTemplate TouchBar.ShowList <Nop>

This feature only works on Mac devices that come with Touch Bars. On the ones
that don't, nothing will show up.

Thank you! Never used menus before, didn't even know where to start looking or what they were called. Down the rabbit hole now...

@ychin ychin added Input Issues related to keyboard or other types of input UI Issues related to UI elements, tabs, scrollbars, window resizing, etc. and removed Input Issues related to keyboard or other types of input labels Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
UI Issues related to UI elements, tabs, scrollbars, window resizing, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants