Skip to content

Commit

Permalink
通过读取注册表来获取 BNS 路径,修复删除唯一课程导致崩溃的问题,新增上下课提醒持续时间设置项
Browse files Browse the repository at this point in the history
  • Loading branch information
STBBRD committed Mar 31, 2024
1 parent 0b50ce1 commit fb7eed9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 20 deletions.
14 changes: 12 additions & 2 deletions ZongziTEK_Blackboard_Sticker/Classes/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Settings
public Blackboard Blackboard { get; set; } = new Blackboard();
public InfoBoard InfoBoard { get; set; } = new InfoBoard();
public Automation Automation { get; set; } = new Automation();
public Update Update { get; set; } = new Update();
}

public class Storage
Expand All @@ -36,8 +37,11 @@ public class TimetableSettings
{
public bool IsTimetableEnabled { get; set; } = true;
public bool IsTimetableNotificationEnabled { get; set; } = true;
public bool UseDefaultBNSPath = true;
public string BNSPath { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Blackboard Notification Service";
/*public bool UseDefaultBNSPath { get; set; } = true;
public string BNSPath { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Blackboard Notification Service";*/
public double BeginNotificationTime { get; set; } = 4;
public double BeginNotificationPreTime { get; set; } = 10;
public double OverNotificationTime { get; set; } = 4;
}

public class Blackboard
Expand All @@ -61,4 +65,10 @@ public class Automation
{
public bool IsAutoHideHugoAssistantEnabled { get; set; } = false;
}

public class Update
{
public bool IsUpdateAutomatic { get; set; } = true;
public int UpdateChannel { get; set; } = 0; // 0 代表 Release 频道,1 代表 Preview 频道,暂未完成
}
}
27 changes: 24 additions & 3 deletions ZongziTEK_Blackboard_Sticker/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
<GroupBox Header="外观" Foreground="{DynamicResource ForegroundColor}">
<ui:SimpleStackPanel Spacing="12">
<ui:SimpleStackPanel Spacing="4">
<Slider Name="SliderWindowScale" ui:ControlHelper.Header="界面缩放" Minimum="0.5" Maximum="1.5" TickFrequency="0.1" IsSnapToTickEnabled="True" Value="1" PreviewMouseUp="SliderWindowScale_MouseUp" TickPlacement="Both" HorizontalAlignment="Left" Width="200"/>
<Slider Name="SliderWindowScale" ui:ControlHelper.Header="界面缩放" Minimum="0.5" Maximum="1.5" TickFrequency="0.1" IsSnapToTickEnabled="True" Value="1" PreviewMouseUp="SliderWindowScale_MouseUp" TickPlacement="Both" HorizontalAlignment="Left" Width="400"/>
<TextBlock FontSize="14" Text="{Binding Value, ElementName=SliderWindowScale}"/>
</ui:SimpleStackPanel>
<ui:ToggleSwitch Name="ToggleSwitchThemeAuto" Header="明暗主题跟随系统" OnContent="" OffContent="" Toggled="ToggleSwitchThemeAuto_Toggled" Foreground="{DynamicResource ForegroundColor}"/>
Expand Down Expand Up @@ -325,10 +325,31 @@
<TextBlock Text="开启上面的开关后,将使用带时间信息的课程表,并且出现上下课提醒功能。关闭上面的开关后,课程表就是文本信息,方便编辑,但没有上下课提醒。&#10;注意:如果需要使用上下课提醒,请下载 黑板通知服务 (https://github.com/STBBRD/Blackboard-Notification-Service)"
TextWrapping="Wrap" Opacity="0.75"/>
<ui:ToggleSwitch Name="ToggleSwitchTimetableNotification" Header="启用上下课提醒" OnContent="" OffContent="" IsOn="True" Toggled="ToggleSwitchTimetableNotification_Toggled" Foreground="{DynamicResource ForegroundColor}" IsEnabled="{Binding IsOn, ElementName=ToggleSwitchUseTimetable}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Name="TextBlockBNSStatus" Opacity="0.75" Text="黑板通知服务" VerticalAlignment="Center"/>
<ui:HyperlinkButton Name="ButtonRefreshBNSStatus" HorizontalContentAlignment="Left" Padding="0" Margin="4,0,0,0" VerticalAlignment="Center" Click="ButtonRefreshBNSStatus_Click">
<StackPanel Orientation="Horizontal">
<ui:FontIcon FontSize="12" FontFamily="{StaticResource SegoeFluentIcons}" Glyph="{x:Static ui:SegoeIcons.Refresh}"/>
<TextBlock FontSize="12" Margin="4,0,0,0" Text="重新检测"/>
</StackPanel>
</ui:HyperlinkButton>
</StackPanel>
<ui:ToggleSwitch Name="ToggleSwitchTempTimetable" Header="展示临时课表" OnContent="" OffContent="" IsOn="False" Toggled="ToggleSwitchTempTimetable_Toggled" Foreground="{DynamicResource ForegroundColor}"/>
<ui:ToggleSwitch Name="ToggleSwitchUseDefaultBNSPath" Header="使用默认黑板通知服务路径" OnContent="" OffContent="" IsOn="True" Toggled="ToggleSwitchUseDefaultBNSPath_Toggled" Foreground="{DynamicResource ForegroundColor}"/>
<!--<ui:ToggleSwitch Name="ToggleSwitchUseDefaultBNSPath" Header="使用默认黑板通知服务路径" OnContent="开" OffContent="关" IsOn="True" Toggled="ToggleSwitchUseDefaultBNSPath_Toggled" Foreground="{DynamicResource ForegroundColor}"/>
<TextBlock Text="黑板通知服务路径" FontSize="14"/>
<TextBox Name="TextBoxBNSPath" TextChanged="TextBoxBNSPath_TextChanged" Foreground="{DynamicResource ForegroundColor}" IsEnabled="{Binding IsOn, ElementName=ToggleSwitchUseDefaultBNSPath,Converter={StaticResource InverseBooleanConverter}}"/>
<TextBox Name="TextBoxBNSPath" TextChanged="TextBoxBNSPath_TextChanged" Foreground="{DynamicResource ForegroundColor}" IsEnabled="{Binding IsOn, ElementName=ToggleSwitchUseDefaultBNSPath,Converter={StaticResource InverseBooleanConverter}}"/>-->
<ui:SimpleStackPanel Spacing="4">
<Slider Name="SliderBeginNotificationTime" ui:ControlHelper.Header="课前通知持续时间(秒)" Minimum="3" Maximum="30" TickFrequency="1" IsSnapToTickEnabled="True" Value="4" TickPlacement="Both" HorizontalAlignment="Left" Width="400" ValueChanged="SliderBeginNotificationTime_ValueChanged"/>
<TextBlock FontSize="14" Text="{Binding Value, ElementName=SliderBeginNotificationTime}"/>
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Spacing="4">
<Slider Name="SliderBeginNotificationPreTime" ui:ControlHelper.Header="课前通知提前时间(秒)" Minimum="6" Maximum="60" TickFrequency="1" IsSnapToTickEnabled="True" Value="10" TickPlacement="Both" HorizontalAlignment="Left" Width="400" ValueChanged="SliderBeginNotificationPreTime_ValueChanged"/>
<TextBlock FontSize="14" Text="{Binding Value, ElementName=SliderBeginNotificationPreTime}"/>
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Spacing="4">
<Slider Name="SliderOverNotificationTime" ui:ControlHelper.Header="下课通知持续时间(秒)" Minimum="3" Maximum="30" TickFrequency="1" IsSnapToTickEnabled="True" Value="4" TickPlacement="Both" HorizontalAlignment="Left" Width="400" ValueChanged="SliderOverNotificationTime_ValueChanged"/>
<TextBlock FontSize="14" Text="{Binding Value, ElementName=SliderOverNotificationTime}"/>
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
</GroupBox>
<GroupBox Header="信息看板" Foreground="{DynamicResource ForegroundColor}">
Expand Down
99 changes: 84 additions & 15 deletions ZongziTEK_Blackboard_Sticker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ private void CheckTimetable(object sender, EventArgs e)

