MahApps.Metro icon indicating copy to clipboard operation
MahApps.Metro copied to clipboard

Feature: Add <mah:MetroWindow.**Center**WindowCommands>

Open BornToBeRoot opened this issue 1 year ago • 5 comments

Hi @punker76,

can you please add a possibility to show controls in the middle of the window title?

Maybe <mah:MetroWindow.CenterWindowCommands> in the MetroWindow.

Like this:

image

Something similar to VS Code:

image

BornToBeRoot avatar Sep 21 '23 09:09 BornToBeRoot

You can use TitleTemplate to customize a center window

William-Joshua avatar Nov 24 '23 07:11 William-Joshua

微信截图_20231205102721 微信截图_20231205102658

You can use mah:Flyout and TilteTempate to do it

The TilteTempate Code Like

         <Button  caliburn:Message.Attach="[Event Click] =[Action NavigateForward]"
             Background="Transparent"  Height="28px" Width="28px"  BorderBrush="Transparent">
             <iconPacks:PackIconMaterial  Kind="ArrowLeft"  VerticalAlignment="Center"
                                                HorizontalAlignment="Center" Foreground="WhiteSmoke" Width="12" Height="12"/>

         </Button>
         <Button   caliburn:Message.Attach="[Event Click] =[Action NavigateBackward]"
             Background="Transparent"  Height="28px" Width="28px" VerticalAlignment="Center" BorderBrush="Transparent">
             <iconPacks:PackIconMaterial  Kind="ArrowRight"  VerticalAlignment="Center"
                              HorizontalAlignment="Center" Foreground="WhiteSmoke"  Width="12" Height="12" />
         </Button>

         <Button   caliburn:Message.Attach="[Event Click] =[Action ShowSearchBar]"
             Background="Transparent" Height="24px" Width="320px" VerticalAlignment="Center" Margin="8 0 ">
             <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
                 <iconPacks:PackIconMaterial  Kind="Magnify"  VerticalAlignment="Center"
          HorizontalAlignment="Center" Foreground="WhiteSmoke"  Width="12" Height="12" />
                 <TextBlock Text="Please Input Key Word ..." Margin="8 0 "
                            Foreground="WhiteSmoke" FontWeight="Light"></TextBlock>
             </StackPanel>
         </Button>
     </StackPanel>
 </Grid>

and the Flyout like mahmaps flyout demo's show top button

William-Joshua avatar Dec 05 '23 02:12 William-Joshua

Thanks! I will try it ;)

BornToBeRoot avatar Dec 05 '23 02:12 BornToBeRoot

@William-Joshua thanks again - it's working now:

RunCommand

If anyone is looking for the code:

  <mah:MetroWindow.TitleTemplate>
        <DataTemplate>
            <Grid
                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}, Path=ActualWidth}">
                <TextBlock Grid.Column="0" Grid.Row="0"
                           Margin="8,-1,1,0"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Left"
                           FontFamily="{DynamicResource MahApps.Fonts.Family.Window.Title}"
                           FontSize="{DynamicResource MahApps.Font.Size.Window.Title}"
                           Text="{TemplateBinding Content}"
                           TextTrimming="CharacterEllipsis" />
                <Button Grid.Column="0" Grid.Row="0"
                        Command="{Binding OpenRunCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Cursor="Hand"
                        Style="{StaticResource CleanButton}">
                    <Grid Height="30">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="270" />
                        </Grid.ColumnDefinitions>
                        <Rectangle Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"
                                   Fill="{DynamicResource MahApps.Brushes.Gray10}" />
                        <Rectangle Grid.Column="0" Grid.Row="0"
                                   Margin="5,0"
                                   Width="20" Height="20"
                                   HorizontalAlignment="Right"
                                   Fill="{DynamicResource MahApps.Brushes.Gray8}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Uniform" Visual="{iconPacks:Material Kind=ChevronRight}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                        <TextBlock Grid.Column="1" Grid.Row="0"
                                   Style="{StaticResource InfoTextBlock}"
                                   Text="{x:Static localization:Strings.Watermark_RunCommandWithHotKey}" />
                    </Grid>
                </Button>
            </Grid>
        </DataTemplate>
    </mah:MetroWindow.TitleTemplate>
    <mah:MetroWindow.InputBindings>
        <KeyBinding Command="{Binding OpenRunCommand}" Modifiers="Control+Shift" Key="P" />
    </mah:MetroWindow.InputBindings>
 <mah:MetroWindow.Flyouts>
        <mah:FlyoutsControl>
            <mah:Flyout x:Name="FlyoutRunCommand"
                        IsKeyboardFocusWithinChanged="FlyoutRunCommand_IsKeyboardFocusWithinChanged"
                        Position="Top"
                        TitleVisibility="Collapsed"
                        CloseButtonVisibility="Collapsed"
                        IsModal="False"
                        Width="500" MaxHeight="400"
                        Theme="Adapt">
                <mah:Flyout.InputBindings>
                    <KeyBinding Command="{Binding RunCommandCloseCommand}" Key="Escape" />
                    <KeyBinding Command="{Binding RunCommandDoCommand}" Key="Enter" />
                </mah:Flyout.InputBindings>
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid Grid.Column="0" Grid.Row="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Rectangle Grid.Column="0" Grid.Row="0"
                                   Margin="5,0"
                                   Width="20" Height="20"
                                   HorizontalAlignment="Right"
                                   Fill="{DynamicResource MahApps.Brushes.Gray8}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Uniform" Visual="{iconPacks:Material Kind=ChevronRight}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                        <TextBox Grid.Column="1" Grid.Row="0"
                                 Text="{Binding RunCommandSearch, UpdateSourceTrigger=PropertyChanged}"
                                 mah:TextBoxHelper.Watermark="{x:Static localization:Strings.Watermark_RunCommand}">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}">
                                    <Setter Property="mah:TextBoxHelper.ClearTextButton" Value="True" />
                                    <Setter Property="BorderThickness" Value="0,0,0,1" />
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                        </Grid>
                    <ListView Grid.Column="0" Grid.Row="2" 
                              x:Name="ListViewRunCommand"
                              ItemsSource="{Binding RunCommands}"
                              SelectedItem="{Binding SelectedRunCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              SelectionMode="Single"
                              PreviewMouseLeftButtonUp="ListViewRunCommand_OnPreviewMouseLeftButtonUp"
                              Style="{StaticResource DefaultListBox}">
                        <ListView.ItemTemplate>
                            <DataTemplate DataType="{x:Type networkManager:RunCommandInfo}">
                                <Border BorderThickness="1">
                                    <Grid Margin="5">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Column="0" Grid.Row="0"
                                                   Text="{Binding TranslatedName}"
                                                   Style="{StaticResource BoldTextBlock}" />
                                        <TextBlock Grid.Column="0" Grid.Row="1"
                                                   Style="{StaticResource AccentTextBlock}"
                                                   FontFamily="Consolas"
                                                   Margin="0,5,0,0">
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="{}{0} {1}">
                                                    <Binding Path="Command" />
                                                    <Binding Path="ExampleArguments" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                    <TextBlock Grid.Column="0" Grid.Row="2" Margin="32,0,0,0">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DefaultTextBlock}">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Gray3}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RunCommands.Count}" Value="0">
                                        <Setter Property="Visibility" Value="Visible" />
                                        <Setter Property="Text" Value="{x:Static localization:Strings.NoMatchingCommands}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </Grid>
            </mah:Flyout>
        </mah:FlyoutsControl>
    </mah:MetroWindow.Flyouts>
    #region Run Command
    
    #region Variables

    private ICollectionView _runCommands;

    public ICollectionView RunCommands
    {
        get => _runCommands;
        private set
        {
            if (value == _runCommands)
                return;

            _runCommands = value;
            OnPropertyChanged();
        }
    }

    private RunCommandInfo _selectedRunCommand;

    public RunCommandInfo SelectedRunCommand
    {
        get => _selectedRunCommand;
        set
        {
            if (value == _selectedRunCommand)
                return;

            _selectedRunCommand = value;
            OnPropertyChanged();
        }
    }

    private string _runCommandSearch;

    public string RunCommandSearch
    {
        get => _runCommandSearch;
        set
        {
            if (value == _runCommandSearch)
                return;

            _runCommandSearch = value;

            RefreshRunCommandsView();

            OnPropertyChanged();
        }
    }
    #endregion
    
    #region ICommands & Actions
    public ICommand OpenRunCommand => new RelayCommand(_ => OpenRunAction());

    private void OpenRunAction()
    {
        FlyoutRunCommand.IsOpen = true;
    }

    public ICommand RunCommandDoCommand => new RelayCommand(_ => RunCommandDoAction());

    private void RunCommandDoAction()
    {
        RunCommandDo();
    }

    public ICommand RunCommandCloseCommand => new RelayCommand(_ => RunCommandCloseAction());

    private void RunCommandCloseAction()
    {
        RunCommandFlyoutClose();
    }
    #endregion
    
    #region Methods
    private void SetRunCommandsView(RunCommandInfo selectedRunCommand = null)
    {
        RunCommands = new CollectionViewSource { Source = RunCommandManager.GetList() }.View;

        RunCommands.Filter = o =>
        {
            if (o is not RunCommandInfo info)
                return false;

            if (string.IsNullOrEmpty(RunCommandSearch))
                return true;

            return info.TranslatedName.IndexOf(RunCommandSearch, StringComparison.OrdinalIgnoreCase) > -1 ||
                   info.Name.IndexOf(RunCommandSearch, StringComparison.OrdinalIgnoreCase) > -1;
        };

        if (selectedRunCommand != null)
            SelectedRunCommand = RunCommands.Cast<RunCommandInfo>()
                .FirstOrDefault(x => x.Name == selectedRunCommand.Name);

        SelectedRunCommand ??= RunCommands.Cast<RunCommandInfo>().FirstOrDefault();
    }

    private void RefreshRunCommandsView()
    {
        SetRunCommandsView(SelectedRunCommand);
    }
    
    /// <summary>
    /// Execute the selected run command.
    /// </summary>
    private void RunCommandDo()
    {
        // Do nothing if no command is selected
        if (SelectedRunCommand == null)
            return;

        // Do the command
        switch (SelectedRunCommand.Type)
        {
            // Redirect to application
            case RunCommandType.Application:
                if (SettingsViewIsOpen)
                    CloseSettings();

                var applicationName = (ApplicationName)Enum.Parse(typeof(ApplicationName), SelectedRunCommand.Name);
                EventSystem.RedirectToApplication(applicationName, string.Empty);
                break;
            // Redirect to settings
            case RunCommandType.Setting:
                EventSystem.RedirectToSettings();
                break;
        }

        // Close the flyout
        RunCommandFlyoutClose();
    }
    
    /// <summary>
    /// Close the run command flyout and clear the search.
    /// </summary>
    private void RunCommandFlyoutClose()
    {
        if (!FlyoutRunCommand.IsOpen)
            return;

        FlyoutRunCommand.AreAnimationsEnabled = false;
        FlyoutRunCommand.IsOpen = false;
        RunCommandSearch = string.Empty;
    }
    #endregion
    
    #region  Events
    private void ListViewRunCommand_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        RunCommandDo();
    }

    private void FlyoutRunCommand_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // Close flyout if the focus is lost.
        if (e.NewValue is not false)
            return;

        RunCommandFlyoutClose();
    }
    #endregion

    #endregion

Complete PR: https://github.com/BornToBeRoot/NETworkManager/pull/2577

BornToBeRoot avatar Dec 14 '23 21:12 BornToBeRoot

@punker76 I'm not sure if this is still relevant now - feel free to close the feature request if you don't want to include it as a CenterWindowCommand by default. It works for me now.

BornToBeRoot avatar Dec 14 '23 21:12 BornToBeRoot

@punker76 I found some issues with the mah:MetroWindow.TitleTemplate implementation (https://github.com/MahApps/MahApps.Metro/issues/4419#issuecomment-1856683453) when i try to center something. E.g. when a LeftWindowCommand is set, it's no longer in the center.

image

But with the code in my pull request it works: https://github.com/MahApps/MahApps.Metro/pull/4421

image

Would it be possible to get this merged for version 3?

I don't want to maintain my own fork for this feature 😄

BornToBeRoot avatar Mar 25 '24 21:03 BornToBeRoot