refactor: rewrite Launcher

* move main tabbar to a standalone control
* simpfy notification
This commit is contained in:
leo 2024-06-26 20:56:29 +08:00
parent 1ce0d0f7bf
commit e330862ec9
No known key found for this signature in database
8 changed files with 406 additions and 349 deletions

View file

@ -112,20 +112,14 @@ namespace SourceGit
public static void RaiseException(string context, string message)
{
if (Current is App app && app._notificationReceiver != null)
{
var notice = new Models.Notification() { IsError = true, Message = message };
app._notificationReceiver.OnReceiveNotification(context, notice);
}
if (Current is App app && app._launcher != null)
app._launcher.DispatchNotification(context, message, true);
}
public static void SendNotification(string context, string message)
{
if (Current is App app && app._notificationReceiver != null)
{
var notice = new Models.Notification() { IsError = false, Message = message };
app._notificationReceiver.OnReceiveNotification(context, notice);
}
if (Current is App app && app._launcher != null)
app._launcher.DispatchNotification(context, message, false);
}
public static void SetLocale(string localeKey)
@ -285,9 +279,8 @@ namespace SourceGit
BindingPlugins.DataValidators.RemoveAt(0);
Native.OS.SetupEnternalTools();
var launcher = new Views.Launcher();
_notificationReceiver = launcher;
desktop.MainWindow = launcher;
_launcher = new ViewModels.Launcher();
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
if (ViewModels.Preference.Instance.ShouldCheck4UpdateOnStartup)
{
@ -330,8 +323,8 @@ namespace SourceGit
return default;
}
private ViewModels.Launcher _launcher = null;
private ResourceDictionary _activeLocale = null;
private ResourceDictionary _colorOverrides = null;
private Models.INotificationReceiver _notificationReceiver = null;
}
}

View file

@ -2,6 +2,9 @@
using System.IO;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
@ -40,7 +43,7 @@ namespace SourceGit.ViewModels
var root = new Commands.QueryRepositoryRootPath(path).Result();
if (string.IsNullOrEmpty(root))
{
Pages[0].Notifications.Add(new Models.Notification
Pages[0].Notifications.Add(new Notification
{
IsError = true,
Message = $"Given path: '{path}' is NOT a valid repository!"
@ -124,7 +127,7 @@ namespace SourceGit.ViewModels
ActivePage = Pages[prevIdx];
}
public void CloseTab(object param)
public void CloseTab(LauncherPage page)
{
if (Pages.Count == 1)
{
@ -148,7 +151,6 @@ namespace SourceGit.ViewModels
return;
}
LauncherPage page = param as LauncherPage;
if (page == null)
page = _activePage;
@ -250,6 +252,108 @@ namespace SourceGit.ViewModels
ActivePage = page;
}
public void DispatchNotification(string pageId, string message, bool isError)
{
var notification = new Notification() {
IsError = isError,
Message = message,
};
foreach (var page in Pages)
{
var id = page.Node.Id.Replace("\\", "/");
if (id == pageId)
{
page.Notifications.Add(notification);
return;
}
}
if (_activePage != null)
_activePage.Notifications.Add(notification);
}
public void DismissNotification(Notification notice)
{
if (notice != null)
ActivePage?.Notifications.Remove(notice);
}
public ContextMenu CreateContextForPageTab(LauncherPage page)
{
if (page == null)
return null;
var menu = new ContextMenu();
var close = new MenuItem();
close.Header = App.Text("PageTabBar.Tab.Close");
close.InputGesture = KeyGesture.Parse(OperatingSystem.IsMacOS() ? "⌘+W" : "Ctrl+W");
close.Click += (o, e) =>
{
CloseTab(page);
e.Handled = true;
};
menu.Items.Add(close);
var closeOthers = new MenuItem();
closeOthers.Header = App.Text("PageTabBar.Tab.CloseOther");
closeOthers.Click += (o, e) =>
{
CloseOtherTabs();
e.Handled = true;
};
menu.Items.Add(closeOthers);
var closeRight = new MenuItem();
closeRight.Header = App.Text("PageTabBar.Tab.CloseRight");
closeRight.Click += (o, e) =>
{
CloseRightTabs();
e.Handled = true;
};
menu.Items.Add(closeRight);
if (page.Node.IsRepository)
{
var bookmark = new MenuItem();
bookmark.Header = App.Text("PageTabBar.Tab.Bookmark");
bookmark.Icon = App.CreateMenuIcon("Icons.Bookmark");
for (int i = 0; i < Models.Bookmarks.Supported.Count; i++)
{
var icon = App.CreateMenuIcon("Icons.Bookmark");
icon.Fill = Models.Bookmarks.Brushes[i];
icon.Stroke = App.Current.FindResource("Brush.FG1") as Brush;
icon.StrokeThickness = i == 0 ? 1.0 : 0;
var dupIdx = i;
var setter = new MenuItem();
setter.Header = icon;
setter.Click += (o, e) =>
{
page.Node.Bookmark = dupIdx;
e.Handled = true;
};
bookmark.Items.Add(setter);
}
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(bookmark);
var copyPath = new MenuItem();
copyPath.Header = App.Text("PageTabBar.Tab.CopyPath");
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyPath.Click += (o, e) =>
{
page.CopyPath();
e.Handled = true;
};
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(copyPath);
}
return menu;
}
private void CloseRepositoryInTab(LauncherPage page)
{
if (page.Data is Repository repo)

View file

@ -24,11 +24,11 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _isTabSplitterVisible, value);
}
public AvaloniaList<Models.Notification> Notifications
public AvaloniaList<Notification> Notifications
{
get;
set;
} = new AvaloniaList<Models.Notification>();
} = new AvaloniaList<Notification>();
public LauncherPage()
{
@ -53,12 +53,6 @@ namespace SourceGit.ViewModels
App.CopyText(_node.Id);
}
public void DismissNotification(object param)
{
if (param is Models.Notification notice)
Notifications.Remove(notice);
}
private RepositoryNode _node = null;
private object _data = null;
private bool _isTabSplitterVisible = true;

View file

@ -1,4 +1,4 @@
namespace SourceGit.Models
namespace SourceGit.ViewModels
{
public class Notification
{
@ -10,9 +10,4 @@
App.CopyText(Message);
}
}
public interface INotificationReceiver
{
void OnReceiveNotification(string ctx, Notification notice);
}
}