TimeSpan currentTime = new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, DateTime.Now.TimeOfDay.Seconds);

if (today != null)
if (today != null && today.Count != 0)
{
// 获取上课状态 lessonIndex 和 isInClass
foreach (var lesson in today)
Expand Down Expand Up @@ -1102,7 +1102,7 @@ private void CheckTimetable(object sender, EventArgs e)
}
else ShowLastClassOverNotification();
}
if (lessonIndex + 1 < today.Count && !isInClass && currentTime == today[lessonIndex + 1].StartTime - TimeSpan.FromSeconds(10)) // 有下一节课,在下一节课开始的数秒前
if (lessonIndex + 1 < today.Count && !isInClass && currentTime == today[lessonIndex + 1].StartTime - TimeSpan.FromSeconds(Settings.TimetableSettings.BeginNotificationPreTime)) // 有下一节课,在下一节课开始的数秒前
{
ShowClassBeginPreNotification(today, lessonIndex);
}
Expand Down Expand Up @@ -1154,7 +1154,7 @@ private void ShowClassBeginPreNotification(List<Lesson> today, int index)
string title = today[nextLessonIndex].Subject + "课 即将开始";
string subtitle = "此课程将从 " + startTimeString + " 开始,到 " + endTimeString + " 结束";

ShowNotificationBNS(title, subtitle, 3, false);
ShowNotificationBNS(title, subtitle, Settings.TimetableSettings.BeginNotificationTime, false);
}
}

