Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve contrast of profile headers #15

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion UniSky/Helpers/Interop/BitmapInterop.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
Expand Down Expand Up @@ -158,6 +158,43 @@ unsafe interface IMemoryBufferByteAccess
void GetBuffer(out byte* buffer, out uint capacity);
}

public static async Task<float> GetImageAverageBrightnessAsync(IRandomAccessStream stream)
{
static float RgbToLightness(double r, double g, double b)
{
float _R = (float)(r / 255f);
float _G = (float)(g / 255f);
float _B = (float)(b / 255f);

float _Min = Math.Min(Math.Min(_R, _G), _B);
float _Max = Math.Max(Math.Max(_R, _G), _B);
float _Delta = _Max - _Min;

float L = (float)((_Max + _Min) / 2.0f);

return L;
}

var decoder = await BitmapDecoder.CreateAsync(stream);
var transform = new BitmapTransform() { ScaledWidth = 1, ScaledHeight = 1, InterpolationMode = BitmapInterpolationMode.Fant };

using var bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
using var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.Read);
using var reference = buffer.CreateReference();

unsafe
{
((IMemoryBufferByteAccess)reference).GetBuffer(out var raw, out var capacity);

var b = raw[0];
var g = raw[1];
var r = raw[2];
var a = raw[3];

return RgbToLightness(r, g, b);
}
}

public static async Task<StorageFile> SaveBitmapToFileAsync(RandomAccessStreamReference streamReference)
{
var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync($"{Guid.NewGuid()}.png");
Expand Down
135 changes: 64 additions & 71 deletions UniSky/Pages/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -296,61 +296,78 @@
ToolTipService.Placement="Top"/>
</Grid>



<Grid x:Name="AppTitleBar"
Background="Transparent"
VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="LeftPaddingColumn" Width="0"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<Grid Grid.Column="1"
MaxWidth="600"
HorizontalAlignment="Stretch">
<UserControl x:Name="AppTitleBarContainer"
VerticalAlignment="Top">
<Grid x:Name="AppTitleBar"
Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition x:Name="LeftPaddingColumn" Width="0"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<TranslateTransform x:Name="TitleHeaderTransform"/>
</Grid.RenderTransform>

<Button x:Name="BackButton"
x:Load="{x:Bind NavigationFrame.CanGoBack, Mode=OneWay}"
Style="{StaticResource NavigationBackButtonNormalStyle}"
Height="32"
FontSize="13"
ToolTipService.ToolTip="Back"
Command="{x:Bind ViewModel.GoBackCommand}"/>
<Grid Grid.Column="1"
MaxWidth="600"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<TranslateTransform x:Name="TitleHeaderTransform"/>
</Grid.RenderTransform>

<Viewbox VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="8,2,0,0"
Width="16"
Height="16"
Grid.Column="1">
<Path x:Name="AppIconPath"
Data="{StaticResource BlueSkyLogoPath}"
Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>
</Viewbox>
<Button x:Name="BackButton"
x:Load="{x:Bind NavigationFrame.CanGoBack, Mode=OneWay}"
Style="{StaticResource NavigationBackButtonNormalStyle}"
Height="32"
FontSize="13"
ToolTipService.ToolTip="Back"
Command="{x:Bind ViewModel.GoBackCommand}"/>

<TextBlock x:Name="AppTitleTextBlock"
x:Uid="AppTitle"
Style="{StaticResource CaptionTextBlockStyle}"
Text="Unisky"
VerticalAlignment="Center"
Grid.Column="2"
Margin="8,0"/>
<Viewbox VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="8,2,0,0"
Width="16"
Height="16"
Grid.Column="1">
<Path x:Name="AppIconPath"
Data="{StaticResource BlueSkyLogoPath}"
Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>
</Viewbox>

<Border x:Name="TitleBarDrag"
Background="Transparent"
Grid.Column="1"
Grid.ColumnSpan="2"/>
<TextBlock x:Name="AppTitleTextBlock"
x:Uid="AppTitle"
Style="{StaticResource CaptionTextBlockStyle}"
Text="Unisky"
VerticalAlignment="Center"
Grid.Column="2"
Margin="8,0"/>

<Border x:Name="TitleBarDrag"
Background="Transparent"
Grid.Column="1"
Grid.ColumnSpan="2"/>
</Grid>

<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Active">
<VisualState.Setters>
<Setter Target="AppIconPath.Fill" Value="{ThemeResource ApplicationForegroundThemeBrush}"/>
<Setter Target="AppTitleTextBlock.Foreground" Value="{ThemeResource ApplicationForegroundThemeBrush}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Inactive">
<VisualState.Setters>
<Setter Target="AppIconPath.Fill" Value="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"/>
<Setter Target="AppTitleTextBlock.Foreground" Value="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</Grid>
</UserControl>
</Grid>

</mux:NavigationView>
Expand Down Expand Up @@ -380,30 +397,6 @@
<Setter Target="FooterNavigation.Visibility" Value="Collapsed"/>
</VisualState.Setters>
</VisualState>
<!--<VisualState x:Name="Wide">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="1023" />
</VisualState.StateTriggers>

<VisualState.Setters>
<Setter Target="NavView.PaneDisplayMode" Value="Left"/>
<Setter Target="FooterNavigation.Visibility" Value="Collapsed"/>
</VisualState.Setters>
</VisualState>-->
</VisualStateGroup>
<VisualStateGroup>
<VisualState x:Name="Active">
<VisualState.Setters>
<Setter Target="AppIconPath.Fill" Value="{ThemeResource ApplicationForegroundThemeBrush}"/>
<Setter Target="AppTitleTextBlock.Foreground" Value="{ThemeResource ApplicationForegroundThemeBrush}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Inactive">
<VisualState.Setters>
<Setter Target="AppIconPath.Fill" Value="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"/>
<Setter Target="AppTitleTextBlock.Foreground" Value="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
Expand Down
5 changes: 3 additions & 2 deletions UniSky/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ private void OnSafeAreaUpdated(object sender, SafeAreaUpdatedEventArgs e)

if (e.SafeArea.IsActive)
{
VisualStateManager.GoToState(this, "Active", true);
VisualStateManager.GoToState(AppTitleBarContainer, "Active", true);
}
else
{
VisualStateManager.GoToState(this, "Inactive", true);
VisualStateManager.GoToState(AppTitleBarContainer, "Inactive", true);
}

AppTitleBarContainer.RequestedTheme = e.SafeArea.Theme;
Margin = new Thickness(e.SafeArea.Bounds.Left, 0, e.SafeArea.Bounds.Right, e.SafeArea.Bounds.Bottom);
}

