Skip to content

Commit 0bcf04d

Browse files
authored
Merge pull request #636 from cairoshell/task-thumb-pos
2 parents 0b02ce1 + cced77b commit 0bcf04d

File tree

8 files changed

+162
-43
lines changed

8 files changed

+162
-43
lines changed

Cairo Desktop/Cairo Desktop/DwmThumbnail.xaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public IntPtr Handle
2929
{
3030
get
3131
{
32-
HwndSource source = (HwndSource)HwndSource.FromVisual(this);
32+
HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
33+
34+
if (source == null)
35+
{
36+
return IntPtr.Zero;
37+
}
38+
3339
IntPtr handle = source.Handle;
3440
return handle;
3541
}
@@ -53,7 +59,7 @@ public IntPtr SourceWindowHandle
5359
}
5460

5561
_sourceWindowHandle = value;
56-
if (_sourceWindowHandle != IntPtr.Zero && NativeMethods.DwmRegisterThumbnail(Handle, _sourceWindowHandle, out _thumbHandle) == 0)
62+
if (_sourceWindowHandle != IntPtr.Zero && Handle != IntPtr.Zero && NativeMethods.DwmRegisterThumbnail(Handle, _sourceWindowHandle, out _thumbHandle) == 0)
5763
Refresh();
5864
}
5965
}

Cairo Desktop/Cairo Desktop/MenuBar.xaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using CairoDesktop.Application.Structs;
44
using CairoDesktop.Common;
55
using CairoDesktop.Configuration;
6-
using CairoDesktop.Interop;
76
using CairoDesktop.SupportingClasses;
87
using ManagedShell;
98
using System;
@@ -15,7 +14,6 @@
1514
using System.Windows.Input;
1615
using CairoDesktop.Infrastructure.ObjectModel;
1716
using CairoDesktop.Interfaces;
18-
using CairoDesktop.Services;
1917
using ManagedShell.AppBar;
2018
using ManagedShell.Common.Helpers;
2119
using ManagedShell.Common.Logging;

Cairo Desktop/Cairo Desktop/SupportingClasses/TaskThumbOrientationConverter.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CairoDesktop.Configuration;
2-
using System;
1+
using System;
32
using System.Windows.Controls;
43
using System.Windows.Data;
54