Expand All @@ -1169,15 +1169,15 @@ private void ShowClassOverNotification(List<Lesson> today, int index)
string title = "下一节 " + today[nextLessonIndex].Subject + "课";
string subtitle = "课堂结束,下一节课将于 " + startTimeString + " 开始";

ShowNotificationBNS(title, subtitle, 3, false);
ShowNotificationBNS(title, subtitle, Settings.TimetableSettings.OverNotificationTime, false);
}
}

private void ShowLastClassOverNotification()
{
if (Settings.TimetableSettings.IsTimetableNotificationEnabled)
{
ShowNotificationBNS("课堂结束", "", 3, false);
ShowNotificationBNS("课堂结束", "", Settings.TimetableSettings.OverNotificationTime, false);
}
}
#endregion
Expand Down Expand Up @@ -1333,6 +1333,8 @@ private void iconShowSettingsPanel_MouseDown(object sender, MouseButtonEventArgs
{
if (borderSettingsPanel.Visibility == Visibility.Collapsed) borderSettingsPanel.Visibility = Visibility.Visible;
else btnHideSettingsPanel_Click(null, null);

ButtonRefreshBNSStatus_Click(null, null);
}
private void btnHideSettingsPanel_Click(object sender, RoutedEventArgs e)
{
Expand Down Expand Up @@ -1477,18 +1479,59 @@ private void ToggleSwitchTimetableNotification_Toggled(object sender, RoutedEven
SaveSettings();
}

private void ToggleSwitchUseDefaultBNSPath_Toggled(object sender, RoutedEventArgs e)
/*private void ToggleSwitchUseDefaultBNSPath_Toggled(object sender, RoutedEventArgs e)
{
if (!isSettingsLoaded) return;
Settings.TimetableSettings.UseDefaultBNSPath = ToggleSwitchUseDefaultBNSPath.IsOn;
TextBoxBNSPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Blackboard Notification Service";
//Settings.TimetableSettings.UseDefaultBNSPath = ToggleSwitchUseDefaultBNSPath.IsOn;
//TextBoxBNSPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Blackboard Notification Service";
SaveSettings();
}
private void TextBoxBNSPath_TextChanged(object sender, TextChangedEventArgs e)
{
if (!isSettingsLoaded) return;
Settings.TimetableSettings.BNSPath = TextBoxBNSPath.Text;
//Settings.TimetableSettings.BNSPath = TextBoxBNSPath.Text;
SaveSettings();
}*/

private void ButtonRefreshBNSStatus_Click(object sender, RoutedEventArgs e)
{
if (GetBNSPath() == null)
{
TextBlockBNSStatus.Text = "未检测到黑板通知服务,上下课提醒将不会出现";
ButtonRefreshBNSStatus.Visibility = Visibility.Visible;
}
else
{
TextBlockBNSStatus.Text = "黑板通知服务正常";
ButtonRefreshBNSStatus.Visibility = Visibility.Collapsed;
}
}

private void SliderBeginNotificationTime_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!isSettingsLoaded) return;

SliderBeginNotificationPreTime.Minimum = SliderBeginNotificationTime.Value;
SliderBeginNotificationPreTime.Maximum = SliderBeginNotificationTime.Value + 20;

Settings.TimetableSettings.BeginNotificationTime = SliderBeginNotificationTime.Value;
SaveSettings();
}

