Tk Source Code

View Ticket
Login
Ticket UUID: 76588c8589df0268d00eb92b1fe1a0aa5c8a45fa
Title: RFE: allow ttk::scale and ttk::scrollbar to match modern system behavior
Type: RFE Version: 8.7
Submitter: bll Created on: 2020-05-13 15:42:20
Subsystem: 88. Themed Tk Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2020-08-18 18:52:48
Resolution: None Closed By: nobody
    Closed on:
Description:
In: https://core.tcl-lang.org/tk/tktview?name=37517ac039 ,
it was noted that ttk::scale and ttk::scrollbar behavior does not
match the behavior of modern systems.

Currently clicking in the trough of a ttk::scale adjusts the scale by an 
increment.
Clicking in the trough of a ttk::scrollbar moves the thumb one page.

Scrollbars on MacOS and X11 generally have click-to-jump
rather than click-to-page.   Windows has click-to-page.

Scales on MacOS and X11 generally have click-to-jump and Windows
appears to be click-to-increment.   I'd like to find better examples
to make sure of these.  (MacOS: color chooser has a scale). 

The ttk::scrollbar code does a have click-to-jump binding on mouse button 2.
This works, but it needs improvement.   The click-to-jump code ends up 
moving the top of the scrollbar handle to the current cursor position rather 
than moving the middle of the scrollbar handle to the current cursor position.

Also, binding mouse button 2 is not portable to MacOS.  On MacOS, buttons 2
and 3 are reversed from X11 and windows.

For ttk::scrollbar, I would suggest:
  a) Fixing the Jump() procedure in ttk/scrollbar.tcl to move the thumb
     to the expected spot properly.
  b) Fixing the bindings for MacOS vs. X11/Windows.
     This is easily fixed by adding a binding for mouse button 3.
  c) Add a variable (e.g. ::ttk::scrollbar::clickbehavior ) so that user can
     easily change the behavior.  Depending on the setting, the scrollbar
     will call use the current click-in-trough behavior or call the Jump()
     procedure.
  d) For 8.7, I would recommend defaulting the scrollbar to be click-to-jump
     to match system behaviors on MacOS and X11.

ttk::scale also has a Jump() procedure defined.  Unlike the scrollbar, it
works correctly and is bound to both button-2 and button-3, so it does have
the same behavior on both MacOS and X11/Windows.

For ttk::scale:
  a) Add a variable (e.g. ::ttk::scale::clickbehavior ) so that user can
     easily change the behavior.  Depending on the setting, the scrollbar
     will call use the current click-in-trough behavior or call the Jump()
     procedure.
User Comments: chrstphrchvz added on 2020-07-15 06:52:21:

My earlier remark about timing mistakenly used units of "ns" rather than "μs"—sorry for the error.


bll added on 2020-05-17 16:54:43:
Updated with Christopher's code.

Currently the aqua windowing system check for jump/page is not protected
by a version check for 8.7.

chrstphrchvz added on 2020-05-17 15:29:24:

I didn't think of using TkpGetSystemDefault() for this, since that appears to be for retrieving (actual or emulated) X11 settings as defined in a .Xdefaults or .Xresources file. If I understand its purpose correctly, I think it would entail making scrollbar binding behavior into widget option passed to configure (I guess closer to how a button widget accepts a -command rather than manually using bind).

Adding a new public command (so that non-core scrollbars might use it right away) seems doable, maybe less invasive as well. I propose adding it to TkMacOSXInit.c for lack of a better place, where the appropriate headers for accessing [NSUserDefaults standardUserDefaults] have already been included. See attached patch for a possible implementation and binding usage.