@@ -10,14 +9,12 @@ public class TaskThumbOrientationConverter : IValueConverter
109
{
1110
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
1211
{
13-
if (Settings.Instance.EnableTaskbarThumbnails)
12+
if (value is bool showThumbnails)
1413
{
15-
return Orientation.Horizontal;
16-
}
17-
else
18-
{
19-
return Orientation.Vertical;
14+
return showThumbnails ? Orientation.Horizontal : Orientation.Vertical;
2015
}
16+
17+
return Orientation.Horizontal;
2118
}
2219

2320
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

Cairo Desktop/Cairo Desktop/TaskThumbWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ResizeMode="NoResize"
1414
Background="Transparent"
1515
SourceInitialized="Window_SourceInitialized"
16+
SizeChanged="ThumbWindow_SizeChanged"
1617
Closing="Window_Closing"
1718
MouseLeave="Window_MouseLeave"
1819
Topmost="True"
@@ -56,7 +57,7 @@
5657
</ItemsControl.ItemTemplate>
5758
<ItemsControl.ItemsPanel>
5859
<ItemsPanelTemplate>
59-
<StackPanel Orientation="{Binding Converter={StaticResource orientationConverter}}"></StackPanel>
60+
<StackPanel Orientation="{Binding ElementName=ThumbWindow, Path=ShowThumbnails, Converter={StaticResource orientationConverter}}"></StackPanel>
6061
</ItemsPanelTemplate>
6162
</ItemsControl.ItemsPanel>
6263
</ItemsControl>

Cairo Desktop/Cairo Desktop/TaskThumbWindow.xaml.cs

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System;
2+
using System.Collections.Specialized;
3+
using System.ComponentModel;
4+
using System.Runtime.CompilerServices;
25
using System.Windows;
36
using System.Windows.Controls;
47
using System.Windows.Input;
58
using System.Windows.Interop;
9+
using CairoDesktop.Configuration;
610
using ManagedShell.Common.Helpers;
711
using ManagedShell.Interop;
812

@@ -11,8 +15,28 @@ namespace CairoDesktop
1115
/// <summary>
1216
/// Interaction logic for TaskThumbWindow.xaml
1317
/// </summary>
14-
public partial class TaskThumbWindow : Window
18+
public partial class TaskThumbWindow : Window, INotifyPropertyChanged
1519
{
20+
const int MAX_THUMBS = 5;
21+
22+
public bool ShowThumbnails
23+
{
24+
get
25+
{
26+
if (!IsDwmEnabled || !Settings.Instance.EnableTaskbarThumbnails)
27+
{
28+
return false;
29+
}
30+
31+
if (TaskButton != null && TaskButton.WindowGroup != null && TaskButton.WindowGroup.Count > MAX_THUMBS)
32+
{
33+
return false;
34+
}
35+
36+
return true;
37+
}
38+
}
39+
1640
internal TaskButton TaskButton;
1741
internal bool IsAnimating = true;
1842
internal bool IsDwmEnabled;
@@ -40,34 +64,76 @@ private void Window_SourceInitialized(object sender, EventArgs e)
4064
handle = helper.Handle;
4165
WindowHelper.HideWindowFromTasks(handle);
4266

43-
// get anchor point
44-
Point taskButtonPoint = TaskButton.GetThumbnailAnchor();
45-
46-
if (Configuration.Settings.Instance.TaskbarPosition == 1)
67+
if (Settings.Instance.TaskbarPosition == 1)
4768
{
4869
// taskbar on top
49-
Top = taskButtonPoint.Y + TaskButton.ActualHeight;
50-
5170
bdrThumb.Style = FindResource("TaskThumbWindowBorderTopStyle") as Style;
5271
bdrThumbInner.Style = FindResource("TaskThumbWindowInnerBorderTopStyle") as Style;
5372

5473
bdrTranslate.Y *= -1;
5574

5675
ToolTipService.SetPlacement(this, System.Windows.Controls.Primitives.PlacementMode.Bottom);
5776
}
77+
78+
SetPosition();
79+
80+
if (TaskButton != null && TaskButton.WindowGroup != null)
81+
{
82+
((INotifyCollectionChanged)TaskButton.WindowGroup).CollectionChanged += TaskThumbWindow_CollectionChanged;
83+
}
84+
}
85+
86+
private void TaskThumbWindow_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
87+
{
88+
OnPropertyChanged("ShowThumbnails");
89+
}
90+
91+
private void SetPosition()
92+
{
93+
if (TaskButton == null || TaskButton.ParentTaskbar == null)
94+
{
95+
return;
96+
}
97+
98+
Point taskButtonPoint = TaskButton.GetThumbnailAnchor();
99+
100+
if (Settings.Instance.TaskbarPosition == 1)
101+
{
102+
// taskbar on top
103+
Top = taskButtonPoint.Y + TaskButton.ActualHeight;
104+
}
58105
else
59106
{
60107
Top = taskButtonPoint.Y - ActualHeight;
61108
}
62109

63-
Left = taskButtonPoint.X - ((ActualWidth - TaskButton.ActualWidth) / 2);
110+
double desiredLeft = taskButtonPoint.X - ((ActualWidth - TaskButton.ActualWidth) / 2);
111+
112+
if (desiredLeft < 0)
113+
{
114+
Left = 0;
115+
}
116+
else if (desiredLeft + ActualWidth > TaskButton.ParentTaskbar.ActualWidth)
117+
{
118+
double bump = desiredLeft + ActualWidth - TaskButton.ParentTaskbar.ActualWidth;
119+
Left = desiredLeft - bump;
120+
}
121+
else
122+
{
123+
Left = desiredLeft;
124+
}
64125
}
65126

66-
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
127+
private void Window_Closing(object sender, CancelEventArgs e)
67128
{
68129
isClosing = true;
69130
TaskButton.ThumbWindow = null;
70131
TaskButton.SetParentAutoHide(true);
132+
133+
if (TaskButton != null && TaskButton.WindowGroup != null)
134+
{
135+
((INotifyCollectionChanged)TaskButton.WindowGroup).CollectionChanged -= TaskThumbWindow_CollectionChanged;
136+
}
71137
}
72138

73139
private void Window_MouseLeave(object sender, MouseEventArgs e)
@@ -80,5 +146,18 @@ private void Storyboard_Completed(object sender, EventArgs e)
80146
{
81147
IsAnimating = false;
82148
}
149+
150+
#region INotifyPropertyChanged
151+
public event PropertyChangedEventHandler PropertyChanged;
152+
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
153+
{
154+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
155+
}
156+
#endregion
157+
158+
private void ThumbWindow_SizeChanged(object sender, SizeChangedEventArgs e)
159+
{
160+
SetPosition();
161+
}
83162
}
84163
}

Cairo Desktop/Cairo Desktop/TaskThumbnail.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
MouseUp="bdrThumbInner_MouseUp">
5555
<StackPanel>
5656
<StackPanel Name="pnlTitle"
57-
Orientation="Horizontal"
58-
Margin="0,0,0,5">
57+
Orientation="Horizontal">
5958
<Image Source="{Binding Path=Icon, Mode=OneWay, FallbackValue={StaticResource NullIcon}, TargetNullValue={StaticResource NullIcon}}"
6059
Width="16"
6160
Height="16" />
@@ -74,7 +73,8 @@
7473
</StackPanel>
7574
<local:DwmThumbnail x:Name="dwmThumbnail"
7675
Width="180"
77-
Height="120" />
76+
Height="120"
77+
Margin="0,5,0,0" />
7878
</StackPanel>
7979
</Border>
8080
</UserControl>

Cairo Desktop/Cairo Desktop/TaskThumbnail.xaml.cs

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,68 @@ public TaskThumbnail()
3434

