Skip to content

Commit

Permalink
Enable scrolling behavior to work correctly by making sure there is a…
Browse files Browse the repository at this point in the history
… background of transparent on the panels for containers.

Update Scroller to have bigger scroll area so it's clearer to see it working
Fix bug in AccessText registration which was breaking comboboxes.
  • Loading branch information
tomlm committed Dec 17, 2024
1 parent 8d96b05 commit 0bd5c7a
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 56 deletions.
134 changes: 134 additions & 0 deletions src/Consolonia.Core/Helpers/Observable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections;

Check warning on line 2 in src/Consolonia.Core/Helpers/Observable.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Helpers/Observable.cs(2,1)
using System.Collections.Generic;

Check warning on line 3 in src/Consolonia.Core/Helpers/Observable.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Helpers/Observable.cs(3,1)
using System.Linq;

Check warning on line 4 in src/Consolonia.Core/Helpers/Observable.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Helpers/Observable.cs(4,1)
using Avalonia.Reactive.Operators;

Check warning on line 5 in src/Consolonia.Core/Helpers/Observable.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Helpers/Observable.cs(5,1)
using Avalonia.Threading;

Check warning on line 6 in src/Consolonia.Core/Helpers/Observable.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Helpers/Observable.cs(6,1)
using global::Avalonia.Reactive;

Check warning on line 7 in src/Consolonia.Core/Helpers/Observable.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantNameQualifier] Qualifier is redundant" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Helpers/Observable.cs(7,7)