View file

@ -10,7 +10,6 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.Launcher"
x:DataType="vm:Launcher"
x:Name="me"
Icon="/App.ico"
Title="SourceGit"
MinWidth="1280" MinHeight="720"
@ -18,9 +17,9 @@
Height="{Binding Source={x:Static vm:Preference.Instance}, Path=Layout.LauncherHeight, Mode=TwoWay}"
WindowState="{Binding Source={x:Static vm:Preference.Instance}, Path=Layout.LauncherWindowState, Mode=TwoWay}"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid x:Name="MainLayout">
<Grid.RowDefinitions>
<RowDefinition Height="{Binding #me.TitleBarHeight}"/>
<RowDefinition Height="38"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
@ -40,7 +39,7 @@
<v:CaptionButtonsMacOS VerticalAlignment="Bottom"/>
</Border>
<!-- Menu -->
<!-- Menu (Windows/Linux) -->
<Button Grid.Column="0" Classes="icon_button" VerticalAlignment="Bottom" IsVisible="{OnPlatform True, macOS=False}">
<Button.Margin>
<OnPlatform Default="4,0,2,3" macOS="4,0,6,3"/>
@ -76,160 +75,7 @@
</Button>
<!-- Pages Tabs-->
<Grid x:Name="launcherTabsContainer" Grid.Column="1" Height="30" ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Bottom" SizeChanged="UpdateScrollIndicator">
<RepeatButton x:Name="leftScrollIndicator" Grid.Column="0" Classes="icon_button" Width="18" Height="30" Click="ScrollTabsLeft">
<Path Width="8" Height="14" Stretch="Fill" Data="{StaticResource Icons.TriangleLeft}"/>
</RepeatButton>
<ScrollViewer Grid.Column="1"
x:Name="launcherTabsScroller"
HorizontalAlignment="Left"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Disabled"
PointerWheelChanged="ScrollTabs"
ScrollChanged="OnTabsScrollChanged">
<StackPanel x:Name="launcherTabsBar" Orientation="Horizontal" SizeChanged="UpdateScrollIndicator">
<ListBox Classes="launcher_page_tabbar"
ItemsSource="{Binding Pages}"
SelectionMode="AlwaysSelected"
SelectedItem="{Binding ActivePage, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" Height="30"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type vm:LauncherPage}">
<Border Classes="launcher_pagetab"
Height="30"
BorderThickness="1,1,1,0"
CornerRadius="6,6,0,0"
PointerPressed="OnPointerPressedTab"
PointerMoved="OnPointerMovedOverTab"
PointerReleased="OnPointerReleasedTab"
Loaded="SetupDragAndDrop">
<Border.ContextMenu>
<ContextMenu>
<MenuItem Header="{DynamicResource Text.PageTabBar.Tab.Close}"
Command="{Binding #me.((vm:Launcher)DataContext).CloseTab}"
CommandParameter="{Binding}"
InputGesture="{OnPlatform Ctrl+W, macOS=⌘+W}"/>
<MenuItem Header="{DynamicResource Text.PageTabBar.Tab.CloseOther}"
Command="{Binding #me.((vm:Launcher)DataContext).CloseOtherTabs}"/>
<MenuItem Header="{DynamicResource Text.PageTabBar.Tab.CloseRight}"
Command="{Binding #me.((vm:Launcher)DataContext).CloseRightTabs}"/>
<MenuItem Header="-" IsVisible="{Binding Node.IsRepository}"/>
<MenuItem IsVisible="{Binding Node.IsRepository}">
<MenuItem.Header>
<Grid Height="20" ColumnDefinitions="Auto,*">
<TextBlock Grid.Column="0" Text="{DynamicResource Text.PageTabBar.Tab.Bookmark}"/>
<ComboBox Grid.Column="1"
HorizontalAlignment="Right" VerticalAlignment="Center"
BorderThickness=".5" Margin="8,0,0,0"
ItemsSource="{x:Static m:Bookmarks.Supported}"
SelectedIndex="{Binding Node.Bookmark, Mode=TwoWay}">
<ComboBox.Resources>
<Thickness x:Key="ComboBoxPadding">12,2,0,2</Thickness>
<Thickness x:Key="ComboBoxEditableTextPadding">11,2,32,2</Thickness>
<x:Double x:Key="ComboBoxMinHeight">22</x:Double>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<Border Height="20" VerticalAlignment="Center">
<Path Width="12" Height="12"
Fill="{Binding Converter={x:Static c:BookmarkConverters.ToBrush}}"
StrokeThickness="{Binding Converter={x:Static c:BookmarkConverters.ToStrokeThickness}}"
Stroke="{DynamicResource Brush.FG1}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Data="{StaticResource Icons.Bookmark}"/>
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</MenuItem.Header>
</MenuItem>
<MenuItem Header="-" IsVisible="{Binding Node.IsRepository}"/>
<MenuItem Header="{DynamicResource Text.PageTabBar.Tab.CopyPath}" Command="{Binding CopyPath}" IsVisible="{Binding Node.IsRepository}"/>
</ContextMenu>
</Border.ContextMenu>
<Grid Width="{Binding Source={x:Static vm:Preference.Instance}, Path=UseFixedTabWidth, Converter={x:Static c:BoolConverters.ToPageTabWidth}}" Height="30" ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center">
<Path Grid.Column="0"
Width="12" Height="12" Margin="12,0"
Fill="{Binding Node.Bookmark, Converter={x:Static c:BookmarkConverters.ToBrush}}"
StrokeThickness="{Binding Node.Bookmark, Converter={x:Static c:BookmarkConverters.ToStrokeThickness}}"
Stroke="{DynamicResource Brush.FG1}"
Data="{StaticResource Icons.Bookmark}"
IsVisible="{Binding Node.IsRepository}"
IsHitTestVisible="False"/>
<Path Grid.Column="0"
Width="12" Height="12" Margin="12,0"
Fill="{DynamicResource Brush.FG1}"
Data="{StaticResource Icons.Repositories}"
IsVisible="{Binding !Node.IsRepository}"
IsHitTestVisible="False"/>
<TextBlock Grid.Column="1"
Classes="monospace"
FontSize="12"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Center"
Text="{Binding Node.Name}"
IsVisible="{Binding Node.IsRepository}"
IsHitTestVisible="False"/>
<TextBlock Grid.Column="1"
Classes="monospace"
FontSize="12"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Center"
Text="{DynamicResource Text.PageTabBar.Welcome.Title}"
IsVisible="{Binding !Node.IsRepository}"
IsHitTestVisible="False"/>
<Button Grid.Column="2"
Classes="icon_button"
Width="16" Height="16" Margin="12,0"
Command="{Binding #me.((vm:Launcher)DataContext).CloseTab}"
CommandParameter="{Binding}">
<ToolTip.Tip>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="{DynamicResource Text.PageTabBar.Tab.Close}" VerticalAlignment="Center"/>
<TextBlock Margin="16,0,0,0" Text="{OnPlatform Ctrl+W, macOS=⌘+W}" Opacity=".6" FontSize="11" VerticalAlignment="Center"/>
</StackPanel>
</ToolTip.Tip>
<Path Width="8" Height="8" Data="{StaticResource Icons.Window.Close}"/>
</Button>
<Rectangle Grid.Column="2"
Width=".5" Height="20"
HorizontalAlignment="Right" VerticalAlignment="Center"
Fill="{DynamicResource Brush.FG2}"
IsVisible="{Binding IsTabSplitterVisible}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Classes="icon_button"
Width="16" Height="16"
Margin="8,0"
Command="{Binding AddNewTab}">
<ToolTip.Tip>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="{DynamicResource Text.PageTabBar.New}" VerticalAlignment="Center"/>
<TextBlock Margin="16,0,0,0" Text="{OnPlatform Ctrl+T, macOS=⌘+T}" Opacity=".6" FontSize="11" VerticalAlignment="Center"/>
</StackPanel>
</ToolTip.Tip>
<Path Width="12" Height="12" Data="{StaticResource Icons.Plus}"/>
</Button>
</StackPanel>
</ScrollViewer>
<RepeatButton x:Name="rightScrollIndicator" Grid.Column="2" Classes="icon_button" Width="18" Height="30" Click="ScrollTabsRight">
<Path Width="8" Height="14" Stretch="Fill" Data="{StaticResource Icons.TriangleRight}"/>
</RepeatButton>
</Grid>
<v:LauncherTabBar Grid.Column="1" Height="30" VerticalAlignment="Bottom"/>
<!-- Caption Buttons (Windows/Linux)-->
<Border Grid.Column="2" Margin="32,0,0,0" IsVisible="{OnPlatform True, macOS=False}">
@ -298,7 +144,7 @@
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding ActivePage.Notifications}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="m:Notification">
<DataTemplate DataType="vm:Notification">
<Border Margin="10,6" HorizontalAlignment="Stretch" VerticalAlignment="Top" Effect="drop-shadow(0 0 12 #A0000000)">
<Border Padding="8" CornerRadius="6" Background="{DynamicResource Brush.Popup}">
<Grid RowDefinitions="26,Auto,32">
@ -330,9 +176,8 @@
<Button Classes="flat primary"
Margin="0,0"
Command="{Binding #me.((vm:Launcher)DataContext).ActivePage.DismissNotification}"
CommandParameter="{Binding}"
Content="{DynamicResource Text.Close}"/>
Content="{DynamicResource Text.Close}"
Click="OnDismissNotification"/>
</StackPanel>
</Grid>
</Border>