private void SliderBeginNotificationPreTime_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!isSettingsLoaded) return;

Settings.TimetableSettings.BeginNotificationPreTime = SliderBeginNotificationPreTime.Value;
SaveSettings();
}

private void SliderOverNotificationTime_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!isSettingsLoaded) return;

Settings.TimetableSettings.OverNotificationTime = SliderOverNotificationTime.Value;
SaveSettings();
}

Expand Down Expand Up @@ -1668,8 +1711,13 @@ private void LoadSettings()

ToggleSwitchUseTimetable.IsOn = Settings.TimetableSettings.IsTimetableEnabled;
ToggleSwitchTimetableNotification.IsOn = Settings.TimetableSettings.IsTimetableNotificationEnabled;
ToggleSwitchUseDefaultBNSPath.IsOn = Settings.TimetableSettings.UseDefaultBNSPath;
TextBoxBNSPath.Text = Settings.TimetableSettings.BNSPath;
//ToggleSwitchUseDefaultBNSPath.IsOn = Settings.TimetableSettings.UseDefaultBNSPath;
//TextBoxBNSPath.Text = Settings.TimetableSettings.BNSPath;
SliderBeginNotificationTime.Value = Settings.TimetableSettings.BeginNotificationTime;
SliderBeginNotificationPreTime.Minimum = SliderBeginNotificationTime.Value;
SliderBeginNotificationPreTime.Maximum = SliderBeginNotificationTime.Value + 20;
SliderBeginNotificationPreTime.Value = Settings.TimetableSettings.BeginNotificationPreTime;
SliderOverNotificationTime.Value = Settings.TimetableSettings.OverNotificationTime;

ToggleButtonLock.IsChecked = Settings.Blackboard.IsLocked;

Expand Down Expand Up @@ -1959,7 +2007,7 @@ public static bool StartAutomaticallyCreate(string exeName)
shortcut.TargetPath = System.Windows.Forms.Application.ExecutablePath;
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
shortcut.WindowStyle = 1;
shortcut.Description = exeName + "_Ink";
shortcut.Description = exeName + "_link";
shortcut.Save();
return true;
}
Expand All @@ -1978,21 +2026,22 @@ public static bool StartAutomaticallyDel(string exeName)
return false;
}

private void ShowNotificationBNS(string title, string subtitle, int time, bool isBottom)
private void ShowNotificationBNS(string title, string subtitle, double time, bool isBottom)
{
string timeString = time.ToString();

try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
if (Settings.TimetableSettings.BNSPath.EndsWith("\\"))
/*&if (Settings.TimetableSettings.BNSPath.EndsWith("\\"))
{
startInfo.FileName = Settings.TimetableSettings.BNSPath + "bns.exe";
}
else
{
startInfo.FileName = Settings.TimetableSettings.BNSPath + "\\bns.exe";
}
}*/
if (GetBNSPath() != null) startInfo.FileName = GetBNSPath();

startInfo.Arguments = "\"" + title + "\"" + " \"" + subtitle + "\" -t " + timeString;
if (isBottom) startInfo.Arguments += " -bottom";
Expand All @@ -2002,6 +2051,26 @@ private void ShowNotificationBNS(string title, string subtitle, int time, bool i
catch { }
}

public static string GetBNSPath()
{
string keyPath = @"Software\Microsoft\Windows\CurrentVersion\Uninstall\{92ECD1B1-7ACD-4523-836F-D1F98FB9AF39}_is1";
string valueName = "InstallLocation";

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath))
{
if (key != null)
{
object value = key.GetValue(valueName);
if (value != null)
{
return value.ToString() + "bns.exe";
}
}
}

return null;
}

private void SwitchLookMode()
{
if (ToggleSwitchLiteMode.IsOn)
Expand Down

0 comments on commit fb7eed9

Please sign in to comment.