namespace Consolonia.Core.Helpers
{

/// <summary>
/// Provides common observable methods as a replacement for the Rx framework.
/// </summary>
/// <remarks>
/// COPIED FROM AVALONIA.BASE BECAUSE IT IS NOT PUBLICLY ACCESSIBLE,
/// it's either do this or pull in all of Rx framework as a dependency
/// </remarks>
///
internal static class Observable
{
public static IObservable<TSource> Create<TSource>(Func<IObserver<TSource>, IDisposable> subscribe)
{
return new CreateWithDisposableObservable<TSource>(subscribe);
}

public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> action)
{
return source.Subscribe(new AnonymousObserver<T>(action));
}

public static IObservable<TResult> Select<TSource, TResult>(this IObservable<TSource> source, Func<TSource, TResult> selector)
{
return Create<TResult>(obs =>
{
return source.Subscribe(new AnonymousObserver<TSource>(
input =>
{
TResult value;
#pragma warning disable CA1031 // Do not catch general exception types
try
{
value = selector(input);
}
catch (Exception ex)
{
obs.OnError(ex);
return;
}
#pragma warning restore CA1031 // Do not catch general exception types

obs.OnNext(value);
}, obs.OnError, obs.OnCompleted));
});
}

public static IObservable<TSource> StartWith<TSource>(this IObservable<TSource> source, TSource value)
{
return Create<TSource>(obs =>
{
obs.OnNext(value);
return source.Subscribe(obs);
});
}

public static IObservable<TSource> Where<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
{
return Create<TSource>(obs =>
{
return source.Subscribe(new AnonymousObserver<TSource>(
input =>
{
bool shouldRun;
#pragma warning disable CA1031 // Do not catch general exception types
try
{
shouldRun = predicate(input);
}
catch (Exception ex)
{
obs.OnError(ex);
return;
}
#pragma warning restore CA1031 // Do not catch general exception types
if (shouldRun)
{
obs.OnNext(input);
}
}, obs.OnError, obs.OnCompleted));
});
}

public static IObservable<T> Skip<T>(this IObservable<T> source, int skipCount)
{
if (skipCount <= 0)
{
throw new ArgumentException("Skip count must be bigger than zero", nameof(skipCount));
}

return Create<T>(obs =>
{
var remaining = skipCount;
return source.Subscribe(new AnonymousObserver<T>(
input =>
{
if (remaining <= 0)
{
obs.OnNext(input);
}
else
{
remaining--;
}
}, obs.OnError, obs.OnCompleted));
});
}


private sealed class CreateWithDisposableObservable<TSource> : IObservable<TSource>
{
private readonly Func<IObserver<TSource>, IDisposable> _subscribe;

public CreateWithDisposableObservable(Func<IObserver<TSource>, IDisposable> subscribe)
{
_subscribe = subscribe;
}

public IDisposable Subscribe(IObserver<TSource> observer)
{
return _subscribe(observer);
}
}
}
}
6 changes: 3 additions & 3 deletions src/Consolonia.Core/Infrastructure/ConsoloniaApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public override void OnFrameworkInitializationCompleted()
DataTemplates.Add(new FuncDataTemplate<object>(
(data, _) =>
{
if (data != null && data is string)
if (data != null)
{
var result = new ConsoloniaAccessText();
// ReSharper disable AccessToStaticMemberViaDerivedType
result.Bind(TextBlock.TextProperty,
result.GetObservable(Control.DataContextProperty));
return result;
result.GetObservable(Control.DataContextProperty).Select(x => x?.ToString()));
return result;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,34 @@
<CheckBox IsChecked="{Binding AllowAutoHide}"
Content="Allow auto hide" />

<StackPanel Orientation="Vertical"
<StackPanel Orientation="Horizontal"
Spacing="1">
<TextBlock Text="Horizontal Scroll" />
<ComboBox ItemsSource="{Binding AvailableVisibility}"
SelectedItem="{Binding HorizontalScrollVisibility}" />
<Label Content="Horizontal Scroll" Target="HorizontalScrollOptions" />
<ComboBox Name="HorizontalScrollOptions" ItemsSource="{Binding AvailableVisibility}"
Width="20" SelectedItem="{Binding HorizontalScrollVisibility}" />
</StackPanel>

<StackPanel Orientation="Vertical"
<StackPanel Orientation="Horizontal"
Spacing="1">
<TextBlock Text="Vertical Scroll" />
<ComboBox ItemsSource="{Binding AvailableVisibility}"
SelectedItem="{Binding VerticalScrollVisibility}" />
<Label Content="Vertical Scroll" Target="VerticalScrollOptions"/>
<ComboBox Name="VerticalScrollOptions" ItemsSource="{Binding AvailableVisibility}"
Width="20" SelectedItem="{Binding VerticalScrollVisibility}" />
</StackPanel>
</StackPanel>

<ScrollViewer x:Name="ScrollViewer"
Grid.Row="1"
Width="30"
Height="20"
Grid.Row="1"
AllowAutoHide="{Binding AllowAutoHide}"
HorizontalScrollBarVisibility="{Binding HorizontalScrollVisibility}"
VerticalScrollBarVisibility="{Binding VerticalScrollVisibility}"
Focusable="True">
<TextBlock xml:space="preserve"
Foreground="{DynamicResource ThemeForegroundBrush}">Focus to ScrollViewer
/; ;\
__ \\____//
/{_\_/ `'\____
\___ (o) (o }
_____________________________/ :--'
,-,'`@@@@@@@@ @@@@@@ \_ `__\
;:( @@@@@@@@@ @@@ \___(o'o)
:: ) @@@@ @@@@@@ ,'@@( `===='
:: : @@@@@: @@@@ `@@@:
:: \ @@@@@: @@@@@@@) ( '@@@'
;; /\ /`, @@@@@@@@@\ :@@@@@)
::/ ) {_----------------: :~`,~~;
;;'`; : ) : / `; ;
;;;; : : ; : ; ; :
`'`' / : : : : : :
)_ \__; ";" :_ ; \_\ `,','
:__\ \ * `,'* \ \ : \ * 8`;'* *
`^' \ :/ `^' `-^-' \v/ : \/
Bill Ames
</TextBlock>

<ItemsControl ItemsSource="{Binding Cows}" Width="200">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="{DynamicResource ThemeForegroundBrush}" Text="{Binding}" Padding="0" Margin="0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -38,8 +39,35 @@ public ScrollViewerPageViewModel()
};

HorizontalScrollVisibility = ScrollBarVisibility.Auto;
VerticalScrollVisibility = ScrollBarVisibility.Auto;
VerticalScrollVisibility = ScrollBarVisibility.Visible;
AllowAutoHide = true;
List<string> text = new List<string>();
for(int i=0; i< 30;i++)
{
text.AddRange("""
/; ;\
__ \\____//
/{_\_/ `'\____
\___ (o) (o }
_____________________________/ :--'
,-,'`@@@@@@@@ @@@@@@ \_ `__\
;:( @@@@@@@@@ @@@ \___(o'o)
:: ) @@@@ @@@@@@ ,'@@( `===='
:: : @@@@@: @@@@ `@@@:
:: \ @@@@@: @@@@@@@) ( '@@@'
;; /\ /`, @@@@@@@@@\ :@@@@@)
::/ ) {_----------------: :~`,~~;
;;'`; : ) : / `; ;
;;;; : : ; : ; ; :
`'`' / : : : : : :
)_ \__; ";" :_ ; \_\ `,','
:__\ \ * `,'* \ \ : \ * 8`;'* *
`^' \ :/ `^' `-^-' \v/ : \/
Bill Ames
""".Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries));
}
Cows = text;
}