View file

@ -7,42 +7,13 @@ using Avalonia.Interactivity;
namespace SourceGit.Views
{
public partial class Launcher : ChromelessWindow, Models.INotificationReceiver
public partial class Launcher : ChromelessWindow
{
public static readonly StyledProperty<GridLength> TitleBarHeightProperty =
AvaloniaProperty.Register<Launcher, GridLength>(nameof(TitleBarHeight), new GridLength(38, GridUnitType.Pixel));
public GridLength TitleBarHeight
{
get => GetValue(TitleBarHeightProperty);
set => SetValue(TitleBarHeightProperty, value);
}
public Launcher()
{
DataContext = new ViewModels.Launcher();
InitializeComponent();
}
public void OnReceiveNotification(string ctx, Models.Notification notice)
{
if (DataContext is ViewModels.Launcher vm)
{
foreach (var page in vm.Pages)
{
var pageId = page.Node.Id.Replace("\\", "/");
if (pageId == ctx)
{
page.Notifications.Add(notice);
return;
}
}
if (vm.ActivePage != null)
vm.ActivePage.Notifications.Add(notice);
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
@ -51,9 +22,9 @@ namespace SourceGit.Views
{
var state = (WindowState)change.NewValue;
if (state == WindowState.Maximized)
SetCurrentValue(TitleBarHeightProperty, new GridLength(OperatingSystem.IsMacOS() ? 34 : 30));
MainLayout.RowDefinitions[0].Height = new GridLength(OperatingSystem.IsMacOS() ? 34 : 30);
else
SetCurrentValue(TitleBarHeightProperty, new GridLength(38, GridUnitType.Pixel));
MainLayout.RowDefinitions[0].Height = new GridLength(38);
}
}
@ -198,137 +169,19 @@ namespace SourceGit.Views
_pressedTitleBar = false;
}
private void ScrollTabs(object sender, PointerWheelEventArgs e)
{
if (!e.KeyModifiers.HasFlag(KeyModifiers.Shift))
{
if (e.Delta.Y < 0)
launcherTabsScroller.LineRight();
else if (e.Delta.Y > 0)
launcherTabsScroller.LineLeft();
e.Handled = true;
}
}
private void ScrollTabsLeft(object sender, RoutedEventArgs e)
{
launcherTabsScroller.LineLeft();
e.Handled = true;
}
private void ScrollTabsRight(object sender, RoutedEventArgs e)
{
launcherTabsScroller.LineRight();
e.Handled = true;
}
private void UpdateScrollIndicator(object sender, SizeChangedEventArgs e)
{
if (launcherTabsBar.Bounds.Width > launcherTabsContainer.Bounds.Width)
{
leftScrollIndicator.IsVisible = true;
rightScrollIndicator.IsVisible = true;
}
else
{
leftScrollIndicator.IsVisible = false;
rightScrollIndicator.IsVisible = false;
}
e.Handled = true;
}
private void OnTabsScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (sender is ScrollViewer scrollViewer)
{
leftScrollIndicator.IsEnabled = scrollViewer.Offset.X > 0;
rightScrollIndicator.IsEnabled = scrollViewer.Offset.X < scrollViewer.Extent.Width - scrollViewer.Viewport.Width;
}
}
private void SetupDragAndDrop(object sender, RoutedEventArgs e)
{
if (sender is Border border)
{
DragDrop.SetAllowDrop(border, true);
border.AddHandler(DragDrop.DropEvent, DropTab);
}
e.Handled = true;
}
private void OnPointerPressedTab(object sender, PointerPressedEventArgs e)
{
var border = sender as Border;
var point = e.GetCurrentPoint(border);
if (point.Properties.IsMiddleButtonPressed)
{
var vm = DataContext as ViewModels.Launcher;
vm.CloseTab(border.DataContext as ViewModels.LauncherPage);
e.Handled = true;
return;
}
_pressedTab = true;
_startDragTab = false;
_pressedTabPosition = e.GetPosition(sender as Border);
}
private void OnPointerReleasedTab(object sender, PointerReleasedEventArgs e)
{
_pressedTab = false;
_startDragTab = false;
}
private void OnPointerMovedOverTab(object sender, PointerEventArgs e)
{
if (_pressedTab && !_startDragTab && sender is Border border)
{
var delta = e.GetPosition(border) - _pressedTabPosition;
var sizeSquired = delta.X * delta.X + delta.Y * delta.Y;
if (sizeSquired < 64)
return;
_startDragTab = true;
var data = new DataObject();
data.Set("MovedTab", border.DataContext);
DragDrop.DoDragDrop(e, data, DragDropEffects.Move);
}
e.Handled = true;
}
private void DropTab(object sender, DragEventArgs e)
{
if (e.Data.Contains("MovedTab") && sender is Border border)
{
var to = border.DataContext as ViewModels.LauncherPage;
var moved = e.Data.Get("MovedTab") as ViewModels.LauncherPage;
if (to != null && moved != null && to != moved && DataContext is ViewModels.Launcher vm)
{
vm.MoveTab(moved, to);
}
}
_pressedTab = false;
_startDragTab = false;
e.Handled = true;
}
private void OnPopupSure(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Launcher vm)
{
vm.ActivePage.ProcessPopup();
}
e.Handled = true;
}
private void OnPopupCancel(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Launcher vm)
{
vm.ActivePage.CancelPopup();
}
e.Handled = true;
}
@ -337,9 +190,14 @@ namespace SourceGit.Views
OnPopupCancel(sender, e);
}
private void OnDismissNotification(object sender, RoutedEventArgs e)
{
if (sender is Button btn && DataContext is ViewModels.Launcher vm)
vm.DismissNotification(btn.DataContext as ViewModels.Notification);
e.Handled = true;
}
private bool _pressedTitleBar = false;
private bool _pressedTab = false;
private Point _pressedTabPosition = new Point();
private bool _startDragTab = false;
}
}

