Skip to content

Commit

Permalink
新增课程表自动滚动,修复大半夜删除课程导致程序崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
STBBRD committed Jul 27, 2024
1 parent c39b0ca commit a37950e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
4 changes: 3 additions & 1 deletion ZongziTEK_Blackboard_Sticker/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@
</Border>
</Grid>
</Border>

<!--课程表-->
<Border Grid.Row="1" Grid.Column="1" Background="{DynamicResource WindowBackgroundColor}" Margin="8,0,8,8" CornerRadius="8" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<ui:ScrollViewerEx Name="ScrollViewerShowCurriculum" PanningMode="VerticalOnly">
<ui:ScrollViewerEx Name="ScrollViewerShowCurriculum" PanningMode="VerticalOnly" ScrollChanged="ScrollViewerShowCurriculum_ScrollChanged">
<Grid Margin="16">
<TextBlock Visibility="Collapsed" Name="textBlockCurriculum" TextWrapping="Wrap" FontSize="28" Foreground="{DynamicResource ForegroundColor}" TextAlignment="Center"/>
<ikw:SimpleStackPanel Name="StackPanelShowTimetable" Spacing="4">
Expand Down Expand Up @@ -244,6 +245,7 @@
<MenuItem Name="MenuItemShowSundayTimetable" IsCheckable="True" Header="周日" Click="MenuItemShowSundayTimetable_Click"/>
<MenuItem Name="MenuItemShowTempTimetable" IsCheckable="True" Header="临时" Click="MenuItemShowTempTimetable_Click"/>
</MenuItem>
<MenuItem Name="MenuItemTimetableAutoScroll" Header="自动滚动" IsCheckable="True" Click="MenuItemTimetableAutoScroll_Click" IsChecked="True"/>
<MenuItem Header="编辑" Click="editCurriculumButton_Click"/>
</ui:MenuFlyout>
</ui:FlyoutService.Flyout>
Expand Down
60 changes: 59 additions & 1 deletion ZongziTEK_Blackboard_Sticker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
using ZongziTEK_Blackboard_Sticker.Pages;
using System.Windows.Interop;
using iNKORE.UI.WPF.Controls;
using System.Security.Permissions;

namespace ZongziTEK_Blackboard_Sticker
{
Expand Down Expand Up @@ -140,6 +139,10 @@ private void window_Loaded(object sender, RoutedEventArgs e)

window.BeginAnimation(LeftProperty, windowAnimation);
}

timetableScrollTimer.Tick += TimetableScrollTimer_Tick;
timetableScrollTimer.Start();
ScrollToCurrentLesson();
}

private void window_Activated(object sender, EventArgs e)
Expand Down Expand Up @@ -1162,6 +1165,7 @@ private void LoadTimetable()

private int lessonIndex = -1; // 第几节课
private bool isInClass = false; // 是否是上课时段
private int lastLessonIndex = -1;

private void CheckTimetable(object sender, EventArgs e)
{
Expand Down Expand Up @@ -1252,6 +1256,10 @@ private void CheckTimetable(object sender, EventArgs e)
}
}
}

// 自动滚动课程表
if (lessonIndex != lastLessonIndex) ScrollToCurrentLesson();
lastLessonIndex = lessonIndex;
}

timetableTimer.Start();
Expand Down Expand Up @@ -1310,6 +1318,56 @@ private void ShowLastClassOverNotification(bool isStrongNotificationEnabled)
}
}
}

private void ScrollToCurrentLesson()
{
if (Settings.TimetableSettings.IsTimetableEnabled && StackPanelShowTimetable.ActualHeight > (ScrollViewerShowCurriculum.ActualHeight - 32))
{
int extraMarginCount = 0;
foreach (Lesson lesson in timetableToShow)
{
if (lesson.IsSplitBelow) extraMarginCount++;
if (timetableToShow.IndexOf(lesson) >= lessonIndex) break;
}

ScrollViewerShowCurriculum.ScrollToVerticalOffset((lessonIndex + 1) * 48 * ScaleTimetable.ScaleX + 8 * extraMarginCount - 48);
}
}

private int scrollFreeTime = 0;

private DispatcherTimer timetableScrollTimer = new()
{
Interval = TimeSpan.FromSeconds(1)
};

private void TimetableScrollTimer_Tick(object sender, EventArgs e)
{
scrollFreeTime++;

if (scrollFreeTime > 5)
{
ScrollToCurrentLesson();
}
}

private void ScrollViewerShowCurriculum_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
scrollFreeTime = 0;
}

private void MenuItemTimetableAutoScroll_Click(object sender, RoutedEventArgs e)
{
if (MenuItemTimetableAutoScroll.IsChecked)
{
ScrollToCurrentLesson();
timetableScrollTimer.Start();
}
else
{
timetableScrollTimer.Stop();
}
}
#endregion
#endregion

Expand Down
14 changes: 13 additions & 1 deletion ZongziTEK_Blackboard_Sticker/TimetableEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,21 @@ private void ScrollViewerTimetable_PreviewMouseWheel(object sender, MouseWheelEv
#endregion

#region Load & Save
private Timetable Timetable = MainWindow.Timetable;
private Timetable Timetable = new Timetable();
private bool isEdited = false;

private async void LoadTimetable()
{
if (File.Exists(MainWindow.GetDataPath() + MainWindow.timetableFileName))
{
try
{
string text = File.ReadAllText(MainWindow.GetDataPath() + MainWindow.timetableFileName);
Timetable = JsonConvert.DeserializeObject<Timetable>(text);
}
catch { }
}

TextBlockLessonCount.Text = GetSelectedDay().Count.ToString();

ListStackPanel.Children.Clear();
Expand Down Expand Up @@ -297,6 +307,8 @@ private void ButtonPaste_Click(object sender, RoutedEventArgs e)
GetSelectedDay().Add(lesson);
}
LoadTimetable();

isEdited = true;
}

#endregion
Expand Down

0 comments on commit a37950e

Please sign in to comment.