public bool AllowAutoHide
Expand All @@ -61,6 +89,9 @@ public ScrollBarVisibility VerticalScrollVisibility
}

public List<ScrollBarVisibility> AvailableVisibility { get; }

private List<string> _cows = new List<string>();
public List<string> Cows { get => _cows; init => RaiseAndSetIfChanged(ref _cows, value); }
}

public class ViewModelBase : INotifyPropertyChanged
Expand Down
3 changes: 2 additions & 1 deletion src/Consolonia.Themes/Templates/Controls/ComboBoxItem.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
Value="Left" />
<Setter Property="Template">
<ControlTemplate>
<helpers:CaretControl IsCaretShown="{TemplateBinding IsFocused}">
<helpers:CaretControl IsCaretShown="{TemplateBinding IsFocused}"
Background="Transparent">
<ContentPresenter Name="PART_ContentPresenter"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
Expand Down
3 changes: 2 additions & 1 deletion src/Consolonia.Themes/Templates/Controls/DataGrid.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
<Setter Property="Template">
<ControlTemplate>
<Grid ColumnDefinitions="*,Auto"
Background="{TemplateBinding Background}">
Background="Transparent">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Margin="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
TextBlock.Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
Expand Down
3 changes: 2 additions & 1 deletion src/Consolonia.Themes/Templates/Controls/ListBoxItem.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
Value="{DynamicResource ThemeTransparentBrush}" />
<Setter Property="Template">
<ControlTemplate>
<Panel Background="{TemplateBinding Background}">
<Panel Background="Transparent">
<helpers:CaretControl Foreground="{TemplateBinding Foreground}"
IsCaretShown="{TemplateBinding IsFocused}">
<ContentPresenter Name="PART_ContentPresenter"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Expand Down
32 changes: 17 additions & 15 deletions src/Consolonia.Themes/Templates/Controls/ScrollViewer.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@
<Grid x:Name="PART_Root"
RowDefinitions="*,Auto"
ColumnDefinitions="*,Auto">
<ScrollContentPresenter Name="PART_ContentPresenter"
Padding="{TemplateBinding Padding}"
HorizontalSnapPointsType="{TemplateBinding HorizontalSnapPointsType}"
VerticalSnapPointsType="{TemplateBinding VerticalSnapPointsType}"
HorizontalSnapPointsAlignment="{TemplateBinding HorizontalSnapPointsAlignment}"
VerticalSnapPointsAlignment="{TemplateBinding VerticalSnapPointsAlignment}"
Background="{TemplateBinding Background}"
ScrollViewer.IsScrollInertiaEnabled="True">
<ScrollContentPresenter.GestureRecognizers>
<ScrollGestureRecognizer
CanHorizontallyScroll="{Binding CanHorizontallyScroll, ElementName=PART_ContentPresenter}"
CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}"
IsScrollInertiaEnabled="{Binding (ScrollViewer.IsScrollInertiaEnabled), ElementName=PART_ContentPresenter}" />
</ScrollContentPresenter.GestureRecognizers>
</ScrollContentPresenter>
<Panel Background="{TemplateBinding Background}">
<ScrollContentPresenter Name="PART_ContentPresenter"
Padding="{TemplateBinding Padding}"
HorizontalSnapPointsType="{TemplateBinding HorizontalSnapPointsType}"
VerticalSnapPointsType="{TemplateBinding VerticalSnapPointsType}"
HorizontalSnapPointsAlignment="{TemplateBinding HorizontalSnapPointsAlignment}"
VerticalSnapPointsAlignment="{TemplateBinding VerticalSnapPointsAlignment}"
Background="Transparent"
ScrollViewer.IsScrollInertiaEnabled="True">
<ScrollContentPresenter.GestureRecognizers>
<ScrollGestureRecognizer
CanHorizontallyScroll="{Binding CanHorizontallyScroll, ElementName=PART_ContentPresenter}"
CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}"
IsScrollInertiaEnabled="{Binding (ScrollViewer.IsScrollInertiaEnabled), ElementName=PART_ContentPresenter}" />
</ScrollContentPresenter.GestureRecognizers>
</ScrollContentPresenter>
</Panel>
<ScrollBar Name="PART_HorizontalScrollBar"
Orientation="Horizontal"
Grid.Row="1"
Expand Down

0 comments on commit 0bd5c7a

Please sign in to comment.