View file

@ -0,0 +1,120 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:m="using:SourceGit.Models"
xmlns:c="using:SourceGit.Converters"
xmlns:v="using:SourceGit.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.LauncherTabBar"
x:DataType="vm:Launcher">
<Grid ColumnDefinitions="Auto,*,Auto">
<RepeatButton x:Name="LeftScrollIndicator" Grid.Column="0" Classes="icon_button" Width="18" Height="30" Click="ScrollTabsLeft">
<Path Width="8" Height="14" Stretch="Fill" Data="{StaticResource Icons.TriangleLeft}"/>
</RepeatButton>
<ScrollViewer Grid.Column="1"
x:Name="LauncherTabsScroller"
HorizontalAlignment="Left"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Disabled"
PointerWheelChanged="ScrollTabs"
LayoutUpdated="OnTabsLayoutUpdated">
<StackPanel Orientation="Horizontal">
<ListBox Classes="launcher_page_tabbar"
ItemsSource="{Binding Pages}"
SelectionMode="AlwaysSelected"
SelectedItem="{Binding ActivePage, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Height="30"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type vm:LauncherPage}">
<Border Classes="launcher_pagetab"
Height="30"
BorderThickness="1,1,1,0"
CornerRadius="6,6,0,0"
PointerPressed="OnPointerPressedTab"
PointerMoved="OnPointerMovedOverTab"
PointerReleased="OnPointerReleasedTab"
Loaded="SetupDragAndDrop"
ContextRequested="OnTabContextRequested">
<Grid Width="{Binding Source={x:Static vm:Preference.Instance}, Path=UseFixedTabWidth, Converter={x:Static c:BoolConverters.ToPageTabWidth}}" Height="30" ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center">
<Path Grid.Column="0"
Width="12" Height="12" Margin="12,0"
Fill="{Binding Node.Bookmark, Converter={x:Static c:BookmarkConverters.ToBrush}}"
StrokeThickness="{Binding Node.Bookmark, Converter={x:Static c:BookmarkConverters.ToStrokeThickness}}"
Stroke="{DynamicResource Brush.FG1}"
Data="{StaticResource Icons.Bookmark}"
IsVisible="{Binding Node.IsRepository}"
IsHitTestVisible="False"/>
<Path Grid.Column="0"
Width="12" Height="12" Margin="12,0"
Fill="{DynamicResource Brush.FG1}"
Data="{StaticResource Icons.Repositories}"
IsVisible="{Binding !Node.IsRepository}"
IsHitTestVisible="False"/>
<TextBlock Grid.Column="1"
Classes="monospace"
FontSize="12"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Center"
Text="{Binding Node.Name}"
IsVisible="{Binding Node.IsRepository}"
IsHitTestVisible="False"/>
<TextBlock Grid.Column="1"
Classes="monospace"
FontSize="12"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Center"
Text="{DynamicResource Text.PageTabBar.Welcome.Title}"
IsVisible="{Binding !Node.IsRepository}"
IsHitTestVisible="False"/>
<Button Grid.Column="2"
Classes="icon_button"
Width="16" Height="16" Margin="12,0"
Click="OnCloseTab">
<ToolTip.Tip>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="{DynamicResource Text.PageTabBar.Tab.Close}" VerticalAlignment="Center"/>
<TextBlock Margin="16,0,0,0" Text="{OnPlatform Ctrl+W, macOS=⌘+W}" Opacity=".6" FontSize="11" VerticalAlignment="Center"/>
</StackPanel>
</ToolTip.Tip>
<Path Width="8" Height="8" Data="{StaticResource Icons.Window.Close}"/>
</Button>
<Rectangle Grid.Column="2"
Width=".5" Height="20"
HorizontalAlignment="Right" VerticalAlignment="Center"
Fill="{DynamicResource Brush.FG2}"
IsVisible="{Binding IsTabSplitterVisible}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Classes="icon_button"
Width="16" Height="16"
Margin="8,0"
Command="{Binding AddNewTab}">
<ToolTip.Tip>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="{DynamicResource Text.PageTabBar.New}" VerticalAlignment="Center"/>
<TextBlock Margin="16,0,0,0" Text="{OnPlatform Ctrl+T, macOS=⌘+T}" Opacity=".6" FontSize="11" VerticalAlignment="Center"/>
</StackPanel>
</ToolTip.Tip>
<Path Width="12" Height="12" Data="{StaticResource Icons.Plus}"/>
</Button>
</StackPanel>
</ScrollViewer>
<RepeatButton x:Name="RightScrollIndicator" Grid.Column="2" Classes="icon_button" Width="18" Height="30" Click="ScrollTabsRight">
<Path Width="8" Height="14" Stretch="Fill" Data="{StaticResource Icons.TriangleRight}"/>
</RepeatButton>
</Grid>
</UserControl>

