Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Move popup placing logic outside (win)
Browse files Browse the repository at this point in the history
  • Loading branch information
atsvirchkova committed Nov 17, 2020
1 parent 7b3d541 commit 2e054cc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<Compile Include="ui\Behaviors\NumericInputTextBoxBehavior.cs" />
<Compile Include="ui\Behaviors\RepositionPopupWithParentWindowBehavior.cs" />
<Compile Include="ui\Behaviors\TextBoxHelper.cs" />
<Compile Include="ui\Behaviors\TimelineTimeEntryPopupHelper.cs" />
<Compile Include="ui\controls\DatePickerHelper.cs" />
<Compile Include="ui\controls\DaysOfWeekSelector.xaml.cs">
<DependentUpon>DaysOfWeekSelector.xaml</DependentUpon>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Windows;
using System.Windows.Controls;
using TogglDesktop.ViewModels;

namespace TogglDesktop.Behaviors
{
public static class TimelineTimeEntryPopupHelper
{
public static void OpenPopup(this TimelineTimeEntryBlockPopup popup, FrameworkElement placementTarget, ScrollViewer scroll)
{
if (placementTarget.DataContext is TimeEntryBlock curBlock)
{
popup.DataContext = curBlock;
popup.Popup.PlacementTarget = placementTarget;
popup.Popup.IsOpen = true;
var visibleTopOffset = scroll.VerticalOffset + 10;
var visibleBottomOffset = scroll.VerticalOffset + scroll.ActualHeight - 10;
var offset = curBlock.VerticalOffset + placementTarget.ActualHeight / 2;
popup.Popup.VerticalOffset = Math.Min(Math.Max(visibleTopOffset, offset), visibleBottomOffset) -
curBlock.VerticalOffset;
}
}

public static void ClosePopup(this TimelineTimeEntryBlockPopup popup)
{
popup.Popup.IsOpen = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using TogglDesktop.Behaviors;
using TogglDesktop.ViewModels;

namespace TogglDesktop
Expand Down Expand Up @@ -40,18 +41,17 @@ private void OnThumbDragStarted(object sender, DragStartedEventArgs e)
ViewModel.IsDragged = true;
}

private TimelineTimeEntryBlockPopup _popup;
private readonly TimelineTimeEntryBlockPopup _popupContainer = new TimelineTimeEntryBlockPopup();
private ScrollViewer _scroll;
protected override void OnMouseEnter(MouseEventArgs e)
{
_popup ??= new TimelineTimeEntryBlockPopup();
_popup.DataContext = ViewModel;
_popup.Popup.PlacementTarget = this;
_popup.Popup.IsOpen = true;
_scroll ??= this.FindParent<ScrollViewer>();
_popupContainer.OpenPopup(this, _scroll);
}

protected override void OnMouseLeave(MouseEventArgs e)
{
_popup.Popup.IsOpen = false;
_popupContainer.ClosePopup();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
</Border>
</Popup>
<togglDesktop:TimelineTimeEntryBlockPopup Grid.Row="1" Grid.Column="2"
x:Name="TimeEntryPopup"/>
x:Name="TimeEntryPopupContainer"/>
</Grid>
</ScrollViewer>
<TextBlock Grid.Row="3" Grid.Column="1" Text="No entries here... Go ahead and track some time!" Width="125" HorizontalAlignment="Center"
Expand Down
14 changes: 4 additions & 10 deletions src/ui/windows/TogglDesktop/TogglDesktop/ui/views/Timeline.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using DynamicData.Binding;
using ReactiveUI;
using TogglDesktop.Behaviors;
using TogglDesktop.Resources;
using TogglDesktop.ViewModels;

Expand Down Expand Up @@ -86,22 +87,15 @@ private void OnMainViewScrollLoaded(object sender, RoutedEventArgs e)

private void OnTimeEntryBlockMouseEnter(object sender, MouseEventArgs e)
{
if (sender is FrameworkElement uiElement && uiElement.DataContext is TimeEntryBlock curBlock)
if (sender is FrameworkElement uiElement)
{
TimeEntryPopup.DataContext = curBlock;
TimeEntryPopup.Popup.PlacementTarget = uiElement;
TimeEntryPopup.Popup.IsOpen = true;
var visibleTopOffset = MainViewScroll.VerticalOffset+10;
var visibleBottomOffset = MainViewScroll.VerticalOffset + MainViewScroll.ActualHeight-10;
var offset = curBlock.VerticalOffset + uiElement.ActualHeight / 2;
TimeEntryPopup.Popup.VerticalOffset = Math.Min(Math.Max(visibleTopOffset, offset), visibleBottomOffset) -
curBlock.VerticalOffset;
TimeEntryPopupContainer.OpenPopup(uiElement, MainViewScroll);
}
}

private void OnTimeEntyrBlockMouseLeave(object sender, MouseEventArgs e)
{
TimeEntryPopup.Popup.IsOpen = false;
TimeEntryPopupContainer.ClosePopup();
}

private double? _dragStartedPoint;
Expand Down

0 comments on commit 2e054cc

Please sign in to comment.