Expand Down
37 changes: 27 additions & 10 deletions UniSky/Pages/ProfilePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
xmlns:extensions="using:UniSky.Extensions"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:mux="using:Microsoft.UI.Xaml.Controls"
xmlns:w1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
Loaded="Page_Loaded"
Expand All @@ -19,13 +20,14 @@

<Grid>
<ListView x:Name="RootList"
ItemsSource="{Binding SelectedFeed.Items}"
ItemTemplate="{StaticResource FeedItemContentTemplate}"
SelectionMode="None"
IsItemClickEnabled="True"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
ScrollViewer.IsVerticalScrollChainingEnabled="True">
ItemsSource="{Binding SelectedFeed.Items}"
ItemTemplate="{StaticResource FeedItemContentTemplate}"
SelectionMode="None"
IsItemClickEnabled="True"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
ScrollViewer.IsVerticalScrollChainingEnabled="True"
w1809:ScrollViewer.CanContentRenderOutsideBounds="True">
<ListView.Resources>
<SolidColorBrush Color="Transparent" x:Key="ListViewItemRevealBackground"/>
<SolidColorBrush Color="Transparent" x:Key="ListViewItemBackgroundPointerOver"/>
Expand All @@ -41,7 +43,6 @@
<Grid x:Name="ProfileContainer"
extensions:Hairline.BorderThickness="0,0,0,1"
BorderBrush="{StaticResource SystemControlRevealSeparatorBrush}"
Background="{ThemeResource SystemControlBackgroundChromeMediumBrush}"
SizeChanged="Page_SizeChanged">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
Expand All @@ -54,6 +55,16 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Border Canvas.ZIndex="-1"
Grid.ColumnSpan="2"
Grid.RowSpan="3"
Background="{ThemeResource SystemControlBackgroundChromeMediumBrush}"/>

<Border Canvas.ZIndex="-1"
Grid.ColumnSpan="2"
Grid.Row="4"
Background="{ThemeResource SystemControlChromeMediumAcrylicElementMediumBrush}"/>

<Grid x:Name="HeaderGrid"
Grid.ColumnSpan="2"
Background="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}">
Expand All @@ -62,6 +73,12 @@
HorizontalAlignment="Center"
Stretch="UniformToFill"/>
</controls:ConstrainedBox>

<ProgressRing VerticalAlignment="Center"
HorizontalAlignment="Center"
Width="48"
Height="48"
IsActive="{Binding IsLoading}"/>
</Grid>

<Ellipse x:Name="ProfileImage"
Expand Down Expand Up @@ -184,7 +201,7 @@
Grid.ColumnSpan="2"
Grid.Row="1"
VerticalAlignment="Bottom"
RequestedTheme="Dark">
RequestedTheme="{Binding Theme}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
Expand All @@ -208,7 +225,7 @@
Tapped="ProfileContainer_Tapped"
Margin="-60,-8,-8,-8"
Grid.ColumnSpan="2"
Grid.RowSpan="2"/>
Grid.RowSpan="2"/>

<Button x:Name="ScrolledFollowButton"
Style="{ThemeResource ButtonRevealStyle}"
Expand Down
3 changes: 2 additions & 1 deletion UniSky/Pages/ProfilePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
{
var safeAreaService = Ioc.Default.GetRequiredService<ISafeAreaService>();
safeAreaService.SafeAreaUpdated -= OnSafeAreaUpdated;
safeAreaService.SetTitlebarTheme(ElementTheme.Default);
}

private void HandleUniskyProtocol(Uri uri)
Expand Down Expand Up @@ -228,7 +229,7 @@ private void UpdateSizeDependentProperties()

var safeAreaService = Ioc.Default.GetRequiredService<ISafeAreaService>();

var titleBarHeight = (float)safeAreaService.State.Bounds.Top;
var titleBarHeight = (float)safeAreaService.State.Bounds.Top + 4;
var stickyHeight = (float)StickyFooter.ActualHeight;
var totalSize = (float)ProfileContainer.ActualHeight;
var clampHeight = (float)(52 + titleBarHeight) + stickyHeight;
Expand Down
3 changes: 3 additions & 0 deletions UniSky/Services/ISafeAreaService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using Windows.UI.Xaml;

namespace UniSky.Services
{
internal interface ISafeAreaService
{
SafeAreaInfo State { get; }
event EventHandler<SafeAreaUpdatedEventArgs> SafeAreaUpdated;

void SetTitlebarTheme(ElementTheme theme);
}
}
Loading