3535
private void UserControl_Loaded(object sender, RoutedEventArgs e)
3636
{
37-
if (_isLoaded)
37+
if (_isLoaded || ThumbWindow == null || ThumbWindow.TaskButton == null || ThumbWindow.TaskButton.ParentTaskbar == null)
3838
{
3939
return;
4040
}
4141

4242
_window = DataContext as ApplicationWindow;
43+
_taskbarHwnd = ThumbWindow.TaskButton.ParentTaskbar.Handle;
4344

44-
// if DWM or thumbnails disabled, hide the thumbnail placeholder
45-
if (!ThumbWindow.IsDwmEnabled || !Settings.Instance.EnableTaskbarThumbnails)
45+
SetupThumbnail();
46+
47+
ThumbWindow.PropertyChanged += ThumbWindow_PropertyChanged;
48+
49+
_isLoaded = true;
50+
}
51+
52+
private void SetupThumbnail()
53+
{
54+
if (!ThumbWindow.ShowThumbnails)
4655
{
47-
dwmThumbnail.Visibility = Visibility.Collapsed;
48-
pnlTitle.Margin = new Thickness(0);
56+
DisableThumbnail();
4957
}
5058
else
5159
{
52-
if (ThumbWindow.TaskButton.ParentTaskbar == null)
53-
{
54-
return;
55-
}
56-
_taskbarHwnd = ThumbWindow.TaskButton.ParentTaskbar.Handle;
60+
EnableThumbnail();
61+
}
62+
}
5763

58-
// set up thumbnail
59-
dwmThumbnail.DpiScale = ThumbWindow.TaskButton.ParentTaskbar.DpiScale;
60-
dwmThumbnail.ThumbnailOpacity = 0;
61-
dwmThumbnail.SourceWindowHandle = _window.Handle;
64+
private void EnableThumbnail()
65+
{
66+
// show thumbnail placeholder
67+
dwmThumbnail.Visibility = Visibility.Visible;
68+
69+
if (ThumbWindow == null || ThumbWindow.TaskButton == null || ThumbWindow.TaskButton.ParentTaskbar == null)
70+
{
71+
return;
72+
}
73+
74+
// set up thumbnail
75+
dwmThumbnail.DpiScale = ThumbWindow.TaskButton.ParentTaskbar.DpiScale;
76+
dwmThumbnail.SourceWindowHandle = _window.Handle;
6277

78+
if (ThumbWindow.IsAnimating)
79+
{
6380
// set up animation
81+
dwmThumbnail.ThumbnailOpacity = 0;
6482
CompositionTarget.Rendering += CompositionTarget_Rendering;
6583
}
84+
else
85+
{
86+
dwmThumbnail.ThumbnailOpacity = 255;
87+
}
88+
}
6689

67-
_isLoaded = true;
90+
private void DisableThumbnail()
91+
{
92+
// hide the thumbnail placeholder if thumbnails are not being used
93+
dwmThumbnail.Visibility = Visibility.Collapsed;
94+
}
95+
96+
private void ThumbWindow_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
97+
{
98+
SetupThumbnail();
6899
}
69100

70101
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
@@ -74,10 +105,16 @@ private void UserControl_Unloaded(object sender, RoutedEventArgs e)
74105
return;
75106
}
76107

77-
if (ThumbWindow.IsDwmEnabled)
108+
if (ThumbWindow != null)
78109
{
79-
dwmThumbnail.SourceWindowHandle = IntPtr.Zero;
110+
if (ThumbWindow.IsDwmEnabled)
111+
{
112+
dwmThumbnail.SourceWindowHandle = IntPtr.Zero;
113+
}
114+
115+
ThumbWindow.PropertyChanged -= ThumbWindow_PropertyChanged;
80116
}
117+
81118
_isLoaded = false;
82119
}
83120

Cairo Desktop/Cairo Desktop/Taskbar.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
313313
break;
314314
case "TaskbarGroupingStyle":
315315
_shellManager.Tasks.SetTaskCategoryProvider(getTaskCategoryProvider());
316+
setTaskButtonSize();
316317
break;
317318
}
318319
}
@@ -336,7 +337,7 @@ private void setTaskButtonSize()
336337
if (TasksList.Items.Groups != null)
337338
{
338339
// calculate the maximum per-button size
339-
double adjustedSize = Math.Floor((ActualWidth - quickLaunchList.ActualWidth - (btnDesktopOverlay.ActualWidth - 5) - btnTaskList.ActualWidth - (TasksList.Items.Groups.Count * 4 - 3) - 11) / TasksList.Items.Count);
340+
double adjustedSize = Math.Floor((ActualWidth - quickLaunchList.ActualWidth - (btnDesktopOverlay.ActualWidth - 5) - btnTaskList.ActualWidth - (TasksList.Items.Groups.Count * 4 - 3) - 11) / (Settings.Instance.TaskbarGroupingStyle == 2 ? TasksList.Items.Groups.Count : TasksList.Items.Count));
340341

341342
if (adjustedSize > baseButtonWidth)
342343
{

0 commit comments

Comments
 (0)