View file

@ -0,0 +1,148 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public partial class LauncherTabBar : UserControl
{
public LauncherTabBar()
{
InitializeComponent();
}
private void ScrollTabs(object sender, PointerWheelEventArgs e)
{
if (!e.KeyModifiers.HasFlag(KeyModifiers.Shift))
{
if (e.Delta.Y < 0)
LauncherTabsScroller.LineRight();
else if (e.Delta.Y > 0)
LauncherTabsScroller.LineLeft();
e.Handled = true;
}
}
private void ScrollTabsLeft(object sender, RoutedEventArgs e)
{
LauncherTabsScroller.LineLeft();
e.Handled = true;
}
private void ScrollTabsRight(object sender, RoutedEventArgs e)
{
LauncherTabsScroller.LineRight();
e.Handled = true;
}
private void OnTabsLayoutUpdated(object sender, EventArgs e)
{
if (LauncherTabsScroller.Extent.Width > LauncherTabsScroller.Viewport.Width)
{
LeftScrollIndicator.IsVisible = true;
LeftScrollIndicator.IsEnabled = LauncherTabsScroller.Offset.X > 0;
RightScrollIndicator.IsVisible = true;
RightScrollIndicator.IsEnabled = LauncherTabsScroller.Offset.X < LauncherTabsScroller.Extent.Width - LauncherTabsScroller.Viewport.Width;
}
else
{
LeftScrollIndicator.IsVisible = false;
RightScrollIndicator.IsVisible = false;
}
}
private void SetupDragAndDrop(object sender, RoutedEventArgs e)
{
if (sender is Border border)
{
DragDrop.SetAllowDrop(border, true);
border.AddHandler(DragDrop.DropEvent, DropTab);
}
e.Handled = true;
}
private void OnPointerPressedTab(object sender, PointerPressedEventArgs e)
{
var border = sender as Border;
var point = e.GetCurrentPoint(border);
if (point.Properties.IsMiddleButtonPressed)
{
var vm = DataContext as ViewModels.Launcher;
vm.CloseTab(border.DataContext as ViewModels.LauncherPage);
e.Handled = true;
return;
}
_pressedTab = true;
_startDragTab = false;
_pressedTabPosition = e.GetPosition(sender as Border);
}
private void OnPointerReleasedTab(object sender, PointerReleasedEventArgs e)
{
_pressedTab = false;
_startDragTab = false;
}
private void OnPointerMovedOverTab(object sender, PointerEventArgs e)
{
if (_pressedTab && !_startDragTab && sender is Border border)
{
var delta = e.GetPosition(border) - _pressedTabPosition;
var sizeSquired = delta.X * delta.X + delta.Y * delta.Y;
if (sizeSquired < 64)
return;
_startDragTab = true;
var data = new DataObject();
data.Set("MovedTab", border.DataContext);
DragDrop.DoDragDrop(e, data, DragDropEffects.Move);
}
e.Handled = true;
}
private void DropTab(object sender, DragEventArgs e)
{
if (e.Data.Contains("MovedTab") && sender is Border border)
{
var to = border.DataContext as ViewModels.LauncherPage;
var moved = e.Data.Get("MovedTab") as ViewModels.LauncherPage;
if (to != null && moved != null && to != moved && DataContext is ViewModels.Launcher vm)
{
vm.MoveTab(moved, to);
}
}
_pressedTab = false;
_startDragTab = false;
e.Handled = true;
}
private void OnTabContextRequested(object sender, ContextRequestedEventArgs e)
{
if (sender is Border border && DataContext is ViewModels.Launcher vm)
{
var menu = vm.CreateContextForPageTab(border.DataContext as ViewModels.LauncherPage);
border.OpenContextMenu(menu);
}
e.Handled = true;
}
private void OnCloseTab(object sender, RoutedEventArgs e)
{
if (sender is Button btn && DataContext is ViewModels.Launcher vm)
vm.CloseTab(btn.DataContext as ViewModels.LauncherPage);
e.Handled = true;
}
private bool _pressedTab = false;
private Point _pressedTabPosition = new Point();
private bool _startDragTab = false;
}
}