I have timed doing [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollerPagingBehavior"] and it usually takes a whopping 20ns* to retrieve; caching it reduces it to 1ns (or something too small to accurately measure). I think there's little or no need to manually cache this setting when redrawing the scrolled content is the expensive task (anywhere from ~1ms to >100ms).

*(on a Core 2 Duo Mac underclocked to <1GHz due to not having a battery installed, running Catalina without Apple's permission; measured by taking difference of calls to CACurrentMediaTime().)

Getting the entire [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] would entail supporting conversions from any plist-supported type (NSString, NSNumber, NSData, NSDate, NSArray, and NSDictionary) to a corresponding Tcl type. Glancing at its contents, I'm not sure how useful most of the settings it reports are, or which don't already have some other preferred means of retrieving programmatically.

Another thing to consider is that settings which haven't been yet been altered from their default value aren't reported, but should be treated as having their default value. The example I gave for the defaults read … command is one example; however the boolforkey: method knows to return 0 for unaltered default settings.


marc_culler (claiming to be Marc Culler) added on 2020-05-16 20:55:41:
When I tried to answer your question I founds what looks an existing framework
for handling this without adding anything to the ::tk::mac namespace.  The
file generic/tkConfig.c implements Tk_InitOptions which is supposed to lookup
default values for widget options.  One way it tries to find a default value
is to call TkpGetSystemDefault, which can lookup an option in a platform-specific
way such as checking the user's system preferences.  Currently the macOS
implementation of TkpGetSystemDefault (in macosx/tkMacOSXConfig.c) is a no-op.
But it could easily access the user's system preferences.

The scale widget currently calls Tk_InitOptions so that might be the easiest
place to start.  The expectation seems to be that you pass in the widget's
optionTable but there might be some flexibility there.

bll added on 2020-05-16 20:24:40:
That's a bit beyond the scope of what I want to do :) .

I wouldn't make it so complex, trying to update the dictionary on change.
The user can just call the routine again to get the current defaults.
Though it appears macos has a hook to make that easy.
But that's extra overhead for something doesn't change very often.

I was wondering which .c file such a utility routine would go into?

marc_culler (claiming to be Marc Culler) added on 2020-05-16 20:07:15:
I would say that it should be part of the tk::mac:: namespace. One approach
would be to create a tk::mac::standardUserDefaults command which returns the
entire standardUSerDefaults object as a Tcl_Dictionary.  It would be good if
that dictionary could get updated when the user updates their preferences.
Possibly one should limit the Tcl_Dictionary to some specified subset of
the possible keys, but offhand I don't see any reason to do that.

bll added on 2020-05-16 19:43:16:
And into which file would such a utility routine be placed?

chrstphrchvz added on 2020-05-15 18:25:25:

I'm sorry, I got those backwards: false (0) indicates "Jump to the next page" and true (1) indicates "Jump to the spot that's clicked".


chrstphrchvz added on 2020-05-15 18:23:03:

Option = Alt on PC keyboard (at least for US English). I agree the default binding should respect the user's preferences. Ways of reading it include using the command:

defaults read NSGlobalDomain AppleScrollerPagingBehavior

and from Objective-C using:

[[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollerPagingBehavior"]

where true (1) indicates "Jump to the next page" and false (0) indicates "Jump to the spot that's clicked". (Based on documentation it sounds that there should be no need to twice-cache this value.)


bll added on 2020-05-15 18:09:56:
That would certainly be nice.  I would need one of your
unsupported mac routines to get that information.

marc_culler (claiming to be Marc Culler) added on 2020-05-15 17:54:45:
It sounds like the macOS code should read the user preferences to decide on
which behavior to use.

bll added on 2020-05-15 17:45:18:
Well, more work to do then.

On MacOS: 
  Settings/general shows: "jump to the spot that's clicked"  as being selected. 
  Let me check the backup admin account I created...
  Ok, it has 'jump to the next page'.
So the MacOS defaults should be page/jump/as-arrow for the scrollbar 
(or page/jump/none).

Now the question is, does this setting affect scales?  No, it does not.

I don't know how to do an option key using VNC.  I'll have to hook up the
non-mac keyboard to the mac later.  I can never recall if that sends the 
Alt or Meta or Super.

chrstphrchvz added on 2020-05-15 17:16:38:

Note that on various platforms (using native non-Tk programs) the behavior of clicking the scrollbar is also modulated by modifier keys and/or user preferences. On Windows, shift-click jumps to where clicked; on X11 shift-click jumps by a page, and on macOS option-click does the opposite of what the user specified in System Preferences > General. (Contrary to the original observation, jumping by a page appears to be the default setting at least on 10.15 Catalina.)


bll added on 2020-05-15 16:35:57:
Notes on current behavior of scales:


X11:
  XFCE: jump/page/as-arrow
      same as an XFCE scrollbar.  consistency is good.
  KDE: page/jump/--
      again, similar to windows. looks like the increment is sort
      of random just like windows.
Windows:
  win-7: page, random increment? / -- / --
  win-10: jump/--/--
MacOS:
      jump/--/--

Scrollbar again for win-10:
  win-10: page/--/context-menu

Suggested defaults for ttk::scale for 8.7:
   page/jump/as-arrow
The big question for scales is should the default be changed to:
   jump/page/as-arrow
This changes the current behavior, and any application that has scales
will have an issue.   

My opinion is to not change the behavior, but make it clear that the 
proposed variable to swap page and jump is available.

Whereas for the scrollbar, I have no issue with changing the default
behavior for 8.7.

Again, note that as-arrow is not currently implemented.

bll added on 2020-05-15 16:14:12:
X11 scrollbars are not necessarily consistent across window managers.

XFCE 14: jump/page/as-arrow
Mate on Ubuntu 16.04: just icky
Gnome 3 on Ubuntu 20.04: jump/page/as-arrow
KDE on Ubuntu 18.04: page/jump/context-menu
  Looks like KDE tried to match windows.
Mate on LinuxMint 19: jump/page/as-arrow

For 8.7, I would recommend as defaults:
   X11:   jump/page/as-arrow
          I would not suggest trying to detect KDE.
   MacOS: jump/page/as-arrow
   Windows: page/jump/as-arrow

The proposed variable to change behavior would swap page and jump.

Note that as-arrow is not currently implemented and would need a TIP.
People will freak out.  It does not need to be implemented though.
Right mouse button can be left inactive.

bll added on 2020-05-15 15:49:57:
More notes on current system behavior when clicking in the trough:

X11 scrollbar:
   left mouse button : jump
   right mouse button : same behavior as arrows (xfce terminal).
      I expect this is available so that the themes that have no arrows
      on the scrollbars have the same functionality available as 
      pressing on the arrow.
   middle mouse button : page
   Note that firefox does its own thing.
   Do different window managers do different things?  Or is this consistent?

Windows scrollbar:
   left mouse button : page
   middle mouse button : no effect
   right mouse button : pops up a context sensitive menu where different
      positions can be selected: current, top, bottom, page up, page down,
      scroll up, scroll down.

MacOS scrollbar:
   left mouse button: jump
   middle mouse button: no effect
   right mouse button: no effect

bll added on 2020-05-15 15:12:33:
I'll leave it as is for the moment.  
I was matching how it was done in the scale code.

Either way is fine by me, though I dislike the use of system specific stuff.
The rfe you listed would be great.  Solves a lot of cross-platform 
portability issues.

Middle mouse is emulated by clicking both mouse buttons at the same time
when no third mouse button is present (At least in X11).  Mouses without
middle buttons have been around a long time.

Patch for 8.6:
   a) Added bindings for mouse button 3 for MacOS.  The mouse button 2 and 
      button 3 bindings are identical.  This matches the ttk::scale code.
   b) Added a new internal command for the ttk::scrollbar: jumplocation.
      This calculates the fraction (as the fraction command) and subtracts
      1/2 of the current thumb length.  ttk/scrollbar.tcl has been modified
      to use it.

This paves the way to use the Jump() procedure as the default in 8.7 
so that the scrollbar in X11 and MacOS can match the system behavior.

chrstphrchvz added on 2020-05-15 14:47:56:

Chiming in after noticing [08d66a82c2]: rather than duplicating the binding (which makes it respond for right-clicking on Windows and X11), I suggest sticking to what has so far been the idiomatic approach:

if {[tk windowingsystem ne "aqua"} {
    # X11 and Windows bindings
} else {
    # Aqua bindings
}

There had been efforts to find any button 2/3 bindings and make sure they were consistent on Aqua (examples: [f15414d525], [1f184c7377]), but as shown here, there are still some instances left to take care of. From what I understand Tk 8.7 might address [6ff8f7b07a] and eliminate this sort of issue entirely (except for programs/libraries/extensions that want to work with older Tcl/Tk versions…). I think of the middle mouse button bindings in particular as mainly legacy bindings from before the existence of scrollwheels or laptop touchpads and other hardware for which middle-clicking is difficult or not obvious; it just seems alright to leave them for anyone who plugs in a three-button UNIX-era mouse and expects these bindings to still work.


bll added on 2020-05-14 20:01:08:
The Jump() procedure in ttk::scrollbar does not have the information 
required to position the handle correctly (ttk::scrollbar (a) above).

I recommend fixing the scrollbar handle positioning in the C code to
position the middle (rather than the top) of the scrollbar handle at 
the current position.

Attachments: