123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.Shapes;
- using Avalonia.Markup.Xaml;
- using Avalonia.Media;
- using Avalonia.VisualTree;
- using System;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- /*
- MIT License
- Original work Copyright (c) 2020 Fichtelcoder
- Modified work Copyright 2022 Veloe Aetess
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- namespace VeloeMinecraftLauncher.Views.TitleBar
- {
- public partial class TitleBarWindow : UserControl
- {
- private Button minimizeButton;
- private Button maximizeButton;
- private Path maximizeIcon;
- private ToolTip maximizeToolTip;
- private Button closeButton;
- private Image windowIcon;
- private DockPanel titleBar;
- private DockPanel titleBarBackground;
- private TextBlock systemChromeTitle;
- private NativeMenuBar seamlessMenuBar;
- private NativeMenuBar defaultMenuBar;
- public static readonly StyledProperty<bool> IsSeamlessProperty =
- AvaloniaProperty.Register<TitleBarWindow, bool>(nameof(IsSeamless));
- public static readonly StyledProperty<bool> IsIconVisibleProperty =
- AvaloniaProperty.Register<TitleBarWindow, bool>(nameof(IsIconVisible));
- public static readonly StyledProperty<bool> IsMaximizeVisibleProperty =
- AvaloniaProperty.Register<TitleBarWindow, bool>(nameof(IsMaximizeVisible));
- public static readonly StyledProperty<string> TitleProperty =
- AvaloniaProperty.Register<TitleBarWindow, string>(nameof(TitleText));
- public bool IsSeamless
- {
- get { return GetValue(IsSeamlessProperty); }
- set
- {
- SetValue(IsSeamlessProperty, value);
- if (titleBarBackground != null &&
- systemChromeTitle != null &&
- seamlessMenuBar != null &&
- defaultMenuBar != null)
- {
- titleBarBackground.IsVisible = IsSeamless ? false : true;
- systemChromeTitle.IsVisible = IsSeamless ? false : true;
- seamlessMenuBar.IsVisible = IsSeamless ? true : false;
- defaultMenuBar.IsVisible = IsSeamless ? false : true;
- if (IsSeamless == false)
- {
- //titleBar.Resources["SystemControlForegroundBaseHighBrush"] = new SolidColorBrush { Color = new Color(255, 0, 0, 0) };
- }
- }
- }
- }
- public bool IsIconVisible
- {
- get { return GetValue(IsIconVisibleProperty); }
- set
- {
- SetValue(IsIconVisibleProperty, value);
- if (windowIcon != null
- )
- {
- windowIcon.IsVisible = IsIconVisible ? true : false;
- //systemChromeTitle.IsVisible = IsIconVisible ? true : false;
- }
- }
- }
- public bool IsMaximizeVisible
- {
- get { return GetValue(IsIconVisibleProperty); }
- set
- {
- SetValue(IsIconVisibleProperty, value);
- if (maximizeButton != null &&
- maximizeIcon != null &&
- maximizeToolTip != null)
- {
- maximizeButton.IsVisible = IsIconVisible ? true : false;
- maximizeIcon.IsVisible = IsIconVisible ? true : false;
- maximizeToolTip.IsVisible = IsIconVisible ? true : false;
- }
- }
- }
- public string TitleText
- {
- get
- {
- if (systemChromeTitle != null)
- return systemChromeTitle.Text;
- return "";
- }
- set
- {
- if (systemChromeTitle != null)
- systemChromeTitle.Text = value;
- }
- }
- public TitleBarWindow()
- {
- this.InitializeComponent();
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == false)
- {
- this.IsVisible = false;
- }
- else
- {
- minimizeButton = this.FindControl<Button>("MinimizeButton");
- maximizeButton = this.FindControl<Button>("MaximizeButton");
- maximizeIcon = this.FindControl<Path>("MaximizeIcon");
- maximizeToolTip = this.FindControl<ToolTip>("MaximizeToolTip");
- closeButton = this.FindControl<Button>("CloseButton");
- windowIcon = this.FindControl<Image>("WindowIcon");
- minimizeButton.Click += MinimizeWindow;
- maximizeButton.Click += MaximizeWindow;
- closeButton.Click += CloseWindow;
- windowIcon.DoubleTapped += CloseWindow;
- titleBar = this.FindControl<DockPanel>("TitleBar");
- titleBarBackground = this.FindControl<DockPanel>("TitleBarBackground");
- systemChromeTitle = this.FindControl<TextBlock>("SystemChromeTitle");
- seamlessMenuBar = this.FindControl<NativeMenuBar>("SeamlessMenuBar");
- defaultMenuBar = this.FindControl<NativeMenuBar>("DefaultMenuBar");
- SubscribeToWindowState();
- }
- }
- private void CloseWindow(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
- {
- (this.GetVisualRoot() as Window)?.Close();
- }
- private void MaximizeWindow(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
- {
- Window? hostWindow = this.GetVisualRoot() as Window;
- if (hostWindow is null) return;
- if (hostWindow.WindowState == WindowState.Normal)
- {
- hostWindow.WindowState = WindowState.Maximized;
- }
- else
- {
- hostWindow.WindowState = WindowState.Normal;
- }
- }
- private void MinimizeWindow(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
- {
- Window? hostWindow = this.GetVisualRoot() as Window;
- if (hostWindow is null) return;
- hostWindow.WindowState = WindowState.Minimized;
- }
- private async void SubscribeToWindowState()
- {
- Window? hostWindow = this.GetVisualRoot() as Window;
- while (hostWindow == null)
- {
- hostWindow = this.GetVisualRoot() as Window;
- await Task.Delay(50);
- }
- hostWindow.GetObservable(Window.WindowStateProperty).Subscribe(s =>
- {
- if (s != WindowState.Maximized)
- {
- maximizeIcon.Data = Geometry.Parse("M2048 2048v-2048h-2048v2048h2048zM1843 1843h-1638v-1638h1638v1638z");
- hostWindow.Padding = new Thickness(0, 0, 0, 0);
- maximizeToolTip.Content = "Maximize";
- }
- if (s == WindowState.Maximized)
- {
- maximizeIcon.Data = Geometry.Parse("M2048 1638h-410v410h-1638v-1638h410v-410h1638v1638zm-614-1024h-1229v1229h1229v-1229zm409-409h-1229v205h1024v1024h205v-1229z");
- hostWindow.Padding = new Thickness(7, 7, 7, 7);
- maximizeToolTip.Content = "Restore Down";
- // This should be a more universal approach in both cases, but I found it to be less reliable, when for example double-clicking the title bar.
- /*hostWindow.Padding = new Thickness(
- hostWindow.OffScreenMargin.Left,
- hostWindow.OffScreenMargin.Top,
- hostWindow.OffScreenMargin.Right,
- hostWindow.OffScreenMargin.Bottom);*/
- }
- });
- }
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
- }
- }
- #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|