MahApps / MahApps/MahApps.Metro

Flyout Left Adapt with Horizontal Alignment Stretch is not changing Theme of CommandButtons

Open
#2,967 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
C#
Stars
9.8k
Forks
2.4k
Avg merge
1h 2m
Merged PRs (30d)
56

Description

## What steps will reproduce this issue?
When setting a Flyout to Adapt, Position Left, and Horizontal Content Alignment Stretch, the command button are not changing theme to the inverted theme.

![flyouts not working](https://cloud.githubusercontent.com/assets/6874149/26124622/c1b3cc10-3a44-11e7-9877-dfd2ff712745.gif)

I updated the MetroWindowHelper to detect if the HorizontalContentAlignment = stretch

```c#
public static void UpdateWindowCommandsForFlyout(this MetroWindow window, Flyout flyout)
{
window.ChangeAllWindowCommandsBrush(flyout.Foreground, flyout.Position, flyout.HorizontalContentAlignment == HorizontalAlignment.Stretch);
}

private static void InvokeActionOnWindowCommands(this MetroWindow window, Action action1, Action action2 = null, Position position = Position.Top, bool horizontalStretch = false)
{
if (window.LeftWindowCommandsPresenter == null || window.RightWindowCommandsPresenter == null || window.WindowButtonCommands == null)
{
return;
}

if ((position == Position.Left || position == Position.Top) && !horizontalStretch)
{
action1(window.LeftWindowCommands);
}

if (position == Position.Right || position == Position.Top || horizontalStretch)
{
action1(window.RightWindowCommands);
if (action2 == null)
{
action1(window.WindowButtonCommands);
}
else
{
action2(window.WindowButtonCommands);
}
}
}

private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush, Position position = Position.Top, bool horizontalStretch = false)
{
if (brush == null)
{
// set the theme to light by default
window.InvokeActionOnWindowCommands(x => x.SetValue(WindowCommands.ThemeProperty, Theme.Light),
x => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Light), position, horizontalStretch);

// clear the foreground property
window.InvokeActionOnWindowCommands(x => x.ClearValue(Control.ForegroundProperty), null, position, horizontalStretch);
}
else
{
// calculate brush color lightness
var color = ((SolidColorBrush)brush).Color;

var r = color.R / 255.0f;
var g = color.G / 255.0f;
var b = color.B / 255.0f;

var max = r;
var min = r;

if (g > max) max = g;
if (b > max) max = b;

if (g < min) min = g;
if (b < min) min = b;

var lightness = (max + min) / 2;

// set the theme based on color lightness
if (lightness > 0.1)
{
window.InvokeActionOnWindowCommands(x => x.SetValue(WindowCommands.ThemeProperty, Theme.Light),
x => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Light), position, horizontalStretch);
}
else
{
window.InvokeActionOnWindowCommands(x => x.SetValue(WindowCommands.ThemeProperty, Theme.Dark),
x => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Dark), position, horizontalStretch);
}

// set the foreground property
window.InvokeActionOnWindowCommands(x => x.SetValue(Control.ForegroundProperty, brush), null, position, horizontalStretch);
}
}
```

![flyouts working](https://cloud.githubusercontent.com/assets/6874149/26124691/fd4fd430-3a44-11e7-835d-f050a433c52a.gif)

I will get a pull request put together once I get some time.

### Environment
- MahApps.Metro 1.5 and tested with 2.0
- Windows 10
- Visual Studio 2017 Community
- .NET Framework 4.5, .NET Framework 4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate MetroWindowHelper and read UpdateWindowCommandsForFlyout, InvokeActionOnWindowCommands, and ChangeAllWindowCommandsBrush. Reproduce the Flyout with Adapt theme, Left position, and Stretch alignment, then verify the command buttons use the inverted theme while existing flyout positions and alignments remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.