JobaDiniz
JobaDiniz

Reputation: 1023

How to listen for folding event in AvalonEdit

I have successfully added folding to my AvalonEdit document, however, to enable a really useful experience, I need to store the folding state, so next time user open the document, the folding state is restored.

My question is how do I know when the FoldSection is folded by the user, for example on click? This class does not notify property change. I can't find any event to get the fold state change.

Upvotes: 2

Views: 478

Answers (3)

sarh
sarh

Reputation: 6637

I ended up with adding a handler action to the FoldingSection, assign it from outside and invoke from IsFolded setter. Pretty similar to events, but in my case it was more convenient to deal with actions. Something like that:

..
public sealed class FoldingSection : TextSegment
{
    ..
    public Action<bool> MyAction { get; set; }
    ..
    public bool IsFolded {
        get { return isFolded; }
        set {
            if (isFolded != value) {
                ..
                MyAction?.Invoke(value);

and somewhere in consuming code:

foreach (var folding in myFoldingManager.AllFoldings)
    folding.MyAction = ..

Upvotes: 0

zaknotzach
zaknotzach

Reputation: 1015

I know this is old, and this isn't quite what you are looking for, but I had a similar need to react to folding, and couldn't modify AvalonEdit for my solution. In my application, I didn't really care what was folded, just that if anything was folded or expanded I needed to re-run some data checks.

I figured out that I could just add a click handler so any clicks in the folding margin can trigger what I want. There's potential for overzealous triggering (any left mouse down triggers it) but my operation is cheap so I'm not worried about extra calls.

// Get the margin off the editor, if it exists
FoldingMargin_foldingMargin = Editor.TextArea.LeftMargins.OfType<FoldingMargin>().FirstOrDefault();

// if we have a margin, add a mouse event to it
if (_foldingMargin != null) {
    _foldingMargin.MouseLeftButtonDown += foldingMargin_MouseLeftButtonDown;
}

Just make sure to only do that after you actually install the margin, or it won't exist. The mouse event in my case is super simple, just one function call, but you can put whatever you need in there. Hopefully this helps anyone who might have a similar situation.

Upvotes: 2

Momo
Momo

Reputation: 484

Unfortunately there is no such event for the AvalonEdit implemented. You will have to get the source from GitHub and add it your self.

The FoldingManager will create a TextSegmentCollection<FoldingSection>. With FoldingManager.AllFoldings you should be able to see all foldings even with it state IsFolded. But there is no possibility the set that from outside the usercontrol. So you have to change the original source.

It seems that the Team from AvalonEdit don't really update there project anymore... At the moment I'm working on my on version with AvalonEdit as base. Maybe i will find the time to implement this feature as well but it could take time... So when you could your problem fixed on your own please tell me :)

Upvotes: 1

Related Questions