Skip to content

Add Scrollbar Context Menu - #19413

Merged
MrJul merged 8 commits into
masterfrom
feature/scroll-bar-context-menu
Jun 25, 2026
Merged

Add Scrollbar Context Menu#19413
MrJul merged 8 commits into
masterfrom
feature/scroll-bar-context-menu

Conversation

@drasticactions

Copy link
Copy Markdown
Contributor

Fixes #19404

What does the pull request do?

Adds a context menu to the scroll bar, matching functionality in WPF

What is the current behavior?

Windows (and WPF) have a context menu for scrollbars where you can right click and navigate them. This PR tries to match that function.

What is the updated/expected behavior with this PR?

Right click the scroll bar, interact with context menu.

How was the solution implemented (if it's not obvious)?

Replicating the functions of the WPF ScrollBar implementation. It should more-or-less match the original code that was used in WPF, apart from the resource file handling, which was put into the Invariant Strings XAML file.

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.0.999-cibuild0058068-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6

maxkatz6 commented Aug 7, 2025

Copy link
Copy Markdown
Member

Any reasons to keep it in C# code? I think whole context flyout can be moved to XAML, and methods like ScrollToTopAction can be made public instance methods (would require an API review but should be straightforward).

For the ":vertical" and ":horizontal" we already have pseudoclasses to be used in XAML. RTL makes it a bit more complicated though.

Upd: nevermind, ScrollHereAction would be pretty tricky in XAML solution

@drasticactions

Copy link
Copy Markdown
Contributor Author

I had cloned it from the original WPF code, which is in C# and internal. So, it's not technical; I wanted to ensure it operated the same. Now that I have unit tests there, I would be open to changing it.

This control is interesting, since it's less a "WPF" feature, but a Windows thing that's implemented in WPF. WPF doesn't allow for changes to this control, but I would be open to it.


private Track? Track => this.GetTemplateChildren().OfType<Track>().FirstOrDefault();

private class SimpleCommand : ICommand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We shouldn't really put classes in the same file, even private ones. This could be used other places in the future. On top of that -- why not just use a more standard RelayCommand-based approach?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This could be used other places in the future

Then it could be moved at that time? This was intended for this specific control, not something for other controls to use, and if it ever did, it could be moved then.

That said I don't really care if it's here or its own file, as long as it's not public.

On top of that -- why not just use a more standard RelayCommand-based approach?

I'm not against it. WPF uses static RoutedCommands. I generally go for simpiler implementations when I can and try to limit what's needed for a thing to just the things needed, and using a straight ICommand seemed right, especially since the intention for this was not a public API for a very specific implementation for this control.

If the intention is to expand it as a public interface to change, then that would change the needs.

}
}

private static void ScrollToTopAction(ScrollBar scrollBar)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Adding the suffix Action at the end doesn't really follow any conventions I'm aware of. Additionally, there is an argument to be made that these could become public in the future. and also... not static.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we made it public, it could be changed then. But it's not. My thought process was "I need a name for this so I can call it" and that's what I wrote, lol.

If there is enough push to change it, I will, especially if it were to be made public since then it would matter more but it's not, so i'm not sure that matters.

Comment thread src/Avalonia.Controls/Primitives/ScrollBar.cs
@timunie

timunie commented Aug 9, 2025

Copy link
Copy Markdown
Collaborator

In case we stick with the code behind context Menu, would it be possible to override it? I mean what if I as the App developer have a special need for own context Menu? Take an IDE where you can have an entry "Scroll to next error" or something.

@timunie

timunie commented Aug 9, 2025

Copy link
Copy Markdown
Collaborator

Since you keep track of lastRightPosition, that field should also be consumeable from the XAML version.

Note

Drawback of the XAML solution is that 3rd party themes have to pick up these changes, so it has to be highlighted in release notes.

# Conflicts:
#	src/Avalonia.Controls/Primitives/ScrollBar.cs
@drasticactions
drasticactions marked this pull request as ready for review June 24, 2026 12:09
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0066880-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Comment thread src/Avalonia.Controls/Primitives/ScrollBar.cs

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See comment below.

}
}

private void OnContextRequested(object? sender, ContextRequestedEventArgs e)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should not trigger the context menu on touch/pen input.

If the finger is outside the thumb, it causes scrolling as expected, but the menu also appears at the same time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated with a5ae6a1 and 743c07b

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@MrJul
MrJul enabled auto-merge June 25, 2026 08:05
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0066918-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul
MrJul added this pull request to the merge queue Jun 25, 2026
Merged via the queue into master with commit 9547bee Jun 25, 2026
11 checks passed
@MrJul
MrJul deleted the feature/scroll-bar-context-menu branch June 25, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ScrollBars lack context menu

6 participants