I want a vertical GtkBox to be vertically scrollable, so I put it into a GtkScrolledWindow with an AUTOMATIC vertical scrollbar (see code). However, now the Scrollbar is hiding part of the contents:
The problem is, that GtkScrolledWindow does not take into account the width of the scrollbar itself (iff the policy is GTK_POLICY_AUTOMATIC).
Code
This is the code producing the problematic behavior.
I think exactly this is the intended use of scrollbar with GTK_POLICY_AUTOMATIC. I have seen that kind of scrollbars generally only for text widgets containing multiline text and for the GtkDrawingArea. For both widgets, when the scrollbar vanished, it uncovers a part of the text or drawing, and when the scrollbar appears, it may cover a part of the text when text lines are long. If the scrollbar should never cover something, why should it hide then at all – there would be no gain of available space. The area of the scrollbar would turn into a void area – no real benefit, only confusing the user.
And generally I regard scrollbars as bad, they make user interaction hard. Vanishing scrollbars are even more bad, I recently used an app with hidden scrollbar, so that some options and entry fields of the app become visible only when I moved the mouse pointer on the right border of the app window. I think I was not aware of the hidden widgets for months.
intended use of scrollbar […]
and when the scrollbar appears, it may cover a part of the text when text lines are long
Do I understand you correctly, that you think the intended behavior of scrollbars is to hide content ?
I would say a scrollbar should generally* not cover the content below it. A scrollable area should work like a table with two columns: content in one column, scollbar in the other. If the scrollbar is AUTOMATIC then sometimes the second column has width=0. (this seemed to be the case for GTK2, btw.)
*exceptions are of course overlay scrollbar.
Below is a concrete example. The user searches for kanji. Each letter is added to the search history on the right (which is a vbox inside a GtkScrolledWindow)
That is what I have observed. I have just searched for the app where the invisible scrollbar was so invisible to me that I missed some widgets. It is the Gnome Shell 40 Tweaks app with Appearance tab. Scrollbar is invisible by default, so it is hard to guess that there are more widgets at the bottom of the window. And when I move the mouse pointer to the right border of the window, the scrollbar becomes visible, without shifting the widgets to the left in any way. For the Tweaks tool there is no covering of widgets, as the window has a wide right border.
You’re running with some random theme, so this may not apply.
The overall design of the scroll bars in GTK is to be overlaid on top of the scrollable content. That’s the intended behaviour, as the scroll bars are just meant to exist as indicators of where you are in the overall scrollable area. They can also be used as actual scroll bar controls, which is why they change shape on hover, but that’s mostly legacy at this point—considering that pointing devices either have smooth scrolling (touchpads and touchscreens) with incremental speed, or discrete scrolling (wheels). That’s also why there are no small button targets for “line” scrolling any more.
If your widget has content that should not be covered by overlaid scroll bars, you should add CSS or widget margin around the scrollable content. Most definitely, you should not mess with the size request of the scrolled window, as that has no bearing over the scroll bars.
The appearance of the scroll bars don’t depend on the policy. The policy only controls the visibility of the scroll bar in relation to the scrollable content. ALWAYS means “show the scroll bars even if the scrollable content is entirely contained in the scrolled window allocation”. By default, that means that the scroll bars will stick on top of the content—which means if you set ALWAYS you’ll also have to compensate with your own margin/padding in a way that looks good for your UI. GTK can’t know what “looks good” for you.
As a side note: there’s really no reason why you should use ALWAYS instead of AUTOMATIC. The ALWAYS value existed because scroll bars appearing would resize the scrolled window and/or the content, back when scroll bars were not overlaid on top of the content. That’s why the default policy is AUTOMATIC.
which means if you set ALWAYS you’ll also have to compensate with your own margin/padding
That’s a very valuable insight!
Here on Gtk 3.24, ALWAYS policy reserves special space for the scrollbar - i.e. my expected behavior. But if I understand you right, that is merely a coincidence and I cannot count on that.
you’ll also have to compensate with your own margin/padding in a way that looks good for your UI
Can you tell me how I would style it so it won’t overlap with the content, then?
I’m playing around a little with the CSS, but I can’t get it to work. Even setting opacity of the scrollbar to 0 would leave an overlapping grey area (see picture).
edit: Maybe what you (two) meant was:
The scrollbar is supposed to overlay.
It’s the theme’s responsibility to make it properly.
However, this still leaves me puzzled on what’s happening in the screenshot above.
Have you considered using GtkScrollbar for the task itself, i.e. for scrolling the box with buttons? Then you have more control over the appearance, and would probably get the expected behaviour.
Is it necessary to use the GtkScrolledWindow? What about making the box with buttons scrollable by inserting it into GtkViewport? Then you have less overhead.
However, I am not sure what will happen to the touch abilities then, because the documentation says: “GtkScrolledWindow has built-in support for touch devices.” This means that GtkViewport lacks this.
All that is very ugly. Adding more buttons by user activity is ugly, using still the legacy program style with the gtk_init() is ugly, dozens of lines of ugly C code just to show some buttons is ugly, and the screenshot pictures look ugly too – is it Windows? Such type of code has generally the high risk to not work properly for new GTK versions, and then nobody wants to touch the code to fix it. I have seen some examples of code like that in the last two decades – I would have not even been able to fix it. Well I guess you are doing what you like to do, regarding your user name, but I would have just asked someone skilled how to do it before writing more than a few lines of code or spending more than a few minutes on that all. And what do you hope for by cross posting to stack overflow. Mr. Bassi is already here.
Yes, unrelated, not helpful, but no insult intended. But I can tell my impression, which was confirmed by some of your other recent posts – closing the app when user clicks elsewhere and such. I got the impression that you just try to do strange things to confuse users and make them angry.
For your task of needing a variable, increasing number of buttons, I was just thinking of using a GtkComboboxText or a GtkListBox instead? A PopOver or HamburgerMenu may be an option maybe. A long list of Buttons wastes a lot of space, and with a scrollbar user experience is bad.
This baffles me!
How can this be theme related?
I’m using Greybird (default on XUbuntu). Setting it to Adwaita has a milding effect on the behavior. It also seems important how big the window is in relation to the button.
Here a video: https://youtu.be/upk9caJ51xU
I got the impression that you just try to do strange things to confuse users and make them angry.
Indeed, behavior without context might seem strange. I mostly try to abstract the problem away from my specific use case to 1) prevent comments on unrelated things and 2) reduce the amount of background information other users need to read in order to understand the problem.
Apart from that, strange behavior like with the scroll bar stays strange behavior. Yes, I might (or might not) use a ComboBox instead. But,
that wouldn’t solve the actual existing problem at hand.